"use client"; import { motion } from "framer-motion"; import { useSession } from "next-auth/react"; import { useEffect, useState } from "react"; type QuotaInfo = { used: number; limit: number | null; remaining: number | null; unlimited: boolean; percentage: number; }; export const Greeting = () => { const { data: session } = useSession(); const [imageCount, setImageCount] = useState(null); const [quota, setQuota] = useState(null); // 获取用户名或邮箱前缀 const getUserDisplayName = () => { if (session?.user?.name) { return session.user.name; } if (session?.user?.email) { // 如果没有名字,使用邮箱的前缀部分 return session.user.email.split('@')[0]; } return ""; }; const displayName = getUserDisplayName(); // 获取用户生图统计数据和配额信息 useEffect(() => { const fetchData = async () => { try { // 获取生图统计 const statsResponse = await fetch("/api/user/image-stats"); if (statsResponse.ok) { const statsData = await statsResponse.json(); setImageCount(statsData.totalImages); } // 获取配额信息 const quotaResponse = await fetch("/api/user/image-quota"); if (quotaResponse.ok) { const quotaData = await quotaResponse.json(); setQuota(quotaData); } } catch (error) { console.error("Failed to fetch data:", error); } }; if (session?.user?.id) { fetchData(); } }, [session?.user?.id]); // 获取进度条颜色 const getProgressColor = (percentage: number) => { if (percentage >= 90) return "bg-red-500"; if (percentage >= 70) return "bg-yellow-500"; return "bg-green-500"; }; return (
你好{displayName && `,${displayName}`},很高兴见到你 {imageCount !== null && imageCount > 0 && ( (截止今日已为您处理 {imageCount.toLocaleString()} 张图) )} 今天我能为你做些什么? {/* 配额进度条 - 仅当有限额时显示 */} {quota && !quota.unlimited && quota.limit && (
本月出图:{quota.used}/{quota.limit} 张 {quota.remaining !== null && quota.remaining <= 10 && quota.remaining > 0 && ( (剩余 {quota.remaining} 张) )} {quota.remaining === 0 && ( (配额已用完) )}
)}
); };