forked from grafana/grafana
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrender-fn-dashboard.tsx
75 lines (63 loc) · 2.22 KB
/
render-fn-dashboard.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
import { merge, isFunction } from 'lodash';
import React, { useEffect, FC, useMemo } from 'react';
import { locationService as locationSrv, HistoryWrapper } from '@grafana/runtime';
import DashboardPage, { DashboardPageProps } from 'app/features/dashboard/containers/DashboardPage';
import { DashboardRoutes, StoreState, useSelector } from 'app/types';
import { FNDashboardProps } from '../types';
const locationService = locationSrv as HistoryWrapper;
const DEFAULT_DASHBOARD_PAGE_PROPS: Pick<DashboardPageProps, 'history' | 'route'> & {
match: Pick<DashboardPageProps['match'], 'isExact' | 'path' | 'url'>;
} = {
match: {
isExact: true,
path: '/d/:uid/:slug?',
url: '',
},
history: {} as DashboardPageProps['history'],
route: {
routeName: DashboardRoutes.Normal,
path: '/d/:uid/:slug?',
pageClass: 'page-dashboard',
component: DashboardPage,
},
};
export const RenderFNDashboard: FC<FNDashboardProps> = (props) => {
const { queryParams, controlsContainer, setErrors, hiddenVariables, isLoading } = props;
const firstError = useSelector((state: StoreState) => {
const { appNotifications } = state;
return Object.values(appNotifications.byId).find(({ severity }) => severity === 'error');
});
/**
* NOTE:
* Grafana renders notifications in StoredNotifications component.
* We do not use this component in FN.
* But we would like to propagate grafana's errors to FN.
*/
useEffect(() => {
if (!isFunction(setErrors)) {
return;
}
setErrors(firstError ? { [firstError.timestamp]: firstError.text } : {});
}, [firstError, setErrors]);
useEffect(() => {
locationService.fnPathnameChange(window.location.pathname, queryParams);
}, [queryParams]);
const dashboardPageProps: DashboardPageProps = useMemo(
() =>
merge({}, DEFAULT_DASHBOARD_PAGE_PROPS, {
...DEFAULT_DASHBOARD_PAGE_PROPS,
match: {
params: {
...props,
},
},
location: locationService.getLocation(),
queryParams,
hiddenVariables,
controlsContainer,
isLoading
}),
[controlsContainer, hiddenVariables, isLoading, props, queryParams]
);
return <DashboardPage {...dashboardPageProps} />;
};