Skip to content

Commit

Permalink
feat: Add workspace in tree extension (kubesphere#748)
Browse files Browse the repository at this point in the history
* feat: Add workspaces in tree plugin

Signed-off-by: Bunny <[email protected]>

* feat: Add workspace base layout

Signed-off-by: Bunny <[email protected]>

* feat: Add list layout

Signed-off-by: Bunny <[email protected]>

* feat: Add devops ks config in globals

Signed-off-by: Bunny <[email protected]>

---------

Signed-off-by: Bunny <[email protected]>
Co-authored-by: Bunny <[email protected]>
  • Loading branch information
bunnymiao and Bunny authored Feb 2, 2023
1 parent a8f6436 commit 0a31edb
Show file tree
Hide file tree
Showing 12 changed files with 195 additions and 6 deletions.
6 changes: 1 addition & 5 deletions packages/access/src/containers/Workspaces/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -167,11 +167,7 @@ export default function Workspaces(): JSX.Element {
render: (value, row) => {
return (
<Field
value={
<Link to={`/workspace/${value}`} replace={true}>
{getDisplayName(row)}
</Link>
}
value={<Link to={`/workspaces/${value}`}>{getDisplayName(row)}</Link>}
avatar={<Enterprise size={40} />}
label={<FieldLable>{row.description || '-'}</FieldLable>}
/>
Expand Down
1 change: 1 addition & 0 deletions packages/bootstrap/cli/webpack.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const alias = {
'@ks-console/access': resolve('packages/access/src'),
'@ks-console/appstore': resolve('packages/appstore/src'),
'@ks-console/projects': resolve('packages/projects/src'),
'@ks-console/workspaces': resolve('packages/workspaces/src'),
};

function devServer(setAlias) {
Expand Down
3 changes: 2 additions & 1 deletion packages/bootstrap/entry/extensions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import consoleApp from '@ks-console/console';
import access from '@ks-console/access';
import platform from '@ks-console/platform';
import projects from '@ks-console/projects';
import workspaces from '@ks-console/workspaces';

const extensions: any[] = [clusters, consoleApp, access, platform, projects];
const extensions: any[] = [clusters, consoleApp, access, platform, projects, workspaces];

export default extensions;
1 change: 1 addition & 0 deletions packages/bootstrap/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"@ks-console/locales": "4.0.0-alpha.2",
"@ks-console/platform": "4.0.0-alpha.11",
"@ks-console/projects": "4.0.0-alpha.11",
"@ks-console/workspaces": "4.0.0-alpha.11",
"mustache": "^4.2.0"
},
"devDependencies": {
Expand Down
5 changes: 5 additions & 0 deletions packages/workspaces/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# `workspaces`

> TODO: description
## Usage
17 changes: 17 additions & 0 deletions packages/workspaces/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"name": "@ks-console/workspaces",
"description": "",
"version": "4.0.0-alpha.11",
"homepage": "",
"main": "cjs/index.js",
"module": "esm/index.js",
"types": "lib/src/index.d.ts",
"files": [
"cjs",
"esm",
"lib"
],
"dependencies": {
"@ks-console/shared": "4.0.0-alpha.11"
}
}
53 changes: 53 additions & 0 deletions packages/workspaces/src/containers/Base/BaseLayout/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import React, { useEffect, useMemo } from 'react';
import { get, set } from 'lodash';
import { useStore } from '@kubed/stook';
import { useQueries } from 'react-query';
import { Loading } from '@kubed/components';
import { Outlet, useParams } from 'react-router-dom';
import { apis, workspaceStore } from '@ks-console/shared';

const { fetchDetail, useClusters } = workspaceStore;

function BaseLayout(): JSX.Element {
const { workspace } = useParams();
const [, setWorkspaceDetail] = useStore('workspaceDetail');
const clusterDetail = useClusters({ workspace });
const [workspaceResult, ruleResult] = useQueries([
{
queryKey: ['workspaces', workspace],
queryFn: () => fetchDetail({ workspace }),
enabled: !!workspace,
onSuccess: (data: any) => setWorkspaceDetail(data),
},
{
queryKey: ['workspace', 'authRule', workspace],
queryFn: () => apis.fetchRules({ workspace, name: globals.user.username }),
enabled: !!workspace,
keepPreviousData: true,
},
]);
const isInitializing = useMemo<boolean>(
() => workspaceResult.isLoading || ruleResult.isLoading,
[workspaceResult.isLoading, ruleResult.isLoading],
);

useEffect(() => {
if (!ruleResult.isSuccess) {
return;
}

set(
globals.ksConfig,
'devops',
clusterDetail.some((cluster: any) => get(cluster, 'configz.devops')),
);
}, [ruleResult.isSuccess, clusterDetail]);

if (isInitializing) {
return <Loading className="page-loading" />;
}

return <Outlet />;
}

export default BaseLayout;
57 changes: 57 additions & 0 deletions packages/workspaces/src/containers/Base/ListLayout/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import React, { useEffect } from 'react';
import styled from 'styled-components';
import { Cluster } from '@kubed/icons';
import { Outlet, useLocation, useParams } from 'react-router-dom';
import { NavMenu, NavTitle, useGlobalStore } from '@ks-console/shared';

import { getWorkspaceNavs } from '../../../utils';

const PageSide = styled.div`
position: fixed;
top: 88px;
padding: 0 20px 40px;
width: 260px;
z-index: 99;
`;

const PageMain = styled.div`
margin-left: 240px;
padding: 20px;
overflow-y: auto;
overflow-x: hidden;
`;

const navKey = 'WORKSPACE_NAV';

function ListLayout(): JSX.Element {
const location = useLocation();
const { workspace: workspaceName = '' } = useParams();
const { getNav, setNav } = useGlobalStore();
let navs = getNav(`${navKey}-${workspaceName}`);

useEffect(() => {
if (!navs) {
navs = getWorkspaceNavs(workspaceName);
setNav(`${navKey}-${workspaceName}`, navs);
}
}, []);

return (
<>
<PageSide>
<NavTitle
icon={<Cluster variant="light" size={40} />}
title={workspaceName}
subtitle={t('WORKSPACE')}
style={{ marginBottom: '20px' }}
/>
<NavMenu navs={navs} prefix={`/workspaces/${workspaceName}`} pathname={location.pathname} />
</PageSide>
<PageMain>
<Outlet />
</PageMain>
</>
);
}

export default ListLayout;
2 changes: 2 additions & 0 deletions packages/workspaces/src/containers/Base/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { default as BaseLayout } from './BaseLayout';
export { default as ListLayout } from './ListLayout';
7 changes: 7 additions & 0 deletions packages/workspaces/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import routes from './routes';

const pluginConfig = {
routes,
};

export default pluginConfig;
28 changes: 28 additions & 0 deletions packages/workspaces/src/routes/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import React from 'react';
import { Navigate } from 'react-router-dom';

import { BaseLayout, ListLayout } from '../containers/Base';

const PATH = '/workspaces/:workspace';

export default [
{
path: PATH,
element: <BaseLayout />,
children: [
{
element: <ListLayout />,
children: [
{
index: true,
element: <Navigate to="overview" replace />,
},
{
path: 'overview',
element: <>Workspace OverView</>,
},
],
},
],
},
];
21 changes: 21 additions & 0 deletions packages/workspaces/src/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { cloneDeep, get, isEmpty } from 'lodash';
import { checkNavItem, hasPermission } from '@ks-console/shared';

export function getWorkspaceNavs(workspace?: string) {
if (!get(globals.user, `workspaceRules[${workspace}]`)) {
return [];
}

const navs: string[] = [];
const workspaceNavs = cloneDeep(globals.config.workspaceNavs);
const filteredItems = workspaceNavs.children.filter((item: any) => {
item.workspace = workspace;
return checkNavItem(item, params => hasPermission({ ...params, workspace }));
});

if (!isEmpty(filteredItems)) {
navs.push({ ...workspaceNavs, children: filteredItems });
}

return navs;
}

0 comments on commit 0a31edb

Please sign in to comment.