Skip to content

Commit

Permalink
Add workspace column into saved objects table (opensearch-project#44)
Browse files Browse the repository at this point in the history
* Add workspace column into saved management page

Signed-off-by: Hailong Cui <ihailong@amazon.com>

* savedObjectsManagement as optional dependency

Signed-off-by: Hailong Cui <ihailong@amazon.com>

* i18n for column title

Signed-off-by: Hailong Cui <ihailong@amazon.com>

---------

Signed-off-by: Hailong Cui <ihailong@amazon.com>
  • Loading branch information
Hailong-am authored and ruanyl committed Aug 3, 2023
1 parent 6cd4015 commit 79d3e6b
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 3 deletions.
1 change: 1 addition & 0 deletions src/plugins/workspace/opensearch_dashboards.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"server": false,
"ui": true,
"requiredPlugins": ["savedObjects"],
"optionalPlugins": ["savedObjectsManagement"],
"requiredBundles": [
"opensearchDashboardsReact"
]
Expand Down
66 changes: 66 additions & 0 deletions src/plugins/workspace/public/components/utils/workspace_column.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

import React from 'react';
import { EuiText } from '@elastic/eui';
import useObservable from 'react-use/lib/useObservable';
import { i18n } from '@osd/i18n';
import { WorkspaceAttribute, CoreSetup } from '../../../../../core/public';
import {
SavedObjectsManagementColumn,
SavedObjectsManagementRecord,
} from '../../../../saved_objects_management/public';

interface WorkspaceColumnProps {
coreSetup: CoreSetup;
workspaces?: string[];
record: SavedObjectsManagementRecord;
}

function WorkspaceColumn({ coreSetup, workspaces, record }: WorkspaceColumnProps) {
const workspaceList = useObservable(coreSetup.workspaces.client.workspaceList$);

const wsLookUp = workspaceList?.reduce((map, ws) => {
return map.set(ws.id, ws.name);
}, new Map<string, string>());

const publicWsName = i18n.translate('workspace.public.name', {
defaultMessage: 'public',
});
wsLookUp?.set('public', publicWsName);

if (record.type === 'workspace') {
return null;
}

if (!workspaces) {
return <EuiText>{publicWsName}</EuiText>;
}

const workspaceNames = workspaces?.map((wsId) => wsLookUp?.get(wsId)).join(' | ');

return <EuiText>{workspaceNames}</EuiText>;
}

export function getWorkspaceColumn(
coreSetup: CoreSetup
): SavedObjectsManagementColumn<WorkspaceAttribute | undefined> {
return {
id: 'workspace_column',
euiColumn: {
align: 'left',
field: 'workspaces',
name: i18n.translate('savedObjectsManagement.objectsTable.table.columnWorkspacesName', {
defaultMessage: 'Workspaces',
}),
render: (workspaces: string[], record: SavedObjectsManagementRecord) => {
return <WorkspaceColumn coreSetup={coreSetup} workspaces={workspaces} record={record} />;
},
},
loadData: () => {
return Promise.resolve(undefined);
},
};
}
16 changes: 13 additions & 3 deletions src/plugins/workspace/public/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
*/

import { i18n } from '@osd/i18n';
import type { Subscription } from 'rxjs';
import {
CoreSetup,
CoreStart,
Expand All @@ -14,9 +15,14 @@ import {
import { WORKSPACE_APP_ID } from '../common/constants';
import { mountDropdownList } from './mount';
import { getWorkspaceIdFromUrl } from '../../../core/public/utils';
import type { Subscription } from 'rxjs';
import { SavedObjectsManagementPluginSetup } from '../../saved_objects_management/public';
import { getWorkspaceColumn } from './components/utils/workspace_column';

interface WorkspacesPluginSetupDeps {
savedObjectsManagement?: SavedObjectsManagementPluginSetup;
}

export class WorkspacesPlugin implements Plugin<{}, {}> {
export class WorkspacesPlugin implements Plugin<{}, {}, WorkspacesPluginSetupDeps> {
private coreSetup?: CoreSetup;
private coreStart?: CoreStart;
private currentWorkspaceSubscription?: Subscription;
Expand All @@ -39,7 +45,7 @@ export class WorkspacesPlugin implements Plugin<{}, {}> {

return newUrl.toString();
};
public async setup(core: CoreSetup) {
public async setup(core: CoreSetup, { savedObjectsManagement }: WorkspacesPluginSetupDeps) {
// If workspace feature is disabled, it will not load the workspace plugin
if (core.uiSettings.get('workspace:enabled') === false) {
return {};
Expand All @@ -63,6 +69,10 @@ export class WorkspacesPlugin implements Plugin<{}, {}> {
);
}
}
/**
* register workspace column into saved objects table
*/
savedObjectsManagement?.columns.register(getWorkspaceColumn(core));

core.application.register({
id: WORKSPACE_APP_ID,
Expand Down

0 comments on commit 79d3e6b

Please sign in to comment.