fix: 客户端资源清理 — ShareCard 依赖 + 定时器 + 死代码

- #20: ShareCardModal useEffect 依赖改为 imageSrc 字符串,避免对象引用变化重复加载
- #21: BlindboxRoomPage 所有 setTimeout/rAF 统一收集,unmount 时清理
- #25: 删除未使用的 confettiCanvasRef 和 canvas 元素
This commit is contained in:
2026-02-26 20:21:59 +08:00
parent cf88d3a1d2
commit 5adfe8d3f1
2 changed files with 21 additions and 13 deletions
+16 -7
View File
@@ -65,7 +65,15 @@ export default function BlindboxRoomPage() {
const boxControls = useAnimation(); const boxControls = useAnimation();
const inputRef = useRef<HTMLInputElement>(null); const inputRef = useRef<HTMLInputElement>(null);
const confettiCanvasRef = useRef<HTMLCanvasElement>(null); const timersRef = useRef<ReturnType<typeof setTimeout>[]>([]);
const confettiAliveRef = useRef(false);
useEffect(() => {
return () => {
timersRef.current.forEach(clearTimeout);
confettiAliveRef.current = false;
};
}, []);
useEffect(() => { useEffect(() => {
if (!isRegistered()) { if (!isRegistered()) {
@@ -121,7 +129,8 @@ export default function BlindboxRoomPage() {
useEffect(() => { useEffect(() => {
if (isMember && inputRef.current) { if (isMember && inputRef.current) {
setTimeout(() => inputRef.current?.focus(), 300); const t = setTimeout(() => inputRef.current?.focus(), 300);
timersRef.current.push(t);
} }
}, [isMember]); }, [isMember]);
@@ -162,7 +171,7 @@ export default function BlindboxRoomPage() {
setPoolCount((c) => c + 1); setPoolCount((c) => c + 1);
setMyIdeas((prev) => [{ id, content: text, createdAt: new Date().toISOString() }, ...prev]); setMyIdeas((prev) => [{ id, content: text, createdAt: new Date().toISOString() }, ...prev]);
setSubmitFlash(true); setSubmitFlash(true);
setTimeout(() => setSubmitFlash(false), 600); timersRef.current.push(setTimeout(() => setSubmitFlash(false), 600));
boxControls.start({ boxControls.start({
scale: [1, 1.08, 1], scale: [1, 1.08, 1],
rotate: [0, -3, 3, 0], rotate: [0, -3, 3, 0],
@@ -256,14 +265,15 @@ export default function BlindboxRoomPage() {
const fireConfetti = () => { const fireConfetti = () => {
const colors = ["#a855f7", "#6366f1", "#ec4899", "#f59e0b", "#10b981"]; const colors = ["#a855f7", "#6366f1", "#ec4899", "#f59e0b", "#10b981"];
confetti({ particleCount: 100, spread: 120, origin: { y: 0.4 }, colors, startVelocity: 45, ticks: 250 }); confetti({ particleCount: 100, spread: 120, origin: { y: 0.4 }, colors, startVelocity: 45, ticks: 250 });
confettiAliveRef.current = true;
const end = Date.now() + 3000; const end = Date.now() + 3000;
const frame = () => { const frame = () => {
if (Date.now() > end) return; if (Date.now() > end || !confettiAliveRef.current) return;
confetti({ particleCount: 3, angle: 60, spread: 55, origin: { x: 0, y: 0.6 }, colors, startVelocity: 35, ticks: 150 }); confetti({ particleCount: 3, angle: 60, spread: 55, origin: { x: 0, y: 0.6 }, colors, startVelocity: 35, ticks: 150 });
confetti({ particleCount: 3, angle: 120, spread: 55, origin: { x: 1, y: 0.6 }, colors, startVelocity: 35, ticks: 150 }); confetti({ particleCount: 3, angle: 120, spread: 55, origin: { x: 1, y: 0.6 }, colors, startVelocity: 35, ticks: 150 });
requestAnimationFrame(frame); requestAnimationFrame(frame);
}; };
setTimeout(frame, 200); timersRef.current.push(setTimeout(frame, 200));
}; };
const { share, copyToClipboard } = useShare(); const { share, copyToClipboard } = useShare();
@@ -287,7 +297,7 @@ export default function BlindboxRoomPage() {
const handleLeaveOrDelete = async () => { const handleLeaveOrDelete = async () => {
if (!confirmLeave) { if (!confirmLeave) {
setConfirmLeave(true); setConfirmLeave(true);
setTimeout(() => setConfirmLeave(false), 3000); timersRef.current.push(setTimeout(() => setConfirmLeave(false), 3000));
return; return;
} }
if (leaving || !profile || !room) return; if (leaving || !profile || !room) return;
@@ -319,7 +329,6 @@ export default function BlindboxRoomPage() {
return ( return (
<div className="relative flex min-h-dvh flex-col items-center bg-background px-5 py-6 overflow-y-auto scrollbar-none"> <div className="relative flex min-h-dvh flex-col items-center bg-background px-5 py-6 overflow-y-auto scrollbar-none">
<canvas ref={confettiCanvasRef} className="pointer-events-none fixed inset-0 z-50" />
{/* Header */} {/* Header */}
<div className="flex w-full max-w-sm items-center gap-3"> <div className="flex w-full max-w-sm items-center gap-3">
+5 -6
View File
@@ -38,21 +38,20 @@ export default function ShareCardModal({
const [imageDataUrl, setImageDataUrl] = useState<string | null>(null); const [imageDataUrl, setImageDataUrl] = useState<string | null>(null);
const [imageLoading, setImageLoading] = useState(false); const [imageLoading, setImageLoading] = useState(false);
const imageSrc = data.type === "restaurant" ? data.restaurant.images?.[0] : undefined;
useEffect(() => { useEffect(() => {
if (!open) { if (!open) {
setImageDataUrl(null); setImageDataUrl(null);
return; return;
} }
if (data.type !== "restaurant") return; if (!imageSrc) return;
const src = data.restaurant.images?.[0];
if (!src) return;
setImageLoading(true); setImageLoading(true);
loadImageAsDataUrl(src) loadImageAsDataUrl(imageSrc)
.then(setImageDataUrl) .then(setImageDataUrl)
.finally(() => setImageLoading(false)); .finally(() => setImageLoading(false));
}, [open, data]); }, [open, imageSrc]);
const handleGenerate = useCallback(async (): Promise<string | null> => { const handleGenerate = useCallback(async (): Promise<string | null> => {
if (!cardRef.current) return null; if (!cardRef.current) return null;