3
3
* SPDX-License-Identifier: Apache-2.0
4
4
*/
5
5
6
+ import { uniqBy } from 'lodash' ;
6
7
import React , { memo , useState , useEffect } from 'react' ;
7
- import { Filter } from 'src/plugins/data/public' ;
8
+ import { Filter , IndexPattern } from 'src/plugins/data/public' ;
8
9
import { useCallback } from 'react' ;
9
10
import { useOpenSearchDashboards } from '../../../../opensearch_dashboards_react/public' ;
10
11
import { getTopNavConfig } from '../top_nav/get_top_nav_config' ;
11
- import {
12
- DashboardAppStateContainer ,
13
- DashboardAppState ,
14
- DashboardServices ,
15
- NavAction ,
16
- } from '../../types' ;
12
+ import { DashboardAppStateContainer , DashboardAppState , DashboardServices } from '../../types' ;
17
13
import { getNavActions } from '../utils/get_nav_actions' ;
18
14
import { DashboardContainer } from '../embeddable' ;
15
+ import { isErrorEmbeddable } from '../../embeddable_plugin' ;
19
16
20
17
interface DashboardTopNavProps {
21
18
isChromeVisible : boolean ;
@@ -45,12 +42,22 @@ const TopNav = ({
45
42
const [ filters , setFilters ] = useState < Filter [ ] > ( [ ] ) ;
46
43
const [ topNavMenu , setTopNavMenu ] = useState < any > ( ) ;
47
44
const [ isFullScreenMode , setIsFullScreenMode ] = useState < any > ( ) ;
45
+ const [ indexPatterns , setIndexPatterns ] = useState < IndexPattern [ ] > ( ) ;
48
46
49
47
const { services } = useOpenSearchDashboards < DashboardServices > ( ) ;
50
48
const { TopNavMenu } = services . navigation . ui ;
51
49
const { data, dashboardConfig, setHeaderActionMenu } = services ;
52
50
const { query : queryService } = data ;
53
51
52
+ const handleRefresh = useCallback (
53
+ ( _payload : any , isUpdate ?: boolean ) => {
54
+ if ( ! isUpdate && dashboardContainer ) {
55
+ dashboardContainer . reload ( ) ;
56
+ }
57
+ } ,
58
+ [ dashboardContainer ]
59
+ ) ;
60
+
54
61
// TODO: this should base on URL
55
62
const isEmbeddedExternally = false ;
56
63
@@ -97,6 +104,33 @@ const TopNav = ({
97
104
setIsFullScreenMode ( currentAppState ?. fullScreenMode ) ;
98
105
} , [ currentAppState , services ] ) ;
99
106
107
+ useEffect ( ( ) => {
108
+ const asyncSetIndexPattern = async ( ) => {
109
+ if ( dashboardContainer ) {
110
+ let panelIndexPatterns : IndexPattern [ ] = [ ] ;
111
+ dashboardContainer . getChildIds ( ) . forEach ( ( id ) => {
112
+ const embeddableInstance = dashboardContainer . getChild ( id ) ;
113
+ if ( isErrorEmbeddable ( embeddableInstance ) ) return ;
114
+ const embeddableIndexPatterns = ( embeddableInstance . getOutput ( ) as any ) . indexPatterns ;
115
+ if ( ! embeddableIndexPatterns ) return ;
116
+ panelIndexPatterns . push ( ...embeddableIndexPatterns ) ;
117
+ } ) ;
118
+ panelIndexPatterns = uniqBy ( panelIndexPatterns , 'id' ) ;
119
+
120
+ if ( panelIndexPatterns . length > 0 ) {
121
+ setIndexPatterns ( panelIndexPatterns ) ;
122
+ } else {
123
+ const defaultIndex = await services . data . indexPatterns . getDefault ( ) ;
124
+ if ( defaultIndex ) {
125
+ setIndexPatterns ( [ defaultIndex ] ) ;
126
+ }
127
+ }
128
+ }
129
+ } ;
130
+
131
+ asyncSetIndexPattern ( ) ;
132
+ } , [ dashboardContainer , stateContainer , currentAppState , services . data . indexPatterns ] ) ;
133
+
100
134
const shouldShowFilterBar = ( forceHide : boolean ) : boolean =>
101
135
! forceHide && ( filters ! . length > 0 || ! currentAppState ?. fullScreenMode ) ;
102
136
@@ -111,19 +145,6 @@ const TopNav = ({
111
145
const showFilterBar = shouldShowFilterBar ( forceHideFilterBar ) ;
112
146
const showSearchBar = showQueryBar || showFilterBar ;
113
147
114
- // TODO: implement handleRefresh
115
- const handleRefresh = useCallback ( ( _payload : any , isUpdate ?: boolean ) => {
116
- /* if (isUpdate === false) {
117
- // The user can still request a reload in the query bar, even if the
118
- // query is the same, and in that case, we have to explicitly ask for
119
- // a reload, since no state changes will cause it.
120
- lastReloadRequestTime = new Date().getTime();
121
- const changes = getChangesFromAppStateForContainerState();
122
- if (changes && dashboardContainer) {
123
- dashboardContainer.updateInput(changes);
124
- }*/
125
- } , [ ] ) ;
126
-
127
148
return isChromeVisible ? (
128
149
< TopNavMenu
129
150
appName = { 'dashboard' }
@@ -138,7 +159,7 @@ const TopNav = ({
138
159
showDatePicker = { showDatePicker }
139
160
showFilterBar = { showFilterBar }
140
161
useDefaultBehaviors = { true }
141
- indexPatterns = { [ ] }
162
+ indexPatterns = { indexPatterns }
142
163
showSaveQuery = { services . dashboardCapabilities . saveQuery as boolean }
143
164
savedQuery = { undefined }
144
165
onSavedQueryIdChange = { ( ) => { } }
0 commit comments