"use client" import { useState } from "react" import { Button } from "@/components/ui/button" import { Input } from "@/components/ui/input" import { Textarea } from "@/components/ui/textarea" import { Label } from "@/components/ui/label" import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card" import { Dialog, DialogContent, DialogHeader, DialogTitle, DialogFooter } from "@/components/ui/dialog" import { Plus, Edit, Trash, BookOpen, Calendar } from "lucide-react" import type { FamilyStory } from "@/types/family" import { v4 as uuidv4 } from "uuid" import { useDialog } from "@/components/ui/alert-dialog-custom" interface StoryManagerProps { stories?: FamilyStory[] onChange: (stories: FamilyStory[]) => void readonly?: boolean } export function StoryManager({ stories = [], onChange, readonly = false }: StoryManagerProps) { const { showAlert, showConfirm } = useDialog() const [isDialogOpen, setIsDialogOpen] = useState(false) const [editingStory, setEditingStory] = useState(null) const [formData, setFormData] = useState>({ title: "", content: "", date: "", }) const handleAdd = () => { setEditingStory(null) setFormData({ title: "", content: "", date: "" }) setIsDialogOpen(true) } const handleEdit = (story: FamilyStory) => { setEditingStory(story) setFormData(story) setIsDialogOpen(true) } const handleDelete = async (storyId: string) => { const confirmed = await showConfirm("确定要删除这个故事吗?", "删除故事") if (confirmed) { onChange(stories.filter(s => s.id !== storyId)) } } const handleSave = async () => { if (!formData.title || !formData.content) { await showAlert("请填写标题和内容", "提示") return } const now = new Date().toISOString() if (editingStory) { // 更新现有故事 onChange( stories.map(s => s.id === editingStory.id ? { ...s, ...formData, updatedAt: now } : s ) ) } else { // 添加新故事 const newStory: FamilyStory = { id: uuidv4(), title: formData.title!, content: formData.content!, date: formData.date, imageIds: formData.imageIds || [], createdAt: now, updatedAt: now, } onChange([...stories, newStory]) } setIsDialogOpen(false) setFormData({ title: "", content: "", date: "" }) } return (

家族故事 ({stories.length})

{!readonly && ( )}
{stories.length > 0 ? (
{stories .sort((a, b) => new Date(b.createdAt).getTime() - new Date(a.createdAt).getTime()) .map((story) => (
{story.title} {story.date && (
{story.date}
)}
{!readonly && (
)}

{story.content}

创建于 {new Date(story.createdAt).toLocaleDateString('zh-CN')}
))}
) : (
暂无家族故事
)} {/* 添加/编辑故事对话框 */} {editingStory ? "编辑故事" : "添加故事"}
setFormData({ ...formData, title: e.target.value })} />
setFormData({ ...formData, date: e.target.value })} />