'use client'; import Link from 'next/link'; import { usePathname, useRouter } from 'next/navigation'; import { useMemo, useState, useEffect } from 'react'; import { createClient } from '@/lib/supabase/client'; import type { User } from '@supabase/supabase-js'; const LINKS = [ { href: '/', label: '홈' }, { href: '/services/music/samples', label: '샘플' }, { href: '/services/music', label: '팩 상세' }, { href: '/studio', label: '스튜디오' }, { href: '/freelance', label: '외주' }, ]; export default function TopNav() { const pathname = usePathname(); const router = useRouter(); const supabase = useMemo(() => createClient(), []); const [open, setOpen] = useState(false); const [scrolled, setScrolled] = useState(false); const [user, setUser] = useState(null); useEffect(() => { const onScroll = () => setScrolled(window.scrollY > 8); onScroll(); window.addEventListener('scroll', onScroll, { passive: true }); return () => window.removeEventListener('scroll', onScroll); }, []); // Supabase auth state subscription (Sidebar.tsx:93-103 패턴) useEffect(() => { let mounted = true; supabase.auth.getSession().then(({ data }) => { if (mounted) setUser(data.session?.user ?? null); }); const { data: { subscription } } = supabase.auth.onAuthStateChange((_event, session) => { if (mounted) setUser(session?.user ?? null); }); return () => { mounted = false; subscription.unsubscribe(); }; }, [supabase]); const handleLogout = async () => { await supabase.auth.signOut(); setOpen(false); router.replace('/'); router.refresh(); }; useEffect(() => { setOpen(false); }, [pathname]); useEffect(() => { if (open) { const prev = document.body.style.overflow; document.body.style.overflow = 'hidden'; return () => { document.body.style.overflow = prev; }; } }, [open]); const isActive = (href: string) => { if (href === '/') return pathname === '/'; if (href === '/services/music') return pathname === '/services/music'; return pathname.startsWith(href); }; return ( <>
{/* 모바일 오버레이 */} {open && (
JSM
{LINKS.map((l) => ( {l.label} ))}
{user ? ( <> 마이페이지 ) : ( <> 로그인 Try now )}
)} ); }