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

Store workspace id in hash #5

Open
wants to merge 28 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
ba6a4ab
feat: add core workspace module (#145)
ruanyl Sep 19, 2023
5d1e1fc
add unit tests for workspace core service (#191)
ruanyl Sep 22, 2023
2123402
add missing type definition
ruanyl Sep 22, 2023
26f38d6
add changelog for workspace service module
ruanyl Sep 25, 2023
088e165
remove unnecessary workspace menu register
ruanyl Sep 26, 2023
f5650b0
Update test description per comment
ruanyl Sep 27, 2023
3bbeaa7
remove unnecessary workspace enabled flag from core workspace module
ruanyl Sep 28, 2023
7f57bd2
fix tests and add comments
ruanyl Sep 28, 2023
22d5b30
update failed snapshot
ruanyl Sep 28, 2023
8267820
update failed snapshots
ruanyl Sep 28, 2023
8126c44
[Discover] A bunch of navigation fixes (#5168)
ashwin-pc Oct 3, 2023
3e4e3cc
feat: make url stateful
SuZhou-Joe Jun 29, 2023
70793dd
feat: optimize code
SuZhou-Joe Jun 29, 2023
9871b30
feat: remove useless change
SuZhou-Joe Jun 29, 2023
3d1dfa3
feat: optimize url listener
SuZhou-Joe Jul 3, 2023
2220feb
feat: make formatUrlWithWorkspaceId extensible
SuZhou-Joe Jul 3, 2023
643ebe1
feat: modify to related components
SuZhou-Joe Jul 3, 2023
7ee4201
feat: modify the async format to be sync function
SuZhou-Joe Jul 3, 2023
ed3e23b
feat: modify the async format to be sync function
SuZhou-Joe Jul 3, 2023
73d595c
feat: some update
SuZhou-Joe Oct 7, 2023
05fda74
feat: remove useless code
SuZhou-Joe Oct 7, 2023
889d28c
feat: merge
SuZhou-Joe Feb 29, 2024
3ba70da
feat: remove useless code
SuZhou-Joe Feb 29, 2024
66df5e8
feat: some update
SuZhou-Joe Feb 29, 2024
803dd11
feat: remove useless code
SuZhou-Joe Feb 29, 2024
bc1a81b
feat: remove useless code
SuZhou-Joe Feb 29, 2024
38a0c8e
feat: enable url in hash
SuZhou-Joe Feb 29, 2024
e1bbc94
feat: remove useless code
SuZhou-Joe Feb 29, 2024
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: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -1028,4 +1028,4 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)

### 🔩 Tests

- Update caniuse to fix failed integration tests ([#2322](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/2322))
- Update caniuse to fix failed integration tests ([#2322](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/2322))
1 change: 1 addition & 0 deletions src/plugins/workspace/common/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
* SPDX-License-Identifier: Apache-2.0
*/

export const WORKSPACE_ID_STATE_KEY = '_w';
export const WORKSPACE_SAVED_OBJECTS_CLIENT_WRAPPER_ID = 'workspace';
5 changes: 3 additions & 2 deletions src/plugins/workspace/opensearch_dashboards.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
"id": "workspace",
"version": "opensearchDashboards",
"server": true,
"ui": false,
"ui": true,
"requiredPlugins": [
"savedObjects"
"savedObjects",
"opensearchDashboardsUtils"
],
"optionalPlugins": [],
"requiredBundles": []
Expand Down
93 changes: 90 additions & 3 deletions src/plugins/workspace/public/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,97 @@
* SPDX-License-Identifier: Apache-2.0
*/

import { Plugin } from '../../../core/public';
import { BehaviorSubject, combineLatest } from 'rxjs';
import { debounce } from 'lodash';
import { CoreSetup, Plugin } from '../../../core/public';
import { getStateFromOsdUrl } from '../../opensearch_dashboards_utils/public';
import { formatUrlWithWorkspaceId } from './utils';
import { WORKSPACE_ID_STATE_KEY } from '../common/constants';

export class WorkspacePlugin implements Plugin<{}, {}> {
private core?: CoreSetup;
private URLChange$ = new BehaviorSubject('');
private getWorkpsaceIdFromURL(): string | null {
return getStateFromOsdUrl(WORKSPACE_ID_STATE_KEY);
}
private async getWorkpsaceId(): Promise<string> {
if (this.getWorkpsaceIdFromURL()) {
return this.getWorkpsaceIdFromURL() || '';
}

return (await this.core?.workspaces.currentWorkspaceId$.getValue()) || '';
}
private getPatchedUrl = (url: string, workspaceId: string) => {
return formatUrlWithWorkspaceId(url, workspaceId);
};
private async listenToHashChange(): Promise<void> {
window.addEventListener('hashchange', async () => {
if (this.shouldPatchUrl()) {
const workspaceId = await this.getWorkpsaceId();
this.URLChange$.next(this.getPatchedUrl(window.location.href, workspaceId));
}
});
}
private shouldPatchUrl(): boolean {
const currentWorkspaceId = this.core?.workspaces.currentWorkspaceId$.getValue();
const workspaceIdFromURL = this.getWorkpsaceIdFromURL();
if (!currentWorkspaceId && !workspaceIdFromURL) {
return false;
}

if (currentWorkspaceId === workspaceIdFromURL) {
return false;
}

return true;
}
private async listenToApplicationChange(): Promise<void> {
const startService = await this.core?.getStartServices();
if (startService) {
combineLatest([
this.core?.workspaces.currentWorkspaceId$,
startService[0].application.currentAppId$,
]).subscribe(async ([]) => {
if (this.shouldPatchUrl()) {
const currentWorkspaceId = await this.getWorkpsaceId();
this.URLChange$.next(this.getPatchedUrl(window.location.href, currentWorkspaceId));
}
});
}
}
public async setup(core: CoreSetup) {
this.core = core;
/**
* Retrive workspace id from url
*/
const workspaceId = this.getWorkpsaceIdFromURL();

if (workspaceId) {
/**
* Enter a workspace
*/
this.core.workspaces.currentWorkspaceId$.next(workspaceId);
}

/**
* listen to application change and patch workspace id in hash
*/
this.listenToApplicationChange();

/**
* listen to application internal hash change and patch workspace id in hash
*/
this.listenToHashChange();

/**
* All the URLChange will flush in this subscriber
*/
this.URLChange$.subscribe(
debounce(async (url) => {
history.replaceState(history.state, '', url);
}, 500)
);

export class WorkspacePlugin implements Plugin<{}, {}, {}> {
public async setup() {
return {};
}

Expand Down
11 changes: 11 additions & 0 deletions src/plugins/workspace/public/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

import { setStateToOsdUrl } from '../../opensearch_dashboards_utils/public';
import { WORKSPACE_ID_STATE_KEY } from '../common/constants';

export const formatUrlWithWorkspaceId = (url: string, workspaceId: string) => {
return setStateToOsdUrl(WORKSPACE_ID_STATE_KEY, workspaceId, undefined, url);
};
Loading