Skip to content

Commit

Permalink
Fix routing
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
abbyhu2000 committed Jun 22, 2023
1 parent 10b2ccb commit 23c5dae
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 28 deletions.
36 changes: 27 additions & 9 deletions src/plugins/dashboard/public/application/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,30 +11,48 @@

import './app.scss';
import { AppMountParameters } from 'opensearch-dashboards/public';
import React from 'react';
import { Route, Switch } from 'react-router-dom';
import React, { useEffect } from 'react';
import { Route, Switch, useLocation } from 'react-router-dom';
import { DashboardConstants, createDashboardEditUrl } from '../dashboard_constants';
import { DashboardEditor, DashboardListing, DashboardNoMatch } from './components';
import { useOpenSearchDashboards } from '../../../opensearch_dashboards_react/public';
import { DashboardServices } from '../types';
import { syncQueryStateWithUrl } from '../../../data/public';

export interface DashboardAppProps {
onAppLeave: AppMountParameters['onAppLeave'];
}

export const DashboardApp = ({ onAppLeave }: DashboardAppProps) => {
const {
services: {
data: { query },
osdUrlStateStorage,
},
} = useOpenSearchDashboards<DashboardServices>();
const { pathname } = useLocation();

Check warning on line 33 in src/plugins/dashboard/public/application/app.tsx

View check run for this annotation

Codecov / codecov/patch

src/plugins/dashboard/public/application/app.tsx#L32-L33

Added lines #L32 - L33 were not covered by tests

useEffect(() => {

Check warning on line 35 in src/plugins/dashboard/public/application/app.tsx

View check run for this annotation

Codecov / codecov/patch

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

Added line #L35 was not covered by tests
// syncs `_g` portion of url with query services
const { stop } = syncQueryStateWithUrl(query, osdUrlStateStorage);

Check warning on line 37 in src/plugins/dashboard/public/application/app.tsx

View check run for this annotation

Codecov / codecov/patch

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

Added line #L37 was not covered by tests

return () => stop();

Check warning on line 39 in src/plugins/dashboard/public/application/app.tsx

View check run for this annotation

Codecov / codecov/patch

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

Added line #L39 was not covered by tests

// this effect should re-run when pathname is changed to preserve querystring part,
// so the global state is always preserved
}, [query, osdUrlStateStorage, pathname]);

return (
<Switch>
<Route exact path={['/', DashboardConstants.LANDING_PAGE_PATH]}>
<DashboardListing />
</Route>
<Route
exact
path={[DashboardConstants.CREATE_NEW_DASHBOARD_URL, createDashboardEditUrl(':id')]}
>
<Route path={[DashboardConstants.CREATE_NEW_DASHBOARD_URL, createDashboardEditUrl(':id')]}>
<div className="app-container dshAppContainer">
<DashboardEditor />
<div id="dashboardViewport" />
</div>
</Route>
<Route exact path={['/', DashboardConstants.LANDING_PAGE_PATH]}>
<DashboardListing />
</Route>
<DashboardNoMatch />
</Switch>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ export const DashboardListing = () => {
notifications,
savedDashboards,
dashboardProviders,
addBasePath,
},
} = useOpenSearchDashboards<DashboardServices>();

Expand Down Expand Up @@ -90,21 +89,14 @@ export const DashboardListing = () => {
};

const editItem = useCallback(
({ editUrl }: any) => {
if (addBasePath) {
history.push(addBasePath(editUrl));
({ appId, editUrl }: any) => {
if (appId === 'dashboard') {
history.push(editUrl);

Check warning on line 94 in src/plugins/dashboard/public/application/components/dashboard_listing.tsx

View check run for this annotation

Codecov / codecov/patch

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

Added line #L94 was not covered by tests
} else {
application.navigateToUrl(editUrl);

Check warning on line 96 in src/plugins/dashboard/public/application/components/dashboard_listing.tsx

View check run for this annotation

Codecov / codecov/patch

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

Added line #L96 was not covered by tests
}
},
[history, addBasePath]
);

const viewItem = useCallback(
({ viewUrl }: any) => {
if (addBasePath) {
history.push(addBasePath(viewUrl));
}
},
[history, addBasePath]
[history, application]
);

const deleteItems = useCallback(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,21 @@
* SPDX-License-Identifier: Apache-2.0
*/

import React from 'react';
import { useEffect } from 'react';
import { useOpenSearchDashboards } from '../../../../opensearch_dashboards_react/public';
import { DashboardServices } from '../../types';

export const DashboardNoMatch = () => {
return <div>Dashboard No Match</div>;
const { services } = useOpenSearchDashboards<DashboardServices>();
useEffect(() => {
const path = window.location.hash.substr(1);
services.restorePreviousUrl();

Check warning on line 14 in src/plugins/dashboard/public/application/components/dashboard_no_match.tsx

View check run for this annotation

Codecov / codecov/patch

src/plugins/dashboard/public/application/components/dashboard_no_match.tsx#L11-L14

Added lines #L11 - L14 were not covered by tests

const { navigated } = services.navigateToLegacyOpenSearchDashboardsUrl(path);

Check warning on line 16 in src/plugins/dashboard/public/application/components/dashboard_no_match.tsx

View check run for this annotation

Codecov / codecov/patch

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

Added line #L16 was not covered by tests
if (!navigated) {
services.navigateToDefaultApp();

Check warning on line 18 in src/plugins/dashboard/public/application/components/dashboard_no_match.tsx

View check run for this annotation

Codecov / codecov/patch

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

Added line #L18 was not covered by tests
}
}, [services]);

return null;

Check warning on line 22 in src/plugins/dashboard/public/application/components/dashboard_no_match.tsx

View check run for this annotation

Codecov / codecov/patch

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

Added line #L22 was not covered by tests
};
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ export const useDashboardContainer = (

useEffect(() => {
const incomingEmbeddable = services.embeddable
.getStateTransfer(services.scopedHistory())
.getStateTransfer(services.scopedHistory)
.getIncomingEmbeddablePackage();

if (
Expand Down
10 changes: 9 additions & 1 deletion src/plugins/dashboard/public/plugin.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,12 @@ export class DashboardPlugin
savedObjects,
} = pluginsStart;

// dispatch synthetic hash change event to update hash history objects
// this is necessary because hash updates triggered by using popState won't trigger this event naturally.
const unlistenParentHistory = params.history.listen(() => {
window.dispatchEvent(new HashChangeEvent('hashchange'));

Check warning on line 391 in src/plugins/dashboard/public/plugin.tsx

View check run for this annotation

Codecov / codecov/patch

src/plugins/dashboard/public/plugin.tsx#L390-L391

Added lines #L390 - L391 were not covered by tests
});

const history = createHashHistory(); // need more research
const services: DashboardServices = {
...coreStart,
Expand Down Expand Up @@ -418,7 +424,7 @@ export class DashboardPlugin
},
localStorage: new Storage(localStorage),
usageCollection,
scopedHistory: () => this.currentHistory!,
scopedHistory: params.history,
setHeaderActionMenu: params.setHeaderActionMenu,
savedObjectsPublic: savedObjects,
restorePreviousUrl,
Expand All @@ -430,6 +436,8 @@ export class DashboardPlugin
const { renderApp } = await import('./application');
const unmount = renderApp(params, services);
return () => {
params.element.classList.remove('dshAppContainer');
unlistenParentHistory();

Check warning on line 440 in src/plugins/dashboard/public/plugin.tsx

View check run for this annotation

Codecov / codecov/patch

src/plugins/dashboard/public/plugin.tsx#L439-L440

Added lines #L439 - L440 were not covered by tests
unmount();
appUnMounted();
};
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/dashboard/public/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ export interface DashboardServices extends CoreStart {
usageCollection?: UsageCollectionSetup;
navigateToDefaultApp: UrlForwardingStart['navigateToDefaultApp'];
navigateToLegacyOpenSearchDashboardsUrl: UrlForwardingStart['navigateToLegacyOpenSearchDashboardsUrl'];
scopedHistory: () => ScopedHistory;
scopedHistory: ScopedHistory;
setHeaderActionMenu: AppMountParameters['setHeaderActionMenu'];
savedObjectsPublic: SavedObjectsStart;
restorePreviousUrl: () => void;
Expand Down

0 comments on commit 23c5dae

Please sign in to comment.