"use client" import { useState } from "react" import { ChevronDown, ChevronRight, Copy, Download } from "lucide-react" import { toast } from "sonner" import { Button } from "@/components/ui/button" import { Input } from "@/components/ui/input" import { Textarea } from "@/components/ui/textarea" import { Badge } from "@/components/ui/badge" import { Checkbox } from "@/components/ui/checkbox" import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue, } from "@/components/ui/select" import { cn } from "@/lib/utils" import { DIFFICULTIES, DIFFICULTY_META } from "@/lib/types" import { AI_CATEGORY_ENUM, type AIDraftItem } from "@/lib/ai/types" export type DraftRow = AIDraftItem & { _id: string; _selected: boolean } // 通过对象定义 value 与展示文案,下方循环渲染,便于维护 const CATEGORY_OPTIONS = AI_CATEGORY_ENUM.map((c) => ({ value: c, label: c })) const DIFFICULTY_OPTIONS = DIFFICULTIES.map((d) => ({ value: d, label: DIFFICULTY_META[d].label })) export function AIResultPreview({ rows, onChange, }: { rows: DraftRow[] onChange: (rows: DraftRow[]) => void }) { const [expanded, setExpanded] = useState>({}) function patch(id: string, patch: Partial) { onChange(rows.map((r) => (r._id === id ? { ...r, ...patch } : r))) } function toggleAll(value: boolean) { onChange(rows.map((r) => ({ ...r, _selected: value }))) } function copyJson() { const items = rows.map(({ _id, _selected, ...rest }) => rest) navigator.clipboard.writeText(JSON.stringify({ items }, null, 2)) toast.success("已复制 JSON") } function exportJson() { const items = rows.map(({ _id, _selected, ...rest }) => rest) const blob = new Blob([JSON.stringify({ items }, null, 2)], { type: "application/json" }) const url = URL.createObjectURL(blob) const a = document.createElement("a") a.href = url a.download = `knowledge-items-${Date.now()}.json` a.click() URL.revokeObjectURL(url) } const allSelected = rows.length > 0 && rows.every((r) => r._selected) const selectedCount = rows.filter((r) => r._selected).length return (
{rows.map((row) => { const open = expanded[row._id] return (
patch(row._id, { _selected: !!v })} /> patch(row._id, { title: e.target.value })} className="h-8 flex-1 bg-input/60 text-sm font-medium" /> {row.category}
{open && (
摘要 patch(row._id, { summary: e.target.value })} className="bg-input/60 text-sm" />
正文