Spaces:
Running
on
CPU Upgrade
Running
on
CPU Upgrade
| import { useState, useRef, useEffect } from "react"; | |
| // Simulation time in milliseconds for pre-calculated documents | |
| const SIMULATION_DURATION = 7000; // Duration in milliseconds | |
| const STEP_DURATION = SIMULATION_DURATION / 5; // Duration of each step | |
| // Starting messages with their timing | |
| const STARTING_MESSAGES = [ | |
| { message: "Initializing evaluation environment", step: 1, totalSteps: 5 }, | |
| { message: "Finding available model providers", step: 2, totalSteps: 5 }, | |
| { message: "Starting evaluation process", step: 3, totalSteps: 5 }, | |
| { message: "Evaluating models", step: 4, totalSteps: 5 }, | |
| { message: "Storing evaluation results", step: 5, totalSteps: 5 }, | |
| ]; | |
| export const useSimulation = (onComplete, shouldStart = false) => { | |
| const [startingMessageIndex, setStartingMessageIndex] = useState(0); | |
| const [evaluationComplete, setEvaluationComplete] = useState(false); | |
| const timeoutsRef = useRef([]); | |
| const hasInitializedRef = useRef(false); | |
| // Effect to start simulation if shouldStart is true | |
| useEffect(() => { | |
| if (!shouldStart || hasInitializedRef.current) return; | |
| // Mark as initialized | |
| hasInitializedRef.current = true; | |
| console.log("Simulation starting with shouldStart =", shouldStart); | |
| // Schedule sequential timeouts for each step | |
| for (let i = 1; i < STARTING_MESSAGES.length; i++) { | |
| const timeout = setTimeout(() => { | |
| console.log(`Setting message index to ${i}`); | |
| setStartingMessageIndex(i); | |
| }, i * STEP_DURATION); | |
| timeoutsRef.current.push(timeout); | |
| } | |
| // Programmer la fin de la simulation | |
| const completeTimeout = setTimeout(() => { | |
| console.log("Completing simulation"); | |
| setEvaluationComplete(true); | |
| if (onComplete) { | |
| onComplete(); | |
| } | |
| }, SIMULATION_DURATION); | |
| timeoutsRef.current.push(completeTimeout); | |
| return () => { | |
| // Clean up all timeouts on unmount | |
| timeoutsRef.current.forEach(clearTimeout); | |
| }; | |
| }, [shouldStart, onComplete]); | |
| return { | |
| startingMessageIndex, | |
| evaluationComplete, | |
| currentMessage: STARTING_MESSAGES[startingMessageIndex], | |
| }; | |
| }; | |