diff --git a/src/components/creater/edit-testcase-panel.tsx b/src/components/creater/edit-testcase-panel.tsx index 81ec644..7c83cd5 100644 --- a/src/components/creater/edit-testcase-panel.tsx +++ b/src/components/creater/edit-testcase-panel.tsx @@ -1,180 +1,240 @@ "use client"; -import { useState, useEffect } from "react"; -import { Label } from "@/components/ui/label"; -import { Input } from "@/components/ui/input"; -import { Button } from "@/components/ui/button"; -import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card"; -import { getProblemData } from "@/app/actions/getProblem"; +import {useState, useEffect} from "react"; +import {generateAITestcase} from "@/app/actions/ai-testcase"; +import {Label} from "@/components/ui/label"; +import {Input} from "@/components/ui/input"; +import {Button} from "@/components/ui/button"; +import {Card, CardContent, CardHeader, CardTitle} from "@/components/ui/card"; +import {getProblemData} from "@/app/actions/getProblem"; export default function EditTestcasePanel({ - problemId, + problemId, }: { - problemId: string; + problemId: string; }) { - const [testcases, setTestcases] = useState< - Array<{ - id: string; - expectedOutput: string; - inputs: Array<{ - name: string; - value: string; - }>; - }> - >([]); + const [testcases, setTestcases] = useState< + Array<{ + id: string; + expectedOutput: string; + inputs: Array<{ + name: string; + value: string; + }>; + }> + >([]); - useEffect(() => { - async function fetchData() { - try { - const problemData = await getProblemData(problemId); - if (problemData && problemData.testcases) { - setTestcases(problemData.testcases); - } else { - setTestcases([]); + useEffect(() => { + async function fetchData() { + try { + const problemData = await getProblemData(problemId); + if (problemData && problemData.testcases) { + setTestcases(problemData.testcases); + } else { + setTestcases([]); + } + } catch (error) { + console.error("加载测试用例失败:", error); + setTestcases([]); + } + } + + fetchData(); + }, [problemId]); + + const handleAddTestcase = () => { + setTestcases([ + ...testcases, + { + id: `new-${Date.now()}`, + expectedOutput: "", + inputs: [{name: "input1", value: ""}], + }, + ]); + }; + + const [isGenerating, setIsGenerating] = useState(false); + + const handleAITestcase = async () => { + setIsGenerating(true); + try { + const AIOutputParsed = await generateAITestcase({problemId: problemId}); + setTestcases([ + ...testcases, + { + id: `new-${Date.now()}`, + expectedOutput: AIOutputParsed.expectedOutput, + inputs: AIOutputParsed.inputs + } + ]) + window.scrollTo({ + top: document.body.scrollHeight, + behavior: 'smooth', + }); + } catch (error) { + console.error(error) + } finally { + setIsGenerating(false); } - } catch (error) { - console.error("加载测试用例失败:", error); - setTestcases([]); - } } - fetchData(); - }, [problemId]); - const handleAddTestcase = () => { - setTestcases([ - ...testcases, - { - id: `new-${Date.now()}`, - expectedOutput: "", - inputs: [{ name: "input1", value: "" }], - }, - ]); - }; + const handleRemoveTestcase = (index: number) => { + const newTestcases = [...testcases]; + newTestcases.splice(index, 1); + setTestcases(newTestcases); + }; - const handleRemoveTestcase = (index: number) => { - const newTestcases = [...testcases]; - newTestcases.splice(index, 1); - setTestcases(newTestcases); - }; + const handleInputChange = ( + testcaseIndex: number, + inputIndex: number, + field: "name" | "value", + value: string + ) => { + const newTestcases = [...testcases]; + newTestcases[testcaseIndex].inputs[inputIndex][field] = value; + setTestcases(newTestcases); + }; - const handleInputChange = ( - testcaseIndex: number, - inputIndex: number, - field: "name" | "value", - value: string - ) => { - const newTestcases = [...testcases]; - newTestcases[testcaseIndex].inputs[inputIndex][field] = value; - setTestcases(newTestcases); - }; + const handleExpectedOutputChange = (testcaseIndex: number, value: string) => { + const newTestcases = [...testcases]; + newTestcases[testcaseIndex].expectedOutput = value; + setTestcases(newTestcases); + }; - const handleExpectedOutputChange = (testcaseIndex: number, value: string) => { - const newTestcases = [...testcases]; - newTestcases[testcaseIndex].expectedOutput = value; - setTestcases(newTestcases); - }; + const handleAddInput = (testcaseIndex: number) => { + const newTestcases = [...testcases]; + newTestcases[testcaseIndex].inputs.push({ + name: `input${newTestcases[testcaseIndex].inputs.length + 1}`, + value: "", + }); + setTestcases(newTestcases); + }; - const handleAddInput = (testcaseIndex: number) => { - const newTestcases = [...testcases]; - newTestcases[testcaseIndex].inputs.push({ - name: `input${newTestcases[testcaseIndex].inputs.length + 1}`, - value: "", - }); - setTestcases(newTestcases); - }; + const handleRemoveInput = (testcaseIndex: number, inputIndex: number) => { + const newTestcases = [...testcases]; + newTestcases[testcaseIndex].inputs.splice(inputIndex, 1); + setTestcases(newTestcases); + }; - const handleRemoveInput = (testcaseIndex: number, inputIndex: number) => { - const newTestcases = [...testcases]; - newTestcases[testcaseIndex].inputs.splice(inputIndex, 1); - setTestcases(newTestcases); - }; - - return ( - - - 测试用例 - - - -
- {testcases.map((testcase, index) => ( -
-
-

测试用例 {index + 1}

+ return ( + + + 测试用例 +
{/* space-x-1 让按钮更接近 */} + -
+
+ + +
+ {testcases.map((testcase, index) => ( +
+
+

测试用例 {index + 1}

+ +
-
- - handleExpectedOutputChange(index, e.target.value)} - placeholder="输入预期输出" - /> -
+
+ + handleExpectedOutputChange(index, e.target.value)} + placeholder="输入预期输出" + /> +
-
-
- - -
+
+
+ + +
- {testcase.inputs.map((input, inputIndex) => ( -
-
- - - handleInputChange(index, inputIndex, "name", e.target.value) - } - placeholder="输入参数名称" - /> -
-
- - - handleInputChange(index, inputIndex, "value", e.target.value) - } - placeholder="输入参数值" - /> -
- {inputIndex > 0 && ( - - )} + {testcase.inputs.map((input, inputIndex) => ( +
+
+ + + handleInputChange(index, inputIndex, "name", e.target.value) + } + placeholder="输入参数名称" + /> +
+
+ + + handleInputChange(index, inputIndex, "value", e.target.value) + } + placeholder="输入参数值" + /> +
+ {inputIndex > 0 && ( + + )} +
+ ))} +
))} -
- ))} -
-
- - ); + + + + ); }