import { InlineCluster } from "@bedrock-layout/inline-cluster"; import { useDispatch, useSelector } from "react-redux"; import { broadcastDirectorSelector } from "../../../../features/broadcast/director/broadcastDirectorSelector"; import { pushSelectedCars, clearSelectedCars, removeSelectedCars, setCursorGridSpot, } from "../../../../features/broadcast/director/broadcastDirectorSlice"; import { eventSelector } from "../../../../features/event/eventSelector"; import { GridSpot } from "../../../../types/state"; import { DriverLabel, GridItem, SelectionLabel } from "./styles"; export function GridSelection() { const dispatch = useDispatch(); const { grid, } = useSelector(eventSelector); const { selectedCars, cursorGridSpot, } = useSelector(broadcastDirectorSelector); const carClick = (gridSpot: GridSpot, selected: boolean) => () => { // Clicking also parks the keyboard cursor here, so the mouse and the // arrow keys always agree on where "here" is. dispatch(setCursorGridSpot(gridSpot)); if (selected) { dispatch(removeSelectedCars(gridSpot)); } else { dispatch(pushSelectedCars(gridSpot)); } }; const carDoubleClick = (gridSpot: GridSpot) => () => { dispatch(setCursorGridSpot(gridSpot)); dispatch(clearSelectedCars(gridSpot)); }; return ( {grid.map((car, gridSpot) => { const selectedIndex = selectedCars.findIndex(g => g === gridSpot); const selected = selectedIndex !== -1; const onClick = carClick(gridSpot, selected); const onDoubleClick = carDoubleClick(gridSpot); return ( {car.driver.initials} {selected && ( {selectedIndex + 1} )} ); })} ); }