"use client"; import React, { useEffect, useState } from "react"; import { Label } from "@/components/ui/label"; import { Input } from "@/components/ui/input"; import { Button } from "@/components/ui/button"; import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card"; import { CoreEditor } from "@/components/core-editor"; import MdxPreview from "@/components/mdx-preview"; import { getProblemData } from "@/app/actions/getProblem"; import { getProblemLocales } from "@/app/actions/getProblemLocales"; import { Accordion } from "@/components/ui/accordion"; import { VideoEmbed } from "@/components/content/video-embed"; import { toast } from "sonner"; import { updateProblemSolution } from "@/components/creater/problem-maintain"; import { Locale } from "@/generated/client"; export default function EditSolutionPanel({ problemId, }: { problemId: string; }) { const [locales, setLocales] = useState([]); const [currentLocale, setCurrentLocale] = useState(""); const [customLocale, setCustomLocale] = useState(""); const [solution, setSolution] = useState({ title: "", content: "" }); const [viewMode, setViewMode] = useState<"edit" | "preview" | "compare">( "edit" ); useEffect(() => { async function fetchLocales() { try { const langs = await getProblemLocales(problemId); setLocales(langs); if (langs.length > 0) setCurrentLocale(langs[0]); } catch (err) { console.error(err); toast.error("获取语言列表失败"); } } fetchLocales(); }, [problemId]); useEffect(() => { if (!currentLocale) return; async function fetchSolution() { try { const data = await getProblemData(problemId, currentLocale); setSolution({ title: (data?.title || "") + " 解析", content: data?.solution || "", }); } catch (err) { console.error(err); toast.error("加载题目解析失败"); } } fetchSolution(); }, [problemId, currentLocale]); const handleAddCustomLocale = () => { if (customLocale && !locales.includes(customLocale)) { setLocales((prev) => [...prev, customLocale]); setCurrentLocale(customLocale); setCustomLocale(""); setSolution({ title: "", content: "" }); } }; const handleSave = async (): Promise => { if (!currentLocale) { toast.error("请选择语言"); return; } try { const locale = currentLocale as Locale; const res = await updateProblemSolution( problemId, locale, solution.content ); if (res.success) { toast.success("保存成功"); } else { toast.error("保存失败"); } } catch (err) { console.error(err); toast.error("保存异常"); } }; return ( 题目解析 {/* 语言切换 */}
setCustomLocale(e.target.value)} />
{/* 标题输入 (仅展示) */}
setSolution({ ...solution, title: e.target.value }) } placeholder="输入题解标题" disabled />
{/* 编辑/预览切换 */}
{/* 编辑/预览区域 */}
{(viewMode === "edit" || viewMode === "compare") && (
setSolution({ ...solution, content: val || "" }) } language="markdown" className="absolute inset-0 rounded-md border border-input" />
)} {viewMode !== "edit" && (
)}
); }