|
3 | 3 | * SPDX-License-Identifier: Apache-2.0
|
4 | 4 | */
|
5 | 5 |
|
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'; |
7 | 12 |
|
8 | 13 | 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 | + |
10 | 97 | return {};
|
11 | 98 | }
|
12 | 99 |
|
|
0 commit comments