import { Menu, MenuButton, MenuItem, MenuItems } from '@headlessui/react'; import deleteSchedule from '@pages/background/lib/deleteSchedule'; import duplicateSchedule from '@pages/background/lib/duplicateSchedule'; import renameSchedule from '@pages/background/lib/renameSchedule'; import type { UserSchedule } from '@shared/types/UserSchedule'; import Text from '@views/components/common/Text/Text'; import useSchedules from '@views/hooks/useSchedules'; import clsx from 'clsx'; import React, { useEffect, useMemo, useState } from 'react'; import DragIndicatorIcon from '~icons/material-symbols/drag-indicator'; import MoreActionsIcon from '~icons/material-symbols/more-vert'; import { Button } from './Button'; import DialogProvider, { usePrompt } from './DialogProvider/DialogProvider'; import { ExtensionRootWrapper, styleResetClass } from './ExtensionRoot/ExtensionRoot'; /** * Props for the ScheduleListItem component. */ export type Props = { style?: React.CSSProperties; schedule: UserSchedule; dragHandleProps?: Omit, 'className'>; onClick?: React.DOMAttributes['onClick']; }; /** * This is a reusable dropdown component that can be used to toggle the visiblity of information */ export default function ScheduleListItem({ schedule, dragHandleProps, onClick }: Props): JSX.Element { const [activeSchedule] = useSchedules(); const [isEditing, setIsEditing] = useState(false); const [editorValue, setEditorValue] = useState(schedule.name); const showDialog = usePrompt(); const editorRef = React.useRef(null); useEffect(() => { const editor = editorRef.current; setEditorValue(schedule.name); if (isEditing && editor) { editor.focus(); editor.setSelectionRange(0, editor.value.length); } }, [isEditing, schedule.name, editorRef]); const isActive = useMemo(() => activeSchedule.id === schedule.id, [activeSchedule, schedule]); const handleBlur = async () => { if (editorValue.trim() !== '' && editorValue.trim() !== schedule.name) { schedule.name = (await renameSchedule(schedule.id, editorValue.trim())) as string; } setIsEditing(false); }; const handleDelete = () => { if (schedule.id === activeSchedule.id) { showDialog({ title: `Unable to delete active schedule.`, description: ( <> Deleting the active schedule {schedule.name} is not allowed. Please switch to another schedule and try again. ), // eslint-disable-next-line react/no-unstable-nested-components buttons: close => ( ), }); } else { showDialog({ title: `Are you sure?`, description: ( <> Deleting {schedule.name} is permanent and will remove all added courses from that schedule. ), // eslint-disable-next-line react/no-unstable-nested-components buttons: close => ( <> ), }); } }; return (
  • !isEditing && onClick?.(...e)} >
    {isEditing && ( setEditorValue(e.target.value)} onKeyDown={e => { if (e.key === 'Enter') handleBlur(); if (e.key === 'Escape') { setIsEditing(false); } }} onBlur={handleBlur} ref={editorRef} /> )} {!isEditing && ( setIsEditing(true)}> {schedule.name} )}
    setIsEditing(true)} className='w-full rounded bg-transparent p-2 text-left data-[focus]:bg-gray-200/40' > Rename duplicateSchedule(schedule.id)} className='w-full rounded bg-transparent p-2 text-left data-[focus]:bg-gray-200/40' > Duplicate Delete
  • ); }