mirror of
https://github.com/elastic/kibana.git
synced 2025-06-27 18:51:07 -04:00
## Summary Why? To simplify the process of migration to react-router@6. https://github.com/remix-run/react-router/discussions/8753 What problems exactly it solves? - In my previous PR I added `CompatRouter` https://github.com/elastic/kibana/pull/159173, which caused changes in ~50 files and pinged 15 Teams. And this is just meant to be a temporary change, so when we're done with the migration I would have to revert these changes and engage everyone to review the PR again. And it is just a single step in the migration strategy. So to make our lives easier I think it would be better to have a common place where we do import our router components because it will allow us to surface some extra logic in single place instead of going through the whole source code again. - `react-router@6` doesn't support a custom `Route` component, so that means our custom `Route` component that we're using almost everywhere today, will need to be replaced by a different solution. I have decided to add `Routes` component, which will be responsible for rendering the proper component (`react-router@6` renamed `Switch` to `Routes`, so I have named this component to align with the dictionary of the new router) and also is going to add the logic that today is done in `Route` (moving logic to `Routes` will be done in the follow-up PR, here I just wanted to focus on using the common router components to make the review process easier) --------- Co-authored-by: kibanamachine <42973632+kibanamachine@users.noreply.github.com>
134 lines
4.7 KiB
TypeScript
134 lines
4.7 KiB
TypeScript
/*
|
|
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
|
|
* or more contributor license agreements. Licensed under the Elastic License
|
|
* 2.0 and the Server Side Public License, v 1; you may not use this file except
|
|
* in compliance with, at your election, the Elastic License 2.0 or the Server
|
|
* Side Public License, v 1.
|
|
*/
|
|
|
|
import ReactDOM from 'react-dom';
|
|
import React, { useMemo } from 'react';
|
|
import { useAsync } from 'react-use/lib';
|
|
import { Redirect } from 'react-router-dom';
|
|
import { Router, Routes, Route } from '@kbn/shared-ux-router';
|
|
import { AppMountParameters } from '@kbn/core/public';
|
|
import { EuiButton, EuiCallOut, EuiSpacer } from '@elastic/eui';
|
|
import { DashboardListingTable } from '@kbn/dashboard-plugin/public';
|
|
import { KibanaPageTemplate } from '@kbn/shared-ux-page-kibana-template';
|
|
|
|
import { DualReduxExample } from './dual_redux_example';
|
|
import { PortableDashboardsExampleStartDeps } from './plugin';
|
|
import { StaticByValueExample } from './static_by_value_example';
|
|
import { StaticByReferenceExample } from './static_by_reference_example';
|
|
import { DynamicByReferenceExample } from './dynamically_add_panels_example';
|
|
import { DashboardWithControlsExample } from './dashboard_with_controls_example';
|
|
|
|
const DASHBOARD_DEMO_PATH = '/dashboardDemo';
|
|
const DASHBOARD_LIST_PATH = '/listingDemo';
|
|
|
|
export const renderApp = async (
|
|
{ data, dashboard }: PortableDashboardsExampleStartDeps,
|
|
{ element, history }: AppMountParameters
|
|
) => {
|
|
ReactDOM.render(
|
|
<PortableDashboardsDemos data={data} history={history} dashboard={dashboard} />,
|
|
element
|
|
);
|
|
return () => ReactDOM.unmountComponentAtNode(element);
|
|
};
|
|
|
|
const PortableDashboardsDemos = ({
|
|
data,
|
|
dashboard,
|
|
history,
|
|
}: {
|
|
data: PortableDashboardsExampleStartDeps['data'];
|
|
dashboard: PortableDashboardsExampleStartDeps['dashboard'];
|
|
history: AppMountParameters['history'];
|
|
}) => {
|
|
return (
|
|
<Router history={history}>
|
|
<Routes>
|
|
<Route exact path="/">
|
|
<Redirect to={DASHBOARD_DEMO_PATH} />
|
|
</Route>
|
|
<Route path={DASHBOARD_LIST_PATH}>
|
|
<PortableDashboardListingDemo history={history} />
|
|
</Route>
|
|
<Route path={DASHBOARD_DEMO_PATH}>
|
|
<DashboardsDemo data={data} dashboard={dashboard} history={history} />
|
|
</Route>
|
|
</Routes>
|
|
</Router>
|
|
);
|
|
};
|
|
|
|
const DashboardsDemo = ({
|
|
data,
|
|
history,
|
|
dashboard,
|
|
}: {
|
|
history: AppMountParameters['history'];
|
|
data: PortableDashboardsExampleStartDeps['data'];
|
|
dashboard: PortableDashboardsExampleStartDeps['dashboard'];
|
|
}) => {
|
|
const { loading, value: dataviewResults } = useAsync(async () => {
|
|
const dataViews = await data.dataViews.find('kibana_sample_data_logs');
|
|
const findDashboardsService = await dashboard.findDashboardsService();
|
|
const logsSampleDashboardId = (await findDashboardsService?.findByTitle('[Logs] Web Traffic'))
|
|
?.id;
|
|
return { dataViews, logsSampleDashboardId };
|
|
}, []);
|
|
|
|
const usageDemos = useMemo(() => {
|
|
if (loading || !dataviewResults) return null;
|
|
if (dataviewResults?.dataViews.length === 0) {
|
|
<div>{'Install web logs sample data to run the embeddable dashboard examples.'}</div>;
|
|
}
|
|
const { dataViews, logsSampleDashboardId } = dataviewResults;
|
|
return (
|
|
<>
|
|
<DashboardWithControlsExample dataView={dataViews[0]} />
|
|
<EuiSpacer size="xl" />
|
|
<DynamicByReferenceExample />
|
|
<EuiSpacer size="xl" />
|
|
<DualReduxExample />
|
|
<EuiSpacer size="xl" />
|
|
<StaticByReferenceExample dashboardId={logsSampleDashboardId} dataView={dataViews[0]} />
|
|
<EuiSpacer size="xl" />
|
|
<StaticByValueExample />
|
|
</>
|
|
);
|
|
}, [dataviewResults, loading]);
|
|
|
|
return (
|
|
<KibanaPageTemplate>
|
|
<KibanaPageTemplate.Header pageTitle="Portable dashboards usage" />
|
|
<KibanaPageTemplate.Section>
|
|
<EuiButton onClick={() => history.push(DASHBOARD_LIST_PATH)}>
|
|
View portable dashboard listing page
|
|
</EuiButton>
|
|
<EuiSpacer size="xl" />
|
|
{usageDemos}
|
|
</KibanaPageTemplate.Section>
|
|
</KibanaPageTemplate>
|
|
);
|
|
};
|
|
|
|
const PortableDashboardListingDemo = ({ history }: { history: AppMountParameters['history'] }) => {
|
|
return (
|
|
<DashboardListingTable
|
|
goToDashboard={(dashboardId) =>
|
|
alert(`Here's where I would redirect you to ${dashboardId ?? 'a new Dashboard'}`)
|
|
}
|
|
getDashboardUrl={() => 'https://www.elastic.co/'}
|
|
>
|
|
<EuiButton onClick={() => history.push(DASHBOARD_DEMO_PATH)}>
|
|
Go back to usage demos
|
|
</EuiButton>
|
|
<EuiSpacer size="xl" />
|
|
<EuiCallOut title="You can render something cool here" iconType="search" />
|
|
<EuiSpacer size="xl" />
|
|
</DashboardListingTable>
|
|
);
|
|
};
|