"use client"; import "../pages/style/top-bar.css"; import { Button } from "@/components/ui/button"; import { GradientText } from "@/components/ui/gradient-text"; import { useTheme } from "next-themes"; import { Sun, Moon, User, Sparkles, LogOut, PanelsLeftBottom, Bell, } from 'lucide-react'; import { motion } from 'framer-motion'; import ReactDOM from 'react-dom'; import { useRouter, usePathname } from 'next/navigation'; import React, { useRef, useEffect, useLayoutEffect, useState } from 'react'; import { logoutUser } from '@/lib/auth'; import { createPortalSession, redirectToPortal, getUserSubscriptionInfo } from '@/lib/stripe'; interface User { id: string; name: string; email: string; avatar: string; } export function TopBar({ collapsed, onToggleSidebar, }: { collapsed: boolean; onToggleSidebar: () => void; }) { const router = useRouter(); const [isOpen, setIsOpen] = React.useState(false); const menuRef = useRef(null); const buttonRef = useRef(null); const currentUser: User = JSON.parse( localStorage.getItem("currentUser") || "{}" ); const pathname = usePathname() const [mounted, setMounted] = React.useState(false); const [isLogin, setIsLogin] = useState(false); const [isManagingSubscription, setIsManagingSubscription] = useState(false); const [subscriptionStatus, setSubscriptionStatus] = useState(''); const [credits, setCredits] = useState(100); const [isLoadingSubscription, setIsLoadingSubscription] = useState(false); // 获取用户订阅信息 const fetchSubscriptionInfo = async () => { if (!currentUser?.id) return; setIsLoadingSubscription(true); try { const response = await getUserSubscriptionInfo(String(currentUser.id)); if (response.successful && response.data) { setSubscriptionStatus(response.data.subscription_status); setCredits(response.data.credits); } } catch (error) { console.error('获取订阅信息失败:', error); } finally { setIsLoadingSubscription(false); } }; useEffect(() => { const token = localStorage.getItem("token"); if (token) { setIsLogin(true); } else { setIsLogin(false); } }); useLayoutEffect(() => { console.log("Setting mounted state"); setMounted(true); return () => console.log("Cleanup mounted effect"); }, []); // 处理订阅管理 const handleManageSubscription = async () => { if (!currentUser?.id) { console.error('用户未登录'); return; } setIsManagingSubscription(true); try { const response = await createPortalSession({ user_id: String(currentUser.id), return_url: window.location.origin + '/dashboard' }); if (response.successful && response.data?.portal_url) { redirectToPortal(response.data.portal_url); } else { console.log("cannot open the manage subscription"); return; } } catch (error) { console.log("cannot open the manage subscription"); return; } finally { setIsManagingSubscription(false); } }; // 处理点击事件 useEffect(() => { if (!isOpen) return; let isClickStartedInside = false; const handleMouseDown = (event: MouseEvent) => { const target = event.target as Node; isClickStartedInside = !!( menuRef.current?.contains(target) || buttonRef.current?.contains(target) ); }; const handleMouseUp = (event: MouseEvent) => { const target = event.target as Node; const isClickEndedInside = !!( menuRef.current?.contains(target) || buttonRef.current?.contains(target) ); // 只有当点击开始和结束都在外部时才关闭 if (!isClickStartedInside && !isClickEndedInside) { setIsOpen(false); } isClickStartedInside = false; }; // 在冒泡阶段处理事件 document.addEventListener("mousedown", handleMouseDown); document.addEventListener("mouseup", handleMouseUp); return () => { document.removeEventListener("mousedown", handleMouseDown); document.removeEventListener("mouseup", handleMouseUp); }; }, [isOpen]); const handleAnimationEnd = (event: React.AnimationEvent) => { const element = event.currentTarget; element.classList.remove("on"); }; const handleMouseEnter = (event: React.MouseEvent) => { const element = event.currentTarget; element.classList.add("on"); }; return (
router.push("/")} onMouseEnter={handleMouseEnter} onAnimationEnd={handleAnimationEnd} >

{/* beta标签 */}
Beta
{ isLogin ?(
{/* Pricing Link */} {/* Notifications */} {/* */} {/* Theme Toggle */} {/* */} {/* User Menu */}
{mounted && isOpen ? ReactDOM.createPortal( e.stopPropagation()} > {/* User Info */}
MF

{currentUser.name}

{currentUser.email}

{ logoutUser(); }} title="退出登录" >
{/* AI Points */}
{isLoadingSubscription ? '...' : `${credits} credits`}
{subscriptionStatus === 'ACTIVE' && ( )}
{/* Menu Items */}
{/* router.push('/my-library')} data-alt="my-library-button" > My Library { // 处理退出登录 setIsOpen(false); }} data-alt="logout-button" > Logout */} {/* Footer */}
Privacy Policy · Terms of Service
250819215404 | 2025/8/20 06:00:50
, document.body ) : null}
):(
) }
); }