judge4c/src/app/(app)/problem-editor/[problemId]/page.tsx
fly6516 45ab8943a1 feat(problem-editor): add feature to preload problem information if there already has had data in database
- 添加了数据预加载功能,通过 getProblemData API 获取题目信息
- 优化了各个编辑面板的实现,提高了代码复用性和可维护性- 新增了测试用例编辑功能,支持多输入参数的管理
- 改进了题解编辑面板,增加了预览和对比功能
- 统一了表单元素的样式和交互方式,提升了用户体验
2025-06-20 16:51:28 +08:00

121 lines
3.2 KiB
TypeScript

"use client";
import { ProblemFlexLayout } from '@/features/problems/components/problem-flexlayout';
import EditDescriptionPanel from '@/components/creater/edit-description-panel';
import EditSolutionPanel from '@/components/creater/edit-solution-panel';
import EditTestcasePanel from '@/components/creater/edit-testcase-panel';
import EditDetailPanel from '@/components/creater/edit-detail-panel';
import EditCodePanel from '@/components/creater/edit-code-panel';
import { updateProblem } from '@/app/actions/updateProblem';
interface ProblemEditorPageProps {
params: Promise<{ problemId: string }>;
}
interface UpdateData {
content: string;
language?: 'c' | 'cpp';
inputs?: Array<{ index: number; name: string; value: string }>;
}
const handleUpdate = async (
updateFn: (data: UpdateData) => Promise<{ success: boolean }>,
data: UpdateData
) => {
try {
const result = await updateFn(data);
if (!result.success) {
// 这里可以添加更具体的错误处理
}
return result;
} catch (error) {
console.error('更新失败:', error);
return { success: false };
}
};
export default async function ProblemEditorPage({
params,
}: ProblemEditorPageProps) {
const { problemId } = await params;
const components: Record<string, React.ReactNode> = {
description: <EditDescriptionPanel
problemId={problemId}
onUpdate={async (data) => {
await handleUpdate(
(descriptionData) => updateProblem({
problemId,
displayId: 0,
description: descriptionData.content
}),
data
);
}}
/>,
solution: <EditSolutionPanel
problemId={problemId}
onUpdate={async (data) => {
await handleUpdate(
(solutionData) => updateProblem({
problemId,
displayId: 0,
solution: solutionData.content
}),
data
);
}}
/>,
detail: <EditDetailPanel
problemId={problemId}
onUpdate={async (data) => {
await handleUpdate(
(detailData) => updateProblem({
problemId,
displayId: 0,
detail: detailData.content
}),
data
);
}}
/>,
code: <EditCodePanel
problemId={problemId}
onUpdate={async (data) => {
await handleUpdate(
(codeData) => updateProblem({
problemId,
displayId: 0,
templates: [{
language: codeData.language || 'c', // 添加默认值
content: codeData.content
}]
}),
data
);
}}
/>,
testcase: <EditTestcasePanel
problemId={problemId}
onUpdate={async (data) => {
await handleUpdate(
(testcaseData) => updateProblem({
problemId,
displayId: 0,
testcases: [{
expectedOutput: testcaseData.content,
inputs: testcaseData.inputs || [] // 添加默认空数组
}]
}),
data
);
}}
/>
};
return (
<div className="relative flex h-full w-full">
<ProblemFlexLayout components={components} />
</div>
);
}