refactor: show menu only if enabled

This commit is contained in:
Carlos Valente
2025-02-17 15:34:00 +01:00
committed by Carlos Valente
parent 4cc138e3c0
commit 2c8b6180d7
4 changed files with 90 additions and 65 deletions
@@ -55,6 +55,17 @@ export const useEventAction = () => {
defaultEndAction, defaultEndAction,
} = useEditorSettings(); } = useEditorSettings();
const getEventById = useCallback(
(eventId: string) => {
const cachedRundown = queryClient.getQueryData<RundownCached>(RUNDOWN);
if (!cachedRundown?.rundown) {
return;
}
return cachedRundown.rundown[eventId];
},
[queryClient],
);
/** /**
* Calls mutation to add new event * Calls mutation to add new event
* @private * @private
@@ -615,6 +626,7 @@ export const useEventAction = () => {
batchUpdateEvents, batchUpdateEvents,
deleteEvent, deleteEvent,
deleteAllEvents, deleteAllEvents,
getEventById,
reorderEvent, reorderEvent,
swapEvents, swapEvents,
updateEvent, updateEvent,
@@ -1,5 +1,4 @@
import { useRef } from 'react'; import { useRef } from 'react';
import { Menu } from '@chakra-ui/react';
import { ColumnDef, flexRender, getCoreRowModel, useReactTable } from '@tanstack/react-table'; import { ColumnDef, flexRender, getCoreRowModel, useReactTable } from '@tanstack/react-table';
import Color from 'color'; import Color from 'color';
import { import {
@@ -23,7 +22,6 @@ import BlockRow from './cuesheet-table-elements/BlockRow';
import CuesheetHeader from './cuesheet-table-elements/CuesheetHeader'; import CuesheetHeader from './cuesheet-table-elements/CuesheetHeader';
import DelayRow from './cuesheet-table-elements/DelayRow'; import DelayRow from './cuesheet-table-elements/DelayRow';
import EventRow from './cuesheet-table-elements/EventRow'; import EventRow from './cuesheet-table-elements/EventRow';
import CuesheetTableMenu from './cuesheet-table-menu/CuesheetTableMenu';
import CuesheetTableSettings from './cuesheet-table-settings/CuesheetTableSettings'; import CuesheetTableSettings from './cuesheet-table-settings/CuesheetTableSettings';
import useColumnManager from './useColumnManager'; import useColumnManager from './useColumnManager';
@@ -165,13 +163,16 @@ export default function CuesheetTable(props: CuesheetTableProps) {
} }
return ( return (
<Menu key={key} variant='ontime-on-dark' size='sm' isLazy>
<EventRow <EventRow
key={key}
eventId={entry.id}
eventIndex={eventIndex} eventIndex={eventIndex}
rowIndex={index}
isPast={isPast} isPast={isPast}
selectedRef={isSelected ? selectedRef : undefined} selectedRef={isSelected ? selectedRef : undefined}
skip={entry.skip} skip={entry.skip}
colour={entry.colour} colour={entry.colour}
showModal={showModal}
> >
{row.getVisibleCells().map((cell) => { {row.getVisibleCells().map((cell) => {
return ( return (
@@ -181,8 +182,6 @@ export default function CuesheetTable(props: CuesheetTableProps) {
); );
})} })}
</EventRow> </EventRow>
<CuesheetTableMenu event={entry} entryIndex={index} showModal={showModal} />
</Menu>
); );
} }
@@ -1,23 +1,27 @@
import { memo, MutableRefObject, PropsWithChildren, useLayoutEffect, useRef, useState } from 'react'; import { memo, MutableRefObject, PropsWithChildren, useLayoutEffect, useRef, useState } from 'react';
import { IconButton, MenuButton } from '@chakra-ui/react'; import { IconButton, Menu, MenuButton } from '@chakra-ui/react';
import { IoEllipsisHorizontal } from '@react-icons/all-files/io5/IoEllipsisHorizontal'; import { IoEllipsisHorizontal } from '@react-icons/all-files/io5/IoEllipsisHorizontal';
import Color from 'color'; import Color from 'color';
import { cx, getAccessibleColour } from '../../../../common/utils/styleUtils'; import { cx, getAccessibleColour } from '../../../../common/utils/styleUtils';
import { useCuesheetOptions } from '../../cuesheet.options'; import { useCuesheetOptions } from '../../cuesheet.options';
import CuesheetTableMenu from '../cuesheet-table-menu/CuesheetTableMenu';
import style from '../CuesheetTable.module.scss'; import style from '../CuesheetTable.module.scss';
interface EventRowProps { interface EventRowProps {
eventId: string;
eventIndex: number; eventIndex: number;
rowIndex: number;
isPast?: boolean; isPast?: boolean;
selectedRef?: MutableRefObject<HTMLTableRowElement | null>; selectedRef?: MutableRefObject<HTMLTableRowElement | null>;
skip?: boolean; skip?: boolean;
colour?: string; colour?: string;
showModal: (eventId: string) => void;
} }
function EventRow(props: PropsWithChildren<EventRowProps>) { function EventRow(props: PropsWithChildren<EventRowProps>) {
const { children, eventIndex, isPast, selectedRef, skip, colour } = props; const { children, eventId, eventIndex, rowIndex, isPast, selectedRef, skip, colour, showModal } = props;
const { hideIndexColumn, showActionMenu } = useCuesheetOptions(); const { hideIndexColumn, showActionMenu } = useCuesheetOptions();
const ownRef = useRef<HTMLTableRowElement>(null); const ownRef = useRef<HTMLTableRowElement>(null);
const [isVisible, setIsVisible] = useState(false); const [isVisible, setIsVisible] = useState(false);
@@ -60,6 +64,7 @@ function EventRow(props: PropsWithChildren<EventRowProps>) {
> >
{showActionMenu && ( {showActionMenu && (
<td className={style.actionColumn}> <td className={style.actionColumn}>
<Menu variant='ontime-on-dark' size='sm' isLazy>
<MenuButton <MenuButton
as={IconButton} as={IconButton}
size='sm' size='sm'
@@ -67,6 +72,8 @@ function EventRow(props: PropsWithChildren<EventRowProps>) {
icon={<IoEllipsisHorizontal />} icon={<IoEllipsisHorizontal />}
variant='ontime-subtle' variant='ontime-subtle'
/> />
<CuesheetTableMenu eventId={eventId} entryIndex={rowIndex} showModal={showModal} />
</Menu>
</td> </td>
)} )}
{!hideIndexColumn && ( {!hideIndexColumn && (
@@ -1,44 +1,50 @@
import { MenuDivider, MenuItem, MenuList } from '@chakra-ui/react'; import { MenuDivider, MenuItem, MenuList, Portal } from '@chakra-ui/react';
import { IoAdd } from '@react-icons/all-files/io5/IoAdd'; import { IoAdd } from '@react-icons/all-files/io5/IoAdd';
import { IoArrowDown } from '@react-icons/all-files/io5/IoArrowDown'; import { IoArrowDown } from '@react-icons/all-files/io5/IoArrowDown';
import { IoArrowUp } from '@react-icons/all-files/io5/IoArrowUp'; import { IoArrowUp } from '@react-icons/all-files/io5/IoArrowUp';
import { IoDuplicateOutline } from '@react-icons/all-files/io5/IoDuplicateOutline'; import { IoDuplicateOutline } from '@react-icons/all-files/io5/IoDuplicateOutline';
import { IoOptions } from '@react-icons/all-files/io5/IoOptions'; import { IoOptions } from '@react-icons/all-files/io5/IoOptions';
import { IoTrash } from '@react-icons/all-files/io5/IoTrash'; import { IoTrash } from '@react-icons/all-files/io5/IoTrash';
import { OntimeEvent, SupportedEvent } from 'ontime-types'; import { isOntimeEvent, SupportedEvent } from 'ontime-types';
import { useEventAction } from '../../../../common/hooks/useEventAction'; import { useEventAction } from '../../../../common/hooks/useEventAction';
import { cloneEvent } from '../../../../common/utils/eventsManager'; import { cloneEvent } from '../../../../common/utils/eventsManager';
interface CuesheetTableMenuProps { interface CuesheetTableMenuProps {
event: OntimeEvent; eventId: string;
entryIndex: number; entryIndex: number;
showModal: (entryId: string) => void; showModal: (entryId: string) => void;
} }
export default function CuesheetTableMenu(props: CuesheetTableMenuProps) { export default function CuesheetTableMenu(props: CuesheetTableMenuProps) {
const { event, entryIndex, showModal } = props; const { eventId, entryIndex, showModal } = props;
const { addEvent, reorderEvent, deleteEvent } = useEventAction(); const { addEvent, getEventById, reorderEvent, deleteEvent } = useEventAction();
const handleCloneEvent = () => { const handleCloneEvent = () => {
const newEvent = cloneEvent(event); const currentEvent = getEventById(eventId);
if (!currentEvent || !isOntimeEvent(currentEvent)) {
return;
}
const newEvent = cloneEvent(currentEvent);
try { try {
addEvent(newEvent, { after: event.id }); addEvent(newEvent, { after: eventId });
} catch (_error) { } catch (_error) {
// we do not handle errors here // we do not handle errors here
} }
}; };
return ( return (
<Portal>
<MenuList> <MenuList>
<MenuItem icon={<IoOptions />} onClick={() => showModal(event.id)}> <MenuItem icon={<IoOptions />} onClick={() => showModal(eventId)}>
Edit ... Edit ...
</MenuItem> </MenuItem>
<MenuDivider /> <MenuDivider />
<MenuItem icon={<IoAdd />} onClick={() => addEvent({ type: SupportedEvent.Event }, { before: event.id })}> <MenuItem icon={<IoAdd />} onClick={() => addEvent({ type: SupportedEvent.Event }, { before: eventId })}>
Add event above Add event above
</MenuItem> </MenuItem>
<MenuItem icon={<IoAdd />} onClick={() => addEvent({ type: SupportedEvent.Event }, { after: event.id })}> <MenuItem icon={<IoAdd />} onClick={() => addEvent({ type: SupportedEvent.Event }, { after: eventId })}>
Add event below Add event below
</MenuItem> </MenuItem>
<MenuItem icon={<IoDuplicateOutline />} onClick={handleCloneEvent}> <MenuItem icon={<IoDuplicateOutline />} onClick={handleCloneEvent}>
@@ -48,16 +54,17 @@ export default function CuesheetTableMenu(props: CuesheetTableMenuProps) {
<MenuItem <MenuItem
isDisabled={entryIndex < 1} isDisabled={entryIndex < 1}
icon={<IoArrowUp />} icon={<IoArrowUp />}
onClick={() => reorderEvent(event.id, entryIndex, entryIndex - 1)} onClick={() => reorderEvent(eventId, entryIndex, entryIndex - 1)}
> >
Move up Move up
</MenuItem> </MenuItem>
<MenuItem icon={<IoArrowDown />} onClick={() => reorderEvent(event.id, entryIndex, entryIndex + 1)}> <MenuItem icon={<IoArrowDown />} onClick={() => reorderEvent(eventId, entryIndex, entryIndex + 1)}>
Move down Move down
</MenuItem> </MenuItem>
<MenuItem icon={<IoTrash />} onClick={() => deleteEvent([event.id])}> <MenuItem icon={<IoTrash />} onClick={() => deleteEvent([eventId])}>
Delete Delete
</MenuItem> </MenuItem>
</MenuList> </MenuList>
</Portal>
); );
} }