"use client" import { useState } 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 } from 'lucide-react' import { cn } from '@/lib/utils' export default function RelationshipTestPage() { const { treeData } = useFamily() const { calculateRelationship, getDirectRelatives, getSiblings } = useRelationship() const [fromId, setFromId] = useState('') const [toId, setToId] = useState('') const [result, setResult] = useState(null) const [openFromCombobox, setOpenFromCombobox] = useState(false) const [openToCombobox, setOpenToCombobox] = useState(false) 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 }) } return (
{/* 标题 */}

家族关系计算器

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

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