forked from opensearch-project/OpenSearch-Dashboards
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add workspace list (opensearch-project#39)
Signed-off-by: tygao <[email protected]>
- Loading branch information
1 parent
dd46956
commit 0f3fb0b
Showing
5 changed files
with
159 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
src/plugins/workspace/public/components/utils/workspace.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import { WORKSPACE_APP_ID, PATHS } from '../../../common/constants'; | ||
import { CoreStart } from '../../../../../core/public'; | ||
|
||
type Core = Pick<CoreStart, 'workspaces' | 'application'>; | ||
|
||
export const switchWorkspace = ({ workspaces, application }: Core, id: string) => { | ||
const newUrl = workspaces?.formatUrlWithWorkspaceId( | ||
application.getUrlForApp(WORKSPACE_APP_ID, { | ||
path: PATHS.update, | ||
absolute: true, | ||
}), | ||
id | ||
); | ||
if (newUrl) { | ||
window.location.href = newUrl; | ||
} | ||
}; |
127 changes: 127 additions & 0 deletions
127
src/plugins/workspace/public/components/workspace_list/index.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
/* | ||
* Copyright OpenSearch Contributors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
import React, { useState } from 'react'; | ||
import { | ||
EuiPage, | ||
EuiPageBody, | ||
EuiPageHeader, | ||
EuiPageContent, | ||
EuiBasicTable, | ||
EuiLink, | ||
Direction, | ||
CriteriaWithPagination, | ||
} from '@elastic/eui'; | ||
import useObservable from 'react-use/lib/useObservable'; | ||
import { useMemo } from 'react'; | ||
import { useCallback } from 'react'; | ||
import { WorkspaceAttribute } from '../../../../../core/public'; | ||
|
||
import { useOpenSearchDashboards } from '../../../../../plugins/opensearch_dashboards_react/public'; | ||
import { switchWorkspace } from '../utils/workspace'; | ||
|
||
export const WorkspaceList = () => { | ||
const { | ||
services: { workspaces, application }, | ||
} = useOpenSearchDashboards(); | ||
|
||
const [pageIndex, setPageIndex] = useState(0); | ||
const [pageSize, setPageSize] = useState(5); | ||
const [sortField, setSortField] = useState<'name' | 'id'>('name'); | ||
const [sortDirection, setSortDirection] = useState<Direction>('asc'); | ||
|
||
const workspaceList = useObservable(workspaces!.client.workspaceList$, []); | ||
|
||
const pageOfItems = useMemo(() => { | ||
return workspaceList | ||
.sort((a, b) => { | ||
const compare = a[sortField].localeCompare(b[sortField]); | ||
return sortDirection === 'asc' ? compare : -compare; | ||
}) | ||
.slice(pageIndex * pageSize, (pageIndex + 1) * pageSize); | ||
}, [workspaceList, pageIndex, pageSize, sortField, sortDirection]); | ||
|
||
const handleSwitchWorkspace = useCallback( | ||
(id: string) => { | ||
if (workspaces && application) { | ||
switchWorkspace({ workspaces, application }, id); | ||
} | ||
}, | ||
[workspaces, application] | ||
); | ||
|
||
const columns = [ | ||
{ | ||
field: 'name', | ||
name: 'Name', | ||
sortable: true, | ||
render: (name: string, item: WorkspaceAttribute) => ( | ||
<span> | ||
<EuiLink onClick={() => handleSwitchWorkspace(item.id)}>{name}</EuiLink> | ||
</span> | ||
), | ||
}, | ||
{ | ||
field: 'id', | ||
name: 'ID', | ||
sortable: true, | ||
}, | ||
{ | ||
field: 'description', | ||
name: 'Description', | ||
truncateText: true, | ||
}, | ||
{ | ||
field: 'features', | ||
name: 'Features', | ||
isExpander: true, | ||
hasActions: true, | ||
}, | ||
]; | ||
|
||
const onTableChange = ({ page, sort }: CriteriaWithPagination<WorkspaceAttribute>) => { | ||
const { field, direction } = sort!; | ||
const { index, size } = page; | ||
|
||
setPageIndex(index); | ||
setPageSize(size); | ||
setSortField(field as 'name' | 'id'); | ||
setSortDirection(direction); | ||
}; | ||
|
||
return ( | ||
<EuiPage paddingSize="none"> | ||
<EuiPageBody panelled> | ||
<EuiPageHeader restrictWidth pageTitle="Workspace list" /> | ||
<EuiPageContent | ||
verticalPosition="center" | ||
horizontalPosition="center" | ||
paddingSize="none" | ||
color="subdued" | ||
hasShadow={false} | ||
style={{ width: '100%', maxWidth: 1000 }} | ||
> | ||
<EuiBasicTable | ||
items={pageOfItems} | ||
columns={columns} | ||
pagination={{ | ||
pageIndex, | ||
pageSize, | ||
totalItemCount: workspaceList.length, | ||
pageSizeOptions: [5, 10, 20], | ||
}} | ||
sorting={{ | ||
sort: { | ||
field: sortField, | ||
direction: sortDirection, | ||
}, | ||
}} | ||
onChange={onTableChange} | ||
/> | ||
</EuiPageContent> | ||
</EuiPageBody> | ||
</EuiPage> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters