Files
personal/components/dom/PasscodeUnlockScreen.tsx
T
virtheart 285028c61d feat: 北极新一中文个人站 — MiSans 分片字体 + 全站内容国内化
- 字体: cn-font-split 分片 MiSans(4字重×~88片, unicode-range 按需加载)
  接入 --font-sans/--font-mono/--font-mono-2 栈尾; 修复 next/font 与
  Tailwind @theme 变量重名导致的死规则(--font-geist-mono/--font-departure-mono)
- 内容: 品牌名/首屏/关于/导航/页脚/滚动段/文章页 UI/解锁屏全部中文化;
  元数据 title 北极新一©2026, lang=zh-CN; 日期中文格式
- 彩蛋: 品牌标签 TikTok→小红书(保留 char-code 混淆与口令机制), /2026
  文案改为个人创作手记语境
- 页脚: 邮箱 + 抖音/微信/小红书; 有链接项点击新开主页, 全部支持悬停
  二维码卡片(正方形白衬+object-contain 防变形); 无链接项(微信)点击弹
  二维码浮层
- 移除原作者手写签名动画(AboutMedia)
- 构建: cn-font-split devDependency + onlyBuiltDependencies 允许列表
2026-09-02 17:18:22 +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}>
{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" ? "验证通过" : "请输入访问口令"}
</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 };