"use client"; import { useMemo } from "react"; import { useTranslation } from "react-i18next"; import { zodResolver } from "@hookform/resolvers/zod"; import { useForm } from "react-hook-form"; import { z } from "zod"; import MagicDownEditor from "@/components/MagicDown/Editor"; import { Form, FormControl, FormField, FormItem, FormLabel, } from "@/components/ui/form"; import { Input } from "@/components/ui/input"; import { Button } from "@/components/ui/button"; import { useKnowledgeStore } from "@/store/knowledge"; import { cn } from "@/utils/style"; type Props = { id: string; editClassName?: string; onSubmit?: (values: { title: string; content: string }) => void; onBack?: () => void; }; const formSchema = z.object({ title: z.string().min(1), content: z.string().min(1), }); function Content({ id, editClassName, onSubmit, onBack }: Props) { const { t } = useTranslation(); const knowledgeStore = useKnowledgeStore(); const defaultValues = useMemo(() => { const { knowledges } = useKnowledgeStore.getState(); const detail = knowledges.find((item) => item.id === id); return detail ? { title: detail.title, content: detail.content } : { title: "", content: "" }; }, [id]); const form = useForm>({ resolver: zodResolver(formSchema), defaultValues, }); function handleSubmit(values: z.infer) { const currentTime = Date.now(); knowledgeStore.update(id, { ...values, updatedAt: currentTime }); if (onSubmit) onSubmit(values); if (onBack) onBack(); } return (
form.reset()} onSubmit={form.handleSubmit(handleSubmit)} > ( {t("knowledge.editor.title")} )} /> ( {t("knowledge.editor.content")} )} />
{onBack ? ( ) : null}
); } export default Content;