创建家族树
记录家族历史,传承家族文化
"use client" import { useState, useEffect } from "react" import { useRouter } from "next/navigation" import { useSession } from "next-auth/react" import { SiteHeader } from "@/components/site-header" import { Button } from "@/components/ui/button" import { Input } from "@/components/ui/input" import { Label } from "@/components/ui/label" import { Textarea } from "@/components/ui/textarea" import { Card, CardContent, CardDescription, CardFooter, CardHeader, CardTitle } from "@/components/ui/card" import { Alert, AlertDescription } from "@/components/ui/alert" import { ArrowLeft, TreePine, User } from "lucide-react" import Link from "next/link" import { EmptyStateBadge } from "@/components/empty-state-badge" export default function NewTreePage() { const router = useRouter() const { data: session, status } = useSession() const [name, setName] = useState("") const [description, setDescription] = useState("") const [error, setError] = useState("") const [loading, setLoading] = useState(false) // 未登录时重定向到登录页 useEffect(() => { if (status === 'unauthenticated') { router.push('/auth/signin') } }, [status, router]) const handleSubmit = async (e: React.FormEvent) => { e.preventDefault() if (!name.trim()) { setError("请输入家族树名称") return } setError("") setLoading(true) try { const response = await fetch("/api/trees", { method: "POST", headers: { "Content-Type": "application/json", }, body: JSON.stringify({ name: name.trim(), description: description.trim() || undefined, }), }) const data = await response.json() if (!response.ok) { throw new Error(data.error || "创建失败") } // 创建成功,强制刷新页面以更新家族树列表 window.location.href = `/?treeId=${data.tree.id}` } catch (err: any) { setError(err.message || "创建失败,请稍后重试") } finally { setLoading(false) } } // 未登录时不渲染内容(已在 useEffect 中重定向) if (status === 'loading' || !session) { return null } return (
记录家族历史,传承家族文化