import { EntryId, PlayableEvent } from 'ontime-types'; import { useState } from 'react'; import { IoArrowBack, IoClose, IoSaveOutline } from 'react-icons/io5'; import { useNavigate } from 'react-router'; import Button from '../../common/components/buttons/Button'; import { ExtendedEntry } from '../../common/utils/rundownMetadata'; import { cx } from '../../common/utils/styleUtils'; import ClockTime from '../common/clock-time/ClockTime'; import { makeSubscriptionsUrl } from './countdown.utils'; import './Countdown.scss'; interface CountdownSelectProps { events: ExtendedEntry[]; subscriptions: EntryId[]; disableEdit: () => void; } export default function CountdownSelect({ events, subscriptions, disableEdit }: CountdownSelectProps) { const [selected, setSelected] = useState(subscriptions); const navigate = useNavigate(); /** * Toggles an entry from the selected set */ const toggleSelect = (entryId: EntryId) => { setSelected((prev) => { if (prev.includes(entryId)) { // If the entry is already selected, remove it return prev.filter((id) => id !== entryId); } return [...prev, entryId]; }); }; /** * Creates a URL with the selected subscriptions * and navigates to it */ const applySelection = () => { // we remove events that no longer exist to avoid stale subscriptions const filteredSelected = selected.filter((id) => events.some((event) => event.id === id)); const url = makeSubscriptionsUrl(window.location.href, filteredSelected); disableEdit(); setSelected([]); navigate(url.search.toString()); }; // make a copy of the selected array for quick lookup const selectedIds = new Set(selected); return (
{events.map((event, index) => { const title = event.title || '{no title}'; const isSelected = selectedIds.has(event.id); return (
toggleSelect(event.id)} onKeyDown={(e) => { if (e.key === 'Enter' || e.key === ' ') { toggleSelect(event.id); e.stopPropagation(); } }} className={cx(['sub', isSelected && 'sub--selected'])} >
{isSelected ? 'Click to remove' : 'Click to add'}
{title}
); })}
); }