"use client" import { useState, useEffect } from 'react' import { SiteHeader } from '@/components/site-header' import { useFamily } from '@/context/family-context' import { useRelationship } from '@/hooks/use-relationship' import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card' import { Button } from '@/components/ui/button' import { Badge } from '@/components/ui/badge' import { Popover, PopoverContent, PopoverTrigger } from '@/components/ui/popover' import { Command, CommandEmpty, CommandGroup, CommandInput, CommandItem, CommandList } from '@/components/ui/command' import { Check, ChevronsUpDown, Users, User } from 'lucide-react' import { cn } from '@/lib/utils' import { MemberNameWithStatus } from '@/components/member-name-with-status' import { RelationshipPathDisplay } from '@/components/relationship-path-display' import { useSession } from 'next-auth/react' import { EmptyStateBadge } from '@/components/empty-state-badge' import { useRouter } from 'next/navigation' export default function RelationshipTestPage() { const { treeData } = useFamily() const { calculateRelationship, getDirectRelatives, getSiblings } = useRelationship() const { data: session, status } = useSession() const router = useRouter() const [fromId, setFromId] = useState('') const [toId, setToId] = useState('') const [result, setResult] = useState(null) const [openFromCombobox, setOpenFromCombobox] = useState(false) const [openToCombobox, setOpenToCombobox] = useState(false) // 未登录时重定向到登录页 useEffect(() => { if (status === 'unauthenticated') { router.push('/auth/signin') } }, [status, router]) const members = Object.values(treeData.members) const handleCalculate = () => { if (!fromId || !toId) return const relationship = calculateRelationship(fromId, toId) const fromMember = treeData.members[fromId] const toMember = treeData.members[toId] setResult({ from: fromMember, to: toMember, relationship }) } // 未登录时不渲染内容(已在 useEffect 中重定向) if (status === 'loading' || !session) { return null } // 无数据时显示空状态 if (members.length === 0) { return (

关系待解

) } return (
{/* 固定标题区域 */}

家族关系计算器

选择两个家族成员,自动计算他们之间的亲属关系

{/* 选择成员 */} 选择成员 请选择要计算关系的两个成员
未找到成员 {members.map((member) => ( { setFromId(member.id) setOpenFromCombobox(false) }} > (第{member.generation}世) ))}
未找到成员 {members.map((member) => ( { setToId(member.id) setOpenToCombobox(false) }} > (第{member.generation}世) ))}
{/* 计算结果 */} {result && ( 计算结果
第{result.from.generation}世
{result.relationship.term} {result.relationship.path && (
路径:
)}
第{result.to.generation}世
{result.relationship.description && (
关系说明
{result.relationship.term}
)} {result.relationship.success === false && (
⚠️ {result.relationship.error || '无法计算关系'}
)}
)} {/* 直系亲属信息 */} {(fromId || toId) && (
{fromId && ( 的直系亲属 {(() => { const relatives = getDirectRelatives(fromId) if (!relatives) return null return ( <>
{relatives.father && (
父亲
)} {relatives.mother && (
母亲
)} {relatives.spouses.length > 0 && (
配偶
{relatives.spouses.map((spouse: any) => (
))}
)} {relatives.children.length > 0 && (
子女 ({relatives.children.length}人)
{relatives.children.slice(0, 3).map((child: any) => (
))} {relatives.children.length > 3 && (
还有 {relatives.children.length - 3} 人...
)}
)}
{(() => { const siblings = getSiblings(fromId) if (siblings.length === 0) return null return (
兄弟姐妹 ({siblings.length}人)
{siblings.map((sibling: any) => ( ))}
) })()} ) })()}
)} {toId && ( {treeData.members[toId]?.fullName} 的直系亲属 {(() => { const relatives = getDirectRelatives(toId) if (!relatives) return null return ( <>
{relatives.father && (
父亲
)} {relatives.mother && (
母亲
)} {relatives.spouses.length > 0 && (
配偶
{relatives.spouses.map((spouse: any) => (
))}
)} {relatives.children.length > 0 && (
子女 ({relatives.children.length}人)
{relatives.children.slice(0, 3).map((child: any) => (
))} {relatives.children.length > 3 && (
还有 {relatives.children.length - 3} 人...
)}
)}
{(() => { const siblings = getSiblings(toId) if (siblings.length === 0) return null return (
兄弟姐妹 ({siblings.length}人)
{siblings.map((sibling: any) => ( ))}
) })()} ) })()}
)}
)}
) }