2025-06-16 10:37:25 +00:00
|
|
|
"use client";
|
|
|
|
|
2025-06-17 07:24:43 +00:00
|
|
|
import { useState, useEffect } from "react";
|
2025-06-16 10:37:25 +00:00
|
|
|
import { Label } from "@/components/ui/label";
|
|
|
|
import { Input } from "@/components/ui/input";
|
|
|
|
import { Button } from "@/components/ui/button";
|
|
|
|
import MdxPreview from "@/components/mdx-preview";
|
2025-06-16 14:41:48 +00:00
|
|
|
import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card";
|
2025-06-17 07:24:43 +00:00
|
|
|
import { CoreEditor } from "@/components/core-editor";
|
|
|
|
import { getProblemData } from "@/app/actions/getProblem";// 修改为你实际路径
|
2025-06-16 10:37:25 +00:00
|
|
|
|
2025-06-17 07:24:43 +00:00
|
|
|
export default function EditSolutionPanel({
|
|
|
|
problemId,
|
|
|
|
}: {
|
2025-06-16 10:37:25 +00:00
|
|
|
problemId: string;
|
2025-06-17 07:24:43 +00:00
|
|
|
}) {
|
|
|
|
const [solution, setSolution] = useState({
|
|
|
|
title: `Solution for Problem ${problemId}`,
|
|
|
|
content: `Solution content for Problem ${problemId}...`,
|
|
|
|
});
|
|
|
|
const [viewMode, setViewMode] = useState<"edit" | "preview" | "compare">("edit");
|
2025-06-16 10:37:25 +00:00
|
|
|
|
2025-06-17 07:24:43 +00:00
|
|
|
useEffect(() => {
|
|
|
|
async function fetchSolution() {
|
|
|
|
try {
|
|
|
|
const data = await getProblemData(problemId);
|
|
|
|
setSolution({
|
|
|
|
title: data.title ? data.title + " 解析" : `Solution for Problem ${problemId}`,
|
|
|
|
content: data.solution || "",
|
|
|
|
});
|
|
|
|
} catch (error) {
|
|
|
|
console.error("加载题解失败", error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fetchSolution();
|
|
|
|
}, [problemId]);
|
2025-06-16 10:37:25 +00:00
|
|
|
|
|
|
|
return (
|
2025-06-17 07:24:43 +00:00
|
|
|
<Card className="w-full">
|
|
|
|
<CardHeader>
|
|
|
|
<CardTitle>题目解析</CardTitle>
|
|
|
|
</CardHeader>
|
|
|
|
<CardContent>
|
|
|
|
<div className="space-y-6">
|
|
|
|
<div className="space-y-2">
|
|
|
|
<Label htmlFor="solution-title">题解标题</Label>
|
|
|
|
<Input
|
|
|
|
id="solution-title"
|
|
|
|
value={solution.title}
|
|
|
|
onChange={(e) => setSolution({ ...solution, title: e.target.value })}
|
|
|
|
placeholder="输入题解标题"
|
|
|
|
/>
|
2025-06-16 14:41:48 +00:00
|
|
|
</div>
|
2025-06-17 07:24:43 +00:00
|
|
|
<div className="flex space-x-2">
|
|
|
|
<Button
|
|
|
|
type="button"
|
|
|
|
variant={viewMode === "edit" ? "default" : "outline"}
|
|
|
|
onClick={() => setViewMode("edit")}
|
|
|
|
>
|
|
|
|
编辑
|
|
|
|
</Button>
|
|
|
|
<Button
|
|
|
|
type="button"
|
|
|
|
variant={viewMode === "preview" ? "default" : "outline"}
|
|
|
|
onClick={() => setViewMode(viewMode === "preview" ? "edit" : "preview")}
|
|
|
|
>
|
|
|
|
{viewMode === "preview" ? "取消" : "预览"}
|
|
|
|
</Button>
|
|
|
|
<Button
|
|
|
|
type="button"
|
|
|
|
variant={viewMode === "compare" ? "default" : "outline"}
|
|
|
|
onClick={() => setViewMode("compare")}
|
|
|
|
>
|
|
|
|
对比
|
|
|
|
</Button>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<div className={viewMode === "compare" ? "grid grid-cols-2 gap-6" : "flex flex-col gap-6"}>
|
|
|
|
<div className={viewMode === "edit" || viewMode === "compare" ? "block" : "hidden"}>
|
|
|
|
<div className="relative h-[600px]">
|
|
|
|
<CoreEditor
|
|
|
|
value={solution.content}
|
|
|
|
onChange={(newContent) => setSolution({ ...solution, content: newContent })}
|
|
|
|
language="markdown"
|
|
|
|
className="absolute inset-0 rounded-md border border-input"
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
{viewMode !== "edit" && (
|
|
|
|
<div className="prose dark:prose-invert">
|
|
|
|
<MdxPreview source={solution.content} />
|
|
|
|
</div>
|
|
|
|
)}
|
2025-06-16 14:41:48 +00:00
|
|
|
</div>
|
|
|
|
</div>
|
2025-06-17 07:24:43 +00:00
|
|
|
</CardContent>
|
|
|
|
</Card>
|
2025-06-16 10:37:25 +00:00
|
|
|
);
|
2025-06-17 07:24:43 +00:00
|
|
|
}
|