Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Workspace]Fix maximum call stack error in use case service #7817

Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions changelogs/fragments/7817.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
fix:
- [Workspace] maximum call stack error in use case service ([#7817](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/7817))
7 changes: 6 additions & 1 deletion src/plugins/workspace/public/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,16 @@ export type Services = CoreStart & {
navigationUI?: NavigationPublicPluginStart['ui'];
};

export interface WorkspaceUseCaseFeature {
id: string;
title?: string;
}

export interface WorkspaceUseCase {
id: string;
title: string;
description: string;
features: Array<{ id: string; title?: string }>;
features: WorkspaceUseCaseFeature[];
systematic?: boolean;
order?: number;
}
17 changes: 17 additions & 0 deletions src/plugins/workspace/public/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -558,6 +558,23 @@ describe('workspace utils: isEqualWorkspaceUseCase', () => {
})
).toEqual(false);
});
it('should return false for duplicate features', () => {
expect(
isEqualWorkspaceUseCase(
{ ...useCaseMock, features: [useCaseMock.features[0], useCaseMock.features[0]] },
{
...useCaseMock,
features: [
useCaseMock.features[0],
{
id: 'another',
title: 'Another',
},
],
}
)
).toEqual(false);
});
it('should return true when all properties equal', () => {
expect(
isEqualWorkspaceUseCase(useCaseMock, {
Expand Down
29 changes: 20 additions & 9 deletions src/plugins/workspace/public/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import {
WorkspaceAvailability,
} from '../../../core/public';
import { DEFAULT_SELECTED_FEATURES_IDS, WORKSPACE_DETAIL_APP_ID } from '../common/constants';
import { WorkspaceUseCase } from './types';
import { WorkspaceUseCase, WorkspaceUseCaseFeature } from './types';
import { formatUrlWithWorkspaceId } from '../../../core/public/utils';
import { SigV4ServiceName } from '../../../plugins/data_source/common/data_sources';

Expand Down Expand Up @@ -260,6 +260,24 @@ export const convertNavGroupToWorkspaceUseCase = ({
order,
});

const compareFeatures = (
features1: WorkspaceUseCaseFeature[],
features2: WorkspaceUseCaseFeature[]
) => {
const featureSerialize = ({ id, title }: WorkspaceUseCaseFeature) => `${id}-${title}`;
const features1Set = new Set(features1.map(featureSerialize));
const features2Set = new Set(features2.map(featureSerialize));
if (features1Set.size !== features2Set.size) {
return false;
}
for (const feature of features1Set) {
if (!features2Set.has(feature)) {
return false;
}
}
return true;
};

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps we can simplify the compare by sorting the serialized array and see if JSON.stringify(sortArray) equals?

export const isEqualWorkspaceUseCase = (a: WorkspaceUseCase, b: WorkspaceUseCase) => {
if (a.id !== b.id) {
return false;
Expand All @@ -276,14 +294,7 @@ export const isEqualWorkspaceUseCase = (a: WorkspaceUseCase, b: WorkspaceUseCase
if (a.order !== b.order) {
return false;
}
if (
a.features.length !== b.features.length ||
a.features.some((aFeature) =>
b.features.some(
(bFeature) => aFeature.id !== bFeature.id || aFeature.title !== bFeature.title
)
)
) {
if (a.features.length !== b.features.length || !compareFeatures(a.features, b.features)) {
return false;
}
return true;
Expand Down
Loading