66import React , { useEffect , useState } from 'react' ;
77import { useParams } from 'react-router-dom' ;
88import { 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' ;
129import { DashboardTopNav } from '../components/dashboard_top_nav' ;
1310import { useChromeVisibility } from '../utils/use/use_chrome_visibility' ;
1411import { useOpenSearchDashboards } from '../../../../opensearch_dashboards_react/public' ;
1512import { 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' ;
3214import { 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' ;
3317
3418export const DashboardEditor = ( ) => {
3519 const { id : dashboardIdFromUrl } = useParams < { id : string } > ( ) ;
3620 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 ;
4121 const isChromeVisible = useChromeVisibility ( services . chrome ) ;
4222 const [ eventEmitter ] = useState ( new EventEmitter ( ) ) ;
23+ const dashboardDom = document . getElementById ( 'dashboardViewport' ) ;
4324
44- const { savedDashboardInstance } = useSavedDashboardInstance (
25+ const savedDashboardInstance = useSavedDashboardInstance (
4526 services ,
4627 eventEmitter ,
4728 isChromeVisible ,
@@ -50,165 +31,50 @@ export const DashboardEditor = () => {
5031
5132 const { appState } = useDashboardAppState ( services , eventEmitter , savedDashboardInstance ) ;
5233
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- } ;
34+ const { dashboardContainer } = useDashboardContainer (
35+ services ,
36+ isChromeVisible ,
37+ eventEmitter ,
38+ dashboardDom ,
39+ savedDashboardInstance ,
40+ appState
41+ ) ;
11542
116- const getDashboardInput = ( ) => {
117- const embeddablesMap : {
118- [ key : string ] : DashboardPanelState ;
119- } = { } ;
120- appStateData . panels . forEach ( ( panel : SavedDashboardPanel ) => {
121- embeddablesMap [ panel . panelIndex ] = convertSavedDashboardPanelToPanelState ( panel ) ;
122- } ) ;
43+ const { isEmbeddableRendered , currentAppState } = useEditorUpdates (
44+ services ,
45+ eventEmitter ,
46+ savedDashboardInstance ,
47+ dashboardContainer ,
48+ appState
49+ ) ;
12350
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 ,
51+ useEffect ( ( ) => {
52+ // clean up all registered listeners if any is left
53+ return ( ) => {
54+ eventEmitter . removeAllListeners ( ) ;
14555 } ;
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 ( ( ) => { } ) ;
56+ } , [ eventEmitter ] ) ;
19557
196- if ( dashboardDom && container ) {
197- container . render ( dashboardDom ) ;
198- }
199- }
200- } ) ;
201- }
58+ console . log ( 'savedDashboardInstance' , savedDashboardInstance ) ;
59+ console . log ( 'appState' , appState ) ;
60+ console . log ( 'currentAppState' , currentAppState ) ;
61+ console . log ( 'isEmbeddableRendered' , isEmbeddableRendered ) ;
62+ console . log ( 'dashboardContainer' , dashboardContainer ) ;
20263
20364 return (
20465 < div >
205- { savedDashboardInstance && appState && (
206- < DashboardTopNav
207- isChromeVisible = { isChromeVisible }
208- savedDashboardInstance = { savedDashboardInstance }
209- appState = { appState }
210- />
211- ) }
66+ < div >
67+ { savedDashboardInstance && appState && dashboardContainer && currentAppState && (
68+ < DashboardTopNav
69+ isChromeVisible = { isChromeVisible }
70+ savedDashboardInstance = { savedDashboardInstance }
71+ stateContainer = { appState }
72+ currentAppState = { currentAppState }
73+ isEmbeddableRendered = { isEmbeddableRendered }
74+ dashboardContainer = { dashboardContainer }
75+ />
76+ ) }
77+ </ div >
21278 </ div >
21379 ) ;
21480} ;
0 commit comments