import { CSSProperties, useCallback, useEffect, useState } from 'react'; import { Button } from '@chakra-ui/react'; import { CustomFieldLabel, isOntimeEvent, OntimeEvent } from 'ontime-types'; import CopyTag from '../../../common/components/copy-tag/CopyTag'; import { useEventAction } from '../../../common/hooks/useEventAction'; import useCustomFields from '../../../common/hooks-query/useCustomFields'; import useRundown from '../../../common/hooks-query/useRundown'; import { getAccessibleColour } from '../../../common/utils/styleUtils'; import { useEventSelection } from '../useEventSelection'; import EventEditorTimes from './composite/EventEditorTimes'; import EventEditorTitles from './composite/EventEditorTitles'; import EventTextArea from './composite/EventTextArea'; import style from './EventEditor.module.scss'; export type EventEditorSubmitActions = keyof OntimeEvent; export type EditorUpdateFields = 'cue' | 'title' | 'note' | 'colour' | CustomFieldLabel; export default function EventEditor() { const selectedEvents = useEventSelection((state) => state.selectedEvents); const { data } = useRundown(); const { data: customFields } = useCustomFields(); const { order, rundown } = data; const { updateEvent } = useEventAction(); const [event, setEvent] = useState(null); useEffect(() => { if (order.length === 0) { setEvent(null); return; } const selectedEventId = order.find((eventId) => selectedEvents.has(eventId)); if (!selectedEventId) { setEvent(null); return; } const event = rundown[selectedEventId]; if (event && isOntimeEvent(event)) { setEvent(event); } else { setEvent(null); } }, [order, rundown, selectedEvents]); const handleSubmit = useCallback( (field: EditorUpdateFields, value: string) => { if (field.startsWith('custom-')) { const fieldLabel = field.split('custom-')[1]; updateEvent({ id: event?.id, custom: { [fieldLabel]: { value } } }); } else { updateEvent({ id: event?.id, [field]: value }); } }, [event?.id, updateEvent], ); if (!event) { return (
Select an event to edit
); } return (
Custom Fields
{Object.keys(customFields).map((label) => { const key = `${event.id}-${label}`; const fieldName = `custom-${label}`; const initialValue = event.custom[label]?.value ?? ''; const { backgroundColor, color } = getAccessibleColour(customFields[label].colour); return ( ); })}
{`/ontime/load/id "${event.id}"`} {`/ontime/load/cue "${event.cue}"`}
); }