grafana/public/app/features/plugins/components/AppPluginLoader.tsx
Torkel Ödegaard 1e85a6f4fd
TopNav: New page layouts (#51510)
* First stab at new page layouts behind feature toggle

* Simplifying PageHeader

* Progress on a new model that can more easily support new and old page layouts

* Progress

* rename folder

* Progress

* Minor change

* fixes

* Fixing tests

* Make breadcrumbs work

* Add tests for old Page component

* Adding tests for new Page component and behavior

* fixing page header test

* Fixed test

* AppChrome outside route

* Renaming folder

* Minor fix

* Updated

* Fixing StoragePage

* Fix for banners

Co-authored-by: Ashley Harrison <ashley.harrison@grafana.com>
2022-07-06 17:00:56 +02:00

45 lines
1.4 KiB
TypeScript

import React, { useState } from 'react';
import { useLocation, useParams } from 'react-router-dom';
import { NavModel } from '@grafana/data';
import { getWarningNav } from 'app/angular/services/nav_model_srv';
import { Page } from 'app/core/components/Page/Page';
import PageLoader from 'app/core/components/PageLoader/PageLoader';
import { useImportAppPlugin } from '../hooks/useImportAppPlugin';
type AppPluginLoaderProps = {
// The id of the app plugin to be loaded
id: string;
// The base URL path - defaults to the current path
basePath?: string;
};
// This component can be used to render an app-plugin based on its plugin ID.
export const AppPluginLoader = ({ id, basePath }: AppPluginLoaderProps) => {
const [nav, setNav] = useState<NavModel | null>(null);
const { value: plugin, error, loading } = useImportAppPlugin(id);
const queryParams = useParams();
const { pathname } = useLocation();
if (error) {
return <Page.Header navItem={getWarningNav(error.message, error.stack).main} />;
}
return (
<>
{loading && <PageLoader />}
{nav && <Page.Header navItem={nav.main} />}
{!loading && plugin && plugin.root && (
<plugin.root
meta={plugin.meta}
basename={basePath || pathname}
onNavChanged={setNav}
query={queryParams}
path={pathname}
/>
)}
</>
);
};