forked from opensearch-project/OpenSearch-Dashboards
-
Notifications
You must be signed in to change notification settings - Fork 0
/
top_nav.tsx
95 lines (80 loc) · 3.08 KB
/
top_nav.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
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/
import React, { useEffect, useMemo, useState } from 'react';
import { TimeRange, Query } from 'src/plugins/data/common';
import { AppMountParameters } from '../../../../../../core/public';
import { PLUGIN_ID } from '../../../../common';
import { useOpenSearchDashboards } from '../../../../../opensearch_dashboards_react/public';
import { DiscoverViewServices } from '../../../build_services';
import { IndexPattern } from '../../../opensearch_dashboards_services';
import { getTopNavLinks } from '../../components/top_nav/get_top_nav_links';
import { useDiscoverContext } from '../context';
import { getRootBreadcrumbs } from '../../helpers/breadcrumbs';
import { opensearchFilters, connectStorageToQueryState } from '../../../../../data/public';
export interface TopNavProps {
opts: {
setHeaderActionMenu: AppMountParameters['setHeaderActionMenu'];
onQuerySubmit: (payload: { dateRange: TimeRange; query?: Query }, isUpdate?: boolean) => void;
};
}
export const TopNav = ({ opts }: TopNavProps) => {
const { services } = useOpenSearchDashboards<DiscoverViewServices>();
const { inspectorAdapters, savedSearch, indexPattern } = useDiscoverContext();
const [indexPatterns, setIndexPatterns] = useState<IndexPattern[] | undefined>(undefined);
const {
navigation: {
ui: { TopNavMenu },
},
core: {
application: { getUrlForApp },
},
data,
chrome,
osdUrlStateStorage,
} = services;
const topNavLinks = savedSearch ? getTopNavLinks(services, inspectorAdapters, savedSearch) : [];
connectStorageToQueryState(services.data.query, osdUrlStateStorage, {
filters: opensearchFilters.FilterStateStore.APP_STATE,
query: true,
});
useEffect(() => {
let isMounted = true;
const getDefaultIndexPattern = async () => {
await data.indexPatterns.ensureDefaultIndexPattern();
const defaultIndexPattern = await data.indexPatterns.getDefault();
if (!isMounted) return;
setIndexPatterns(defaultIndexPattern ? [defaultIndexPattern] : undefined);
};
getDefaultIndexPattern();
return () => {
isMounted = false;
};
}, [data.indexPatterns]);
useEffect(() => {
const pageTitleSuffix = savedSearch?.id && savedSearch.title ? `: ${savedSearch.title}` : '';
chrome.docTitle.change(`Discover${pageTitleSuffix}`);
if (savedSearch?.id) {
chrome.setBreadcrumbs([...getRootBreadcrumbs(), { text: savedSearch.title }]);
} else {
chrome.setBreadcrumbs([...getRootBreadcrumbs()]);
}
}, [chrome, getUrlForApp, savedSearch?.id, savedSearch?.title]);
const showDatePicker = useMemo(() => (indexPattern ? indexPattern.isTimeBased() : false), [
indexPattern,
]);
return (
<TopNavMenu
appName={PLUGIN_ID}
config={topNavLinks}
showSearchBar
showDatePicker={showDatePicker}
showSaveQuery
useDefaultBehaviors
setMenuMountPoint={opts.setHeaderActionMenu}
indexPatterns={indexPattern ? [indexPattern] : indexPatterns}
onQuerySubmit={opts.onQuerySubmit}
/>
);
};