1
0
Fork 0
polaris-task-force/src/components/frontend/voiceover/SpoilerGate.tsx
Z8MB1E 4d08122464 feat(voiceover): spoiler gate, pacing pattern, and recorder updates
Recorder and pages gain a spoiler gate; voiceover lib adds clause-pause pacing support.

Ultraworked with [Sisyphus](https://github.com/code-yeongyu/oh-my-openagent)

Co-authored-by: Sisyphus <clio-agent@sisyphuslabs.ai>
2026-09-14 15:37:12 -04:00

162 lines
4.7 KiB
TypeScript

"use client";
import { useRouter } from "next/navigation";
import { createContext, useContext, useMemo, useRef, useState, type ReactNode } from "react";
import { TriangleAlertIcon } from "lucide-react";
import { cn } from "@/lib/utils";
import { redactText } from "@/lib/redaction";
import {
AlertDialog,
AlertDialogCancel,
AlertDialogContent,
AlertDialogDescription,
AlertDialogFooter,
AlertDialogHeader,
AlertDialogTitle,
} from "@/components/ui/alert-dialog";
import { buttonVariants } from "@/components/ui/button";
const HOLD_CONFIRM_MS = 1000;
interface SpoilerAcknowledgment {
readonly acknowledged: boolean;
readonly acknowledge: () => void;
}
// Default to acknowledged so consumers outside a gate are never blocked.
const SpoilerContext = createContext<SpoilerAcknowledgment>({
acknowledged: true,
acknowledge: () => undefined,
});
export function useSpoilerAcknowledgment(): SpoilerAcknowledgment {
return useContext(SpoilerContext);
}
function HoldToConfirmButton({
onConfirm,
children,
}: {
readonly onConfirm: () => void;
readonly children: ReactNode;
}) {
const [holding, setHolding] = useState(false);
const timerRef = useRef<number | null>(null);
const doneRef = useRef(false);
const cancel = () => {
if (timerRef.current !== null) {
clearTimeout(timerRef.current);
timerRef.current = null;
}
setHolding(false);
};
const begin = () => {
if (timerRef.current !== null || doneRef.current) return;
setHolding(true);
timerRef.current = window.setTimeout(() => {
timerRef.current = null;
doneRef.current = true;
onConfirm();
}, HOLD_CONFIRM_MS);
};
return (
<button
type="button"
className={cn(buttonVariants(), "relative touch-none select-none overflow-hidden")}
onPointerDown={(event) => {
if (event.button === 0) begin();
}}
onPointerUp={cancel}
onPointerLeave={cancel}
onPointerCancel={cancel}
onContextMenu={(event) => event.preventDefault()}
onKeyDown={(event) => {
if ((event.key === " " || event.key === "Enter") && !event.repeat) begin();
}}
onKeyUp={(event) => {
if (event.key === " " || event.key === "Enter") cancel();
}}
onBlur={cancel}
>
<span aria-hidden="true" className="absolute inset-0">
<span
className={cn(
"absolute inset-0 origin-left bg-primary-foreground/40 transition-transform ease-linear",
holding ? "scale-x-100 duration-[1000ms]" : "scale-x-0 duration-0",
)}
/>
</span>
<span className="relative">{children}</span>
</button>
);
}
export function SpoilerGate({
spoiler,
children,
}: {
readonly spoiler: boolean;
readonly children: ReactNode;
}) {
const router = useRouter();
const [acknowledged, setAcknowledged] = useState(false);
const value = useMemo(
() => ({
acknowledged: !spoiler || acknowledged,
acknowledge: () => setAcknowledged(true),
}),
[acknowledged, spoiler],
);
return (
<SpoilerContext.Provider value={value}>
{children}
{spoiler && !acknowledged && (
<AlertDialog open>
<AlertDialogContent>
<AlertDialogHeader>
<AlertDialogTitle className="flex items-center gap-2">
<TriangleAlertIcon className="size-5 text-amber-400" />
Potential campaign spoilers
</AlertDialogTitle>
<AlertDialogDescription>
This recording page may include, infer, or otherwise expose campaign spoilers.
Continue only if you are comfortable with that. Press and hold the confirmation
button for one second to continue.
</AlertDialogDescription>
</AlertDialogHeader>
<AlertDialogFooter>
<AlertDialogCancel onClick={() => router.push("/voiceover")}>
Go back
</AlertDialogCancel>
<HoldToConfirmButton onConfirm={value.acknowledge}>I understand</HoldToConfirmButton>
</AlertDialogFooter>
</AlertDialogContent>
</AlertDialog>
)}
</SpoilerContext.Provider>
);
}
/**
* Renders the script description, redacted until the spoiler gate is
* acknowledged (plain text for non-spoiler scripts).
*/
export function SpoilerDescription({
spoiler,
description,
}: {
readonly spoiler: boolean;
readonly description: string | null;
}) {
const { acknowledged } = useSpoilerAcknowledgment();
const text = spoiler && !acknowledged && description ? redactText(description) : description;
if (!text?.trim()) return null;
return <p className="text-sm text-foreground/80">{text}</p>;
}