mirror of
https://github.com/massbug/judge4c.git
synced 2025-05-17 23:12:23 +00:00
refactor(app): 重构问题页面并添加 AI 优化功能
- 重构了问题页面的布局和组件 - 添加了 AI 优化代码的功能和相关组件 - 更新了 Dockview 配置,移除了 AI 优化相关的冗余代码 - 优化了代码结构,提高了组件的可复用性和可维护性
This commit is contained in:
parent
027f0fa4e9
commit
88be549430
@ -1,11 +1,30 @@
|
||||
import { ProblemEditor } from "@/components/problem-editor";
|
||||
import { AIProblemEditor } from "@/components/ai-optimized-editor";
|
||||
import { useParams } from "next/navigation";
|
||||
|
||||
export default function CodePage() {
|
||||
const params = useParams();
|
||||
const problemId = params.id as string;
|
||||
|
||||
const handleOptimize = async () => {
|
||||
// 这里实现调用AI优化逻辑
|
||||
console.log("Optimizing code for problem", problemId);
|
||||
};
|
||||
|
||||
return (
|
||||
<div className="relative flex-1">
|
||||
<div className="absolute w-full h-full">
|
||||
<ProblemEditor />
|
||||
<div className="absolute w-full h-full flex flex-col">
|
||||
<div className="p-2 border-b border-border flex justify-between items-center">
|
||||
<button
|
||||
onClick={handleOptimize}
|
||||
className="px-3 py-1 bg-primary text-primary-foreground rounded-md hover:bg-primary/90 transition-colors"
|
||||
>
|
||||
Optimize Code
|
||||
</button>
|
||||
</div>
|
||||
<AIProblemEditor
|
||||
problemId={problemId}
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
}
|
@ -11,10 +11,8 @@ import {
|
||||
import { Locale } from "@/config/i18n";
|
||||
import { useEffect, useState } from "react";
|
||||
import { useTranslations } from "next-intl";
|
||||
import { usePathname } from "next/navigation";
|
||||
import Dockview from "@/components/dockview";
|
||||
import { useDockviewStore } from "@/stores/dockview";
|
||||
import { AIProblemEditor } from "@/components/ai-optimized-editor";
|
||||
|
||||
interface ProblemPageProps {
|
||||
locale: Locale;
|
||||
@ -28,169 +26,123 @@ interface ProblemPageProps {
|
||||
}
|
||||
|
||||
export default function ProblemPage({
|
||||
locale,
|
||||
Description,
|
||||
Solutions,
|
||||
Submissions,
|
||||
Details,
|
||||
Code,
|
||||
Testcase,
|
||||
Bot,
|
||||
}: ProblemPageProps) {
|
||||
locale,
|
||||
Description,
|
||||
Solutions,
|
||||
Submissions,
|
||||
Details,
|
||||
Code,
|
||||
Testcase,
|
||||
Bot,
|
||||
}: ProblemPageProps) {
|
||||
const [key, setKey] = useState(0);
|
||||
const { setApi } = useDockviewStore();
|
||||
const t = useTranslations("ProblemPage");
|
||||
const pathname = usePathname();
|
||||
const problemId = pathname.split("/").pop(); // 从URL提取problemId
|
||||
|
||||
// AI优化相关状态
|
||||
const [showAIEditor, setShowAIEditor] = useState(false);
|
||||
const [userCode, setUserCode] = useState(`function example() {
|
||||
// 初始代码
|
||||
return "Hello World";
|
||||
}`);
|
||||
|
||||
// 修改Code面板内容以包含切换功能
|
||||
const CodeWithToggle = (
|
||||
<div className="p-2">
|
||||
<div className="flex justify-between items-center mb-2">
|
||||
<button
|
||||
onClick={() => setShowAIEditor(!showAIEditor)}
|
||||
className="px-3 py-1 bg-primary text-primary-foreground rounded-md text-sm"
|
||||
>
|
||||
{showAIEditor ? "普通编辑器" : "AI优化编辑器"}
|
||||
</button>
|
||||
|
||||
{showAIEditor && (
|
||||
<button
|
||||
onClick={() => setShowAIEditor(false)}
|
||||
className="px-3 py-1 bg-secondary text-secondary-foreground rounded-md text-sm"
|
||||
>
|
||||
返回编辑
|
||||
</button>
|
||||
)}
|
||||
</div>
|
||||
|
||||
{showAIEditor ? (
|
||||
<AIProblemEditor
|
||||
initialCode={userCode}
|
||||
problemId={problemId}
|
||||
onCodeChange={setUserCode}
|
||||
/>
|
||||
) : (
|
||||
// 原始Code组件保持不变
|
||||
<div className="h-[500px]">
|
||||
{Code}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
|
||||
useEffect(() => {
|
||||
setKey((prevKey) => prevKey + 1);
|
||||
}, [locale]);
|
||||
|
||||
// 修改Dockview配置:更新Code面板引用
|
||||
return (
|
||||
<Dockview
|
||||
key={key}
|
||||
storageKey="dockview:problem"
|
||||
onApiReady={setApi}
|
||||
options={[
|
||||
{
|
||||
id: "Description",
|
||||
component: "Description",
|
||||
tabComponent: "Description",
|
||||
params: {
|
||||
icon: FileTextIcon,
|
||||
content: Description,
|
||||
title: t("Description"),
|
||||
},
|
||||
},
|
||||
{
|
||||
id: "Solutions",
|
||||
component: "Solutions",
|
||||
tabComponent: "Solutions",
|
||||
params: {
|
||||
icon: FlaskConicalIcon,
|
||||
content: Solutions,
|
||||
title: t("Solutions"),
|
||||
},
|
||||
position: {
|
||||
referencePanel: "Description",
|
||||
direction: "within",
|
||||
},
|
||||
inactive: true,
|
||||
},
|
||||
{
|
||||
id: "Submissions",
|
||||
component: "Submissions",
|
||||
tabComponent: "Submissions",
|
||||
params: {
|
||||
icon: CircleCheckBigIcon,
|
||||
content: Submissions,
|
||||
title: t("Submissions"),
|
||||
},
|
||||
position: {
|
||||
referencePanel: "Solutions",
|
||||
direction: "within",
|
||||
},
|
||||
inactive: true,
|
||||
},
|
||||
{
|
||||
id: "Details",
|
||||
component: "Details",
|
||||
tabComponent: "Details",
|
||||
params: {
|
||||
icon: CircleCheckBigIcon,
|
||||
content: Details,
|
||||
title: t("Details"),
|
||||
autoAdd: false,
|
||||
},
|
||||
},
|
||||
{
|
||||
id: "Code",
|
||||
component: "Code",
|
||||
tabComponent: "Code",
|
||||
params: {
|
||||
icon: SquarePenIcon,
|
||||
content: CodeWithToggle, // 替换为带切换功能的容器
|
||||
title: t("Code"),
|
||||
},
|
||||
position: {
|
||||
referencePanel: "Submissions",
|
||||
direction: "right",
|
||||
},
|
||||
},
|
||||
{
|
||||
id: "Testcase",
|
||||
component: "Testcase",
|
||||
tabComponent: "Testcase",
|
||||
params: {
|
||||
icon: SquareCheckIcon,
|
||||
content: Testcase,
|
||||
title: t("Testcase"),
|
||||
},
|
||||
position: {
|
||||
referencePanel: "Code",
|
||||
direction: "below",
|
||||
},
|
||||
},
|
||||
{
|
||||
id: "Bot",
|
||||
component: "Bot",
|
||||
tabComponent: "Bot",
|
||||
params: {
|
||||
icon: BotIcon,
|
||||
content: Bot,
|
||||
title: t("Bot"),
|
||||
autoAdd: false,
|
||||
},
|
||||
position: {
|
||||
direction: "right",
|
||||
},
|
||||
},
|
||||
]}
|
||||
/>
|
||||
<Dockview
|
||||
key={key}
|
||||
storageKey="dockview:problem"
|
||||
onApiReady={setApi}
|
||||
options={[
|
||||
{
|
||||
id: "Description",
|
||||
component: "Description",
|
||||
tabComponent: "Description",
|
||||
params: {
|
||||
icon: FileTextIcon,
|
||||
content: Description,
|
||||
title: t("Description"),
|
||||
},
|
||||
},
|
||||
{
|
||||
id: "Solutions",
|
||||
component: "Solutions",
|
||||
tabComponent: "Solutions",
|
||||
params: {
|
||||
icon: FlaskConicalIcon,
|
||||
content: Solutions,
|
||||
title: t("Solutions"),
|
||||
},
|
||||
position: {
|
||||
referencePanel: "Description",
|
||||
direction: "within",
|
||||
},
|
||||
inactive: true,
|
||||
},
|
||||
{
|
||||
id: "Submissions",
|
||||
component: "Submissions",
|
||||
tabComponent: "Submissions",
|
||||
params: {
|
||||
icon: CircleCheckBigIcon,
|
||||
content: Submissions,
|
||||
title: t("Submissions"),
|
||||
},
|
||||
position: {
|
||||
referencePanel: "Solutions",
|
||||
direction: "within",
|
||||
},
|
||||
inactive: true,
|
||||
},
|
||||
{
|
||||
id: "Details",
|
||||
component: "Details",
|
||||
tabComponent: "Details",
|
||||
params: {
|
||||
icon: CircleCheckBigIcon,
|
||||
content: Details,
|
||||
title: t("Details"),
|
||||
autoAdd: false,
|
||||
},
|
||||
},
|
||||
{
|
||||
id: "Code",
|
||||
component: "Code",
|
||||
tabComponent: "Code",
|
||||
params: {
|
||||
icon: SquarePenIcon,
|
||||
content: Code,
|
||||
title: t("Code"),
|
||||
},
|
||||
position: {
|
||||
referencePanel: "Submissions",
|
||||
direction: "right",
|
||||
},
|
||||
},
|
||||
{
|
||||
id: "Testcase",
|
||||
component: "Testcase",
|
||||
tabComponent: "Testcase",
|
||||
params: {
|
||||
icon: SquareCheckIcon,
|
||||
content: Testcase,
|
||||
title: t("Testcase"),
|
||||
},
|
||||
position: {
|
||||
referencePanel: "Code",
|
||||
direction: "below",
|
||||
},
|
||||
},
|
||||
{
|
||||
id: "Bot",
|
||||
component: "Bot",
|
||||
tabComponent: "Bot",
|
||||
params: {
|
||||
icon: BotIcon,
|
||||
content: Bot,
|
||||
title: t("Bot"),
|
||||
autoAdd: false,
|
||||
},
|
||||
position: {
|
||||
direction: "right",
|
||||
},
|
||||
},
|
||||
]}
|
||||
/>
|
||||
);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user