-
Notifications
You must be signed in to change notification settings - Fork 8.3k
/
Copy pathpage_template.tsx
81 lines (74 loc) · 2.06 KB
/
page_template.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
/*
* 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 { EuiTab, EuiTabs } from '@elastic/eui';
import React, { useContext, useState, useEffect } from 'react';
import { useTitle } from '../hooks/use_title';
import { MonitoringToolbar } from '../../components/shared/toolbar';
import { MonitoringTimeContainer } from './use_monitoring_time';
import { PageLoading } from '../../components';
export interface TabMenuItem {
id: string;
label: string;
description: string;
disabled: boolean;
onClick: () => void;
testSubj: string;
}
interface PageTemplateProps {
title: string;
pageTitle?: string;
tabs?: TabMenuItem[];
getPageData?: () => Promise<void>;
}
export const PageTemplate: React.FC<PageTemplateProps> = ({
title,
pageTitle,
tabs,
getPageData,
children,
}) => {
useTitle('', title);
const { currentTimerange } = useContext(MonitoringTimeContainer.Context);
const [loaded, setLoaded] = useState(false);
useEffect(() => {
getPageData?.()
.catch((err) => {
// TODO: handle errors
})
.finally(() => {
setLoaded(true);
});
}, [getPageData, currentTimerange]);
const onRefresh = () => {
getPageData?.().catch((err) => {
// TODO: handle errors
});
};
return (
<div className="app-container">
<MonitoringToolbar pageTitle={pageTitle} onRefresh={onRefresh} />
{tabs && (
<EuiTabs>
{tabs.map((item, idx) => {
return (
<EuiTab
key={idx}
disabled={item.disabled}
onClick={item.onClick}
title={item.label}
data-test-subj={item.testSubj}
>
{item.label}
</EuiTab>
);
})}
</EuiTabs>
)}
<div>{!getPageData ? children : loaded ? children : <PageLoading />}</div>
</div>
);
};