8ce5471c25
- 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
141 lines
4.0 KiB
TypeScript
141 lines
4.0 KiB
TypeScript
"use client";
|
|
|
|
/**
|
|
* PasscodeAccessProvider + hooks — 1:1 port of the original module 4278
|
|
* (d59f7a97fb1c563f.js): a module-level store (the original React context
|
|
* store object) with a snapshot + listener set, window-event driven refresh,
|
|
* and the same POST contract against /api/passcode.
|
|
*/
|
|
import {
|
|
createContext,
|
|
useCallback,
|
|
useContext,
|
|
useEffect,
|
|
useMemo,
|
|
useRef,
|
|
useSyncExternalStore,
|
|
} from "react";
|
|
import {
|
|
PASSCODE_ACCESS_CHANGED_EVENT,
|
|
PASSCODE_PROTECTED_PATHS,
|
|
emptyPasscodeAccessState,
|
|
isPasscodeProtectedPath,
|
|
normalizePath,
|
|
workHrefToPath,
|
|
type PasscodeAccessState,
|
|
} from "@/lib/passcode";
|
|
|
|
async function fetchAccess(): Promise<PasscodeAccessState> {
|
|
const res = await fetch("/api/passcode", { cache: "no-store" });
|
|
return res.ok ? ((res.json() as unknown) as Promise<PasscodeAccessState>) : emptyPasscodeAccessState();
|
|
}
|
|
|
|
async function postAccess(body: {
|
|
scope?: string;
|
|
code?: string;
|
|
action?: string;
|
|
}): Promise<Response> {
|
|
return fetch("/api/passcode", {
|
|
method: "POST",
|
|
headers: { "Content-Type": "application/json" },
|
|
body: JSON.stringify(body),
|
|
});
|
|
}
|
|
|
|
type Store = {
|
|
snapshot: PasscodeAccessState;
|
|
listeners: Set<() => void>;
|
|
refresh: () => Promise<void>;
|
|
};
|
|
|
|
const AccessContext = createContext<Store | null>(null);
|
|
|
|
function createStore(initialAccess: PasscodeAccessState): Store {
|
|
return {
|
|
snapshot: initialAccess,
|
|
listeners: new Set(),
|
|
async refresh() {
|
|
const next = await fetchAccess();
|
|
const keys = Object.keys(next);
|
|
const same =
|
|
keys.length === Object.keys(this.snapshot).length &&
|
|
keys.every((k) => next[k] === this.snapshot[k]);
|
|
if (same) return;
|
|
this.snapshot = next;
|
|
this.listeners.forEach((l) => l());
|
|
},
|
|
};
|
|
}
|
|
|
|
export function PasscodeAccessProvider({
|
|
initialAccess,
|
|
children,
|
|
}: {
|
|
initialAccess: PasscodeAccessState;
|
|
children: React.ReactNode;
|
|
}) {
|
|
const storeRef = useRef<Store | null>(null);
|
|
if (!storeRef.current) storeRef.current = createStore(initialAccess);
|
|
|
|
useEffect(() => {
|
|
const store = storeRef.current;
|
|
if (!store) return;
|
|
const onChange = () => void store.refresh();
|
|
window.addEventListener(PASSCODE_ACCESS_CHANGED_EVENT, onChange);
|
|
return () => window.removeEventListener(PASSCODE_ACCESS_CHANGED_EVENT, onChange);
|
|
}, []);
|
|
|
|
return <AccessContext.Provider value={storeRef.current}>{children}</AccessContext.Provider>;
|
|
}
|
|
|
|
/** notifyPasscodeAccessChanged (original `L`). */
|
|
export function notifyPasscodeAccessChanged() {
|
|
window.dispatchEvent(new CustomEvent(PASSCODE_ACCESS_CHANGED_EVENT));
|
|
}
|
|
|
|
/** submitPasscodeUnlock (original `B`). */
|
|
export function submitPasscodeUnlock(scope: string, code: string): Promise<Response> {
|
|
return postAccess({ scope, code });
|
|
}
|
|
|
|
/** logoutPasscodeAccess (original `j`). */
|
|
export async function logoutPasscodeAccess(opts?: { reload?: boolean }): Promise<boolean> {
|
|
const res = await postAccess({ action: "logout" });
|
|
if (!(res as Response & { ok: boolean }).ok) return false;
|
|
if (opts?.reload) window.location.reload();
|
|
notifyPasscodeAccessChanged();
|
|
return true;
|
|
}
|
|
|
|
/** usePasscodeAccessLookup (original `F`): (href) => unlocked?. */
|
|
export function usePasscodeAccessLookup() {
|
|
const store = useContext(AccessContext);
|
|
const fallback = useMemo(() => emptyPasscodeAccessState(), []);
|
|
const snapshot = useSyncExternalStore(
|
|
useCallback(
|
|
(notify: () => void) => {
|
|
if (!store) return () => {};
|
|
store.listeners.add(notify);
|
|
return () => store.listeners.delete(notify);
|
|
},
|
|
[store]
|
|
),
|
|
() => store?.snapshot ?? fallback,
|
|
() => store?.snapshot ?? fallback
|
|
);
|
|
return useCallback(
|
|
(href: string) => {
|
|
const path = workHrefToPath(href) ?? href;
|
|
// Original: `!S(e) || (s[e] ?? false)` — unprotected paths always pass.
|
|
if (!isPasscodeProtectedPath(path)) return true;
|
|
return snapshot[path] ?? false;
|
|
},
|
|
[snapshot]
|
|
);
|
|
}
|
|
|
|
/** usePasscodeAllowed (original `I`). */
|
|
export function usePasscodeAllowed(path: string): boolean {
|
|
return usePasscodeAccessLookup()(path);
|
|
}
|