import { Action, KBarProvider } from 'kbar'; import React, { ComponentType } from 'react'; import { Provider } from 'react-redux'; import { Router, Redirect, Switch, RouteComponentProps } from 'react-router-dom'; import { CompatRouter, CompatRoute } from 'react-router-dom-v5-compat'; import { config, locationService, navigationLogger, reportInteraction } from '@grafana/runtime'; import { ErrorBoundaryAlert, GlobalStyles, ModalRoot, ModalsProvider, PortalContainer } from '@grafana/ui'; import { getAppRoutes } from 'app/routes/routes'; import { store } from 'app/store/store'; import { AngularRoot } from './angular/AngularRoot'; import { loadAndInitAngularIfEnabled } from './angular/loadAndInitAngularIfEnabled'; import { GrafanaApp } from './app'; import { AppChrome } from './core/components/AppChrome/AppChrome'; import { AppNotificationList } from './core/components/AppNotifications/AppNotificationList'; import { GrafanaContext } from './core/context/GrafanaContext'; import { GrafanaRoute } from './core/navigation/GrafanaRoute'; import { RouteDescriptor } from './core/navigation/types'; import { contextSrv } from './core/services/context_srv'; import { ThemeProvider } from './core/utils/ConfigProvider'; import { LiveConnectionWarning } from './features/live/LiveConnectionWarning'; interface AppWrapperProps { app: GrafanaApp; } interface AppWrapperState { ready?: boolean; } /** Used by enterprise */ let bodyRenderHooks: ComponentType[] = []; let pageBanners: ComponentType[] = []; export function addBodyRenderHook(fn: ComponentType) { bodyRenderHooks.push(fn); } export function addPageBanner(fn: ComponentType) { pageBanners.push(fn); } export class AppWrapper extends React.Component { constructor(props: AppWrapperProps) { super(props); this.state = {}; } async componentDidMount() { await loadAndInitAngularIfEnabled(); this.setState({ ready: true }); $('.preloader').remove(); } renderRoute = (route: RouteDescriptor) => { const roles = route.roles ? route.roles() : []; return ( { // TODO[Router]: test this logic if (roles?.length) { if (!roles.some((r: string) => contextSrv.hasRole(r))) { return ; } } return ; }} /> ); }; renderRoutes() { return {getAppRoutes().map((r) => this.renderRoute(r))}; } render() { const { app } = this.props; const { ready } = this.state; navigationLogger('AppWrapper', false, 'rendering'); const commandPaletteActionSelected = (action: Action) => { reportInteraction('command_palette_action_selected', { actionId: action.id, actionName: action.name, }); }; return (
{pageBanners.map((Banner, index) => ( ))} {ready && this.renderRoutes()} {bodyRenderHooks.map((Hook, index) => ( ))}
); } }