import React, { useState, useEffect } from 'react';
function PredefinedPuzzlePage() {
// For puzzle index and puzzle data
const [puzzleIndex, setPuzzleIndex] = useState(0);
const [puzzleText, setPuzzleText] = useState("");
const [expectedSolution, setExpectedSolution] = useState(null);
// sysContent can be editted, default using Example.txt
const [sysContent, setSysContent] = useState("");
// Interaction results
const [generatedCode, setGeneratedCode] = useState("");
const [executionSuccess, setExecutionSuccess] = useState(null);
const [attempts, setAttempts] = useState(0);
const [isSolving, setIsSolving] = useState(false);
const [problematicConstraints, setProblematicConstraints] = useState("");
// UI state
const [currentStep, setCurrentStep] = useState(1);
const [expandedSections, setExpandedSections] = useState({
puzzle: true,
sysContent: false,
result: false
});
// Frontend fetch sysContent in default
useEffect(() => {
fetch(`/default_sys_content`)
.then(res => res.json())
.then(data => {
if(data.success) {
setSysContent(data.sysContent);
}
})
.catch(e => console.error(e));
}, []);
// When puzzleIndex changing,auto get puzzle
useEffect(() => {
fetch(`/get_puzzle?index=${puzzleIndex}`)
.then(res => res.json())
.then(data => {
if(data.success) {
setPuzzleText(data.puzzle);
setExpectedSolution(data.expected_solution);
} else {
console.error("Failed to fetch puzzle", data.error);
setPuzzleText("");
setExpectedSolution(null);
}
})
.catch(e => console.error(e));
}, [puzzleIndex]);
const handleSolve = () => {
if(!puzzleText || !expectedSolution) {
alert("puzzle or expectedSolution incomplete");
return;
}
const payload = {
index: puzzleIndex,
puzzle: puzzleText,
expected_solution: expectedSolution,
sys_content: sysContent,
problematic_constraints: problematicConstraints
};
setIsSolving(true);
setCurrentStep(3);
setExpandedSections({...expandedSections, result: true});
fetch(`/solve`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(payload)
})
.then(res => res.json())
.then(data => {
if(!data.success) {
alert("Backend error: " + data.error);
return;
}
const result = data.result;
setGeneratedCode(result.generatedCode || "");
setExecutionSuccess(result.success);
setAttempts(result.attempts || 0);
setProblematicConstraints(result.problematicConstraints || "");
})
.catch(e => console.error(e))
.finally(() => {
setIsSolving(false);
});
};
const toggleSection = (section) => {
setExpandedSections({
...expandedSections,
[section]: !expandedSections[section]
});
};
const nextStep = () => {
if (currentStep < 3) {
setCurrentStep(currentStep + 1);
if (currentStep === 1) {
setExpandedSections({puzzle: false, sysContent: true, result: false});
} else if (currentStep === 2) {
setExpandedSections({puzzle: false, sysContent: false, result: true});
}
}
};
const prevStep = () => {
if (currentStep > 1) {
setCurrentStep(currentStep - 1);
if (currentStep === 2) {
setExpandedSections({puzzle: true, sysContent: false, result: false});
} else if (currentStep === 3) {
setExpandedSections({puzzle: false, sysContent: true, result: false});
}
}
};
return (
<>
{/* Progress Steps */}
= 1 ? 'active' : ''}`}>
1
Select Puzzle
= 2 ? 'active' : ''}`}>
2
Configure System
= 3 ? 'active' : ''}`}>
3
Solve & Results
{/* Step 1: Puzzle Selection */}
toggleSection('puzzle')}>
📋 Puzzle Selection
{expandedSections.puzzle ? '▼' : '▶'}
{expandedSections.puzzle && (
📄 Puzzle Text
{puzzleText || "Loading puzzle..."}
🎯 Expected Solution
{expectedSolution ? (
{JSON.stringify(expectedSolution, null, 2)}
) : (
"Loading solution..."
)}
)}
{/* Step 2: System Configuration */}
toggleSection('sysContent')}>
⚙️ System Configuration
{expandedSections.sysContent ? '▼' : '▶'}
{expandedSections.sysContent && (
📝 System Content
Edit the system prompt that will guide the AI in solving the puzzle:
)}
{/* Step 3: Solve & Results */}
toggleSection('result')}>
🚀 Solve & Results
{expandedSections.result ? '▼' : '▶'}
{expandedSections.result && (
Status:
{executionSuccess === null ? "⏳ Pending" : executionSuccess ? "✅ Success" : "❌ Failed"}
Attempts:
{attempts}
{problematicConstraints && (
)}
{generatedCode && (
)}
)}
{/* Navigation */}
>
);
}
export default PredefinedPuzzlePage;