"use client"; import { useEffect, useRef, useState } from "react"; import styles from "../reuse.module.css"; export interface SideIndicatorItem { id: string; label: string; // 상단/공용 라벨 sideLabel?: string; // 사이드 인디게이터 전용 라벨(옵션) } export default function SideIndicator({ items, activeId, }: { items: SideIndicatorItem[]; activeId: string; }) { const [manualActiveId, setManualActiveId] = useState(null); const clearTimerRef = useRef(null); const currentId = (manualActiveId ?? activeId ?? "main") || "main"; // ✅ manualActiveId가 observer(activeId)와 불일치하기 시작하면 즉시 해제 (stuck 방지) useEffect(() => { if (!manualActiveId) return; if (activeId && activeId !== manualActiveId) setManualActiveId(null); }, [activeId, manualActiveId]); // ✅ fallback: 스무스 스크롤 시간이 길어도 manual이 오래 남지 않게 타임아웃 해제 useEffect(() => { return () => { if (clearTimerRef.current) window.clearTimeout(clearTimerRef.current); }; }, []); const scrollTo = (id: string) => { setManualActiveId(id); // 기존 타이머 제거 후 새로 설정 if (clearTimerRef.current) window.clearTimeout(clearTimerRef.current); clearTimerRef.current = window.setTimeout(() => { setManualActiveId(null); }, 1600); if (id === "main") { window.scrollTo({ top: 0, behavior: "smooth" }); return; } document.getElementById(id)?.scrollIntoView({ behavior: "smooth", block: "start", }); }; return ( ); }