File size: 2,173 Bytes
81e0b0c
 
 
a86c1f9
 
81e0b0c
 
 
 
 
 
 
 
 
 
a86c1f9
81e0b0c
 
a86c1f9
 
81e0b0c
a86c1f9
81e0b0c
a86c1f9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
81e0b0c
 
 
 
 
 
a86c1f9
 
81e0b0c
a86c1f9
 
81e0b0c
a86c1f9
81e0b0c
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import { useState, useRef, useEffect } from "react";

// Simulation time in milliseconds for pre-calculated documents
const SIMULATION_DURATION = 8000; // 8 secondes au total
const STEP_DURATION = SIMULATION_DURATION / 5; // Durée de chaque étape

// 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);

  // Effet pour démarrer la simulation si shouldStart est true
  useEffect(() => {
    if (!shouldStart || hasInitializedRef.current) return;

    // Marquer comme initialisé
    hasInitializedRef.current = true;
    console.log("Simulation starting with shouldStart =", shouldStart);

    // Programmer des timeouts séquentiels pour chaque étape
    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 () => {
      // Nettoyer tous les timeouts lors du démontage
      timeoutsRef.current.forEach(clearTimeout);
    };
  }, [shouldStart, onComplete]);

  return {
    startingMessageIndex,
    evaluationComplete,
    currentMessage: STARTING_MESSAGES[startingMessageIndex],
  };
};