|
| 1 | +import { useState, useEffect, useRef } from "react"; |
| 2 | +import { useLingui } from "@lingui/react/macro"; |
| 3 | +import { Popover, PopoverButton, PopoverPanel } from "@headlessui/react"; |
| 4 | +import { HiCog6Tooth } from "react-icons/hi2"; |
| 5 | + |
| 6 | +import { SettingsSpeedTab } from "./SettingsSpeedTab"; |
| 7 | +import { SettingsRootTab } from "./SettingsRootTab"; |
| 8 | +import { useVideoActions, useVideoState } from "../../hooks"; |
| 9 | +import { useSetAtom } from "jotai"; |
| 10 | +import { settingsPopoverOpenAtom } from "../../store/video"; |
| 11 | + |
| 12 | +export function SettingsPopover() { |
| 13 | + const { t } = useLingui(); |
| 14 | + const [settingsTab, setSettingsTab] = useState<"root" | "speed">("root"); |
| 15 | + const popoverOpenRef = useRef(false); |
| 16 | + const setSettingsPopoverOpen = useSetAtom(settingsPopoverOpenAtom); |
| 17 | + |
| 18 | + const videoActions = useVideoActions(); |
| 19 | + const videoState = useVideoState(); |
| 20 | + |
| 21 | + // Update the global popover state when ref changes |
| 22 | + useEffect(() => { |
| 23 | + setSettingsPopoverOpen(popoverOpenRef.current); |
| 24 | + }); |
| 25 | + |
| 26 | + return ( |
| 27 | + <Popover> |
| 28 | + {({ open }) => { |
| 29 | + // Update ref when popover state changes |
| 30 | + popoverOpenRef.current = open; |
| 31 | + |
| 32 | + return ( |
| 33 | + <> |
| 34 | + <PopoverButton |
| 35 | + onClick={() => { |
| 36 | + setSettingsTab("root"); |
| 37 | + }} |
| 38 | + className="inline-flex items-center gap-2 rounded-md px-3 py-1.5 text-sm/6 font-semibold text-white shadow-inner shadow-white/10 focus:not-data-focus:outline-none data-focus:outline data-focus:outline-white data-hover:bg-gray-900/95 data-open:bg-gray-900/95" |
| 39 | + title={t`Settings`} |
| 40 | + > |
| 41 | + <HiCog6Tooth |
| 42 | + className={`h-5 w-5 transition-transform duration-300 ${ |
| 43 | + open ? "rotate-90" : "rotate-0" |
| 44 | + }`} |
| 45 | + /> |
| 46 | + </PopoverButton> |
| 47 | + |
| 48 | + <PopoverPanel |
| 49 | + transition |
| 50 | + anchor="bottom end" |
| 51 | + // Prevent clicks within the panel from propagating to the underlying |
| 52 | + // video container, which could interpret them as play/pause toggles. |
| 53 | + onClick={(event) => { |
| 54 | + event.stopPropagation(); |
| 55 | + }} |
| 56 | + onMouseDown={(event) => { |
| 57 | + event.stopPropagation(); |
| 58 | + }} |
| 59 | + className="w-64 origin-top-right rounded-xl border border-white/5 bg-gray-900/95 p-1 text-sm/6 text-white transition duration-100 ease-out [--anchor-gap:--spacing(1)] focus:outline-none data-closed:scale-95 data-closed:opacity-0" |
| 60 | + > |
| 61 | + {settingsTab === "root" ? ( |
| 62 | + <SettingsRootTab |
| 63 | + toggleLoop={videoActions.toggleLoop} |
| 64 | + loop={videoState.loop} |
| 65 | + onSpeedTab={() => { |
| 66 | + setSettingsTab("speed"); |
| 67 | + }} |
| 68 | + /> |
| 69 | + ) : null} |
| 70 | + |
| 71 | + {settingsTab === "speed" ? ( |
| 72 | + <SettingsSpeedTab |
| 73 | + onBack={() => { |
| 74 | + setSettingsTab("root"); |
| 75 | + }} |
| 76 | + /> |
| 77 | + ) : null} |
| 78 | + </PopoverPanel> |
| 79 | + </> |
| 80 | + ); |
| 81 | + }} |
| 82 | + </Popover> |
| 83 | + ); |
| 84 | +} |
0 commit comments