6
6
import React , { useEffect , useState } from 'react' ;
7
7
import { useParams } from 'react-router-dom' ;
8
8
import { EventEmitter } from 'events' ;
9
- import { EMPTY , Subscription , merge } from 'rxjs' ;
10
- import { catchError , distinctUntilChanged , map , mapTo , startWith , switchMap } from 'rxjs/operators' ;
11
- import deepEqual from 'fast-deep-equal' ;
12
9
import { DashboardTopNav } from '../components/dashboard_top_nav' ;
13
10
import { useChromeVisibility } from '../utils/use/use_chrome_visibility' ;
14
11
import { useOpenSearchDashboards } from '../../../../opensearch_dashboards_react/public' ;
15
12
import { useSavedDashboardInstance } from '../utils/use/use_saved_dashboard_instance' ;
16
-
17
- import { DashboardServices , SavedDashboardPanel } from '../../types' ;
18
- import {
19
- DASHBOARD_CONTAINER_TYPE ,
20
- DashboardContainer ,
21
- DashboardContainerInput ,
22
- DashboardPanelState ,
23
- } from '../embeddable' ;
24
- import {
25
- ContainerOutput ,
26
- ErrorEmbeddable ,
27
- ViewMode ,
28
- isErrorEmbeddable ,
29
- } from '../../embeddable_plugin' ;
30
- import { DashboardEmptyScreen , DashboardEmptyScreenProps } from '../dashboard_empty_screen' ;
31
- import { convertSavedDashboardPanelToPanelState } from '../lib/embeddable_saved_object_converters' ;
13
+ import { DashboardServices } from '../../types' ;
32
14
import { useDashboardAppState } from '../utils/use/use_dashboard_app_state' ;
15
+ import { useDashboardContainer } from '../utils/use/use_dashboard_container' ;
16
+ import { useEditorUpdates } from '../utils/use/use_editor_updates' ;
33
17
34
18
export const DashboardEditor = ( ) => {
35
19
const { id : dashboardIdFromUrl } = useParams < { id : string } > ( ) ;
36
20
const { services } = useOpenSearchDashboards < DashboardServices > ( ) ;
37
- const { embeddable, data, dashboardConfig, embeddableCapabilities, uiSettings, http } = services ;
38
- const { query : queryService } = data ;
39
- const { visualizeCapabilities, mapsCapabilities } = embeddableCapabilities ;
40
- const timefilter = queryService . timefilter . timefilter ;
41
21
const isChromeVisible = useChromeVisibility ( services . chrome ) ;
42
22
const [ eventEmitter ] = useState ( new EventEmitter ( ) ) ;
43
23
44
- const { savedDashboardInstance } = useSavedDashboardInstance (
24
+ const savedDashboardInstance = useSavedDashboardInstance (
45
25
services ,
46
26
eventEmitter ,
47
27
isChromeVisible ,
@@ -50,165 +30,49 @@ export const DashboardEditor = () => {
50
30
51
31
const { appState } = useDashboardAppState ( services , eventEmitter , savedDashboardInstance ) ;
52
32
53
- const appStateData = appState ?. get ( ) ;
54
- if ( ! appStateData ) {
55
- return null ;
56
- }
57
- let dashboardContainer : DashboardContainer | undefined ;
58
- let inputSubscription : Subscription | undefined ;
59
- let outputSubscription : Subscription | undefined ;
60
-
61
- const dashboardDom = document . getElementById ( 'dashboardViewport' ) ;
62
- const dashboardFactory = embeddable . getEmbeddableFactory <
63
- DashboardContainerInput ,
64
- ContainerOutput ,
65
- DashboardContainer
66
- > ( DASHBOARD_CONTAINER_TYPE ) ;
67
-
68
- const getShouldShowEditHelp = ( ) => {
69
- return (
70
- ! appStateData . panels . length &&
71
- appStateData . viewMode === ViewMode . EDIT &&
72
- ! dashboardConfig . getHideWriteControls ( )
73
- ) ;
74
- } ;
75
-
76
- const getShouldShowViewHelp = ( ) => {
77
- return (
78
- ! appStateData . panels . length &&
79
- appStateData . viewMode === ViewMode . VIEW &&
80
- ! dashboardConfig . getHideWriteControls ( )
81
- ) ;
82
- } ;
83
-
84
- const shouldShowUnauthorizedEmptyState = ( ) => {
85
- const readonlyMode =
86
- ! appStateData . panels . length &&
87
- ! getShouldShowEditHelp ( ) &&
88
- ! getShouldShowViewHelp ( ) &&
89
- dashboardConfig . getHideWriteControls ( ) ;
90
- const userHasNoPermissions =
91
- ! appStateData . panels . length && ! visualizeCapabilities . save && ! mapsCapabilities . save ;
92
- return readonlyMode || userHasNoPermissions ;
93
- } ;
94
-
95
- const getEmptyScreenProps = (
96
- shouldShowEditHelp : boolean ,
97
- isEmptyInReadOnlyMode : boolean
98
- ) : DashboardEmptyScreenProps => {
99
- const emptyScreenProps : DashboardEmptyScreenProps = {
100
- onLinkClick : ( ) => { } , // TODO
101
- showLinkToVisualize : shouldShowEditHelp ,
102
- uiSettings,
103
- http,
104
- } ;
105
- if ( shouldShowEditHelp ) {
106
- emptyScreenProps . onVisualizeClick = ( ) => {
107
- alert ( 'click' ) ; // TODO
108
- } ;
109
- }
110
- if ( isEmptyInReadOnlyMode ) {
111
- emptyScreenProps . isReadonlyMode = true ;
112
- }
113
- return emptyScreenProps ;
114
- } ;
33
+ const { dashboardContainer } = useDashboardContainer (
34
+ services ,
35
+ isChromeVisible ,
36
+ eventEmitter ,
37
+ savedDashboardInstance ,
38
+ appState
39
+ ) ;
115
40
116
- const getDashboardInput = ( ) => {
117
- const embeddablesMap : {
118
- [ key : string ] : DashboardPanelState ;
119
- } = { } ;
120
- appStateData . panels . forEach ( ( panel : SavedDashboardPanel ) => {
121
- embeddablesMap [ panel . panelIndex ] = convertSavedDashboardPanelToPanelState ( panel ) ;
122
- } ) ;
41
+ const { isEmbeddableRendered , currentAppState } = useEditorUpdates (
42
+ services ,
43
+ eventEmitter ,
44
+ savedDashboardInstance ,
45
+ dashboardContainer ,
46
+ appState
47
+ ) ;
123
48
124
- const lastReloadRequestTime = 0 ;
125
- return {
126
- id : savedDashboardInstance . id || '' ,
127
- filters : appStateData . filters ,
128
- hidePanelTitles : appStateData ?. options . hidePanelTitles ,
129
- query : appStateData . query ,
130
- timeRange : {
131
- ..._ . cloneDeep ( timefilter . getTime ( ) ) ,
132
- } ,
133
- refreshConfig : timefilter . getRefreshInterval ( ) ,
134
- viewMode : appStateData . viewMode ,
135
- panels : embeddablesMap ,
136
- isFullScreenMode : appStateData ?. fullScreenMode ,
137
- isEmbeddedExternally : false , // TODO
138
- // isEmptyState: shouldShowEditHelp || shouldShowViewHelp || isEmptyInReadonlyMode,
139
- isEmptyState : false , // TODO
140
- useMargins : appStateData . options . useMargins ,
141
- lastReloadRequestTime, // TODO
142
- title : appStateData . title ,
143
- description : appStateData . description ,
144
- expandedPanelId : appStateData . expandedPanelId ,
49
+ useEffect ( ( ) => {
50
+ // clean up all registered listeners if any is left
51
+ return ( ) => {
52
+ eventEmitter . removeAllListeners ( ) ;
145
53
} ;
146
- } ;
147
-
148
- if ( dashboardFactory ) {
149
- dashboardFactory
150
- . create ( getDashboardInput ( ) )
151
- . then ( ( container : DashboardContainer | ErrorEmbeddable | undefined ) => {
152
- if ( container && ! isErrorEmbeddable ( container ) ) {
153
- dashboardContainer = container ;
154
-
155
- dashboardContainer . renderEmpty = ( ) => {
156
- const shouldShowEditHelp = getShouldShowEditHelp ( ) ;
157
- const shouldShowViewHelp = getShouldShowViewHelp ( ) ;
158
- const isEmptyInReadOnlyMode = shouldShowUnauthorizedEmptyState ( ) ;
159
- const isEmptyState = shouldShowEditHelp || shouldShowViewHelp || isEmptyInReadOnlyMode ;
160
- return isEmptyState ? (
161
- < DashboardEmptyScreen
162
- { ...getEmptyScreenProps ( shouldShowEditHelp , isEmptyInReadOnlyMode ) }
163
- />
164
- ) : null ;
165
- } ;
166
-
167
- outputSubscription = merge (
168
- // output of dashboard container itself
169
- dashboardContainer . getOutput$ ( ) ,
170
- // plus output of dashboard container children,
171
- // children may change, so make sure we subscribe/unsubscribe with switchMap
172
- dashboardContainer . getOutput$ ( ) . pipe (
173
- map ( ( ) => dashboardContainer ! . getChildIds ( ) ) ,
174
- distinctUntilChanged ( deepEqual ) ,
175
- switchMap ( ( newChildIds : string [ ] ) =>
176
- merge (
177
- ...newChildIds . map ( ( childId ) =>
178
- dashboardContainer !
179
- . getChild ( childId )
180
- . getOutput$ ( )
181
- . pipe ( catchError ( ( ) => EMPTY ) )
182
- )
183
- )
184
- )
185
- )
186
- )
187
- . pipe (
188
- mapTo ( dashboardContainer ) ,
189
- startWith ( dashboardContainer ) // to trigger initial index pattern update
190
- // updateIndexPatternsOperator //TODO
191
- )
192
- . subscribe ( ) ;
193
-
194
- inputSubscription = dashboardContainer . getInput$ ( ) . subscribe ( ( ) => { } ) ;
54
+ } , [ eventEmitter ] ) ;
195
55
196
- if ( dashboardDom && container ) {
197
- container . render ( dashboardDom ) ;
198
- }
199
- }
200
- } ) ;
201
- }
56
+ console . log ( 'savedDashboardInstance' , savedDashboardInstance ) ;
57
+ console . log ( 'appState' , appState ) ;
58
+ console . log ( 'currentAppState' , currentAppState ) ;
59
+ console . log ( 'isEmbeddableRendered' , isEmbeddableRendered ) ;
60
+ console . log ( 'dashboardContainer' , dashboardContainer ) ;
202
61
203
62
return (
204
63
< div >
205
- { savedDashboardInstance && appState && (
206
- < DashboardTopNav
207
- isChromeVisible = { isChromeVisible }
208
- savedDashboardInstance = { savedDashboardInstance }
209
- appState = { appState }
210
- />
211
- ) }
64
+ < div >
65
+ { savedDashboardInstance && appState && dashboardContainer && currentAppState && (
66
+ < DashboardTopNav
67
+ isChromeVisible = { isChromeVisible }
68
+ savedDashboardInstance = { savedDashboardInstance }
69
+ stateContainer = { appState }
70
+ currentAppState = { currentAppState }
71
+ isEmbeddableRendered = { isEmbeddableRendered }
72
+ dashboardContainer = { dashboardContainer }
73
+ />
74
+ ) }
75
+ </ div >
212
76
</ div >
213
77
) ;
214
78
} ;
0 commit comments