import { useCallback } from 'react'; import { IoCheckmark } from '@react-icons/all-files/io5/IoCheckmark'; import { CellContext, ColumnDef } from '@tanstack/react-table'; import { CustomFields, isOntimeEvent, OntimeEvent, OntimeRundownEntry } from 'ontime-types'; import DelayIndicator from '../../common/components/delay-indicator/DelayIndicator'; import RunningTime from '../viewers/common/running-time/RunningTime'; import EditableCell from './cuesheet-table-elements/EditableCell'; import { useCuesheetSettings } from './store/CuesheetSettings'; import style from './Cuesheet.module.scss'; function makePublic(row: CellContext) { const cellValue = row.getValue(); return cellValue ? : ''; } function MakeTimer({ getValue, row: { original } }: CellContext) { const showDelayedTimes = useCuesheetSettings((state) => state.showDelayedTimes); const hideSeconds = useCuesheetSettings((state) => state.hideSeconds); const cellValue = (getValue() as number | null) ?? 0; const delayValue = (original as OntimeEvent)?.delay ?? 0; return ( {delayValue !== 0 && showDelayedTimes && ( )} ); } function MakeDuration({ getValue }: CellContext) { const hideSeconds = useCuesheetSettings((state) => state.hideSeconds); const cellValue = (getValue() as number | null) ?? 0; return ; } function MakeCustomField({ row, column, table }: CellContext) { const update = useCallback( (newValue: string) => { // @ts-expect-error -- we inject this into react-table table.options.meta?.handleUpdate(row.index, column.id, newValue); }, // eslint-disable-next-line react-hooks/exhaustive-deps -- we skip table.options.meta since the reference seems unstable [column.id, row.index], ); const event = row.original; if (!isOntimeEvent(event)) { return null; } // events dont necessarily contain all custom fields const initialValue = event.custom[column.id] ?? ''; return ; } export function makeCuesheetColumns(customFields: CustomFields): ColumnDef[] { const dynamicCustomFields = Object.keys(customFields).map((key) => ({ accessorKey: key, id: key, header: customFields[key].label, meta: { colour: customFields[key].colour }, cell: MakeCustomField, size: 250, })); return [ { accessorKey: 'cue', id: 'cue', header: 'Cue', cell: (row) => row.getValue(), size: 75, }, { accessorKey: 'isPublic', id: 'isPublic', header: 'Public', cell: makePublic, size: 45, }, { accessorKey: 'timeStart', id: 'timeStart', header: 'Start', cell: MakeTimer, size: 75, }, { accessorKey: 'timeEnd', id: 'timeEnd', header: 'End', cell: MakeTimer, size: 75, }, { accessorKey: 'duration', id: 'duration', header: 'Duration', cell: MakeDuration, size: 75, }, { accessorKey: 'title', id: 'title', header: 'Title', cell: (row) => row.getValue(), size: 250, }, { accessorKey: 'note', id: 'note', header: 'Note', cell: (row) => row.getValue(), size: 250, }, ...dynamicCustomFields, ]; }