Skip to content

Commit 43a3d96

Browse files
committed
Fix the edit action routing on the dashboard listing page; also fix routing when the route has no match. Add '_g' param to the URL on both dashboard listing page and dashboard editor page. Signed-off-by: abbyhu2000 <abigailhu2000@gmail.com>
1 parent b5e11fa commit 43a3d96

File tree

6 files changed

+69
-27
lines changed

6 files changed

+69
-27
lines changed

src/plugins/dashboard/public/application/app.tsx

+27-9
Original file line numberDiff line numberDiff line change
@@ -11,30 +11,48 @@
1111

1212
import './app.scss';
1313
import { AppMountParameters } from 'opensearch-dashboards/public';
14-
import React from 'react';
15-
import { Route, Switch } from 'react-router-dom';
14+
import React, { useEffect } from 'react';
15+
import { Route, Switch, useLocation } from 'react-router-dom';
1616
import { DashboardConstants, createDashboardEditUrl } from '../dashboard_constants';
1717
import { DashboardEditor, DashboardListing, DashboardNoMatch } from './components';
18+
import { useOpenSearchDashboards } from '../../../opensearch_dashboards_react/public';
19+
import { DashboardServices } from '../types';
20+
import { syncQueryStateWithUrl } from '../../../data/public';
1821

1922
export interface DashboardAppProps {
2023
onAppLeave: AppMountParameters['onAppLeave'];
2124
}
2225

2326
export const DashboardApp = ({ onAppLeave }: DashboardAppProps) => {
27+
const {
28+
services: {
29+
data: { query },
30+
osdUrlStateStorage,
31+
},
32+
} = useOpenSearchDashboards<DashboardServices>();
33+
const { pathname } = useLocation();
34+
35+
useEffect(() => {
36+
// syncs `_g` portion of url with query services
37+
const { stop } = syncQueryStateWithUrl(query, osdUrlStateStorage);
38+
39+
return () => stop();
40+
41+
// this effect should re-run when pathname is changed to preserve querystring part,
42+
// so the global state is always preserved
43+
}, [query, osdUrlStateStorage, pathname]);
44+
2445
return (
2546
<Switch>
26-
<Route exact path={['/', DashboardConstants.LANDING_PAGE_PATH]}>
27-
<DashboardListing />
28-
</Route>
29-
<Route
30-
exact
31-
path={[DashboardConstants.CREATE_NEW_DASHBOARD_URL, createDashboardEditUrl(':id')]}
32-
>
47+
<Route path={[DashboardConstants.CREATE_NEW_DASHBOARD_URL, createDashboardEditUrl(':id')]}>
3348
<div className="app-container dshAppContainer">
3449
<DashboardEditor />
3550
<div id="dashboardViewport" />
3651
</div>
3752
</Route>
53+
<Route exact path={['/', DashboardConstants.LANDING_PAGE_PATH]}>
54+
<DashboardListing />
55+
</Route>
3856
<DashboardNoMatch />
3957
</Switch>
4058
);

src/plugins/dashboard/public/application/components/dashboard_listing.tsx

+16-13
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ export const DashboardListing = () => {
3131
notifications,
3232
savedDashboards,
3333
dashboardProviders,
34-
addBasePath,
3534
},
3635
} = useOpenSearchDashboards<DashboardServices>();
3736

@@ -90,22 +89,26 @@ export const DashboardListing = () => {
9089
};
9190

