Skip to content

Commit

Permalink
optimize the code
Browse files Browse the repository at this point in the history
Signed-off-by: yubonluo <[email protected]>
  • Loading branch information
yubonluo committed Aug 22, 2024
1 parent 0ebb64e commit ae926a2
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 12 deletions.
7 changes: 2 additions & 5 deletions src/plugins/workspace/public/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -386,10 +386,7 @@ export class WorkspacePlugin
async mount() {
const [coreStart] = await core.getStartServices();
const { application, http, workspaces } = coreStart;
const urlParams = new URLSearchParams(location.search);
const navigateWorkspaceId = urlParams.get('workspaceId');
const workspaceList = workspaces.workspaceList$.getValue();
const workspace = workspaceList.find((ws) => ws.id === navigateWorkspaceId);
const workspace = workspaces.currentWorkspace$.getValue();

Check warning on line 389 in src/plugins/workspace/public/plugin.ts

View check run for this annotation

Codecov / codecov/patch

src/plugins/workspace/public/plugin.ts#L387-L389

Added lines #L387 - L389 were not covered by tests
if (workspace) {
const availableUseCases = registeredUseCases$.getValue();
const currentUseCase = availableUseCases.find(

Check warning on line 392 in src/plugins/workspace/public/plugin.ts

View check run for this annotation

Codecov / codecov/patch

src/plugins/workspace/public/plugin.ts#L391-L392

Added lines #L391 - L392 were not covered by tests
Expand All @@ -402,7 +399,7 @@ export class WorkspacePlugin
}
return () => {};

Check warning on line 400 in src/plugins/workspace/public/plugin.ts

View check run for this annotation

Codecov / codecov/patch

src/plugins/workspace/public/plugin.ts#L400

Added line #L400 was not covered by tests
},
workspaceAvailability: WorkspaceAvailability.outsideWorkspace,
workspaceAvailability: WorkspaceAvailability.insideWorkspace,
});

// workspace list
Expand Down
15 changes: 10 additions & 5 deletions src/plugins/workspace/server/plugin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,12 @@
* SPDX-License-Identifier: Apache-2.0
*/

import { OnPostAuthHandler, OnPreRoutingHandler } from 'src/core/server';
import { coreMock, httpServerMock } from '../../../core/server/mocks';
import {
OnPostAuthHandler,
OnPreRoutingHandler,
SavedObjectsClientContract,
} from 'src/core/server';
import { coreMock, httpServerMock, savedObjectsClientMock } from '../../../core/server/mocks';
import { WorkspacePlugin } from './plugin';
import { getWorkspaceState, updateWorkspaceState } from '../../../core/server/utils';
import * as utilsExports from './utils';
Expand Down Expand Up @@ -240,7 +244,7 @@ describe('Workspace server plugin', () => {
await registerOnPostAuthFn(request, response, toolKitMock);
expect(response.redirected).toBeCalledWith({
headers: {
location: '/mock-server-basepath/app/workspace_navigation/?workspaceId=workspace-1',
location: '/mock-server-basepath/w/workspace-1/app/workspace_navigation',
},
});
});
Expand Down Expand Up @@ -277,13 +281,14 @@ describe('Workspace server plugin', () => {
const request = httpServerMock.createOpenSearchDashboardsRequest({
path: '/',
});
const savedObjectsClient: jest.Mocked<SavedObjectsClientContract> = savedObjectsClientMock.create();
const mockUiSettings = {
get: jest.fn().mockResolvedValue('default'),
};
setupMock.getStartServices.mockResolvedValue([
{
uiSettings: { asScopedToClient: () => mockUiSettings },
savedObjects: { getScopedClient: () => null },
savedObjects: { getScopedClient: () => savedObjectsClient },
},
{},
{},
Expand All @@ -307,7 +312,7 @@ describe('Workspace server plugin', () => {
await registerOnPostAuthFn(request, response, toolKitMock);
expect(response.redirected).toBeCalledWith({
headers: {
location: '/mock-server-basepath/app/workspace_navigation/?workspaceId=default',
location: '/mock-server-basepath/w/default/app/workspace_navigation',
},
});
});
Expand Down
4 changes: 2 additions & 2 deletions src/plugins/workspace/server/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ export class WorkspacePlugin implements Plugin<WorkspacePluginSetup, WorkspacePl
if (workspaceList.length === 1) {
return response.redirected({
headers: {
location: `${basePath}/app/${WORKSPACE_NAVIGATION_APP_ID}/?workspaceId=${workspaceList[0].id}`,
location: `${basePath}/w/${workspaceList[0].id}/app/${WORKSPACE_NAVIGATION_APP_ID}`,
},
});
}
Expand All @@ -144,7 +144,7 @@ export class WorkspacePlugin implements Plugin<WorkspacePluginSetup, WorkspacePl
if (defaultWorkspace) {
return response.redirected({
headers: {
location: `${basePath}/app/${WORKSPACE_NAVIGATION_APP_ID}/?workspaceId=${defaultWorkspace.id}`,
location: `${basePath}/w/${defaultWorkspace.id}/app/${WORKSPACE_NAVIGATION_APP_ID}`,
},
});
} else {
Expand Down

0 comments on commit ae926a2

Please sign in to comment.