import { IconPlayerSkipForwardFilled, IconX } from "@tabler/icons-react"; import { emit } from "@tauri-apps/api/event"; import { getCurrentWindow } from "@tauri-apps/api/window"; import { useCallback, useEffect, useRef, useState } from "react"; const COUNTDOWN_STEP_MS = 1000; /** * Full-screen transparent countdown overlay. Runs 3 → 2 → 1 on full-second * beats, then emits `clips:countdown-done` and closes its own window. The * recorder waits for that event before it starts capturing. */ export function Countdown() { const [n, setN] = useState(3); const closingRef = useRef(false); const closeWithEvent = useCallback( ( eventName: "clips:countdown-done" | "clips:countdown-cancel", cause: "escape" | "return" | "button" | "timer", ) => { if (closingRef.current) return; closingRef.current = true; emit(eventName, { cause }).finally(() => { getCurrentWindow() .close() .catch(() => {}); }); }, [], ); useEffect(() => { const onKeyDown = (event: KeyboardEvent) => { if (event.key === "Escape") { event.preventDefault(); closeWithEvent("clips:countdown-cancel", "escape"); } else if (event.key === "Enter") { event.preventDefault(); closeWithEvent("clips:countdown-done", "return"); } }; window.addEventListener("keydown", onKeyDown); return () => { window.removeEventListener("keydown", onKeyDown); }; }, [closeWithEvent]); useEffect(() => { if (n <= 0) { closeWithEvent("clips:countdown-done", "timer"); return; } const t = setTimeout(() => setN((v) => v - 1), COUNTDOWN_STEP_MS); return () => clearTimeout(t); }, [closeWithEvent, n]); return (