Files
personal/components/dom/WorkSection.tsx
T
virtheart a844b26250 feat: 路由转场+详情页+自定义滚动条+常显网格线+邮箱修复
- 新增 FullscreenTransitionProvider + RouteTransitionOverlay:
  cover(0.8s b1 遮罩 wipe) → push → reveal 状态机(原 73475/34841 移植),
  Work 卡片与详情页链接全部走 startNavigation(同路径点击回顶部)
- 新增 app/work/[slug] 详情页:content/workArticles.ts 从原站 6 个详情页
  1:1 提取(MDX 文章+Metadata+iframe 预览+Links),/2026 锁定时渲染解锁屏
- 新增 CustomScrollbar:桌面右缘 SVG 轨道+拖拽 thumb(188 轨道/min20)、
  点击跳转、2s 自动隐藏;移动端底部进度环回顶按钮(原 72443 移植)
- BackgroundGrid 重写为原版算法(3 行十字/12px 缺口/12 列内分数纵线,
  主页 loading 时隐藏其余时间常显);层叠改为原版结构:canvas z-auto
  最前 → grid z-20 → DOM 内容,移除各 section 的 z-30
- 修复 BGM:持久化开启时首次手势自动恢复播放(autoplay 策略被拒后
  pointerdown/keydown/touchstart 重试),enabled 闭包改用 ref
- 页脚邮箱改为 chenchun@virtheart.com
2026-09-01 20:33:36 +08:00

210 lines
7.3 KiB
TypeScript

