1
- import * as React from 'react'
1
+ import React , { memo , useEffect } from 'react'
2
2
import _ from 'underscore'
3
3
import ClassNames from 'classnames'
4
4
import {
7
7
RundownLayoutMiniRundown ,
8
8
} from '@sofie-automation/meteor-lib/dist/collections/RundownLayouts'
9
9
import { RundownLayoutsAPI } from '../../lib/rundownLayouts.js'
10
- import { withTracker } from '../../lib/ReactMeteorData/ReactMeteorData.js'
10
+ import { useTracker } from '../../lib/ReactMeteorData/ReactMeteorData.js'
11
11
import { DBRundownPlaylist } from '@sofie-automation/corelib/dist/dataModel/RundownPlaylist'
12
12
import { PartInstance } from '@sofie-automation/meteor-lib/dist/collections/PartInstances'
13
13
import { DBSegment } from '@sofie-automation/corelib/dist/dataModel/Segment'
@@ -18,77 +18,80 @@ import { UIPartInstances } from '../Collections.js'
18
18
import { RundownPlaylistClientUtil } from '../../lib/rundownPlaylistUtil.js'
19
19
20
20
interface IMiniRundownPanelProps {
21
- key : string
22
21
visible ?: boolean
23
22
layout : RundownLayoutBase
24
23
panel : RundownLayoutMiniRundown
25
24
playlist : DBRundownPlaylist
26
25
}
27
26
28
- interface IMiniRundownPanelTrackedProps {
29
- currentPartInstance ?: PartInstance
30
- nextPartInstance ?: PartInstance
31
- allSegments ?: DBSegment [ ]
32
- }
33
-
34
- interface IState { }
35
-
36
27
interface MiniRundownSegment {
37
28
identifier : string
38
29
segmentName : string
39
30
cssClass : string
40
31
}
41
32
42
- export class MiniRundownPanelInner extends React . Component < IMiniRundownPanelProps & IMiniRundownPanelTrackedProps > {
43
- static currentSegmentCssClass = 'current-segment'
44
- static nextSegmentCssClass = 'next-segment'
45
- static panelContainerId = 'mini-rundown-panel__container'
46
- static nextSegmentId = 'mini-rundown__next-segment'
47
- static currentSegmentId = 'mini-rundown__current-segment'
33
+ const MiniRundownPanelClassesAndIds = {
34
+ currentSegmentCssClass : 'current-segment' ,
35
+ nextSegmentCssClass : 'next-segment' ,
36
+ panelContainerId : 'mini-rundown-panel__container' ,
37
+ nextSegmentId : 'mini-rundown__next-segment' ,
38
+ currentSegmentId : 'mini-rundown__current-segment' ,
39
+ }
48
40
49
- componentDidMount ( ) : void {
50
- this . scrollIntoView ( )
51
- }
41
+ export const MiniRundownPanel = memo (
42
+ function MiniRundownPanelInner2 ( props : IMiniRundownPanelProps ) {
43
+ // Trigger on mount, or when any props change
44
+ useEffect ( ( ) => {
45
+ Meteor . setTimeout ( ( ) => {
46
+ const container = document . getElementById ( MiniRundownPanelClassesAndIds . panelContainerId )
47
+ if ( ! container ) return
48
+ const nextElement = document . getElementById ( MiniRundownPanelClassesAndIds . nextSegmentId )
49
+ const currentElement = document . getElementById ( MiniRundownPanelClassesAndIds . currentSegmentId )
50
+ if ( nextElement ) {
51
+ container . scrollTop = nextElement . offsetTop - nextElement . clientHeight
52
+ } else if ( currentElement ) {
53
+ container . scrollTop = currentElement . offsetTop
54
+ }
55
+ } , 500 )
56
+ } , [ ...Object . values < any > ( props ) ] )
52
57
53
- componentDidUpdate ( ) : void {
54
- this . scrollIntoView ( )
55
- }
56
-
57
- private scrollIntoView ( ) {
58
- Meteor . setTimeout ( ( ) => {
59
- const container = document . getElementById ( MiniRundownPanelInner . panelContainerId )
60
- if ( ! container ) return
61
- const nextElement = document . getElementById ( MiniRundownPanelInner . nextSegmentId )
62
- const currentElement = document . getElementById ( MiniRundownPanelInner . currentSegmentId )
63
- if ( nextElement ) {
64
- container . scrollTop = nextElement . offsetTop - nextElement . clientHeight
65
- } else if ( currentElement ) {
66
- container . scrollTop = currentElement . offsetTop
67
- }
68
- } , 500 )
69
- }
58
+ const currentPartInstanceId : PartInstanceId | undefined = props . playlist . currentPartInfo ?. partInstanceId
59
+ const currentPartInstance = useTracker (
60
+ ( ) => currentPartInstanceId && UIPartInstances . findOne ( currentPartInstanceId ) ,
61
+ [ currentPartInstanceId ]
62
+ )
70
63
71
- render ( ) : JSX . Element {
72
- const isDashboardLayout = RundownLayoutsAPI . isDashboardLayout ( this . props . layout )
73
- const style = getElementStyle ( this . props , isDashboardLayout )
64
+ const nextPartInstanceId : PartInstanceId | undefined = props . playlist . nextPartInfo ?. partInstanceId
65
+ const nextPartInstance = useTracker (
66
+ ( ) => nextPartInstanceId && UIPartInstances . findOne ( nextPartInstanceId ) ,
67
+ [ nextPartInstanceId ]
68
+ )
74
69
75
- const miniRundowns : MiniRundownSegment [ ] = getMiniRundownList (
76
- this . props . allSegments ,
77
- this . props . currentPartInstance ,
78
- this . props . nextPartInstance
70
+ const allSegments : DBSegment [ ] = useTracker (
71
+ ( ) => RundownPlaylistClientUtil . getSegments ( props . playlist ) ,
72
+ [ props . playlist . _id , props . playlist . rundownIdsInOrder ] ,
73
+ [ ]
79
74
)
80
75
76
+ const isDashboardLayout = RundownLayoutsAPI . isDashboardLayout ( props . layout )
77
+ const style = getElementStyle ( props , isDashboardLayout )
78
+
79
+ const miniRundowns : MiniRundownSegment [ ] = getMiniRundownList ( allSegments , currentPartInstance , nextPartInstance )
80
+
81
81
return (
82
82
< div
83
83
key = { 'miniRundownContainer' }
84
- className = { ClassNames ( 'dashboard-panel mini-rundown-panel' , getContainerClass ( this . props , isDashboardLayout ) ) }
85
- style = { getContainerStyle ( this . props , isDashboardLayout ) }
84
+ className = { ClassNames ( 'dashboard-panel mini-rundown-panel' , getContainerClass ( props , isDashboardLayout ) ) }
85
+ style = { getContainerStyle ( props , isDashboardLayout ) }
86
86
>
87
87
< span className = "mini-rundown-panel__name" style = { style } key = { 'miniRundownHeader' } >
88
- { this . props . panel . name } { ' ' }
88
+ { props . panel . name } { ' ' }
89
89
</ span >
90
90
91
- < div className = { MiniRundownPanelInner . panelContainerId } id = { MiniRundownPanelInner . panelContainerId } >
91
+ < div
92
+ className = { MiniRundownPanelClassesAndIds . panelContainerId }
93
+ id = { MiniRundownPanelClassesAndIds . panelContainerId }
94
+ >
92
95
{ miniRundowns . map ( ( miniRundown : MiniRundownSegment , index : number ) => (
93
96
< div
94
97
className = { miniRundown . cssClass }
@@ -113,31 +116,9 @@ export class MiniRundownPanelInner extends React.Component<IMiniRundownPanelProp
113
116
</ div >
114
117
</ div >
115
118
)
116
- }
117
- }
118
-
119
- export const MiniRundownPanel = withTracker < IMiniRundownPanelProps , IState , IMiniRundownPanelTrackedProps > (
120
- ( props : IMiniRundownPanelProps & IMiniRundownPanelTrackedProps ) => {
121
- let currentPartInstance : PartInstance | undefined = undefined
122
- let nextPartInstance : PartInstance | undefined = undefined
123
-
124
- const currentPartInstanceId : PartInstanceId | undefined = props . playlist . currentPartInfo ?. partInstanceId
125
- if ( currentPartInstanceId ) {
126
- currentPartInstance = UIPartInstances . findOne ( currentPartInstanceId )
127
- }
128
-
129
- if ( props . playlist . nextPartInfo ) {
130
- nextPartInstance = UIPartInstances . findOne ( props . playlist . nextPartInfo . partInstanceId )
131
- }
132
-
133
- const allSegments : DBSegment [ ] = RundownPlaylistClientUtil . getSegments ( props . playlist )
134
-
135
- return { currentPartInstance, nextPartInstance, allSegments }
136
119
} ,
137
- ( _data , props : IMiniRundownPanelProps , nextProps : IMiniRundownPanelProps ) => {
138
- return ! _ . isEqual ( props , nextProps )
139
- }
140
- ) ( MiniRundownPanelInner )
120
+ ( prevProps , nextProps ) => _ . isEqual ( prevProps , nextProps )
121
+ )
141
122
142
123
function getMiniRundownList (
143
124
allSegments ?: DBSegment [ ] ,
@@ -160,11 +141,11 @@ function getMiniRundownList(
160
141
161
142
function getSegmentCssClass ( segment : DBSegment , currentPart ?: PartInstance , nextPart ?: PartInstance ) : string {
162
143
if ( segment . _id === currentPart ?. segmentId ) {
163
- return MiniRundownPanelInner . currentSegmentCssClass
144
+ return MiniRundownPanelClassesAndIds . currentSegmentCssClass
164
145
}
165
146
166
147
if ( segment . _id === nextPart ?. segmentId ) {
167
- return MiniRundownPanelInner . nextSegmentCssClass
148
+ return MiniRundownPanelClassesAndIds . nextSegmentCssClass
168
149
}
169
150
170
151
return ''
@@ -200,10 +181,10 @@ function getElementKey(prefix: string, identifier: string, index: number): strin
200
181
201
182
function getIdAttributeForNextSegment ( cssClass : string ) {
202
183
switch ( cssClass ) {
203
- case MiniRundownPanelInner . nextSegmentCssClass :
204
- return { id : MiniRundownPanelInner . nextSegmentId }
205
- case MiniRundownPanelInner . currentSegmentCssClass :
206
- return { id : MiniRundownPanelInner . currentSegmentId }
184
+ case MiniRundownPanelClassesAndIds . nextSegmentCssClass :
185
+ return { id : MiniRundownPanelClassesAndIds . nextSegmentId }
186
+ case MiniRundownPanelClassesAndIds . currentSegmentCssClass :
187
+ return { id : MiniRundownPanelClassesAndIds . currentSegmentId }
207
188
default :
208
189
return { }
209
190
}
0 commit comments