Skip to content

Commit

Permalink
refacter workspace template into hooks
Browse files Browse the repository at this point in the history
Signed-off-by: Hailong Cui <[email protected]>
  • Loading branch information
Hailong-am committed Jun 9, 2023
1 parent 2b5e689 commit 80b87af
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 18 deletions.
20 changes: 2 additions & 18 deletions src/plugins/workspace/public/components/workspace_app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,30 +10,14 @@ import { matchPath, Route, Switch, useLocation } from 'react-router-dom';

import { ROUTES } from './routes';
import { useOpenSearchDashboards } from '../../../opensearch_dashboards_react/public';
import { ChromeBreadcrumb, PublicAppInfo } from '../../../../core/public';
import { ChromeBreadcrumb } from '../../../../core/public';
import { WORKSPACE_APP_NAME } from '../../common/constants';
import { WorkspaceTemplate } from '../../../../core/types';

export const WorkspaceApp = ({ appBasePath }: { appBasePath: string }) => {
const {
services: { chrome, application },
services: { chrome },
} = useOpenSearchDashboards();
const location = useLocation();
let workspaceTemplates = [] as WorkspaceTemplate[];
const templateFeatureMap = new Map<string, PublicAppInfo[]>();
application?.applications$.subscribe((map) =>
map.forEach((app) => {
const { workspaceTemplate: templates = [] } = app;
workspaceTemplates.push(...templates);
for (const template of templates) {
const features = templateFeatureMap.get(template.id) || [];
features.push(app);
templateFeatureMap.set(template.id, features);
}
})
);

workspaceTemplates = [...new Set(workspaceTemplates)];

/**
* map the current pathname to breadcrumbs
Expand Down
32 changes: 32 additions & 0 deletions src/plugins/workspace/public/hooks.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

import { ApplicationStart, PublicAppInfo } from 'opensearch-dashboards/public';
import { WorkspaceTemplate } from '../../../core/types';

export function useWorkspaceTemplate(application: ApplicationStart) {
let workspaceTemplates = [] as WorkspaceTemplate[];
const templateFeatureMap = new Map<string, PublicAppInfo[]>();

application.applications$.subscribe((applications) =>
applications.forEach((app) => {
const { workspaceTemplate: templates = [] } = app;
workspaceTemplates.push(...templates);
for (const template of templates) {
const features = templateFeatureMap.get(template.id) || [];
features.push(app);
templateFeatureMap.set(template.id, features);
}
})
);

workspaceTemplates.sort((a, b) => (a.order || 0) - (b.order || 0));
workspaceTemplates = [...new Set(workspaceTemplates)];

return {
workspaceTemplates,
templateFeatureMap,
};
}

0 comments on commit 80b87af

Please sign in to comment.