Skip to content

Commit 52c7cf6

Browse files
committed
fix: remove id and use ElementId as reference in context provider
1 parent 78fa4d8 commit 52c7cf6

File tree

2 files changed

+15
-20
lines changed

2 files changed

+15
-20
lines changed

packages/webui/src/client/ui/RundownView/SelectedElementsContext.tsx

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,39 +10,35 @@ import { assertNever } from '@sofie-automation/corelib/dist/lib'
1010

1111
interface RundownElement {
1212
type: 'rundown'
13-
id: string
1413
elementId: RundownId
1514
}
1615

1716
interface SegmentElement {
1817
type: 'segment'
19-
id: string
2018
elementId: SegmentId
2119
}
2220

2321
interface PartInstanceElement {
2422
type: 'partInstance'
25-
id: string
2623
elementId: PartInstanceId
2724
}
2825

2926
interface PieceElement {
3027
type: 'piece'
31-
id: string
3228
elementId: PieceId
3329
}
3430

3531
interface AdlibActionElement {
3632
type: 'adlibAction'
37-
id: string
3833
elementId: AdLibActionId
3934
}
4035

41-
// Union type for all possible elements
36+
// Union types for all possible elements
4237
type SelectedElement = RundownElement | SegmentElement | PartInstanceElement | PieceElement | AdlibActionElement
38+
type ElementId = SelectedElement['elementId']
4339

4440
interface SelectionContextType {
45-
isSelected: (elementId: string) => boolean
41+
isSelected: (elementId: ElementId) => boolean
4642
listSelectedElements: () => SelectedElement[]
4743
clearAndSetSelection: (element: SelectedElement) => void
4844
toggleSelection: (element: SelectedElement) => void
@@ -56,33 +52,33 @@ type SelectionAction =
5652
| { type: 'CLEAR_AND_SET_SELECTION'; payload: SelectedElement }
5753
| { type: 'TOGGLE_SELECTION'; payload: SelectedElement }
5854
| { type: 'ADD_SELECTION'; payload: SelectedElement }
59-
| { type: 'REMOVE_SELECTION'; payload: string }
55+
| { type: 'REMOVE_SELECTION'; payload: ElementId }
6056
| { type: 'CLEAR_SELECTIONS' }
6157

6258
// Reducer function
6359
const selectionReducer = (
64-
state: Map<string, SelectedElement>,
60+
state: Map<ElementId, SelectedElement>,
6561
action: SelectionAction,
6662
maxSelections: number
67-
): Map<string, SelectedElement> => {
63+
): Map<ElementId, SelectedElement> => {
6864
switch (action.type) {
6965
case 'CLEAR_AND_SET_SELECTION': {
70-
const newMap = new Map([[action.payload.id, action.payload]])
66+
const newMap = new Map([[action.payload.elementId, action.payload]])
7167
return newMap
7268
}
7369
case 'TOGGLE_SELECTION': {
7470
const next = new Map(state)
75-
if (next.has(action.payload.id)) {
76-
next.delete(action.payload.id)
71+
if (next.has(action.payload.elementId)) {
72+
next.delete(action.payload.elementId)
7773
} else if (next.size < maxSelections) {
78-
next.set(action.payload.id, action.payload)
74+
next.set(action.payload.elementId, action.payload)
7975
}
8076
return next
8177
}
8278
case 'ADD_SELECTION': {
8379
if (state.size >= maxSelections) return state
8480
const next = new Map(state)
85-
next.set(action.payload.id, action.payload)
81+
next.set(action.payload.elementId, action.payload)
8682
return next
8783
}
8884
case 'REMOVE_SELECTION': {
@@ -106,13 +102,13 @@ export const SelectedElementProvider: React.FC<{
106102
maxSelections?: number // Optional prop to limit maximum selections
107103
}> = ({ children, maxSelections = 10 }) => {
108104
const [selectedElements, dispatch] = React.useReducer(
109-
(state: Map<string, SelectedElement>, action: SelectionAction) => selectionReducer(state, action, maxSelections),
105+
(state: Map<ElementId, SelectedElement>, action: SelectionAction) => selectionReducer(state, action, maxSelections),
110106
new Map()
111107
)
112108

113109
const value = React.useMemo(
114110
() => ({
115-
isSelected: (elementId: string) => {
111+
isSelected: (elementId: ElementId) => {
116112
return selectedElements.has(elementId)
117113
},
118114

@@ -132,7 +128,7 @@ export const SelectedElementProvider: React.FC<{
132128
dispatch({ type: 'ADD_SELECTION', payload: element })
133129
},
134130

135-
removeSelection: (elementId: string) => {
131+
removeSelection: (elementId: ElementId) => {
136132
dispatch({ type: 'REMOVE_SELECTION', payload: elementId })
137133
},
138134

packages/webui/src/client/ui/SegmentTimeline/SegmentTimelineContainer.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,13 +152,12 @@ function SegmentTimelineContainerWithSelection(props: Readonly<IProps>): JSX.Ele
152152
console.log('handleSegmentSelect', segmentId)
153153
clearAndSetSelection({
154154
type: 'segment',
155-
id: String(segmentId),
156155
elementId: segmentId,
157156
})
158157
}
159158

160159
const handleIsSelected = (segmentId: SegmentId) => {
161-
return isSelected(String(segmentId))
160+
return isSelected(segmentId)
162161
}
163162

164163
return (

0 commit comments

Comments
 (0)