"use client" import type React from "react" import Link from "next/link" import { Button } from "@/components/ui/button" import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card" import { SiteHeader } from "@/components/site-header" import { ScrollArea } from "@/components/ui/scroll-area" import { Tabs, TabsContent, TabsList, TabsTrigger } from "@/components/ui/tabs" import { UserPlus, Network, Map, Calendar, Users, ArrowRight, Clock, Search, BookOpen, BarChart3 } from "lucide-react" import { useFamily } from "@/context/family-context" import { useMemo, useState, useEffect } from "react" import { StatisticsCharts } from "@/components/dashboard/statistics-charts" import { getActivityLogs } from "@/lib/activity-logger" import type { ActivityLog } from "@/lib/db" import { format } from "date-fns" export default function DashboardPage() { const { treeData } = useFamily() const [recentActivities, setRecentActivities] = useState([]) // 加载最近的操作日志 useEffect(() => { getActivityLogs(10).then(setRecentActivities) }, [treeData]) // 计算统计数据 const stats = useMemo(() => { const members = Object.values(treeData.members) const totalMembers = members.length // 计算最大代数 const maxGeneration = Math.max(...members.map(m => m.generation || 0)) // 计算最早出生年份 const birthYears = members .map(m => m.birthDate ? new Date(m.birthDate).getFullYear() : null) .filter(y => y !== null) as number[] const earliestYear = birthYears.length > 0 ? Math.min(...birthYears) : new Date().getFullYear() const yearsSpan = new Date().getFullYear() - earliestYear return { totalMembers, maxGeneration, yearsSpan, earliestYear } }, [treeData]) // 获取最近的成员(按ID排序,取最新的5个) const recentMembers = useMemo(() => { return Object.values(treeData.members) .sort((a, b) => parseInt(b.id) - parseInt(a.id)) .slice(0, 5) }, [treeData]) // 获取本月纪念日(生日和忌日) const monthlyAnniversaries = useMemo(() => { const currentMonth = new Date().getMonth() + 1 const members = Object.values(treeData.members) const anniversaries: Array<{ member: any type: 'birth' | 'death' date: string day: number }> = [] members.forEach(member => { // 检查生日 if (member.birthDate) { const birthMonth = new Date(member.birthDate).getMonth() + 1 const birthDay = new Date(member.birthDate).getDate() if (birthMonth === currentMonth) { anniversaries.push({ member, type: 'birth', date: member.birthDate, day: birthDay }) } } // 检查忌日 if (member.deathDate) { const deathMonth = new Date(member.deathDate).getMonth() + 1 const deathDay = new Date(member.deathDate).getDate() if (deathMonth === currentMonth) { anniversaries.push({ member, type: 'death', date: member.deathDate, day: deathDay }) } } }) // 按日期排序 return anniversaries.sort((a, b) => a.day - b.day).slice(0, 5) }, [treeData]) return (

李氏家族族谱

陇西堂 · 第 24 代传人

} /> } /> } />
最近更新 近期纪念日 统计图表 迁徙记录
近期修谱动态 记录家族成员的最新变动与修订
{recentActivities.length > 0 ? ( recentActivities.map((activity, i) => (
{activity.action === 'create' ? '创建' : activity.action === 'update' ? '更新' : activity.action === 'delete' ? '删除' : '导入'} {activity.entityName || '未知'} {format(new Date(activity.timestamp), 'MM-dd HH:mm')}

{activity.action === 'create' && '新增了家族成员'} {activity.action === 'update' && '更新了成员信息'} {activity.action === 'delete' && '删除了成员'} {activity.action === 'import' && '导入了家族数据'}

)) ) : (
暂无修谱记录
)}
本月祭祖/纪念 {monthlyAnniversaries.length > 0 ? ( monthlyAnniversaries.map((anniversary, index) => { const yearsAgo = new Date().getFullYear() - new Date(anniversary.date).getFullYear() return (
{new Date(anniversary.date).getMonth() + 1}月 {anniversary.day}

{anniversary.member.fullName} {anniversary.type === 'birth' ? '诞辰' : '忌日'}

第 {anniversary.member.generation} 世 · 距今 {yearsAgo} 年 {anniversary.type === 'death' && ' · 已故'}

) }) ) : (
本月暂无纪念日
)}
{/* 近期纪念日标签页 */} 近期纪念日 未来三个月内的生日和忌日
{(() => { const now = new Date() const threeMonthsLater = new Date(now.getFullYear(), now.getMonth() + 3, now.getDate()) const members = Object.values(treeData.members) const upcomingEvents: Array<{ member: any type: 'birth' | 'death' date: Date originalDate: string }> = [] members.forEach(member => { // 生日 if (member.birthDate) { const birthDate = new Date(member.birthDate) const thisYearBirth = new Date(now.getFullYear(), birthDate.getMonth(), birthDate.getDate()) if (thisYearBirth >= now && thisYearBirth <= threeMonthsLater) { upcomingEvents.push({ member, type: 'birth', date: thisYearBirth, originalDate: member.birthDate }) } } // 忌日 if (member.deathDate) { const deathDate = new Date(member.deathDate) const thisYearDeath = new Date(now.getFullYear(), deathDate.getMonth(), deathDate.getDate()) if (thisYearDeath >= now && thisYearDeath <= threeMonthsLater) { upcomingEvents.push({ member, type: 'death', date: thisYearDeath, originalDate: member.deathDate }) } } }) upcomingEvents.sort((a, b) => a.date.getTime() - b.date.getTime()) return upcomingEvents.length > 0 ? ( upcomingEvents.map((event, index) => { const yearsAgo = now.getFullYear() - new Date(event.originalDate).getFullYear() return (
{event.date.getMonth() + 1}月 {event.date.getDate()}

{event.member.fullName} {event.type === 'birth' ? '诞辰' : '忌日'}

第 {event.member.generation} 世 · {yearsAgo} 年 {event.type === 'death' && ' · 已故'}

) }) ) : (
未来三个月暂无纪念日
) })()}
{/* 统计图表标签页 */} {/* 迁徙记录标签页 */} 家族迁徙记录 记录家族成员的籍贯分布
{useMemo(() => { const members = Object.values(treeData.members) const locationGroups: Record = {} members.forEach(member => { if (member.ancestralHome) { if (!locationGroups[member.ancestralHome]) { locationGroups[member.ancestralHome] = [] } locationGroups[member.ancestralHome].push(member) } }) const sortedLocations = Object.entries(locationGroups) .sort((a, b) => b[1].length - a[1].length) return sortedLocations.length > 0 ? ( sortedLocations.map(([location, locationMembers]) => (

{location}

{locationMembers.length} 人
{locationMembers.map((member: any) => (

{member.fullName}

第 {member.generation} 世

))}
)) ) : (
暂无迁徙记录
) }, [treeData])}
) } function StatCard({ title, value, subtitle, icon, }: { title: string; value: string; subtitle: string; icon: React.ReactNode }) { return (

{title}

{icon}
{value}

{subtitle}

) }