import { createSlice, PayloadAction } from "@reduxjs/toolkit"; import { ChyronSettings, RootState, GridSpot } from "../../../types/state"; import { Optional } from "../../../types/util"; const initialState: RootState["broadcastDirector"] = { selectedCars: [], cursorGridSpot: null, chyronSettings: { mode: "driver", subMode: "medium", flagMode: "country", showPosFlag: true, showDriverNumber: true, showPortrait: false, }, }; export const broadcastDirectorSlice = createSlice({ name: "broadcastDirector", initialState, reducers: { pushSelectedCars: (state, action: PayloadAction) => { if (!state.selectedCars.includes(action.payload)) { state.selectedCars.push(action.payload); } }, setSelectedCars: (state, action: PayloadAction) => { // Might be removed. state.selectedCars = [action.payload]; }, removeSelectedCars: (state, action: PayloadAction) => { state.selectedCars = state.selectedCars.filter(grid => grid !== action.payload); }, /** * Shortcut to clear out selected cars. There is also a trick - if you * provide a single Car it'll replace all selected cars with that single * car. * @param action Car (optional) */ clearSelectedCars: (state, action: PayloadAction>) => { if (action.payload == null) { state.selectedCars = []; } else { state.selectedCars = [action.payload]; } }, /** Replaces the whole selection in one go. Used by the keyboard layer. */ replaceSelectedCars: (state, action: PayloadAction) => { state.selectedCars = action.payload.filter( (spot, index) => action.payload.indexOf(spot) === index, ); }, /** Adds or removes a single car from the selection. Capped at two cars. */ toggleSelectedCar: (state, action: PayloadAction) => { const existing = state.selectedCars.indexOf(action.payload); if (existing !== -1) { state.selectedCars.splice(existing, 1); } else { state.selectedCars.push(action.payload); if (state.selectedCars.length > 2) { state.selectedCars.splice(0, state.selectedCars.length - 2); } } }, /** * Parks the keyboard cursor. Kept apart from the selection so the cursor * can be moved around the running order without wiping a built selection. */ setCursorGridSpot: (state, action: PayloadAction>) => { state.cursorGridSpot = action.payload; }, updateChyronSettings: (state, action: PayloadAction>) => { state.chyronSettings = { ...state.chyronSettings, ...action.payload }; }, }, }); export const { pushSelectedCars, setSelectedCars, removeSelectedCars, clearSelectedCars, replaceSelectedCars, toggleSelectedCar, setCursorGridSpot, updateChyronSettings, } = broadcastDirectorSlice.actions; export default broadcastDirectorSlice.reducer;