Skip to content

Commit 657372f

Browse files
authored
Make url stateful in hash
Make url stateful in hash
1 parent 03e4897 commit 657372f

File tree

5 files changed

+105
-5
lines changed

5 files changed

+105
-5
lines changed

CHANGELOG.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1028,4 +1028,4 @@ Inspired from [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
10281028

10291029
### 🔩 Tests
10301030

1031-
- Update caniuse to fix failed integration tests ([#2322](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/2322))
1031+
- Update caniuse to fix failed integration tests ([#2322](https://github.com/opensearch-project/OpenSearch-Dashboards/pull/2322))

src/plugins/workspace/common/constants.ts

+1
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@
33
* SPDX-License-Identifier: Apache-2.0
44
*/
55

6+
export const WORKSPACE_ID_STATE_KEY = '_w';
67
export const WORKSPACE_SAVED_OBJECTS_CLIENT_WRAPPER_ID = 'workspace';

src/plugins/workspace/opensearch_dashboards.json

+3-2
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22
"id": "workspace",
33
"version": "opensearchDashboards",
44
"server": true,
5-
"ui": false,
5+
"ui": true,
66
"requiredPlugins": [
7-
"savedObjects"
7+
"savedObjects",
8+
"opensearchDashboardsUtils"
89
],
910
"optionalPlugins": [],
1011
"requiredBundles": []

src/plugins/workspace/public/plugin.ts

+89-2
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,97 @@
33
* SPDX-License-Identifier: Apache-2.0
44
*/
55

6-
import { Plugin } from '../../../core/public';
6+
import { BehaviorSubject, combineLatest } from 'rxjs';
7+
import { debounce } from 'lodash';
8+
import { CoreSetup, Plugin } from '../../../core/public';
9+
import { getStateFromOsdUrl } from '../../opensearch_dashboards_utils/public';
10+
import { formatUrlWithWorkspaceId } from './utils';
11+
import { WORKSPACE_ID_STATE_KEY } from '../common/constants';
712

813
export class WorkspacePlugin implements Plugin<{}, {}, {}> {
9-
public async setup() {
14+
private core?: CoreSetup;
15+
private URLChange$ = new BehaviorSubject('');
16+
private getWorkpsaceIdFromURL(): string | null {
17+
return getStateFromOsdUrl(WORKSPACE_ID_STATE_KEY);
18+
}
19+
private async getWorkpsaceId(): Promise<string> {
20+
if (this.getWorkpsaceIdFromURL()) {
21+
return this.getWorkpsaceIdFromURL() || '';
22+
}
23+
24+
return (await this.core?.workspaces.currentWorkspaceId$.getValue()) || '';
25+
}
26+
private getPatchedUrl = (url: string, workspaceId: string) => {
27+
return formatUrlWithWorkspaceId(url, workspaceId);
28+
};
29+
private async listenToHashChange(): Promise<void> {
30+
window.addEventListener('hashchange', async () => {
31+
if (this.shouldPatchUrl()) {
32+
const workspaceId = await this.getWorkpsaceId();
33+
this.URLChange$.next(this.getPatchedUrl(window.location.href, workspaceId));
34+
}
35+
});
36+
}
37+
private shouldPatchUrl(): boolean {
38+
const currentWorkspaceId = this.core?.workspaces.currentWorkspaceId$.getValue();
39+
const workspaceIdFromURL = this.getWorkpsaceIdFromURL();
40+
if (!currentWorkspaceId && !workspaceIdFromURL) {
41+
return false;
42+
}
43+
44+
if (currentWorkspaceId === workspaceIdFromURL) {
45+
return false;
46+
}
47+
48+
return true;
49+
}
50+
private async listenToApplicationChange(): Promise<void> {
51+
const startService = await this.core?.getStartServices();
52+
if (startService) {
53+
combineLatest([
54+
this.core?.workspaces.currentWorkspaceId$,
55+
startService[0].application.currentAppId$,
56+
]).subscribe(async ([]) => {
57+
if (this.shouldPatchUrl()) {
58+
const currentWorkspaceId = await this.getWorkpsaceId();
59+
this.URLChange$.next(this.getPatchedUrl(window.location.href, currentWorkspaceId));
60+
}
61+
});
62+
}
63+
}
64+
public async setup(core: CoreSetup) {
65+
this.core = core;
66+
/**
67+
* Retrive workspace id from url
68+
*/
69+
const workspaceId = this.getWorkpsaceIdFromURL();
70+
71+
if (workspaceId) {
72+
/**
73+
* Enter a workspace
74+
*/
75+
this.core.workspaces.currentWorkspaceId$.next(workspaceId);
76+
}
77+
78+
/**
79+
* listen to application change and patch workspace id in hash
80+
*/
81+
this.listenToApplicationChange();
82+
83+
/**
84+
* listen to application internal hash change and patch workspace id in hash
85+
*/
86+
this.listenToHashChange();
87+
88+
/**
89+
* All the URLChange will flush in this subscriber
90+
*/
91+
this.URLChange$.subscribe(
92+
debounce(async (url) => {
93+
history.replaceState(history.state, '', url);
94+
}, 500)
95+
);
96+
1097
return {};
1198
}
1299

src/plugins/workspace/public/utils.ts

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/*
2+
* Copyright OpenSearch Contributors
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
import { setStateToOsdUrl } from '../../opensearch_dashboards_utils/public';
7+
import { WORKSPACE_ID_STATE_KEY } from '../common/constants';
8+
9+
export const formatUrlWithWorkspaceId = (url: string, workspaceId: string) => {
10+
return setStateToOsdUrl(WORKSPACE_ID_STATE_KEY, workspaceId, undefined, url);
11+
};

0 commit comments

Comments
 (0)