"use client"; /** * PasscodeUnlockScreen — 1:1 port of the original module 4278 component `O` * (d59f7a97fb1c563f.js): * * - A fixed z-1000 bg-b1 overlay; a hidden text input holds the code. * - PASSCODE.slotCount circles (inset ring: label-1 filled/current, * label-3 empty); the current slot shows a blinking caret. * - At slotCount characters the code auto-submits: failure shakes the row * (x keyframes 0,-10,10,-8,8,-4,4,0 over 0.5s) and clears; success flips * the hint to "Access granted" and navigates to the return path after * 600ms (router.replace, like the original startNavigation(...,{replace})). * - If the scope is already unlocked the screen immediately returns. * - Renders null when the scope is not passcode-protected. */ import { useEffect, useRef, useState } from "react"; import { useRouter } from "next/navigation"; import { PASSCODE, isPasscodeProtectedPath, normalizePath, sanitizePasscodeReturnTo, scopeToPath, } from "@/lib/passcode"; import { notifyPasscodeAccessChanged, submitPasscodeUnlock, usePasscodeAllowed, } from "@/components/dom/PasscodeAccess"; /** Original framer `T.start({x:[0,-10,10,-8,8,-4,4,0], duration:.5, easeInOut})`. */ function shake(el: HTMLElement | null): Promise { if (!el || typeof el.animate !== "function") return Promise.resolve(); const anim = el.animate( { transform: ["translateX(0)", "translateX(-10px)", "translateX(10px)", "translateX(-8px)", "translateX(8px)", "translateX(-4px)", "translateX(4px)", "translateX(0)"], }, { duration: 500, easing: "ease-in-out" } ); return anim.finished.then( () => undefined, () => undefined ); } export function PasscodeUnlockScreen({ scope, returnTo, }: { scope: string; returnTo?: string | null; }) { const router = useRouter(); const path = scopeToPath(scope); const protectedScope = isPasscodeProtectedPath(path); const target = sanitizePasscodeReturnTo(returnTo) ?? path; const allowed = usePasscodeAllowed(path); const inputRef = useRef(null); const rowRef = useRef(null); const [code, setCode] = useState(""); const [status, setStatus] = useState<"idle" | "success">("idle"); const slotCount = PASSCODE.slotCount; const inputId = `passcode-${path}`; useEffect(() => { if (!protectedScope) return; if (allowed) { router.replace(target); return; } inputRef.current?.focus(); }, [target, protectedScope, allowed, router]); if (!protectedScope) return null; const onChange = (value: string) => { if (status === "success") return; const next = value.slice(0, slotCount); setCode(next); if (next.length === slotCount) { void (async () => { const res = await submitPasscodeUnlock(path, next); if (!res.ok) { await shake(rowRef.current); setCode(""); inputRef.current?.focus(); return; } notifyPasscodeAccessChanged(); setStatus("success"); window.setTimeout(() => { router.replace(target); }, 600); })(); } }; return (
inputRef.current?.focus()} > onChange(e.target.value)} className="absolute opacity-0 w-px h-px" />
); } /** Kept for parity with the original import surface (normalizePath helper). */ export { normalizePath };