-
Notifications
You must be signed in to change notification settings - Fork 8.3k
/
ml_page.tsx
153 lines (141 loc) · 4.96 KB
/
ml_page.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
/*
* 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; you may not use this file except in compliance with the Elastic License
* 2.0.
*/
import React, { createContext, FC, useMemo, useState } from 'react';
import { EuiPageContentBody } from '@elastic/eui';
import { i18n } from '@kbn/i18n';
import { Redirect, Route, Switch } from 'react-router-dom';
import type { AppMountParameters } from '@kbn/core/public';
import { KibanaPageTemplate, RedirectAppLinks } from '@kbn/kibana-react-plugin/public';
import { createHtmlPortalNode, HtmlPortalNode } from 'react-reverse-portal';
import { MlPageHeaderRenderer } from '../page_header/page_header';
import { useSideNavItems } from './side_nav';
import * as routes from '../../routing/routes';
import { MlPageWrapper } from '../../routing/ml_page_wrapper';
import { useMlKibana, useNavigateToPath } from '../../contexts/kibana';
import { MlRoute, PageDependencies } from '../../routing/router';
import { DatePickerWrapper } from '../navigation_menu/date_picker_wrapper';
import { useActiveRoute } from '../../routing/use_active_route';
import { useDocTitle } from '../../routing/use_doc_title';
export const MlPageControlsContext = createContext<{
headerPortal: HtmlPortalNode;
setHeaderActionMenu?: AppMountParameters['setHeaderActionMenu'];
setIsHeaderMounted: (v: boolean) => void;
isHeaderMounted: boolean;
}>({
setHeaderActionMenu: () => {},
headerPortal: createHtmlPortalNode(),
isHeaderMounted: false,
setIsHeaderMounted: () => {},
});
/**
* Main page component of the ML App
* @constructor
*/
export const MlPage: FC<{ pageDeps: PageDependencies }> = React.memo(({ pageDeps }) => {
const navigateToPath = useNavigateToPath();
const {
services: {
http: { basePath },
},
} = useMlKibana();
const headerPortalNode = useMemo(() => createHtmlPortalNode(), []);
const [isHeaderMounted, setIsHeaderMounted] = useState(false);
const routeList = useMemo(
() =>
Object.values(routes)
.map((routeFactory) => routeFactory(navigateToPath, basePath.get()))
.filter((d) => !d.disabled),
[]
);
const activeRoute = useActiveRoute(routeList);
const rightSideItems = useMemo(() => {
return [...(activeRoute.enableDatePicker ? [<DatePickerWrapper />] : [])];
}, [activeRoute.enableDatePicker]);
useDocTitle(activeRoute);
return (
<MlPageControlsContext.Provider
value={{
setHeaderActionMenu: pageDeps.setHeaderActionMenu,
headerPortal: headerPortalNode,
setIsHeaderMounted,
isHeaderMounted,
}}
>
<KibanaPageTemplate
className={'ml-app'}
data-test-subj={'mlApp'}
restrictWidth={false}
// EUI TODO
// The different template options need to be manually recreated by the individual pages.
// These classes help enforce the layouts.
pageContentProps={{ className: 'kbnAppWrapper' }}
pageContentBodyProps={{ className: 'kbnAppWrapper' }}
solutionNav={{
name: i18n.translate('xpack.ml.plugin.title', {
defaultMessage: 'Machine Learning',
}),
icon: 'machineLearningApp',
items: useSideNavItems(activeRoute),
}}
pageHeader={{
pageTitle: <MlPageHeaderRenderer />,
rightSideItems,
restrictWidth: false,
}}
pageBodyProps={{
'data-test-subj': activeRoute?.['data-test-subj'],
}}
>
<CommonPageWrapper
headerPortal={headerPortalNode}
setIsHeaderMounted={setIsHeaderMounted}
pageDeps={pageDeps}
routeList={routeList}
/>
</KibanaPageTemplate>
</MlPageControlsContext.Provider>
);
});
interface CommonPageWrapperProps {
setIsHeaderMounted: (v: boolean) => void;
pageDeps: PageDependencies;
routeList: MlRoute[];
headerPortal: HtmlPortalNode;
}
const CommonPageWrapper: FC<CommonPageWrapperProps> = React.memo(({ pageDeps, routeList }) => {
const {
services: { application },
} = useMlKibana();
return (
/** RedirectAppLinks intercepts all <a> tags to use navigateToUrl
* avoiding full page reload **/
<RedirectAppLinks application={application}>
<EuiPageContentBody restrictWidth={false}>
<Switch>
{routeList.map((route) => {
return (
<Route
key={route.id}
path={route.path}
exact
render={(props) => {
window.setTimeout(() => {
pageDeps.setBreadcrumbs(route.breadcrumbs);
});
return (
<MlPageWrapper path={route.path}>{route.render(props, pageDeps)}</MlPageWrapper>
);
}}
/>
);
})}
<Redirect to="/overview" />
</Switch>
</EuiPageContentBody>
</RedirectAppLinks>
);
});