@@ -12,6 +12,7 @@ import { BucketAdLib } from '@sofie-automation/corelib/dist/dataModel/BucketAdLi
1212import { interpollateTranslation } from '@sofie-automation/corelib/dist/TranslatableMessage'
1313import { AdLibActionType , AdLibStatus } from './adLibsTopic'
1414import { CollectionHandlers } from '../liveStatusServer'
15+ import { sortContent , WithSortingMetadata } from './helpers/contentSorting'
1516
1617const THROTTLE_PERIOD_MS = 100
1718
@@ -50,63 +51,29 @@ export class BucketsTopic extends WebSocketTopicBase implements WebSocketTopic {
5051 }
5152
5253 sendStatus ( subscribers : Iterable < WebSocket > ) : void {
53- const buckets : BucketStatus [ ] = this . _buckets . map ( ( bucket ) => {
54+ const sortedBuckets = sortContent ( this . _buckets . map ( this . addBucketSortingMetadata ) )
55+
56+ const bucketStatuses : BucketStatus [ ] = sortedBuckets . map ( ( bucket ) => {
5457 const bucketId = unprotectString ( bucket . _id )
55- const bucketAdLibs = ( this . _adLibsByBucket ?. [ bucketId ] ?? [ ] ) . map ( ( adLib ) => {
56- const sourceLayerName = this . _sourceLayersMap . get ( adLib . sourceLayerId )
57- const outputLayerName = this . _outputLayersMap . get ( adLib . outputLayerId )
58- return literal < BucketAdLibStatus > ( {
59- id : unprotectString ( adLib . _id ) ,
60- name : adLib . name ,
61- sourceLayer : sourceLayerName ?? 'invalid' ,
62- outputLayer : outputLayerName ?? 'invalid' ,
63- actionType : [ ] ,
64- tags : adLib . tags ,
65- externalId : adLib . externalId ,
66- publicData : adLib . publicData ,
67- } )
68- } )
69- const bucketAdLibActions = ( this . _adLibActionsByBucket ?. [ bucketId ] ?? [ ] ) . map ( ( action ) => {
70- const sourceLayerName = this . _sourceLayersMap . get (
71- ( action . display as IBlueprintActionManifestDisplayContent ) . sourceLayerId
72- )
73- const outputLayerName = this . _outputLayersMap . get (
74- ( action . display as IBlueprintActionManifestDisplayContent ) . outputLayerId
75- )
76- const triggerModes = action . triggerModes
77- ? action . triggerModes . map ( ( t ) =>
78- literal < AdLibActionType > ( {
79- name : t . data ,
80- label : interpollateTranslation ( t . display . label . key , t . display . label . args ) ,
81- } )
82- )
83- : [ ]
84- return literal < BucketAdLibStatus > ( {
85- id : unprotectString ( action . _id ) ,
86- name : interpollateTranslation ( action . display . label . key , action . display . label . args ) ,
87- sourceLayer : sourceLayerName ?? 'invalid' ,
88- outputLayer : outputLayerName ?? 'invalid' ,
89- actionType : triggerModes ,
90- tags : action . display . tags ,
91- externalId : action . externalId ,
92- publicData : action . publicData ,
93- } )
94- } )
58+
59+ const bucketAdLibs = ( this . _adLibsByBucket ?. [ bucketId ] ?? [ ] ) . map ( this . toSortableBucketAdLib )
60+ const bucketAdLibActions = ( this . _adLibActionsByBucket ?. [ bucketId ] ?? [ ] ) . map (
61+ this . toSortableBucketAdLibAction
62+ )
63+
9564 return {
9665 id : bucketId ,
9766 name : bucket . name ,
98- adLibs : [ ...bucketAdLibs , ...bucketAdLibActions ] ,
67+ adLibs : sortContent ( [ ...bucketAdLibs , ...bucketAdLibActions ] ) ,
9968 }
10069 } )
10170
10271 const bucketsStatus : BucketsStatus = {
10372 event : 'buckets' ,
104- buckets : buckets ,
73+ buckets : bucketStatuses ,
10574 }
10675
107- for ( const subscriber of subscribers ) {
108- this . sendMessage ( subscriber , bucketsStatus )
109- }
76+ this . sendMessage ( subscribers , bucketsStatus )
11077 }
11178
11279 private onShowStyleBaseUpdate = ( showStyleBase : ShowStyle | undefined ) : void => {
@@ -118,7 +85,8 @@ export class BucketsTopic extends WebSocketTopicBase implements WebSocketTopic {
11885
11986 private onBucketsUpdate = ( buckets : Bucket [ ] | undefined ) : void => {
12087 this . logUpdateReceived ( 'buckets' )
121- this . _buckets = buckets ?? [ ]
88+ buckets ??= [ ]
89+ this . _buckets = sortContent ( buckets . map ( this . addBucketSortingMetadata ) )
12290 this . throttledSendStatusToAll ( )
12391 }
12492
@@ -133,4 +101,66 @@ export class BucketsTopic extends WebSocketTopicBase implements WebSocketTopic {
133101 this . _adLibsByBucket = _ . groupBy ( adLibs ?? [ ] , 'bucketId' )
134102 this . throttledSendStatusToAll ( )
135103 }
104+
105+ private addBucketSortingMetadata = ( bucket : Bucket ) : WithSortingMetadata < Bucket > => {
106+ return {
107+ obj : bucket ,
108+ id : unprotectString ( bucket . _id ) ,
109+ itemRank : bucket . _rank ,
110+ label : bucket . name ,
111+ }
112+ }
113+
114+ private toSortableBucketAdLib = ( adLib : BucketAdLib ) : WithSortingMetadata < BucketAdLibStatus > => {
115+ const sourceLayerName = this . _sourceLayersMap . get ( adLib . sourceLayerId )
116+ const outputLayerName = this . _outputLayersMap . get ( adLib . outputLayerId )
117+ return {
118+ obj : {
119+ id : unprotectString ( adLib . _id ) ,
120+ name : adLib . name ,
121+ sourceLayer : sourceLayerName ?? 'invalid' ,
122+ outputLayer : outputLayerName ?? 'invalid' ,
123+ actionType : [ ] ,
124+ tags : adLib . tags ,
125+ externalId : adLib . externalId ,
126+ publicData : adLib . publicData ,
127+ } ,
128+ id : unprotectString ( adLib . _id ) ,
129+ itemRank : adLib . _rank ,
130+ label : adLib . name ,
131+ }
132+ }
133+
134+ private toSortableBucketAdLibAction = ( action : BucketAdLibAction ) : WithSortingMetadata < BucketAdLibStatus > => {
135+ const sourceLayerName = this . _sourceLayersMap . get (
136+ ( action . display as IBlueprintActionManifestDisplayContent ) . sourceLayerId
137+ )
138+ const outputLayerName = this . _outputLayersMap . get (
139+ ( action . display as IBlueprintActionManifestDisplayContent ) . outputLayerId
140+ )
141+ const triggerModes = action . triggerModes
142+ ? action . triggerModes . map ( ( t ) =>
143+ literal < AdLibActionType > ( {
144+ name : t . data ,
145+ label : interpollateTranslation ( t . display . label . key , t . display . label . args ) ,
146+ } )
147+ )
148+ : [ ]
149+ const name = interpollateTranslation ( action . display . label . key , action . display . label . args )
150+ return {
151+ obj : {
152+ id : unprotectString ( action . _id ) ,
153+ name,
154+ sourceLayer : sourceLayerName ?? 'invalid' ,
155+ outputLayer : outputLayerName ?? 'invalid' ,
156+ actionType : triggerModes ,
157+ tags : action . display . tags ,
158+ externalId : action . externalId ,
159+ publicData : action . publicData ,
160+ } ,
161+ id : unprotectString ( action . _id ) ,
162+ itemRank : action . display . _rank ,
163+ label : name ,
164+ }
165+ }
136166}
0 commit comments