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';
|
|
|
|
import { Accordion } from "@/components/ui/accordion"; // ← 这里导入 Accordion
|
2025-06-16 10:37:25 +00:00
|
|
|
|
2025-06-17 07:24:43 +00:00
|
|
|
export default function EditDescriptionPanel({
|
|
|
|
problemId,
|
|
|
|
}: {
|
2025-06-16 10:37:25 +00:00
|
|
|
problemId: string;
|
2025-06-17 07:24:43 +00:00
|
|
|
}) {
|
|
|
|
const [description, setDescription] = useState({
|
|
|
|
title: `Description for Problem ${problemId}`,
|
|
|
|
content: `Description content for Problem ${problemId}...`
|
|
|
|
});
|
2025-06-16 10:37:25 +00:00
|
|
|
const [viewMode, setViewMode] = useState<'edit' | 'preview' | 'compare'>('edit');
|
|
|
|
|
2025-06-17 07:24:43 +00:00
|
|
|
useEffect(() => {
|
|
|
|
async function fetchData() {
|
|
|
|
try {
|
|
|
|
const problemData = await getProblemData(problemId);
|
|
|
|
setDescription({
|
|
|
|
title: problemData.title,
|
|
|
|
content: problemData.description
|
|
|
|
});
|
|
|
|
} catch (error) {
|
|
|
|
console.error('获取题目信息失败:', error);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fetchData();
|
|
|
|
}, [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="description-title">标题</Label>
|
|
|
|
<Input
|
|
|
|
id="description-title"
|
|
|
|
value={description.title}
|
|
|
|
onChange={(e) => setDescription({...description, 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={description.content}
|
|
|
|
onChange={(newContent) =>
|
|
|
|
setDescription({ ...description, 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={description.content}
|
|
|
|
components={{ Accordion }} // ← 这里传入 Accordion
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
)}
|
2025-06-16 14:41:48 +00:00
|
|
|
</div>
|
2025-06-17 07:24:43 +00:00
|
|
|
|
|
|
|
<Button>保存更改</Button>
|
2025-06-16 14:41:48 +00:00
|
|
|
</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
|
|
|
}
|