"use client" import { useState, useEffect } from "react" import { useRouter, usePathname } from "next/navigation" import { Button } from "@/components/ui/button" import { MoonIcon, SunIcon, MenuIcon, XIcon, GlobeIcon, UserIcon, LogOutIcon, SettingsIcon } from "lucide-react" import { useTheme } from "next-themes" import { DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuTrigger, DropdownMenuSeparator } from "@/components/ui/dropdown-menu" import { useTranslations } from "next-intl" import { useToast } from "@/hooks/use-toast" import Image from "next/image" import { useSession, signOut } from 'next-auth/react' import { useAuth } from "./providers" interface NavigationProps { locale: string } interface User { id: string email: string username: string | null isEmailVerified: boolean credits: number } export default function Navigation({ locale }: NavigationProps) { const { theme, setTheme } = useTheme() const [isMenuOpen, setIsMenuOpen] = useState(false) const { user, isLoading, refreshUser } = useAuth() const router = useRouter() const pathname = usePathname() const t = useTranslations("navigation") const tAuth = useTranslations("dashboard") const tErrors = useTranslations("auth.errors") const { toast } = useToast() const toggleMenu = () => { setIsMenuOpen(!isMenuOpen) } const handleLogout = async () => { try { await signOut({ redirect: false }) await refreshUser() // 刷新认证状态 router.push(`/${locale}`) toast({ title: t("logoutSuccess"), description: t("logoutSuccessDesc"), }) } catch (error) { toast({ title: t("logoutFailed"), description: t("logoutFailedDesc"), variant: "destructive", }) } } const scrollToSection = (sectionId: string) => { // 检查当前路径是否为首页 const isHomePage = pathname === `/${locale}` || pathname === `/${locale}/` if (isHomePage) { // 如果在首页,直接滚动到对应部分 const element = document.getElementById(sectionId) if (element) { element.scrollIntoView({ behavior: "smooth" }) } } else { // 如果不在首页,导航到首页的对应锚点 router.push(`/${locale}#${sectionId}`) } setIsMenuOpen(false) } const scrollToTop = () => { const isHomePage = pathname === `/${locale}` || pathname === `/${locale}/` if (isHomePage) { window.scrollTo({ top: 0, behavior: "smooth" }) } else { router.push(`/${locale}`) } setIsMenuOpen(false) } const switchLanguage = (newLocale: string) => { const newPath = pathname.replace(`/${locale}`, `/${newLocale}`) router.push(newPath) } return ( ) }