"use client"; import { motion } from "framer-motion"; import { useSession } from "next-auth/react"; import { useEffect, useState } from "react"; export const Greeting = () => { const { data: session } = useSession(); const [imageCount, setImageCount] = 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 fetchImageStats = async () => { try { const response = await fetch("/api/user/image-stats"); if (response.ok) { const data = await response.json(); setImageCount(data.totalImages); } } catch (error) { console.error("Failed to fetch image stats:", error); } }; if (session?.user?.id) { fetchImageStats(); } }, [session?.user?.id]); return (
你好{displayName && `,${displayName}`},很高兴见到你 {imageCount !== null && imageCount > 0 && ( (截止今日已为您处理 {imageCount.toLocaleString()} 张图) )} 今天我能为你做些什么?
); };