Merge branch 'master' of gitlab.prodcontest.ru:team-15/project

This commit is contained in:
ITQ
2025-03-03 13:08:41 +03:00
2 changed files with 41 additions and 93 deletions
@@ -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<SolutionHistorySheetProps> = ({
@@ -19,10 +18,8 @@ const SolutionHistorySheet: React.FC<SolutionHistorySheetProps> = ({
onOpenChange,
solutions,
maxPoints,
onSolutionSelect,
currentSolutionId
onSolutionSelect
}) => {
return (
<Sheet open={isOpen} onOpenChange={onOpenChange}>
<SheetContent className="w-[350px] sm:w-[450px] p-0">
@@ -39,17 +36,15 @@ const SolutionHistorySheet: React.FC<SolutionHistorySheetProps> = ({
<div className="flex flex-col mt-3 space-y-2.5 overflow-y-auto max-h-[calc(100vh-80px)] px-4 pb-4">
{solutions.length > 0 ? (
solutions.map((solution) => (
solutions.map((solution, index) => (
<div
key={solution.id}
className={`w-full cursor-pointer relative ${solution.id === currentSolutionId ? 'ring-2 ring-blue-500 rounded-lg' : ''}`}
onClick={() => 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 && (
<div className="absolute top-2 right-2 z-10 bg-blue-500 text-white rounded-full p-1">
<Check size={14} />
</div>
)}
<SolutionStatus solution={solution} maxPoints={maxPoints} />
</div>
))
@@ -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<TaskSolutionProps> = ({
const fileInputRef = useRef<HTMLInputElement>(null);
const [isHistoryOpen, setIsHistoryOpen] = useState(false);
const [selectedSolutionUrl, setSelectedSolutionUrl] = useState<string | null>(null);
const [currentSolution, setCurrentSolution] = useState<Solution | null>(null);
const { id: competitionId } = useParams<{ id: string }>();
const taskIdRef = useRef<string | null>(null);
const solutionsQuery = useQuery({
queryKey: ['solutionHistory', competitionId, task.id],
@@ -41,48 +39,24 @@ const TaskSolution: React.FC<TaskSolutionProps> = ({
});
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]);
const handleOpenHistory = () => {
setIsHistoryOpen(true);
};
// 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]);
const latestSolution = solutionHistory && solutionHistory.length > 0 ? solutionHistory[0] : null;
// Load solution content when current solution changes
useEffect(() => {
const loadSolutionContent = async () => {
if (!currentSolution || !currentSolution.content) return;
const handleSolutionSelect = async (solution: Solution) => {
if (!solution.content) return;
try {
if (task.type === TaskType.FILE) {
setSelectedFile(null);
setSelectedSolutionUrl(currentSolution.content);
// For file tasks, just store the URL
setSelectedFile(null); // Clear any selected file first
setSelectedSolutionUrl(solution.content);
} else {
const response = await fetch(currentSolution.content);
// 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}`);
}
@@ -94,30 +68,16 @@ const TaskSolution: React.FC<TaskSolutionProps> = ({
}
};
loadSolutionContent();
}, [currentSolution, task.type, setAnswer, setSelectedFile]);
const handleOpenHistory = () => {
setIsHistoryOpen(true);
};
const handleSolutionSelect = (solution: Solution) => {
setCurrentSolution(solution);
setIsHistoryOpen(false);
};
// Function to clear the existing file URL
const handleClearExistingFile = () => {
setSelectedSolutionUrl(null);
};
const handleSubmitWrapper = () => {
onSubmit();
};
return (
<div className="md:w-[500px] flex flex-col gap-4">
{currentSolution ? (
<SolutionStatus solution={currentSolution} maxPoints={task.points}/>
{latestSolution ? (
<SolutionStatus solution={latestSolution} maxPoints={task.points}/>
) : (
<div className="bg-gray-100 rounded-lg p-4 text-gray-600 font-hse-sans">
Решение еще не отправлено
@@ -125,10 +85,7 @@ const TaskSolution: React.FC<TaskSolutionProps> = ({
)}
{task.type === TaskType.INPUT && (
<InputSolution
answer={answer}
setAnswer={setAnswer}
/>
<InputSolution answer={answer} setAnswer={setAnswer} />
)}
{task.type === TaskType.FILE && (
@@ -142,14 +99,11 @@ const TaskSolution: React.FC<TaskSolutionProps> = ({
)}
{task.type === TaskType.CODE && (
<CodeSolution
answer={answer}
setAnswer={setAnswer}
/>
<CodeSolution answer={answer} setAnswer={setAnswer} />
)}
<ActionButtons
onSubmit={handleSubmitWrapper}
onSubmit={onSubmit}
onHistoryClick={handleOpenHistory}
/>
@@ -159,7 +113,6 @@ const TaskSolution: React.FC<TaskSolutionProps> = ({
solutions={solutionHistory}
maxPoints={task.points}
onSolutionSelect={handleSolutionSelect}
currentSolutionId={currentSolution?.id}
/>
</div>
);