mirror of
https://github.com/cfngc4594/monaco-editor-lsp-next.git
synced 2025-07-04 01:10:53 +00:00
style: normalize quotes and indentation
This commit is contained in:
parent
055a532f3f
commit
c372856c3a
@ -25,7 +25,8 @@ export const optimizeCode = async (
|
||||
if (input.problemId) {
|
||||
try {
|
||||
// 尝试获取英文描述
|
||||
const problemLocalizationEn = await prisma.problemLocalization.findUnique({
|
||||
const problemLocalizationEn = await prisma.problemLocalization.findUnique(
|
||||
{
|
||||
where: {
|
||||
problemId_locale_type: {
|
||||
problemId: input.problemId,
|
||||
@ -36,7 +37,8 @@ export const optimizeCode = async (
|
||||
include: {
|
||||
problem: true,
|
||||
},
|
||||
});
|
||||
}
|
||||
);
|
||||
|
||||
if (problemLocalizationEn) {
|
||||
problemDetails = `
|
||||
@ -46,7 +48,8 @@ Description: ${problemLocalizationEn.content}
|
||||
`;
|
||||
} else {
|
||||
// 回退到中文描述
|
||||
const problemLocalizationZh = await prisma.problemLocalization.findUnique({
|
||||
const problemLocalizationZh =
|
||||
await prisma.problemLocalization.findUnique({
|
||||
where: {
|
||||
problemId_locale_type: {
|
||||
problemId: input.problemId,
|
||||
@ -65,10 +68,14 @@ Problem Requirements:
|
||||
-------------------
|
||||
Description: ${problemLocalizationZh.content}
|
||||
`;
|
||||
console.warn(`Fallback to Chinese description for problemId: ${input.problemId}`);
|
||||
console.warn(
|
||||
`Fallback to Chinese description for problemId: ${input.problemId}`
|
||||
);
|
||||
} else {
|
||||
problemDetails = "Problem description not found in any language.";
|
||||
console.warn(`No description found for problemId: ${input.problemId}`);
|
||||
console.warn(
|
||||
`No description found for problemId: ${input.problemId}`
|
||||
);
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
@ -77,7 +84,6 @@ Description: ${problemLocalizationZh.content}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// 构建AI提示词
|
||||
const prompt = `
|
||||
Analyze the following programming code for potential errors, inefficiencies or code style issues.
|
||||
|
@ -1,12 +1,15 @@
|
||||
"use server";
|
||||
|
||||
import {AITestCaseInput, AITestCaseOutput, AITestCaseOutputSchema} from "@/types/ai-testcase";
|
||||
import {
|
||||
AITestCaseInput,
|
||||
AITestCaseOutput,
|
||||
AITestCaseOutputSchema,
|
||||
} from "@/types/ai-testcase";
|
||||
|
||||
import { deepseek } from "@/lib/ai";
|
||||
import { CoreMessage, generateText } from "ai";
|
||||
import prisma from "@/lib/prisma";
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
* @param input
|
||||
@ -22,7 +25,8 @@ export const generateAITestcase = async (
|
||||
if (input.problemId) {
|
||||
try {
|
||||
// 尝试获取英文描述
|
||||
const problemLocalizationEn = await prisma.problemLocalization.findUnique({
|
||||
const problemLocalizationEn = await prisma.problemLocalization.findUnique(
|
||||
{
|
||||
where: {
|
||||
problemId_locale_type: {
|
||||
problemId: input.problemId,
|
||||
@ -33,7 +37,8 @@ export const generateAITestcase = async (
|
||||
include: {
|
||||
problem: true,
|
||||
},
|
||||
});
|
||||
}
|
||||
);
|
||||
|
||||
if (problemLocalizationEn) {
|
||||
problemDetails = `
|
||||
@ -43,7 +48,8 @@ Description: ${problemLocalizationEn.content}
|
||||
`;
|
||||
} else {
|
||||
// 回退到中文描述
|
||||
const problemLocalizationZh = await prisma.problemLocalization.findUnique({
|
||||
const problemLocalizationZh =
|
||||
await prisma.problemLocalization.findUnique({
|
||||
where: {
|
||||
problemId_locale_type: {
|
||||
problemId: input.problemId,
|
||||
@ -62,10 +68,14 @@ Problem Requirements:
|
||||
-------------------
|
||||
Description: ${problemLocalizationZh.content}
|
||||
`;
|
||||
console.warn(`Fallback to Chinese description for problemId: ${input.problemId}`);
|
||||
console.warn(
|
||||
`Fallback to Chinese description for problemId: ${input.problemId}`
|
||||
);
|
||||
} else {
|
||||
problemDetails = "Problem description not found in any language.";
|
||||
console.warn(`No description found for problemId: ${input.problemId}`);
|
||||
console.warn(
|
||||
`No description found for problemId: ${input.problemId}`
|
||||
);
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
@ -74,8 +84,6 @@ Description: ${problemLocalizationZh.content}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
// 构建AI提示词
|
||||
const prompt = `
|
||||
Analyze the problem statement to get the expected input structure, constraints, and output logic. Generate **novel, randomized** inputs/outputs that strictly adhere to the problem's requirements. Focus on:
|
||||
@ -128,16 +136,13 @@ Respond **ONLY** with this JSON structure.
|
||||
// 解析LLM响应
|
||||
let llmResponseJson;
|
||||
try {
|
||||
llmResponseJson = JSON.parse(text)
|
||||
|
||||
|
||||
llmResponseJson = JSON.parse(text);
|
||||
} catch (error) {
|
||||
console.error("Failed to parse LLM response as JSON:", error);
|
||||
console.error("LLM raw output:", text);
|
||||
throw new Error("Invalid JSON response from LLM");
|
||||
}
|
||||
|
||||
|
||||
// 验证响应格式
|
||||
const validationResult = AITestCaseOutputSchema.safeParse(llmResponseJson);
|
||||
if (!validationResult.success) {
|
||||
|
@ -1,9 +1,9 @@
|
||||
// app/actions/get-problem-data.ts
|
||||
'use server';
|
||||
"use server";
|
||||
|
||||
import prisma from '@/lib/prisma';
|
||||
import { Locale } from '@/generated/client';
|
||||
import { serialize } from 'next-mdx-remote/serialize';
|
||||
import prisma from "@/lib/prisma";
|
||||
import { Locale } from "@/generated/client";
|
||||
import { serialize } from "next-mdx-remote/serialize";
|
||||
|
||||
export async function getProblemData(problemId: string, locale?: string) {
|
||||
const selectedLocale = locale as Locale;
|
||||
@ -13,7 +13,7 @@ export async function getProblemData(problemId: string, locale?: string) {
|
||||
include: {
|
||||
templates: true,
|
||||
testcases: {
|
||||
include: { inputs: true }
|
||||
include: { inputs: true },
|
||||
},
|
||||
localizations: {
|
||||
where: {
|
||||
@ -24,13 +24,13 @@ export async function getProblemData(problemId: string, locale?: string) {
|
||||
});
|
||||
|
||||
if (!problem) {
|
||||
throw new Error('Problem not found');
|
||||
throw new Error("Problem not found");
|
||||
}
|
||||
|
||||
const getContent = (type: string) =>
|
||||
problem.localizations.find(loc => loc.type === type)?.content || '';
|
||||
problem.localizations.find((loc) => loc.type === type)?.content || "";
|
||||
|
||||
const rawDescription = getContent('DESCRIPTION');
|
||||
const rawDescription = getContent("DESCRIPTION");
|
||||
|
||||
const mdxDescription = await serialize(rawDescription, {
|
||||
parseFrontmatter: false,
|
||||
@ -43,18 +43,18 @@ export async function getProblemData(problemId: string, locale?: string) {
|
||||
isPublished: problem.isPublished,
|
||||
timeLimit: problem.timeLimit,
|
||||
memoryLimit: problem.memoryLimit,
|
||||
title: getContent('TITLE'),
|
||||
title: getContent("TITLE"),
|
||||
description: rawDescription,
|
||||
mdxDescription,
|
||||
solution: getContent('SOLUTION'),
|
||||
templates: problem.templates.map(t => ({
|
||||
solution: getContent("SOLUTION"),
|
||||
templates: problem.templates.map((t) => ({
|
||||
language: t.language,
|
||||
content: t.content,
|
||||
})),
|
||||
testcases: problem.testcases.map(tc => ({
|
||||
testcases: problem.testcases.map((tc) => ({
|
||||
id: tc.id,
|
||||
expectedOutput: tc.expectedOutput,
|
||||
inputs: tc.inputs.map(input => ({
|
||||
inputs: tc.inputs.map((input) => ({
|
||||
name: input.name,
|
||||
value: input.value,
|
||||
})),
|
||||
|
@ -1,5 +1,5 @@
|
||||
// src/app/actions/getProblemLocales.ts
|
||||
'use server';
|
||||
"use server";
|
||||
|
||||
import prisma from "@/lib/prisma";
|
||||
|
||||
@ -7,8 +7,8 @@ export async function getProblemLocales(problemId: string): Promise<string[]> {
|
||||
const locales = await prisma.problemLocalization.findMany({
|
||||
where: { problemId },
|
||||
select: { locale: true },
|
||||
distinct: ['locale'],
|
||||
distinct: ["locale"],
|
||||
});
|
||||
|
||||
return locales.map(l => l.locale);
|
||||
return locales.map((l) => l.locale);
|
||||
}
|
||||
|
@ -25,18 +25,21 @@ export const AIEditorWrapper = ({
|
||||
problemId,
|
||||
languageServerConfigs,
|
||||
onChange,
|
||||
// className,
|
||||
}: AIEditorWrapperProps) => {
|
||||
}: // className,
|
||||
AIEditorWrapperProps) => {
|
||||
const [currentCode, setCurrentCode] = useState(value ?? "");
|
||||
const [optimizedCode, setOptimizedCode] = useState("");
|
||||
const [isOptimizing, setIsOptimizing] = useState(false);
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
const [showDiff, setShowDiff] = useState(false);
|
||||
|
||||
const handleCodeChange = useCallback((val: string) => {
|
||||
const handleCodeChange = useCallback(
|
||||
(val: string) => {
|
||||
setCurrentCode(val);
|
||||
onChange?.(val);
|
||||
}, [onChange]);
|
||||
},
|
||||
[onChange]
|
||||
);
|
||||
|
||||
const handleOptimize = useCallback(async () => {
|
||||
if (!problemId || !currentCode) return;
|
||||
|
@ -1,14 +1,14 @@
|
||||
"use client"
|
||||
"use client";
|
||||
|
||||
import React, { useEffect, useState } from 'react';
|
||||
import { getProblemData } from '@/app/actions/getProblem';
|
||||
import { updateProblemTemplate } from '@/components/creater/problem-maintain';
|
||||
import { Label } from '@/components/ui/label';
|
||||
import { Button } from '@/components/ui/button';
|
||||
import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card';
|
||||
import { CoreEditor } from '@/components/core-editor';
|
||||
import { Language } from '@/generated/client';
|
||||
import { toast } from 'sonner';
|
||||
import React, { useEffect, useState } from "react";
|
||||
import { getProblemData } from "@/app/actions/getProblem";
|
||||
import { updateProblemTemplate } from "@/components/creater/problem-maintain";
|
||||
import { Label } from "@/components/ui/label";
|
||||
import { Button } from "@/components/ui/button";
|
||||
import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card";
|
||||
import { CoreEditor } from "@/components/core-editor";
|
||||
import { Language } from "@/generated/client";
|
||||
import { toast } from "sonner";
|
||||
|
||||
interface Template {
|
||||
language: string;
|
||||
@ -21,7 +21,7 @@ interface EditCodePanelProps {
|
||||
|
||||
export default function EditCodePanel({ problemId }: EditCodePanelProps) {
|
||||
const [codeTemplate, setCodeTemplate] = useState<Template>({
|
||||
language: 'cpp',
|
||||
language: "cpp",
|
||||
content: `// 默认代码模板 for Problem ${problemId}`,
|
||||
});
|
||||
const [templates, setTemplates] = useState<Template[]>([]);
|
||||
@ -31,18 +31,20 @@ export default function EditCodePanel({ problemId }: EditCodePanelProps) {
|
||||
try {
|
||||
const problem = await getProblemData(problemId);
|
||||
setTemplates(problem.templates);
|
||||
const sel = problem.templates.find(t => t.language === 'cpp') || problem.templates[0];
|
||||
const sel =
|
||||
problem.templates.find((t) => t.language === "cpp") ||
|
||||
problem.templates[0];
|
||||
if (sel) setCodeTemplate(sel);
|
||||
} catch (err) {
|
||||
console.error('加载问题数据失败:', err);
|
||||
toast.error('加载问题数据失败');
|
||||
console.error("加载问题数据失败:", err);
|
||||
toast.error("加载问题数据失败");
|
||||
}
|
||||
}
|
||||
fetchTemplates();
|
||||
}, [problemId]);
|
||||
|
||||
const handleLanguageChange = (language: string) => {
|
||||
const sel = templates.find(t => t.language === language);
|
||||
const sel = templates.find((t) => t.language === language);
|
||||
if (sel) setCodeTemplate(sel);
|
||||
};
|
||||
|
||||
@ -54,13 +56,13 @@ export default function EditCodePanel({ problemId }: EditCodePanelProps) {
|
||||
codeTemplate.content
|
||||
);
|
||||
if (res.success) {
|
||||
toast.success('保存成功');
|
||||
toast.success("保存成功");
|
||||
} else {
|
||||
toast.error('保存失败');
|
||||
toast.error("保存失败");
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('保存异常:', error);
|
||||
toast.error('保存异常');
|
||||
console.error("保存异常:", error);
|
||||
toast.error("保存异常");
|
||||
}
|
||||
};
|
||||
|
||||
@ -77,9 +79,9 @@ export default function EditCodePanel({ problemId }: EditCodePanelProps) {
|
||||
id="language-select"
|
||||
className="block w-full p-2 border border-gray-300 rounded-md dark:bg-gray-800 dark:border-gray-700"
|
||||
value={codeTemplate.language}
|
||||
onChange={e => handleLanguageChange(e.target.value)}
|
||||
onChange={(e) => handleLanguageChange(e.target.value)}
|
||||
>
|
||||
{templates.map(t => (
|
||||
{templates.map((t) => (
|
||||
<option key={t.language} value={t.language}>
|
||||
{t.language.toUpperCase()}
|
||||
</option>
|
||||
@ -94,8 +96,8 @@ export default function EditCodePanel({ problemId }: EditCodePanelProps) {
|
||||
language={codeTemplate.language}
|
||||
value={codeTemplate.content}
|
||||
path={`/${problemId}.${codeTemplate.language}`}
|
||||
onChange={value =>
|
||||
setCodeTemplate({ ...codeTemplate, content: value || '' })
|
||||
onChange={(value) =>
|
||||
setCodeTemplate({ ...codeTemplate, content: value || "" })
|
||||
}
|
||||
/>
|
||||
</div>
|
||||
|
@ -12,16 +12,25 @@ import { getProblemLocales } from "@/app/actions/getProblemLocales";
|
||||
import { Accordion } from "@/components/ui/accordion";
|
||||
import { VideoEmbed } from "@/components/content/video-embed";
|
||||
import { toast } from "sonner";
|
||||
import { updateProblemDescription, updateProblemTitle } from '@/components/creater/problem-maintain';
|
||||
import {
|
||||
updateProblemDescription,
|
||||
updateProblemTitle,
|
||||
} from "@/components/creater/problem-maintain";
|
||||
import { Locale } from "@/generated/client";
|
||||
|
||||
export default function EditDescriptionPanel({ problemId }: { problemId: string }) {
|
||||
export default function EditDescriptionPanel({
|
||||
problemId,
|
||||
}: {
|
||||
problemId: string;
|
||||
}) {
|
||||
const [locales, setLocales] = useState<string[]>([]);
|
||||
const [currentLocale, setCurrentLocale] = useState<string>("");
|
||||
const [customLocale, setCustomLocale] = useState("");
|
||||
|
||||
const [description, setDescription] = useState({ title: "", content: "" });
|
||||
const [viewMode, setViewMode] = useState<"edit" | "preview" | "compare">("edit");
|
||||
const [viewMode, setViewMode] = useState<"edit" | "preview" | "compare">(
|
||||
"edit"
|
||||
);
|
||||
|
||||
useEffect(() => {
|
||||
async function fetchLocales() {
|
||||
@ -31,7 +40,7 @@ export default function EditDescriptionPanel({ problemId }: { problemId: string
|
||||
if (langs.length > 0) setCurrentLocale(langs[0]);
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
toast.error('获取语言列表失败');
|
||||
toast.error("获取语言列表失败");
|
||||
}
|
||||
}
|
||||
fetchLocales();
|
||||
@ -42,10 +51,13 @@ export default function EditDescriptionPanel({ problemId }: { problemId: string
|
||||
async function fetchProblem() {
|
||||
try {
|
||||
const data = await getProblemData(problemId, currentLocale);
|
||||
setDescription({ title: data?.title || "", content: data?.description || "" });
|
||||
setDescription({
|
||||
title: data?.title || "",
|
||||
content: data?.description || "",
|
||||
});
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
toast.error('加载题目描述失败');
|
||||
toast.error("加载题目描述失败");
|
||||
}
|
||||
}
|
||||
fetchProblem();
|
||||
@ -63,21 +75,29 @@ export default function EditDescriptionPanel({ problemId }: { problemId: string
|
||||
|
||||
const handleSave = async (): Promise<void> => {
|
||||
if (!currentLocale) {
|
||||
toast.error('请选择语言');
|
||||
toast.error("请选择语言");
|
||||
return;
|
||||
}
|
||||
try {
|
||||
const locale = currentLocale as Locale;
|
||||
const resTitle = await updateProblemTitle(problemId, locale, description.title);
|
||||
const resDesc = await updateProblemDescription(problemId, locale, description.content);
|
||||
const resTitle = await updateProblemTitle(
|
||||
problemId,
|
||||
locale,
|
||||
description.title
|
||||
);
|
||||
const resDesc = await updateProblemDescription(
|
||||
problemId,
|
||||
locale,
|
||||
description.content
|
||||
);
|
||||
if (resTitle.success && resDesc.success) {
|
||||
toast.success('保存成功');
|
||||
toast.success("保存成功");
|
||||
} else {
|
||||
toast.error('保存失败');
|
||||
toast.error("保存失败");
|
||||
}
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
toast.error('保存异常');
|
||||
toast.error("保存异常");
|
||||
}
|
||||
};
|
||||
|
||||
@ -117,7 +137,9 @@ export default function EditDescriptionPanel({ problemId }: { problemId: string
|
||||
<Input
|
||||
id="description-title"
|
||||
value={description.title}
|
||||
onChange={(e) => setDescription({ ...description, title: e.target.value })}
|
||||
onChange={(e) =>
|
||||
setDescription({ ...description, title: e.target.value })
|
||||
}
|
||||
placeholder="输入题目标题"
|
||||
/>
|
||||
</div>
|
||||
@ -134,7 +156,9 @@ export default function EditDescriptionPanel({ problemId }: { problemId: string
|
||||
<Button
|
||||
type="button"
|
||||
variant={viewMode === "preview" ? "default" : "outline"}
|
||||
onClick={() => setViewMode(viewMode === "preview" ? "edit" : "preview")}
|
||||
onClick={() =>
|
||||
setViewMode(viewMode === "preview" ? "edit" : "preview")
|
||||
}
|
||||
>
|
||||
{viewMode === "preview" ? "取消" : "预览"}
|
||||
</Button>
|
||||
@ -148,12 +172,20 @@ export default function EditDescriptionPanel({ problemId }: { problemId: string
|
||||
</div>
|
||||
|
||||
{/* 编辑/预览区域 */}
|
||||
<div className={viewMode === "compare" ? "grid grid-cols-2 gap-6" : "flex flex-col gap-6"}>
|
||||
<div
|
||||
className={
|
||||
viewMode === "compare"
|
||||
? "grid grid-cols-2 gap-6"
|
||||
: "flex flex-col gap-6"
|
||||
}
|
||||
>
|
||||
{(viewMode === "edit" || viewMode === "compare") && (
|
||||
<div className="relative h-[600px]">
|
||||
<CoreEditor
|
||||
value={description.content}
|
||||
onChange={(newVal) => setDescription({ ...description, content: newVal || "" })}
|
||||
onChange={(newVal) =>
|
||||
setDescription({ ...description, content: newVal || "" })
|
||||
}
|
||||
language="markdown"
|
||||
className="absolute inset-0 rounded-md border border-input"
|
||||
/>
|
||||
@ -161,7 +193,10 @@ export default function EditDescriptionPanel({ problemId }: { problemId: string
|
||||
)}
|
||||
{viewMode !== "edit" && (
|
||||
<div className="prose dark:prose-invert">
|
||||
<MdxPreview source={description.content} components={{ Accordion, VideoEmbed }} />
|
||||
<MdxPreview
|
||||
source={description.content}
|
||||
components={{ Accordion, VideoEmbed }}
|
||||
/>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
|
@ -7,7 +7,7 @@ import { Button } from "@/components/ui/button";
|
||||
import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card";
|
||||
import { getProblemData } from "@/app/actions/getProblem";
|
||||
import { toast } from "sonner";
|
||||
import { updateProblemDetail } from '@/components/creater/problem-maintain';
|
||||
import { updateProblemDetail } from "@/components/creater/problem-maintain";
|
||||
import { Difficulty } from "@/generated/client";
|
||||
|
||||
export default function EditDetailPanel({ problemId }: { problemId: string }) {
|
||||
@ -32,7 +32,7 @@ export default function EditDetailPanel({ problemId }: { problemId: string }) {
|
||||
});
|
||||
} catch (error) {
|
||||
console.error("获取题目信息失败:", error);
|
||||
toast.error('加载详情失败');
|
||||
toast.error("加载详情失败");
|
||||
}
|
||||
}
|
||||
fetchData();
|
||||
@ -66,13 +66,13 @@ export default function EditDetailPanel({ problemId }: { problemId: string }) {
|
||||
isPublished: problemDetails.isPublished,
|
||||
});
|
||||
if (res.success) {
|
||||
toast.success('保存成功');
|
||||
toast.success("保存成功");
|
||||
} else {
|
||||
toast.error('保存失败');
|
||||
toast.error("保存失败");
|
||||
}
|
||||
} catch (err) {
|
||||
console.error('保存异常:', err);
|
||||
toast.error('保存异常');
|
||||
console.error("保存异常:", err);
|
||||
toast.error("保存异常");
|
||||
}
|
||||
};
|
||||
|
||||
@ -138,7 +138,10 @@ export default function EditDetailPanel({ problemId }: { problemId: string }) {
|
||||
type="checkbox"
|
||||
checked={problemDetails.isPublished}
|
||||
onChange={(e) =>
|
||||
setProblemDetails({ ...problemDetails, isPublished: e.target.checked })
|
||||
setProblemDetails({
|
||||
...problemDetails,
|
||||
isPublished: e.target.checked,
|
||||
})
|
||||
}
|
||||
className="w-4 h-4 text-blue-600 bg-gray-100 border-gray-300 rounded focus:ring-blue-500 dark:focus:ring-blue-600 focus:ring-2"
|
||||
/>
|
||||
|
@ -12,16 +12,22 @@ 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 { updateProblemSolution } from "@/components/creater/problem-maintain";
|
||||
import { Locale } from "@/generated/client";
|
||||
|
||||
export default function EditSolutionPanel({ problemId }: { problemId: string }) {
|
||||
export default function EditSolutionPanel({
|
||||
problemId,
|
||||
}: {
|
||||
problemId: string;
|
||||
}) {
|
||||
const [locales, setLocales] = useState<string[]>([]);
|
||||
const [currentLocale, setCurrentLocale] = useState<string>("");
|
||||
const [customLocale, setCustomLocale] = useState("");
|
||||
|
||||
const [solution, setSolution] = useState({ title: "", content: "" });
|
||||
const [viewMode, setViewMode] = useState<"edit" | "preview" | "compare">("edit");
|
||||
const [viewMode, setViewMode] = useState<"edit" | "preview" | "compare">(
|
||||
"edit"
|
||||
);
|
||||
|
||||
useEffect(() => {
|
||||
async function fetchLocales() {
|
||||
@ -31,7 +37,7 @@ export default function EditSolutionPanel({ problemId }: { problemId: string })
|
||||
if (langs.length > 0) setCurrentLocale(langs[0]);
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
toast.error('获取语言列表失败');
|
||||
toast.error("获取语言列表失败");
|
||||
}
|
||||
}
|
||||
fetchLocales();
|
||||
@ -42,10 +48,13 @@ export default function EditSolutionPanel({ problemId }: { problemId: string })
|
||||
async function fetchSolution() {
|
||||
try {
|
||||
const data = await getProblemData(problemId, currentLocale);
|
||||
setSolution({ title: (data?.title || "") + " 解析", content: data?.solution || "" });
|
||||
setSolution({
|
||||
title: (data?.title || "") + " 解析",
|
||||
content: data?.solution || "",
|
||||
});
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
toast.error('加载题目解析失败');
|
||||
toast.error("加载题目解析失败");
|
||||
}
|
||||
}
|
||||
fetchSolution();
|
||||
@ -53,7 +62,7 @@ export default function EditSolutionPanel({ problemId }: { problemId: string })
|
||||
|
||||
const handleAddCustomLocale = () => {
|
||||
if (customLocale && !locales.includes(customLocale)) {
|
||||
setLocales(prev => [...prev, customLocale]);
|
||||
setLocales((prev) => [...prev, customLocale]);
|
||||
setCurrentLocale(customLocale);
|
||||
setCustomLocale("");
|
||||
setSolution({ title: "", content: "" });
|
||||
@ -62,20 +71,24 @@ export default function EditSolutionPanel({ problemId }: { problemId: string })
|
||||
|
||||
const handleSave = async (): Promise<void> => {
|
||||
if (!currentLocale) {
|
||||
toast.error('请选择语言');
|
||||
toast.error("请选择语言");
|
||||
return;
|
||||
}
|
||||
try {
|
||||
const locale = currentLocale as Locale;
|
||||
const res = await updateProblemSolution(problemId, locale, solution.content);
|
||||
const res = await updateProblemSolution(
|
||||
problemId,
|
||||
locale,
|
||||
solution.content
|
||||
);
|
||||
if (res.success) {
|
||||
toast.success('保存成功');
|
||||
toast.success("保存成功");
|
||||
} else {
|
||||
toast.error('保存失败');
|
||||
toast.error("保存失败");
|
||||
}
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
toast.error('保存异常');
|
||||
toast.error("保存异常");
|
||||
}
|
||||
};
|
||||
|
||||
@ -105,7 +118,9 @@ export default function EditSolutionPanel({ problemId }: { problemId: string })
|
||||
value={customLocale}
|
||||
onChange={(e) => setCustomLocale(e.target.value)}
|
||||
/>
|
||||
<Button type="button" onClick={handleAddCustomLocale}>添加</Button>
|
||||
<Button type="button" onClick={handleAddCustomLocale}>
|
||||
添加
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@ -115,7 +130,9 @@ export default function EditSolutionPanel({ problemId }: { problemId: string })
|
||||
<Input
|
||||
id="solution-title"
|
||||
value={solution.title}
|
||||
onChange={(e) => setSolution({ ...solution, title: e.target.value })}
|
||||
onChange={(e) =>
|
||||
setSolution({ ...solution, title: e.target.value })
|
||||
}
|
||||
placeholder="输入题解标题"
|
||||
disabled
|
||||
/>
|
||||
@ -123,20 +140,46 @@ export default function EditSolutionPanel({ problemId }: { problemId: string })
|
||||
|
||||
{/* 编辑/预览切换 */}
|
||||
<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")}>
|
||||
<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>
|
||||
<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 === "compare"
|
||||
? "grid grid-cols-2 gap-6"
|
||||
: "flex flex-col gap-6"
|
||||
}
|
||||
>
|
||||
{(viewMode === "edit" || viewMode === "compare") && (
|
||||
<div className="relative h-[600px]">
|
||||
<CoreEditor
|
||||
value={solution.content}
|
||||
onChange={(val) => setSolution({ ...solution, content: val || "" })}
|
||||
onChange={(val) =>
|
||||
setSolution({ ...solution, content: val || "" })
|
||||
}
|
||||
language="markdown"
|
||||
className="absolute inset-0 rounded-md border border-input"
|
||||
/>
|
||||
@ -144,12 +187,17 @@ export default function EditSolutionPanel({ problemId }: { problemId: string })
|
||||
)}
|
||||
{viewMode !== "edit" && (
|
||||
<div className="prose dark:prose-invert">
|
||||
<MdxPreview source={solution.content} components={{ Accordion, VideoEmbed }} />
|
||||
<MdxPreview
|
||||
source={solution.content}
|
||||
components={{ Accordion, VideoEmbed }}
|
||||
/>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
|
||||
<Button type="button" onClick={handleSave}>保存更改</Button>
|
||||
<Button type="button" onClick={handleSave}>
|
||||
保存更改
|
||||
</Button>
|
||||
</CardContent>
|
||||
</Card>
|
||||
);
|
||||
|
@ -20,7 +20,11 @@ interface Testcase {
|
||||
inputs: { name: string; value: string }[];
|
||||
}
|
||||
|
||||
export default function EditTestcasePanel({ problemId }: { problemId: string }) {
|
||||
export default function EditTestcasePanel({
|
||||
problemId,
|
||||
}: {
|
||||
problemId: string;
|
||||
}) {
|
||||
const [testcases, setTestcases] = useState<Testcase[]>([]);
|
||||
const [isGenerating, setIsGenerating] = useState(false);
|
||||
|
||||
@ -42,7 +46,11 @@ export default function EditTestcasePanel({ problemId }: { problemId: string })
|
||||
const handleAddTestcase = () =>
|
||||
setTestcases((prev) => [
|
||||
...prev,
|
||||
{ id: `new-${Date.now()}-${Math.random()}`, expectedOutput: "", inputs: [{ name: "input1", value: "" }] },
|
||||
{
|
||||
id: `new-${Date.now()}-${Math.random()}`,
|
||||
expectedOutput: "",
|
||||
inputs: [{ name: "input1", value: "" }],
|
||||
},
|
||||
]);
|
||||
|
||||
// AI 生成测试用例
|
||||
@ -52,7 +60,11 @@ export default function EditTestcasePanel({ problemId }: { problemId: string })
|
||||
const ai = await generateAITestcase({ problemId });
|
||||
setTestcases((prev) => [
|
||||
...prev,
|
||||
{ id: `new-${Date.now()}-${Math.random()}`, expectedOutput: ai.expectedOutput, inputs: ai.inputs },
|
||||
{
|
||||
id: `new-${Date.now()}-${Math.random()}`,
|
||||
expectedOutput: ai.expectedOutput,
|
||||
inputs: ai.inputs,
|
||||
},
|
||||
]);
|
||||
window.scrollTo({ top: document.body.scrollHeight, behavior: "smooth" });
|
||||
} catch (err) {
|
||||
@ -106,7 +118,10 @@ export default function EditTestcasePanel({ problemId }: { problemId: string })
|
||||
const handleAddInput = (tIdx: number) =>
|
||||
setTestcases((prev) => {
|
||||
const c = [...prev];
|
||||
const inputs = [...c[tIdx].inputs, { name: `input${c[tIdx].inputs.length + 1}`, value: "" }];
|
||||
const inputs = [
|
||||
...c[tIdx].inputs,
|
||||
{ name: `input${c[tIdx].inputs.length + 1}`, value: "" },
|
||||
];
|
||||
c[tIdx] = { ...c[tIdx], inputs };
|
||||
return c;
|
||||
});
|
||||
@ -125,20 +140,32 @@ export default function EditTestcasePanel({ problemId }: { problemId: string })
|
||||
try {
|
||||
for (let i = 0; i < testcases.length; i++) {
|
||||
const tc = testcases[i];
|
||||
if (tc.expectedOutput.trim() === "" || tc.inputs.some(inp => !inp.name.trim() || !inp.value.trim())) {
|
||||
if (
|
||||
tc.expectedOutput.trim() === "" ||
|
||||
tc.inputs.some((inp) => !inp.name.trim() || !inp.value.trim())
|
||||
) {
|
||||
toast.error(`第 ${i + 1} 个测试用例存在空的输入或输出,保存失败`);
|
||||
return;
|
||||
}
|
||||
|
||||
if (tc.id.startsWith("new-")) {
|
||||
const res = await addProblemTestcase(problemId, tc.expectedOutput, tc.inputs);
|
||||
const res = await addProblemTestcase(
|
||||
problemId,
|
||||
tc.expectedOutput,
|
||||
tc.inputs
|
||||
);
|
||||
if (res.success) {
|
||||
toast.success(`新增测试用例 ${i + 1} 成功`);
|
||||
} else {
|
||||
toast.error(`新增测试用例 ${i + 1} 失败`);
|
||||
}
|
||||
} else {
|
||||
const res = await updateProblemTestcase(problemId, tc.id, tc.expectedOutput, tc.inputs);
|
||||
const res = await updateProblemTestcase(
|
||||
problemId,
|
||||
tc.id,
|
||||
tc.expectedOutput,
|
||||
tc.inputs
|
||||
);
|
||||
if (res.success) toast.success(`更新测试用例 ${i + 1} 成功`);
|
||||
else toast.error(`更新测试用例 ${i + 1} 失败`);
|
||||
}
|
||||
@ -173,7 +200,10 @@ export default function EditTestcasePanel({ problemId }: { problemId: string })
|
||||
<div key={tc.id} className="border p-4 rounded space-y-4">
|
||||
<div className="flex justify-between items-center">
|
||||
<h3 className="font-medium">测试用例 {idx + 1}</h3>
|
||||
<Button variant="destructive" onClick={() => handleRemoveTestcase(idx)}>
|
||||
<Button
|
||||
variant="destructive"
|
||||
onClick={() => handleRemoveTestcase(idx)}
|
||||
>
|
||||
删除
|
||||
</Button>
|
||||
</div>
|
||||
@ -181,7 +211,9 @@ export default function EditTestcasePanel({ problemId }: { problemId: string })
|
||||
<Label>预期输出</Label>
|
||||
<Input
|
||||
value={tc.expectedOutput}
|
||||
onChange={(e) => handleExpectedOutputChange(idx, e.target.value)}
|
||||
onChange={(e) =>
|
||||
handleExpectedOutputChange(idx, e.target.value)
|
||||
}
|
||||
placeholder="输入预期输出"
|
||||
/>
|
||||
</div>
|
||||
@ -196,7 +228,9 @@ export default function EditTestcasePanel({ problemId }: { problemId: string })
|
||||
<Label>名称</Label>
|
||||
<Input
|
||||
value={inp.name}
|
||||
onChange={(e) => handleInputChange(idx, iIdx, "name", e.target.value)}
|
||||
onChange={(e) =>
|
||||
handleInputChange(idx, iIdx, "name", e.target.value)
|
||||
}
|
||||
placeholder="参数名称"
|
||||
/>
|
||||
</div>
|
||||
@ -204,12 +238,17 @@ export default function EditTestcasePanel({ problemId }: { problemId: string })
|
||||
<Label>值</Label>
|
||||
<Input
|
||||
value={inp.value}
|
||||
onChange={(e) => handleInputChange(idx, iIdx, "value", e.target.value)}
|
||||
onChange={(e) =>
|
||||
handleInputChange(idx, iIdx, "value", e.target.value)
|
||||
}
|
||||
placeholder="参数值"
|
||||
/>
|
||||
</div>
|
||||
{iIdx > 0 && (
|
||||
<Button variant="outline" onClick={() => handleRemoveInput(idx, iIdx)}>
|
||||
<Button
|
||||
variant="outline"
|
||||
onClick={() => handleRemoveInput(idx, iIdx)}
|
||||
>
|
||||
删除输入
|
||||
</Button>
|
||||
)}
|
||||
|
@ -2,9 +2,23 @@
|
||||
|
||||
import prisma from "@/lib/prisma";
|
||||
import { revalidatePath } from "next/cache";
|
||||
import { Difficulty, Locale, ProblemContentType, Language } from "@/generated/client";
|
||||
import {
|
||||
Difficulty,
|
||||
Locale,
|
||||
ProblemContentType,
|
||||
Language,
|
||||
} from "@/generated/client";
|
||||
|
||||
export async function updateProblemDetail(problemId: string, data: { displayId?: number; difficulty?: Difficulty; timeLimit?: number; memoryLimit?: number; isPublished?: boolean }) {
|
||||
export async function updateProblemDetail(
|
||||
problemId: string,
|
||||
data: {
|
||||
displayId?: number;
|
||||
difficulty?: Difficulty;
|
||||
timeLimit?: number;
|
||||
memoryLimit?: number;
|
||||
isPublished?: boolean;
|
||||
}
|
||||
) {
|
||||
try {
|
||||
const updatedProblem = await prisma.problem.update({
|
||||
where: { id: problemId },
|
||||
@ -13,8 +27,8 @@ export async function updateProblemDetail(problemId: string, data: { displayId?:
|
||||
difficulty: data.difficulty,
|
||||
timeLimit: data.timeLimit,
|
||||
memoryLimit: data.memoryLimit,
|
||||
isPublished: data.isPublished
|
||||
}
|
||||
isPublished: data.isPublished,
|
||||
},
|
||||
});
|
||||
|
||||
revalidatePath(`/problem-editor/${problemId}`);
|
||||
@ -25,25 +39,29 @@ export async function updateProblemDetail(problemId: string, data: { displayId?:
|
||||
}
|
||||
}
|
||||
|
||||
export async function updateProblemDescription(problemId: string, locale: Locale, content: string) {
|
||||
export async function updateProblemDescription(
|
||||
problemId: string,
|
||||
locale: Locale,
|
||||
content: string
|
||||
) {
|
||||
try {
|
||||
const updatedLocalization = await prisma.problemLocalization.upsert({
|
||||
where: {
|
||||
problemId_locale_type: {
|
||||
problemId: problemId,
|
||||
locale: locale,
|
||||
type: ProblemContentType.DESCRIPTION
|
||||
}
|
||||
type: ProblemContentType.DESCRIPTION,
|
||||
},
|
||||
},
|
||||
create: {
|
||||
problemId: problemId,
|
||||
locale: locale,
|
||||
type: ProblemContentType.DESCRIPTION,
|
||||
content: content
|
||||
content: content,
|
||||
},
|
||||
update: {
|
||||
content: content
|
||||
}
|
||||
content: content,
|
||||
},
|
||||
});
|
||||
|
||||
revalidatePath(`/problem-editor/${problemId}`);
|
||||
@ -54,25 +72,29 @@ export async function updateProblemDescription(problemId: string, locale: Locale
|
||||
}
|
||||
}
|
||||
|
||||
export async function updateProblemSolution(problemId: string, locale: Locale, content: string) {
|
||||
export async function updateProblemSolution(
|
||||
problemId: string,
|
||||
locale: Locale,
|
||||
content: string
|
||||
) {
|
||||
try {
|
||||
const updatedLocalization = await prisma.problemLocalization.upsert({
|
||||
where: {
|
||||
problemId_locale_type: {
|
||||
problemId: problemId,
|
||||
locale: locale,
|
||||
type: ProblemContentType.SOLUTION
|
||||
}
|
||||
type: ProblemContentType.SOLUTION,
|
||||
},
|
||||
},
|
||||
create: {
|
||||
problemId: problemId,
|
||||
locale: locale,
|
||||
type: ProblemContentType.SOLUTION,
|
||||
content: content
|
||||
content: content,
|
||||
},
|
||||
update: {
|
||||
content: content
|
||||
}
|
||||
content: content,
|
||||
},
|
||||
});
|
||||
|
||||
revalidatePath(`/problem-editor/${problemId}`);
|
||||
@ -83,23 +105,27 @@ export async function updateProblemSolution(problemId: string, locale: Locale, c
|
||||
}
|
||||
}
|
||||
|
||||
export async function updateProblemTemplate(problemId: string, language: Language, content: string) {
|
||||
export async function updateProblemTemplate(
|
||||
problemId: string,
|
||||
language: Language,
|
||||
content: string
|
||||
) {
|
||||
try {
|
||||
const updatedTemplate = await prisma.template.upsert({
|
||||
where: {
|
||||
problemId_language: {
|
||||
problemId: problemId,
|
||||
language: language
|
||||
}
|
||||
language: language,
|
||||
},
|
||||
},
|
||||
create: {
|
||||
problemId: problemId,
|
||||
language: language,
|
||||
content: content
|
||||
content: content,
|
||||
},
|
||||
update: {
|
||||
content: content
|
||||
}
|
||||
content: content,
|
||||
},
|
||||
});
|
||||
|
||||
revalidatePath(`/problem-editor/${problemId}`);
|
||||
@ -110,19 +136,24 @@ export async function updateProblemTemplate(problemId: string, language: Languag
|
||||
}
|
||||
}
|
||||
|
||||
export async function updateProblemTestcase(problemId: string, testcaseId: string, expectedOutput: string, inputs: { name: string; value: string }[]) {
|
||||
export async function updateProblemTestcase(
|
||||
problemId: string,
|
||||
testcaseId: string,
|
||||
expectedOutput: string,
|
||||
inputs: { name: string; value: string }[]
|
||||
) {
|
||||
try {
|
||||
// Update testcase
|
||||
const updatedTestcase = await prisma.testcase.update({
|
||||
where: { id: testcaseId },
|
||||
data: {
|
||||
expectedOutput: expectedOutput
|
||||
}
|
||||
expectedOutput: expectedOutput,
|
||||
},
|
||||
});
|
||||
|
||||
// Delete old inputs
|
||||
await prisma.testcaseInput.deleteMany({
|
||||
where: { testcaseId: testcaseId }
|
||||
where: { testcaseId: testcaseId },
|
||||
});
|
||||
|
||||
// Create new inputs
|
||||
@ -131,15 +162,15 @@ export async function updateProblemTestcase(problemId: string, testcaseId: strin
|
||||
testcaseId: testcaseId,
|
||||
index: index,
|
||||
name: input.name,
|
||||
value: input.value
|
||||
}))
|
||||
value: input.value,
|
||||
})),
|
||||
});
|
||||
|
||||
revalidatePath(`/problem-editor/${problemId}`);
|
||||
return {
|
||||
success: true,
|
||||
testcase: updatedTestcase,
|
||||
inputs: createdInputs
|
||||
inputs: createdInputs,
|
||||
};
|
||||
} catch (error) {
|
||||
console.error("Failed to update problem testcase:", error);
|
||||
@ -147,14 +178,18 @@ export async function updateProblemTestcase(problemId: string, testcaseId: strin
|
||||
}
|
||||
}
|
||||
|
||||
export async function addProblemTestcase(problemId: string, expectedOutput: string, inputs: { name: string; value: string }[]) {
|
||||
export async function addProblemTestcase(
|
||||
problemId: string,
|
||||
expectedOutput: string,
|
||||
inputs: { name: string; value: string }[]
|
||||
) {
|
||||
try {
|
||||
// Create testcase
|
||||
const newTestcase = await prisma.testcase.create({
|
||||
data: {
|
||||
problemId: problemId,
|
||||
expectedOutput: expectedOutput
|
||||
}
|
||||
expectedOutput: expectedOutput,
|
||||
},
|
||||
});
|
||||
|
||||
// Create inputs
|
||||
@ -163,15 +198,15 @@ export async function addProblemTestcase(problemId: string, expectedOutput: stri
|
||||
testcaseId: newTestcase.id,
|
||||
index: index,
|
||||
name: input.name,
|
||||
value: input.value
|
||||
}))
|
||||
value: input.value,
|
||||
})),
|
||||
});
|
||||
|
||||
revalidatePath(`/problem-editor/${problemId}`);
|
||||
return {
|
||||
success: true,
|
||||
testcase: newTestcase,
|
||||
inputs: createdInputs
|
||||
inputs: createdInputs,
|
||||
};
|
||||
} catch (error) {
|
||||
console.error("Failed to add problem testcase:", error);
|
||||
@ -179,10 +214,13 @@ export async function addProblemTestcase(problemId: string, expectedOutput: stri
|
||||
}
|
||||
}
|
||||
|
||||
export async function deleteProblemTestcase(problemId: string, testcaseId: string) {
|
||||
export async function deleteProblemTestcase(
|
||||
problemId: string,
|
||||
testcaseId: string
|
||||
) {
|
||||
try {
|
||||
const deletedTestcase = await prisma.testcase.delete({
|
||||
where: { id: testcaseId }
|
||||
where: { id: testcaseId },
|
||||
});
|
||||
|
||||
revalidatePath(`/problem-editor/${problemId}`);
|
||||
|
@ -1,21 +1,19 @@
|
||||
import {z} from "zod";
|
||||
import { z } from "zod";
|
||||
|
||||
export const AITestCaseInputSchema = z.object({
|
||||
problemId: z.string(),
|
||||
})
|
||||
});
|
||||
|
||||
export type AITestCaseInput = z.infer<typeof AITestCaseInputSchema>
|
||||
export type AITestCaseInput = z.infer<typeof AITestCaseInputSchema>;
|
||||
|
||||
const input = z.object({
|
||||
name: z.string(),
|
||||
value: z.string()
|
||||
})
|
||||
value: z.string(),
|
||||
});
|
||||
|
||||
export const AITestCaseOutputSchema = z.object({
|
||||
expectedOutput: z.string(),
|
||||
inputs: z.array(input)
|
||||
})
|
||||
|
||||
export type AITestCaseOutput = z.infer<typeof AITestCaseOutputSchema>
|
||||
|
||||
inputs: z.array(input),
|
||||
});
|
||||
|
||||
export type AITestCaseOutput = z.infer<typeof AITestCaseOutputSchema>;
|
||||
|
Loading…
Reference in New Issue
Block a user