1- import { useContext , useEffect , useMemo , useState } from 'react'
1+ import { useEffect , useState } from 'react'
22import { useSubscription , useTracker } from '../../../lib/ReactMeteorData/react-meteor-data'
3- import { PeripheralDevice , PeripheralDeviceType } from '@sofie-automation/corelib/dist/dataModel/PeripheralDevice'
3+ import { PeripheralDevice } from '@sofie-automation/corelib/dist/dataModel/PeripheralDevice'
44import { useTranslation } from 'react-i18next'
5- import { protectString , unprotectString } from '../../../lib/tempLib'
5+ import { unprotectString } from '../../../lib/tempLib'
66import { NotificationCenter , NoticeLevel , Notification } from '../../../lib/notifications/notifications'
77import { StatusResponse } from '@sofie-automation/meteor-lib/dist/api/systemStatus'
88import { MeteorCall } from '../../../lib/meteorApi'
99import { CoreSystem , PeripheralDevices } from '../../../collections'
1010import { PeripheralDeviceId } from '@sofie-automation/shared-lib/dist/core/model/Ids'
1111import { logger } from '../../../lib/logging'
1212import { CorelibPubSub } from '@sofie-automation/corelib/dist/pubsub'
13- import { stringifyError } from '@sofie-automation/shared-lib/dist/lib/stringifyError'
1413import { CoreItem } from './CoreItem'
1514import { DeviceItem } from './DeviceItem'
16- import { UserPermissions , UserPermissionsContext } from '../../UserPermissions '
15+ import { useDebugStatesForPlayoutDevice } from '../../Settings/components/useDebugStatesForPlayoutDevice '
1716
1817export function SystemStatus ( ) : JSX . Element {
1918 const { t } = useTranslation ( )
2019
21- const userPermissions = useContext ( UserPermissionsContext )
22-
2320 // Subscribe to data:
2421 useSubscription ( CorelibPubSub . peripheralDevices , null )
2522
2623 const coreSystem = useTracker ( ( ) => CoreSystem . findOne ( ) , [ ] )
2724 const devices = useTracker ( ( ) => PeripheralDevices . find ( { } , { sort : { lastConnected : - 1 } } ) . fetch ( ) , [ ] , [ ] )
2825
2926 const systemStatus = useSystemStatus ( )
30- const playoutDebugStates = usePlayoutDebugStates ( devices , userPermissions )
31-
3227 const devicesHierarchy = convertDevicesIntoHeirarchy ( devices )
3328
3429 return (
@@ -40,12 +35,7 @@ export function SystemStatus(): JSX.Element {
4035 { coreSystem && < CoreItem coreSystem = { coreSystem } systemStatus = { systemStatus } /> }
4136
4237 { devicesHierarchy . map ( ( d ) => (
43- < DeviceItemWithChildren
44- key = { unprotectString ( d . device . _id ) }
45- playoutDebugStates = { playoutDebugStates }
46- parentDevice = { null }
47- device = { d }
48- />
38+ < ParentDeviceItemWithChildren key = { unprotectString ( d . device . _id ) } device = { d } />
4939 ) ) }
5040 </ div >
5141 </ div >
@@ -101,66 +91,6 @@ function useSystemStatus(): StatusResponse | undefined {
10191 return sytemStatus
10292}
10393
104- function usePlayoutDebugStates (
105- devices : PeripheralDevice [ ] ,
106- userPermissions : UserPermissions
107- ) : Map < PeripheralDeviceId , object > {
108- const { t } = useTranslation ( )
109-
110- const [ playoutDebugStates , setPlayoutDebugStates ] = useState < Map < PeripheralDeviceId , object > > ( new Map ( ) )
111-
112- const playoutDeviceIds = useMemo ( ( ) => {
113- const deviceIds : PeripheralDeviceId [ ] = [ ]
114-
115- for ( const device of devices ) {
116- if ( device . type === PeripheralDeviceType . PLAYOUT && device . settings && ( device . settings as any ) [ 'debugState' ] ) {
117- deviceIds . push ( device . _id )
118- }
119- }
120-
121- deviceIds . sort ( )
122- return deviceIds
123- } , [ devices ] )
124-
125- useEffect ( ( ) => {
126- if ( ! userPermissions . developer ) {
127- setPlayoutDebugStates ( new Map ( ) )
128- return
129- }
130-
131- let destroyed = false
132-
133- const refreshDebugStates = ( ) => {
134- for ( const deviceId of playoutDeviceIds ) {
135- MeteorCall . systemStatus
136- . getDebugStates ( deviceId )
137- . then ( ( res ) => {
138- if ( destroyed ) return
139-
140- setPlayoutDebugStates ( ( oldState ) => {
141- // Create a new map based on the old one
142- const newStates = new Map ( oldState . entries ( ) )
143- for ( const [ key , state ] of Object . entries < any > ( res ) ) {
144- newStates . set ( protectString ( key ) , state )
145- }
146- return newStates
147- } )
148- } )
149- . catch ( ( err ) => console . log ( `Error fetching device states: ${ stringifyError ( err ) } ` ) )
150- }
151- }
152-
153- const interval = setInterval ( refreshDebugStates , 1000 )
154-
155- return ( ) => {
156- clearInterval ( interval )
157- destroyed = true
158- }
159- } , [ t , JSON . stringify ( playoutDeviceIds ) , userPermissions . developer ] )
160-
161- return playoutDebugStates
162- }
163-
16494function convertDevicesIntoHeirarchy ( devices : PeripheralDevice [ ] ) : DeviceInHierarchy [ ] {
16595 const devicesMap = new Map < PeripheralDeviceId , DeviceInHierarchy > ( )
16696 const devicesToAdd : DeviceInHierarchy [ ] = [ ]
@@ -194,8 +124,18 @@ function convertDevicesIntoHeirarchy(devices: PeripheralDevice[]): DeviceInHiera
194124 return devicesHeirarchy
195125}
196126
127+ interface ParentDeviceItemWithChildrenProps {
128+ device : DeviceInHierarchy
129+ }
130+
131+ function ParentDeviceItemWithChildren ( { device } : ParentDeviceItemWithChildrenProps ) {
132+ const playoutDebugStates = useDebugStatesForPlayoutDevice ( device . device )
133+
134+ return < DeviceItemWithChildren playoutDebugStates = { playoutDebugStates } parentDevice = { null } device = { device } />
135+ }
136+
197137interface DeviceItemWithChildrenProps {
198- playoutDebugStates : Map < PeripheralDeviceId , object >
138+ playoutDebugStates : ReadonlyMap < PeripheralDeviceId , object >
199139 parentDevice : DeviceInHierarchy | null
200140 device : DeviceInHierarchy
201141}
0 commit comments