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

[Dashboard De-Angular] Add additional routing configurations #4357

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
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();

useEffect(() => {
// syncs `_g` portion of url with query services
const { stop } = syncQueryStateWithUrl(query, osdUrlStateStorage);

return () => stop();

// 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')]}>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This part could be polished later. I think maybe get render logic completely out and only routing logic look nicer? something like

return (
  <Switch>
    <Route 
      path={[DashboardConstants.CREATE_NEW_DASHBOARD_URL, createDashboardEditUrl(':id')]} 
      render={renderDashboardViewPoint}
    />
    <Route 
      exact 
      path={['/', DashboardConstants.LANDING_PAGE_PATH]} 
      render={renderDashboardListing}
    />
    <Route render={renderDashboardNoMatch} />
  </Switch>
);

Copy link
Member Author

@abbyhu2000 abbyhu2000 Jun 23, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yea this can prob be polished better. Originally I wish to do it like:

<Switch>
   <Route path= ...> 
        <DashboardEditor>
   </Route>

   <Route path= ...> 
        <DashboardListing>
   </Route>

   <Route path= ...> 
        <DashboardNoMatch>
   </Route>

</Switch>

However, since we we are rendering the dashboard page by calling the render() function of dashboard container: dashboardContainer.render(dashboardDom); and we need to pass in a HTML element here, so i added <div id="dashboardViewport" /> so we can refer the <div> element by its id and pass it to the render function for it to be rendered or updated. There might be a better way but will need to look more into it.

<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,22 +89,26 @@ export const DashboardListing = () => {
};

const editItem = useCallback(
({ editUrl }: any) => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we will likely need to revisit the dashboards providers work as this appears to have broken functionality of that.

if (addBasePath) {
history.push(addBasePath(editUrl));
({ appId, editUrl }: any) => {
if (appId === 'dashboard') {
history.push(editUrl);
} else {
application.navigateToUrl(editUrl);
}
},
[history, addBasePath]
[history, application]
);

const viewItem = useCallback(
({ viewUrl }: any) => {
if (addBasePath) {
history.push(addBasePath(viewUrl));
}
},
[history, addBasePath]
);
// const viewItem = useCallback(
// ({ appId, viewUrl }: any) => {
// if (appId === 'dashboard') {
// history.push(viewUrl);
// } else {
// application.navigateToUrl(viewUrl);
// }
// },
// [history, application]
// );

const deleteItems = useCallback(
(dashboards: object[]) => {
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.substring(1);
services.restorePreviousUrl();

const { navigated } = services.navigateToLegacyOpenSearchDashboardsUrl(path);
if (!navigated) {
services.navigateToDefaultApp();
}
}, [services]);

return null;
};
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'));
});

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();
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