|
11 | 11 |
|
12 | 12 | import './app.scss';
|
13 | 13 | 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'; |
16 | 16 | import { DashboardConstants, createDashboardEditUrl } from '../dashboard_constants';
|
17 | 17 | 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'; |
18 | 21 |
|
19 | 22 | export interface DashboardAppProps {
|
20 | 23 | onAppLeave: AppMountParameters['onAppLeave'];
|
21 | 24 | }
|
22 | 25 |
|
23 | 26 | 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 | + |
24 | 45 | return (
|
25 | 46 | <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')]}> |
33 | 48 | <div className="app-container dshAppContainer">
|
34 | 49 | <DashboardEditor />
|
35 | 50 | <div id="dashboardViewport" />
|
36 | 51 | </div>
|
37 | 52 | </Route>
|
| 53 | + <Route exact path={['/', DashboardConstants.LANDING_PAGE_PATH]}> |
| 54 | + <DashboardListing /> |
| 55 | + </Route> |
38 | 56 | <DashboardNoMatch />
|
39 | 57 | </Switch>
|
40 | 58 | );
|
|
0 commit comments