forked from grafana/grafana
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AppWrapper.tsx
160 lines (138 loc) · 5.35 KB
/
AppWrapper.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
import { Action, KBarProvider } from 'kbar';
import React, { ComponentType } from 'react';
import { Provider } from 'react-redux';
import { Router, Route, Redirect, Switch } from 'react-router-dom';
import { config, locationService, navigationLogger, reportInteraction } from '@grafana/runtime';
import { ErrorBoundaryAlert, GlobalStyles, ModalRoot, ModalsProvider, PortalContainer } from '@grafana/ui';
import { SearchWrapper } from 'app/features/search';
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 { NavBar } from './core/components/NavBar/NavBar';
import { GrafanaContext } from './core/context/GrafanaContext';
import { I18nProvider } from './core/internationalization';
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 { CommandPalette } from './features/commandPalette/CommandPalette';
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<AppWrapperProps, AppWrapperState> {
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 (
<Route
exact={route.exact === undefined ? true : route.exact}
path={route.path}
key={route.path}
render={(props) => {
navigationLogger('AppWrapper', false, 'Rendering route', route, 'with match', props.location);
// TODO[Router]: test this logic
if (roles?.length) {
if (!roles.some((r: string) => contextSrv.hasRole(r))) {
return <Redirect to="/" />;
}
}
return <GrafanaRoute {...props} route={route} />;
}}
/>
);
};
renderRoutes() {
return <Switch>{getAppRoutes().map((r) => this.renderRoute(r))}</Switch>;
}
renderNavBar() {
if (config.isPublicDashboardView || !this.state.ready || config.featureToggles.topnav) {
return null;
}
return <NavBar />;
}
commandPaletteEnabled() {
return config.featureToggles.commandPalette && !config.isPublicDashboardView;
}
searchBarEnabled() {
return !config.isPublicDashboardView;
}
render() {
const { app } = this.props;
const { ready } = this.state;
navigationLogger('AppWrapper', false, 'rendering');
const commandPaletteActionSelected = (action: Action) => {
reportInteraction('commandPalette_action_selected', {
actionId: action.id,
actionName: action.name,
});
};
return (
<React.StrictMode>
<Provider store={store}>
<I18nProvider>
<ErrorBoundaryAlert style="page">
<GrafanaContext.Provider value={app.context}>
<ThemeProvider value={config.theme2}>
<KBarProvider
actions={[]}
options={{ enableHistory: true, callbacks: { onSelectAction: commandPaletteActionSelected } }}
>
<ModalsProvider>
<GlobalStyles />
{this.commandPaletteEnabled() && <CommandPalette />}
<div className="grafana-app">
<Router history={locationService.getHistory()}>
{this.renderNavBar()}
<AppChrome>
{pageBanners.map((Banner, index) => (
<Banner key={index.toString()} />
))}
<AngularRoot />
<AppNotificationList />
{this.searchBarEnabled() && <SearchWrapper />}
{ready && this.renderRoutes()}
{bodyRenderHooks.map((Hook, index) => (
<Hook key={index.toString()} />
))}
</AppChrome>
</Router>
</div>
<LiveConnectionWarning />
<ModalRoot />
<PortalContainer />
</ModalsProvider>
</KBarProvider>
</ThemeProvider>
</GrafanaContext.Provider>
</ErrorBoundaryAlert>
</I18nProvider>
</Provider>
</React.StrictMode>
);
}
}