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] feat: support use case populate for workspace create and list page #8422

Merged
merged 5 commits into from
Oct 3, 2024
Merged
Show file tree
Hide file tree
Changes from all 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/8422.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
feat:
- Support use case populate for workspace create and list page ([#8422](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/8422))
16 changes: 14 additions & 2 deletions src/plugins/workspace/public/application.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,13 @@ export const renderCreatorApp = (
) => {
ReactDOM.render(
<OpenSearchDashboardsContextProvider services={services}>
<WorkspaceCreatorApp {...props} />
<Router>
<Switch>
<Route>
<WorkspaceCreatorApp {...props} />
</Route>
</Switch>
</Router>
Comment on lines +28 to +34
Copy link
Collaborator

Choose a reason for hiding this comment

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

why do we need add router?

Copy link
Member

Choose a reason for hiding this comment

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

I have the same concern that is the HashRouter here necessary?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, we need hashRouter to wrap switch and route.

</OpenSearchDashboardsContextProvider>,
element
);
Expand Down Expand Up @@ -56,7 +62,13 @@ export const renderListApp = (
) => {
ReactDOM.render(
<OpenSearchDashboardsContextProvider services={services}>
<WorkspaceListApp {...props} />
<Router>
<Switch>
<Route>
<WorkspaceListApp {...props} />
</Route>
</Switch>
</Router>
</OpenSearchDashboardsContextProvider>,
element
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* SPDX-License-Identifier: Apache-2.0
*/

import { navigateToWorkspaceDetail } from './workspace';
import { navigateToWorkspaceDetail, navigateToWorkspaceListWithUseCase } from './workspace';
import { formatUrlWithWorkspaceId } from '../../../../../core/public/utils';
jest.mock('../../../../../core/public/utils');

Expand Down Expand Up @@ -62,4 +62,16 @@ describe('workspace utils', () => {
expect(mockNavigateToUrl).not.toBeCalled();
});
});

describe('navigateToWorkspaceListWithUseCase', () => {
it('should redirect if newUrl is returned', () => {
coreStartMock.application.getUrlForApp.mockImplementation(
() => 'localhost:5601/app/workspace_list'
);
navigateToWorkspaceListWithUseCase(coreStartMock.application, 'Search');
expect(mockNavigateToUrl).toHaveBeenCalledWith(
'localhost:5601/app/workspace_list#/?useCase=Search'
);
});
});
});
14 changes: 13 additions & 1 deletion src/plugins/workspace/public/components/utils/workspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* SPDX-License-Identifier: Apache-2.0
*/

import { WORKSPACE_DETAIL_APP_ID } from '../../../common/constants';
import { WORKSPACE_DETAIL_APP_ID, WORKSPACE_LIST_APP_ID } from '../../../common/constants';
import { CoreStart } from '../../../../../core/public';
import { formatUrlWithWorkspaceId } from '../../../../../core/public/utils';
import { DetailTab } from '../workspace_form/constants';
Expand All @@ -23,6 +23,18 @@ export const navigateToWorkspaceDetail = (
);
};

export const navigateToWorkspaceListWithUseCase = (
application: Core['application'],
useCaseTitle: string
) => {
const newUrl = application.getUrlForApp(WORKSPACE_LIST_APP_ID, { absolute: true });
if (newUrl) {
const url = new URL(newUrl);
url.hash = `/?useCase=${useCaseTitle}`;
application.navigateToUrl(url.toString());
}
};

export const navigateToAppWithinWorkspace = (
{ application, http }: Core,
workspaceId: string,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,16 @@ import { DataSourceEngineType } from '../../../../data_source/common/data_source
import { DataSourceConnectionType } from '../../../common/types';
import * as utils from '../../utils';

jest.mock('react-router-dom', () => ({
...jest.requireActual('react-router-dom'),
useLocation: () => ({
search: '',
pathname: '',
hash: '',
state: undefined,
}),
}));

const workspaceClientCreate = jest
.fn()
.mockReturnValue({ result: { id: 'successResult' }, success: true });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
* SPDX-License-Identifier: Apache-2.0
*/

import React, { useCallback, useState } from 'react';
import React, { useCallback, useState, useMemo } from 'react';
import { EuiPage, EuiPageBody, EuiPageContent, euiPaletteColorBlind } from '@elastic/eui';
import { i18n } from '@osd/i18n';
import { BehaviorSubject } from 'rxjs';

import { useLocation } from 'react-router-dom';
import { useOpenSearchDashboards } from '../../../../opensearch_dashboards_react/public';
import { WorkspaceFormSubmitData, WorkspaceOperationType } from '../workspace_form';
import { WORKSPACE_DETAIL_APP_ID } from '../../../common/constants';
Expand Down Expand Up @@ -53,16 +54,28 @@
onlyAllowEssentialEnabled: true,
});

const defaultSelectedUseCase = availableUseCases?.[0];
const defaultWorkspaceFormValues: Partial<WorkspaceFormSubmitData> = {
color: euiPaletteColorBlind()[0],
...(defaultSelectedUseCase
? {
name: defaultSelectedUseCase.title,
features: [getUseCaseFeatureConfig(defaultSelectedUseCase.id)],
}
: {}),
};
const location = useLocation();

const defaultWorkspaceFormValues = useMemo(() => {
let defaultSelectedUseCase;
const params = new URLSearchParams(location.search);
const useCaseTitle = params.get('useCase');
SuZhou-Joe marked this conversation as resolved.
Show resolved Hide resolved
if (useCaseTitle) {
defaultSelectedUseCase =

Check warning on line 64 in src/plugins/workspace/public/components/workspace_creator/workspace_creator.tsx

View check run for this annotation

Codecov / codecov/patch

src/plugins/workspace/public/components/workspace_creator/workspace_creator.tsx#L64

Added line #L64 was not covered by tests
availableUseCases?.find(({ title }) => title === useCaseTitle) || availableUseCases?.[0];
} else {
defaultSelectedUseCase = availableUseCases?.[0];
}
return {
color: euiPaletteColorBlind()[0],
...(defaultSelectedUseCase
? {
name: defaultSelectedUseCase.title,
features: [getUseCaseFeatureConfig(defaultSelectedUseCase.id)],
}
: {}),
};
}, [location.search, availableUseCases]);

const handleWorkspaceFormSubmit = useCallback(
async (data: WorkspaceFormSubmitData) => {
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,16 @@ jest.mock('../../utils', () => {
};
});

jest.mock('react-router-dom', () => ({
...jest.requireActual('react-router-dom'),
useLocation: () => ({
search: '',
pathname: '',
hash: '',
state: undefined,
}),
}));

function getWrapUserDefaultWorkspaceList(
workspaceList = [
{
Expand Down
Loading
Loading