Files
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

97 lines
3.1 KiB
TypeScript

"use client";
import { useEffect, useRef } from "react";
/**
* Background music loader — port of the original effect (chunk
* 2689132c4e070b68.js): fetch /bgm.mp3 once, create a looping Audio at
* volume 0.35, play/pause with `enabled`. Keeps the same abort/cleanup
* semantics (revoke object URL on unmount).
*/
export function useBgm(enabled: boolean) {
const audioRef = useRef<HTMLAudioElement | null>(null);
const readyRef = useRef(false);
// Track the latest enabled state for the load-once effect (its closure
// captures the initial false otherwise).
const enabledRef = useRef(enabled);
enabledRef.current = enabled;
useEffect(() => {
let cancelled = false;
let objectUrl: string | null = null;
let audio: HTMLAudioElement | null = null;
readyRef.current = false;
fetch("/bgm.mp3")
.then((res) => {
if (!res.ok) throw new Error(`HTTP ${res.status}`);
return res.blob();
})
.then((blob) => {
if (cancelled) return;
objectUrl = URL.createObjectURL(blob);
audio = new Audio(objectUrl);
audio.loop = true;
audio.volume = 0.35;
audioRef.current = audio;
readyRef.current = true;
if (enabledRef.current) void audio.play().catch(() => {});
})
.catch((err) => {
if (!cancelled) console.warn("Failed to load BGM:", err);
});
return () => {
cancelled = true;
if (audioRef.current === audio) audioRef.current = null;
audio?.pause();
audio?.removeAttribute("src");
audio?.load();
if (objectUrl) URL.revokeObjectURL(objectUrl);
};
// Load once on mount (original: deps [enabled-guarded flag]).
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
useEffect(() => {
const audio = audioRef.current;
if (!audio || !readyRef.current) return;
if (enabled) void audio.play().catch(() => {});
else audio.pause();
}, [enabled]);
// Autoplay-policy recovery: when sound is persisted ON, the mount-time
// play() is rejected (NotAllowedError — no user gesture yet) and the
// header would show SOUND[|] forever while staying silent. Retry on the
// first user gesture (click / key / touch), like the original which
// resumes music as soon as the user interacts.
const retriedRef = useRef(false);
useEffect(() => {
if (!enabled || retriedRef.current) return;
const tryPlay = () => {
const audio = audioRef.current;
if (!audio || !readyRef.current) return;
if (!audio.paused) {
retriedRef.current = true;
cleanup();
return;
}
audio
.play()
.then(() => {
retriedRef.current = true;
cleanup();
})
.catch(() => {});
};
const events = ["pointerdown", "keydown", "touchstart"] as const;
const cleanup = () => {
for (const ev of events)
window.removeEventListener(ev, tryPlay, { capture: true });
};
for (const ev of events)
window.addEventListener(ev, tryPlay, { capture: true });
return cleanup;
}, [enabled]);
}