"use client";
import { useEffect, useMemo, useRef, useState } from "react";
import {
SELECTED_WORK_SECTION_ID,
WORK_ITEMS,
type WorkItem,
} from "@/lib/workItems";
import {
getLayerTarget,
setLayerDef,
touchLayerRegistry,
} from "@/lib/imageLayers";
import { usePasscodeAllowed, usePasscodeAccessLookup } from "@/components/dom/PasscodeAccess";
import { buildPasscodeUnlockHref } from "@/lib/passcode";
import { useRouteTransitionController } from "@/lib/routeTransition";
/**
* Selected Work grid — 1:1 port of the original section `h` (chunk
* 2689132c4e070b68.js, exports SELECTED_WORK_SECTION_ID / default):
*
* - `<section id="selected-work" className="px-4 lg:px-14 py-18 lg:py-24 w-full">`
* with an inner `grid grid-cols-12` whose `rowGap` is the measured column
* width (container.clientWidth / 12, tracked by ResizeObserver).
* - Each card is `<article className={gridClass}>` wrapping a `group block
* space-y-3 p-2` anchor (next Link for internal routes, <a> for external).
* - The visual is NOT a DOM <img>: the anchor's first child is an
* aspect-ratio placeholder div registered as a WebGL image-layer target;
* the canvas-side ImageLayers projects imageUrl/hoverImageUrl onto its
* screen rect (curl + negative reveal + hover dot mask).
* - Aspect ratios come from preloading each imageUrl and reading
* naturalWidth/naturalHeight into a module-level cache (original `o`).
* - Passcode-protected items are filtered exactly like the original
* `WORK_ITEMS.filter(t => !t.passcodeProtected || lookup(t.href))`: the
* TikTok entry only appears once /2026 is unlocked.
*/
// Module-level cache mirroring the original `o` (persists across remounts).
const ratioCache: Record<string, string> = {};
export default function WorkSection() {
const [ratios, setRatios] = useState<Record<string, string>>({ ...ratioCache });
const [colWidth, setColWidth] = useState(0);
const gridRef = useRef<HTMLDivElement | null>(null);
const lookup = usePasscodeAccessLookup();
// Original `f` — passcode-protected items stay hidden until unlocked.
const visibleItems = useMemo(
() => WORK_ITEMS.filter((item) => !item.passcodeProtected || lookup(item.href)),
[lookup]
);
// Register canvas layer defs for every item (locked ones simply have no
// target on screen; idempotent data write — version bump happens once
// targets exist, in the effect).
useMemo(() => {
for (const item of WORK_ITEMS) {
setLayerDef(item.href, {
imageUrl: item.imageUrl,
hoverImageUrl: item.hoverImageUrl,
});
}
}, []);
useEffect(() => {
touchLayerRegistry();
}, []);
// rowGap = column width (original RO on the grid container).
useEffect(() => {
const el = gridRef.current;
if (!el) return;
const measure = () => setColWidth(el.clientWidth / 12);
measure();
const ro = new ResizeObserver(measure);
ro.observe(el);
return () => ro.disconnect();
}, []);
// Preload images → natural aspect ratios (original effect).
useEffect(() => {
let cancelled = false;
const update = (imageUrl: string, w: number, h: number) => {
if (cancelled || w <= 0 || h <= 0) return;
const ratio = `${w} / ${h}`;
if (ratioCache[imageUrl] !== ratio) ratioCache[imageUrl] = ratio;
setRatios((prev) => (prev[imageUrl] === ratio ? prev : { ...prev, [imageUrl]: ratio }));
};
const images = visibleItems.map((item) => {
const img = new Image();
img.decoding = "async";
img.onload = () => update(item.imageUrl, img.naturalWidth, img.naturalHeight);
img.src = item.imageUrl;
if (img.complete && img.naturalWidth > 0 && img.naturalHeight > 0) {
update(item.imageUrl, img.naturalWidth, img.naturalHeight);
}
return img;
});
return () => {
cancelled = true;
for (const img of images) img.onload = null;
};
// eslint-disable-next-line react-hooks/exhaustive-deps -- ratios-only effect; matches the original mount-time preload
}, [visibleItems]);
return (
<section
id={SELECTED_WORK_SECTION_ID}
className="px-4 lg:px-14 py-18 lg:py-24 w-full"
>
<div
ref={gridRef}
className="grid grid-cols-12 w-full"
style={{ rowGap: `${colWidth}px` }}
>
{visibleItems.map((item) => (
<WorkCard key={item.href} item={item} ratio={ratios[item.imageUrl]} />
))}
</div>
</section>
);
}
function WorkCard({ item, ratio }: { item: WorkItem; ratio?: string }) {
const external = item.href.startsWith("http://") || item.href.startsWith("https://");
const cls = "group block space-y-3 p-2";
const label = `${item.name} - ${item.year}${external ? " (external)" : ""}`;
const target = getLayerTarget(item.href);
const { startNavigation } = useRouteTransitionController();
const allowed = usePasscodeAllowed(item.href);
const body = (
<>
<div
ref={(el) => {
target.current = el;
}}
aria-hidden="true"
className="relative w-full pointer-events-none select-none"
style={{ aspectRatio: ratio ?? "1 / 1" }}
>
{item.codingProject && (
<span
className="top-0 right-0 z-10 absolute bg-selection px-1 font-mono-2 text-black text-xs uppercase pointer-events-none select-none"
aria-hidden="true"
>
Coding Project
</span>
)}
</div>
<div className="flex justify-between items-center gap-3 min-w-0 text-xs lg:text-sm uppercase">
<span className="flex-1 min-w-0 truncate">{item.name}</span>
<div className="flex items-center gap-2 sm:gap-3 font-mono-2 tabular-nums whitespace-nowrap shrink-0">
<span>{item.year}</span>
{item.type !== "post" && (
<span className="hidden lg:inline-flex items-center gap-1" aria-hidden="true">
<span>{item.type}</span>
<span>↗</span>
</span>
)}
</div>
</div>
</>
);
return (
<article className={item.gridClass ?? "col-span-12 lg:col-span-6 xl:col-span-4"}>
{external ? (
<a
href={item.href}
target="_blank"
rel="noopener noreferrer"
className={cls}
aria-label={label}
>
{body}
</a>
) : (
// Internal detail routes (/reunimos, /inspire_mono, …) navigate
// through the fullscreen cover/reveal transition (original Link
// wrapper module 80894: preventDefault + startNavigation; same-path
// clicks scroll back to top).
<a
href={item.href}
className={cls}
aria-label={label}
onClick={(e) => {
if (e.defaultPrevented) return;
if (e.button !== 0 || e.metaKey || e.altKey || e.ctrlKey || e.shiftKey)
return;
e.preventDefault();
if (!allowed) {
const scope = item.href.replace(/^\//, "");
window.location.assign(
buildPasscodeUnlockHref(scope, item.href)
);
return;
}
const detail = item.href.startsWith("/2026")
? item.href
: `/work${item.href}`;
startNavigation(detail);
}}
>
{body}
</a>
)}
</article>
);
}