diff --git a/services/frontend/src/pages/CompetitionSession/modules/TaskSolution/components/SolutionHistorySheet/index.tsx b/services/frontend/src/pages/CompetitionSession/modules/TaskSolution/components/SolutionHistorySheet/index.tsx index 83d8cda..4a21731 100644 --- a/services/frontend/src/pages/CompetitionSession/modules/TaskSolution/components/SolutionHistorySheet/index.tsx +++ b/services/frontend/src/pages/CompetitionSession/modules/TaskSolution/components/SolutionHistorySheet/index.tsx @@ -1,9 +1,9 @@ import React from 'react'; import { Sheet, SheetClose, SheetContent, SheetHeader, SheetTitle } from "@/components/ui/sheet"; import { Button } from "@/components/ui/button"; -import { X, Check } from "lucide-react"; +import { X } from "lucide-react"; import SolutionStatus from '../SolutionStatus'; -import { Solution } from '@/shared/types/task'; +import { Solution, TaskType } from '@/shared/types/task'; interface SolutionHistorySheetProps { isOpen: boolean; @@ -11,7 +11,6 @@ interface SolutionHistorySheetProps { solutions: Solution[]; maxPoints: number; onSolutionSelect: (solution: Solution) => void; - currentSolutionId?: string | null; } const SolutionHistorySheet: React.FC = ({ @@ -19,10 +18,8 @@ const SolutionHistorySheet: React.FC = ({ onOpenChange, solutions, maxPoints, - onSolutionSelect, - currentSolutionId + onSolutionSelect }) => { - return ( @@ -39,17 +36,15 @@ const SolutionHistorySheet: React.FC = ({
{solutions.length > 0 ? ( - solutions.map((solution) => ( + solutions.map((solution, index) => (
onSolutionSelect(solution)} + key={solution.id || index} + className="w-full cursor-pointer transition-transform hover:scale-[1.01]" + onClick={() => { + onSolutionSelect(solution); + onOpenChange(false); + }} > - {solution.id === currentSolutionId && ( -
- -
- )}
)) @@ -64,4 +59,4 @@ const SolutionHistorySheet: React.FC = ({ ); }; -export default SolutionHistorySheet; \ No newline at end of file +export default SolutionHistorySheet; diff --git a/services/frontend/src/pages/CompetitionSession/modules/TaskSolution/index.tsx b/services/frontend/src/pages/CompetitionSession/modules/TaskSolution/index.tsx index 06cebdd..6c7899a 100644 --- a/services/frontend/src/pages/CompetitionSession/modules/TaskSolution/index.tsx +++ b/services/frontend/src/pages/CompetitionSession/modules/TaskSolution/index.tsx @@ -1,4 +1,4 @@ -import React, { useState, useRef, useEffect } from 'react'; +import React, { useState, useRef } from 'react'; import { useParams } from 'react-router-dom'; import { Task, TaskType, Solution } from '@/shared/types/task'; import { useQuery } from '@tanstack/react-query'; @@ -30,9 +30,7 @@ const TaskSolution: React.FC = ({ const fileInputRef = useRef(null); const [isHistoryOpen, setIsHistoryOpen] = useState(false); const [selectedSolutionUrl, setSelectedSolutionUrl] = useState(null); - const [currentSolution, setCurrentSolution] = useState(null); const { id: competitionId } = useParams<{ id: string }>(); - const taskIdRef = useRef(null); const solutionsQuery = useQuery({ queryKey: ['solutionHistory', competitionId, task.id], @@ -41,83 +39,45 @@ const TaskSolution: React.FC = ({ }); const solutionHistory = solutionsQuery.data || []; - // Handle task changes - useEffect(() => { - if (taskIdRef.current !== task.id) { - setCurrentSolution(null); - setSelectedSolutionUrl(null); - setAnswer(""); - setSelectedFile(null); - taskIdRef.current = task.id; - - // Wait for the query to complete - if (!solutionsQuery.isLoading && solutionHistory.length > 0) { - // Get the most recent solution (last in the array) - const latestSolution = solutionHistory[solutionHistory.length - 1]; - setCurrentSolution(latestSolution); - } - } - }, [task.id, solutionHistory, solutionsQuery.isLoading, setAnswer, setSelectedFile]); - - // Refresh current solution when the solution history changes (after a new submission) - useEffect(() => { - if (!solutionsQuery.isLoading && solutionHistory.length > 0) { - // If we don't have a current solution or there's a new submission - // (which would be the last item in the array) - if (!currentSolution || - currentSolution.id !== solutionHistory[solutionHistory.length - 1].id) { - // Set to the latest solution (last in the array) - setCurrentSolution(solutionHistory[solutionHistory.length - 1]); - } - } - }, [solutionHistory, currentSolution, solutionsQuery.isLoading]); - - // Load solution content when current solution changes - useEffect(() => { - const loadSolutionContent = async () => { - if (!currentSolution || !currentSolution.content) return; - - try { - if (task.type === TaskType.FILE) { - setSelectedFile(null); - setSelectedSolutionUrl(currentSolution.content); - } else { - const response = await fetch(currentSolution.content); - if (!response.ok) { - throw new Error(`Failed to fetch solution content: ${response.status}`); - } - const text = await response.text(); - setAnswer(text); - } - } catch (error) { - console.error('Error loading solution content:', error); - } - }; - - loadSolutionContent(); - }, [currentSolution, task.type, setAnswer, setSelectedFile]); const handleOpenHistory = () => { setIsHistoryOpen(true); }; - const handleSolutionSelect = (solution: Solution) => { - setCurrentSolution(solution); - setIsHistoryOpen(false); + const latestSolution = solutionHistory && solutionHistory.length > 0 ? solutionHistory[0] : null; + + const handleSolutionSelect = async (solution: Solution) => { + if (!solution.content) return; + + try { + if (task.type === TaskType.FILE) { + // For file tasks, just store the URL + setSelectedFile(null); // Clear any selected file first + setSelectedSolutionUrl(solution.content); + } else { + // For INPUT and CODE tasks, fetch the content and set as answer + const response = await fetch(solution.content); + if (!response.ok) { + throw new Error(`Failed to fetch solution content: ${response.status}`); + } + const text = await response.text(); + setAnswer(text); + } + } catch (error) { + console.error('Error loading solution content:', error); + } }; + // Function to clear the existing file URL const handleClearExistingFile = () => { setSelectedSolutionUrl(null); }; - const handleSubmitWrapper = () => { - onSubmit(); - }; return (
- {currentSolution ? ( - + {latestSolution ? ( + ) : (
Решение еще не отправлено @@ -125,10 +85,7 @@ const TaskSolution: React.FC = ({ )} {task.type === TaskType.INPUT && ( - + )} {task.type === TaskType.FILE && ( @@ -142,14 +99,11 @@ const TaskSolution: React.FC = ({ )} {task.type === TaskType.CODE && ( - + )} @@ -159,10 +113,9 @@ const TaskSolution: React.FC = ({ solutions={solutionHistory} maxPoints={task.points} onSolutionSelect={handleSolutionSelect} - currentSolutionId={currentSolution?.id} />
); }; -export default TaskSolution; \ No newline at end of file +export default TaskSolution;