import { Pipeline , PipelinesGraphics } from '@/services/data/tuples/pipeline-types' ; import { generateUuid } from '@/services/data/tuples/utils' ; import { getCurrentTime } from '@/services/data/utils' ; import { AdminCacheData } from '@/services/local-persist/types' ; import React , { Fragment , useEffect , useState } from 'react' ; import { useAdminCacheEventBus } from '../../cache/cache-event-bus' ; import { AdminCacheEventTypes } from '../../cache/cache-event-bus-types' ; import { usePipelinesEventBus } from '../pipelines-event-bus' ; import { PipelinesEventTypes } from '../pipelines-event-bus-types' ; import { HoldSettings } from './types' ; import { useReplier } from './use-replier' ; export const SettingsHolder = ( ) => { const { fire : fireCache } = useAdminCacheEventBus ( ) ; const { on , off , fire } = usePipelinesEventBus ( ) ; const [ holdSettings , setHoldSettings ] = useState < HoldSettings > ( { initialized : false , pipelines : [ ] , topics : [ ] , graphics : [ { pipelineGraphId : generateUuid ( ) , name : '' , topics : [ ] , createTime : getCurrentTime ( ) , lastModified : getCurrentTime ( ) } ] } ) ; useEffect ( ( ) => { if ( ! holdSettings . initialized ) { const askData = ( ) => { fireCache ( AdminCacheEventTypes . ASK_DATA_LOADED , ( loaded ) => { if ( loaded ) { fireCache ( AdminCacheEventTypes . ASK_DATA , ( data ? ) => { setHoldSettings ( { initialized : true , ... data ! } ) ; fire ( PipelinesEventTypes . SETTINGS_LOADED , data ! ) ; } ) ; } else { setTimeout ( ( ) => askData ( ) , 100 ) ; } } ) ; } ; askData ( ) ; } } , [ fire , fireCache , holdSettings . initialized ] ) ; useEffect ( ( ) => { const onAskSettingsLoaded = ( onSettingsLoadedGet ) => { onSettingsLoadedGet ( holdSettings . initialized ) ; } ; on ( PipelinesEventTypes . ASK_SETTINGS_LOADED , onAskSettingsLoaded ) ; return ( ) => { off ( PipelinesEventTypes . ASK_SETTINGS_LOADED , onAskSettingsLoaded ) ; } ; } , [ fire , on , off , holdSettings . initialized ] ) ; useEffect ( ( ) => { const onPipelineAdded = ( pipeline ) => { holdSettings . pipelines . push ( pipeline ) ; } ; on ( PipelinesEventTypes . PIPELINE_ADDED , onPipelineAdded ) ; return ( ) => { off ( PipelinesEventTypes . PIPELINE_ADDED , onPipelineAdded ) ; } ; } , [ on , off , holdSettings . pipelines ] ) ; useEffect ( ( ) => { const onGraphicsChanged = ( graphics ) => { holdSettings . graphics = [ graphics , ... holdSettings . graphics . filter ( g => g . pipelineGraphId != graphics . pipelineGraphId ) ] ; } ; on ( PipelinesEventTypes . GRAPHICS_CHANGED , onGraphicsChanged ) ; return ( ) => { off ( PipelinesEventTypes . GRAPHICS_CHANGED , onGraphicsChanged ) ; } ; } , [ on , off , holdSettings ] ) ; useReplier ( { holdSettings } ) ; return < Fragment />; } ;