|
| 1 | +// AddCueMenu.res - FAB button with dropdown menu for adding/splitting cues |
| 2 | + |
| 3 | +@react.component |
| 4 | +let make = ( |
| 5 | + ~hasPauseBefore: bool, |
| 6 | + ~hasPauseAfter: bool, |
| 7 | + ~wordChunks: array<WordTimestamps.wordChunk>, |
| 8 | + ~onAddBefore: unit => unit, |
| 9 | + ~onAddAfter: unit => unit, |
| 10 | + ~onSplit: int => unit, |
| 11 | + ~onSplitPreviewChange: option<int> => unit, |
| 12 | +) => { |
| 13 | + let (isOpen, setIsOpen) = React.useState(_ => false) |
| 14 | + let (splitPreview, setSplitPreview) = React.useState(_ => None) |
| 15 | + |
| 16 | + let wordCount = Array.length(wordChunks) |
| 17 | + let canSplit = wordCount >= 2 |
| 18 | + |
| 19 | + // Reset preview when dropdown closes |
| 20 | + React.useEffect1(() => { |
| 21 | + if !isOpen { |
| 22 | + setSplitPreview(_ => None) |
| 23 | + onSplitPreviewChange(None) |
| 24 | + } |
| 25 | + None |
| 26 | + }, [isOpen]) |
| 27 | + |
| 28 | + // Update parent with preview changes |
| 29 | + React.useEffect1(() => { |
| 30 | + onSplitPreviewChange(splitPreview) |
| 31 | + None |
| 32 | + }, [splitPreview]) |
| 33 | + |
| 34 | + <DropdownMenu.Root open_=isOpen onOpenChange={v => setIsOpen(_ => v)}> |
| 35 | + <DropdownMenu.Trigger asChild=true> |
| 36 | + <button |
| 37 | + type_="button" |
| 38 | + className="absolute -bottom-4 right-1 rounded-full bg-zinc-900 border-2 border-orange-500 p-2 shadow-lg transition-all opacity-0 scale-90 group-focus-within:opacity-100 group-focus-within:scale-100 hover:scale-115 hover:bg-zinc-800 focus:outline-none focus-visible:ring-2 focus-visible:ring-orange-400 focus-visible:ring-offset-2 focus-visible:ring-offset-zinc-900 z-50"> |
| 39 | + <Icons.AdjustmentsVerticalIcon className="size-5 text-orange-500" /> |
| 40 | + </button> |
| 41 | + </DropdownMenu.Trigger> |
| 42 | + <DropdownMenu.Content sideOffset={8} align=#end> |
| 43 | + <DropdownMenu.Label> {React.string("Add or split cue")} </DropdownMenu.Label> |
| 44 | + // Add cue BEFORE |
| 45 | + <DropdownMenu.Item |
| 46 | + onClick={_ => { |
| 47 | + onAddBefore() |
| 48 | + setIsOpen(_ => false) |
| 49 | + }} |
| 50 | + disabled={!hasPauseBefore}> |
| 51 | + <span className="flex items-center gap-2"> |
| 52 | + <Icons.ArrowUpLeftIcon className="size-4" /> |
| 53 | + {React.string("Add cue before")} |
| 54 | + </span> |
| 55 | + </DropdownMenu.Item> |
| 56 | + {!hasPauseBefore |
| 57 | + ? <div className="px-3 py-1 text-xs text-zinc-500"> |
| 58 | + {React.string("No pause gap available")} |
| 59 | + </div> |
| 60 | + : React.null} |
| 61 | + // Add cue AFTER |
| 62 | + <DropdownMenu.Item |
| 63 | + onClick={_ => { |
| 64 | + onAddAfter() |
| 65 | + setIsOpen(_ => false) |
| 66 | + }} |
| 67 | + disabled={!hasPauseAfter}> |
| 68 | + <span className="flex items-center gap-2"> |
| 69 | + <Icons.ArrowDownLeftIcon className="size-4" /> |
| 70 | + {React.string("Add cue after")} |
| 71 | + </span> |
| 72 | + </DropdownMenu.Item> |
| 73 | + {!hasPauseAfter |
| 74 | + ? <div className="px-3 py-1 text-xs text-zinc-500"> |
| 75 | + {React.string("No pause gap available")} |
| 76 | + </div> |
| 77 | + : React.null} |
| 78 | + <DropdownMenu.Separator /> |
| 79 | + // Split current cue by words |
| 80 | + {canSplit |
| 81 | + ? <> |
| 82 | + <DropdownMenu.Label> |
| 83 | + <span className="flex items-center gap-2"> |
| 84 | + <Icons.ScissorsIcon className="size-4" /> |
| 85 | + {React.string("Split current cue")} |
| 86 | + </span> |
| 87 | + </DropdownMenu.Label> |
| 88 | + <div className="flex flex-col gap-2 p-3 min-w-48"> |
| 89 | + <Slider |
| 90 | + value={splitPreview->Option.getOr(wordCount / 2)} |
| 91 | + min={1} |
| 92 | + max={wordCount} |
| 93 | + step={1} |
| 94 | + onValueChange={v => setSplitPreview(_ => Some(v))} |
| 95 | + /> |
| 96 | + <button |
| 97 | + type_="button" |
| 98 | + onClick={_ => { |
| 99 | + onSplit(splitPreview->Option.getOr(wordCount / 2)) |
| 100 | + setIsOpen(_ => false) |
| 101 | + }} |
| 102 | + className="w-full py-1.5 px-3 rounded-lg bg-orange-500 hover:bg-orange-400 text-white text-sm font-medium transition-colors"> |
| 103 | + {React.string("Split")} |
| 104 | + </button> |
| 105 | + </div> |
| 106 | + </> |
| 107 | + : <div className="px-3 py-2 text-xs text-zinc-500"> |
| 108 | + {React.string("Need at least 2 words to split")} |
| 109 | + </div>} |
| 110 | + </DropdownMenu.Content> |
| 111 | + </DropdownMenu.Root> |
| 112 | +} |
0 commit comments