9291
const editItem = useCallback(
93-
({ editUrl }: any) => {
94-
if (addBasePath) {
95-
history.push(addBasePath(editUrl));
92+
({ appId, editUrl }: any) => {
93+
if (appId === 'dashboard') {
94+
history.push(editUrl);
95+
} else {
96+
application.navigateToUrl(editUrl);
9697
}
9798
},
98-
[history, addBasePath]
99+
[history, application]
99100
);
100101

101-
const viewItem = useCallback(
102-
({ viewUrl }: any) => {
103-
if (addBasePath) {
104-
history.push(addBasePath(viewUrl));
105-
}
106-
},
107-
[history, addBasePath]
108-
);
102+
// const viewItem = useCallback(
103+
// ({ appId, viewUrl }: any) => {
104+
// if (appId === 'dashboard') {
105+
// history.push(viewUrl);
106+
// } else {
107+
// application.navigateToUrl(viewUrl);
108+
// }
109+
// },
110+
// [history, application]
111+
// );
109112

110113
const deleteItems = useCallback(
111114
(dashboards: object[]) => {

src/plugins/dashboard/public/application/components/dashboard_no_match.tsx

+15-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,21 @@
33
* SPDX-License-Identifier: Apache-2.0
44
*/
55

6-
import React from 'react';
6+
import { useEffect } from 'react';
7+
import { useOpenSearchDashboards } from '../../../../opensearch_dashboards_react/public';
8+
import { DashboardServices } from '../../types';
79

810
export const DashboardNoMatch = () => {
9-
return <div>Dashboard No Match</div>;
11+
const { services } = useOpenSearchDashboards<DashboardServices>();
12+
useEffect(() => {
13+
const path = window.location.hash.substring(1);
14+
services.restorePreviousUrl();
15+
16+
const { navigated } = services.navigateToLegacyOpenSearchDashboardsUrl(path);
17+
if (!navigated) {
18+
services.navigateToDefaultApp();
19+
}
20+
}, [services]);
21+
22+
return null;
1023
};

src/plugins/dashboard/public/application/utils/use/use_dashboard_container.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ export const useDashboardContainer = (
8686

8787
useEffect(() => {
8888
const incomingEmbeddable = services.embeddable
89-
.getStateTransfer(services.scopedHistory())
89+
.getStateTransfer(services.scopedHistory)
9090
.getIncomingEmbeddablePackage();
9191

9292
if (

src/plugins/dashboard/public/plugin.tsx

+9-1
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,12 @@ export class DashboardPlugin
385385
savedObjects,
386386
} = pluginsStart;
387387

388+
// dispatch synthetic hash change event to update hash history objects
389+
// this is necessary because hash updates triggered by using popState won't trigger this event naturally.
390+
const unlistenParentHistory = params.history.listen(() => {
391+
window.dispatchEvent(new HashChangeEvent('hashchange'));
392+
});
393+
388394
const history = createHashHistory(); // need more research
389395
const services: DashboardServices = {
390396
...coreStart,
@@ -418,7 +424,7 @@ export class DashboardPlugin
418424
},
419425
localStorage: new Storage(localStorage),
420426
usageCollection,
421-
scopedHistory: () => this.currentHistory!,
427+
scopedHistory: params.history,
422428
setHeaderActionMenu: params.setHeaderActionMenu,
423429
savedObjectsPublic: savedObjects,
424430
restorePreviousUrl,
@@ -430,6 +436,8 @@ export class DashboardPlugin
430436
const { renderApp } = await import('./application');
431437
const unmount = renderApp(params, services);
432438
return () => {
439+
params.element.classList.remove('dshAppContainer');
440+
unlistenParentHistory();
433441
unmount();
434442
appUnMounted();
435443
};

src/plugins/dashboard/public/types.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ export interface DashboardServices extends CoreStart {
270270
usageCollection?: UsageCollectionSetup;
271271
navigateToDefaultApp: UrlForwardingStart['navigateToDefaultApp'];
272272
navigateToLegacyOpenSearchDashboardsUrl: UrlForwardingStart['navigateToLegacyOpenSearchDashboardsUrl'];
273-
scopedHistory: () => ScopedHistory;
273+
scopedHistory: ScopedHistory;
274274
setHeaderActionMenu: AppMountParameters['setHeaderActionMenu'];
275275
savedObjectsPublic: SavedObjectsStart;
276276
restorePreviousUrl: () => void;

0 commit comments

Comments
 (0)