Files
personal/components/dom/PasscodeUnlockScreen.tsx
T
virtheart 8ce5471c25 feat: 移植首页 passcode 彩蛋(■■■■■■ → /unlock/2026 解锁 TikTok 品牌名与隐藏作品卡)
- lib/passcode.ts:module 58156/28116 移植——primaryPasscodePath("/2026")、buildPasscodeUnlockHref("/unlock/2026?return=%2F")、slotCount:4、品牌名按原站 charCode-1 混淆解码(TikTok)、锁定占位符与字符/散段样式类
- PasscodeAccess.tsx:module 4278 移植——access store(useSyncExternalStore + passcode-access-changed 事件驱动刷新)、usePasscodeAllowed/usePasscodeAccessLookup、submit/logout(POST /api/passcode)
- PasscodeUnlockScreen:4 格圆环输入(inset ring label-1/label-3 + caret-blink 光标)、满 4 位自动提交、错误 shake(WAAPI 等价原 framer x 关键帧抖动)、成功 'Access granted' 600ms 后 replace 回来源、已解锁进入立即跳回
- layout 挂 PasscodeAccessProvider(服务端 cookies 读初始 access);新增 /api/passcode GET/POST(httpOnly cookie 持久化,解锁码经 PASSCODE_CODE 配置,默认 2026)与 /unlock/[scope] 页
- ScrambleReveal 支持 slot part({length,scramble,className,settled}):打散期显示占位字符、收尾替换为交互节点,1:1 原版语义
- 首页 about 段换 c9/c2 移植:锁定点击跳解锁页、解锁显示 TikTok、长按 500ms/右键 confirm 重锁;WorkSection 按 access 过滤,解锁后 TikTok 卡(tt01/tt02 素材)出现在首位
- CDP 全流程验证:锁定→解锁→持久化→重锁 循环 + 错码抖动清空 + 卡片数 10↔11
2026-09-01 19:21:52 +08:00

156 lines
5.0 KiB
TypeScript

"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<void> {
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<HTMLInputElement | null>(null);
const rowRef = useRef<HTMLDivElement | null>(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 (
<main
className="z-1000 fixed inset-0 flex justify-center items-center bg-b1"
onClick={() => inputRef.current?.focus()}
>
<label className="sr-only" htmlFor={inputId}>
Passcode for {path}
</label>
<input
ref={inputRef}
id={inputId}
type="text"
autoComplete="off"
spellCheck={false}
value={code}
onChange={(e) => onChange(e.target.value)}
className="absolute opacity-0 w-px h-px"
/>
<div className="relative" aria-hidden="true">
<span
className={`-top-10 left-1/2 absolute font-mono text-sm whitespace-nowrap -translate-x-1/2 select-none transition-colors ${
status === "success" ? "text-l1" : "text-l3"
}`}
>
{status === "success" ? "Access granted" : "Please enter passcode"}
</span>
<div ref={rowRef} className="flex gap-3">
{Array.from({ length: slotCount }).map((_, i) => {
const filled = i < code.length;
const current = i === code.length;
return (
<div
key={i}
className="flex justify-center items-center rounded-full w-12 h-12 font-mono tabular-nums text-l1 text-lg transition-shadow duration-300 ease-66"
style={{
boxShadow: `inset 0 0 0 4px ${
filled || current ? "var(--label-1)" : "var(--label-3)"
}`,
}}
>
{filled ? (
code[i]
) : current ? (
<span className="block bg-l1 w-px h-5 caret-blink" aria-hidden="true" />
) : null}
</div>
);
})}
</div>
</div>
</main>
);
}
/** Kept for parity with the original import surface (normalizePath helper). */
export { normalizePath };