File size: 4,498 Bytes
ebdfd67
81e0b0c
ebdfd67
 
81e0b0c
83106dd
 
373381c
81e0b0c
ebdfd67
 
 
 
 
 
373381c
 
83106dd
 
ebdfd67
3964afa
373381c
 
 
ebdfd67
 
 
3964afa
ebdfd67
 
 
 
 
373381c
ebdfd67
3964afa
373381c
 
ebdfd67
 
373381c
ebdfd67
3964afa
ebdfd67
 
373381c
ebdfd67
373381c
3964afa
373381c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ebdfd67
373381c
 
ebdfd67
 
 
 
 
373381c
ebdfd67
 
3964afa
373381c
3964afa
373381c
 
 
 
3964afa
373381c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3964afa
373381c
 
 
 
3964afa
373381c
 
 
ebdfd67
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
373381c
81e0b0c
ebdfd67
 
 
83106dd
ebdfd67
 
83106dd
ebdfd67
 
81e0b0c
ebdfd67
 
 
 
 
 
 
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import React, { useState, useEffect } from "react";
import { Box, CircularProgress } from "@mui/material";
import { useSearchParams, Navigate } from "react-router-dom";
import Intro from "../components/Intro";
import Display from "../components/Evaluation/Display";
import { useThemeMode } from "../hooks/useThemeMode";
import getTheme from "../config/theme";
import API_CONFIG from "../config/api";
import ErrorDisplay from "../components/common/ErrorDisplay";

function EvaluationDisplayPage() {
  const [searchParams] = useSearchParams();
  const sessionId = searchParams.get("session");
  const [isValidSession, setIsValidSession] = useState(true);
  const [isLoading, setIsLoading] = useState(true);
  const [evaluationResults, setEvaluationResults] = useState(null);
  const [error, setError] = useState(null);
  const { mode } = useThemeMode();
  const theme = getTheme(mode);

  // List of base documents that should not be deleted
  const baseDocuments = ["the-bitter-lesson", "hurricane-faq", "pokemon-guide"];
  const isBaseDocument = baseDocuments.includes(sessionId);

  useEffect(() => {
    if (!sessionId) {
      console.log(
        "Session ID missing for displaying results, redirecting to home"
      );
      setIsValidSession(false);
      return;
    }

    const fetchEvaluationResults = async () => {
      try {
        // First check if the session exists
        const sessionCheckResponse = await fetch(
          `${API_CONFIG.BASE_URL}/benchmark-questions/${sessionId}`
        );

        if (!sessionCheckResponse.ok) {
          console.error(
            `Invalid session or server error: ${sessionCheckResponse.status}`
          );
          setIsValidSession(false);
          return;
        }

        // Retrieve evaluation results
        const evalResponse = await fetch(
          `${API_CONFIG.BASE_URL}/evaluation-results/${sessionId}`
        );

        if (!evalResponse.ok) {
          setError(`Failed to fetch results: ${evalResponse.status}`);
          setIsLoading(false);
          return;
        }

        const data = await evalResponse.json();

        if (!data.success) {
          setError(data.message || "Failed to fetch evaluation results");
          setIsLoading(false);
          return;
        }

        setEvaluationResults(data.results);
      } catch (error) {
        console.error("Error fetching evaluation results:", error);
        setError(error.message);
      } finally {
        setIsLoading(false);
      }
    };

    fetchEvaluationResults();
  }, [sessionId]);

  // Effect to clean up the session folder after displaying results
  useEffect(() => {
    // Do not clean up if it's a base document or if results are not yet loaded
    if (isBaseDocument || isLoading || !evaluationResults) {
      return;
    }

    // Function to clean up the session folder
    const cleanupSession = async () => {
      try {
        const response = await fetch(
          `${API_CONFIG.BASE_URL}/cleanup-session/${sessionId}`,
          {
            method: "DELETE",
          }
        );

        if (response.ok) {
          console.log(`Session ${sessionId} cleaned up successfully`);
        } else {
          console.warn(`Failed to clean up session ${sessionId}`);
        }
      } catch (error) {
        console.error("Error cleaning up session:", error);
      }
    };

    // Call the function after a delay to ensure the user had time to see the results
    const cleanupTimeout = setTimeout(() => {
      cleanupSession();
    }, 2000);

    // Clean up the timeout if component is unmounted
    return () => clearTimeout(cleanupTimeout);
  }, [sessionId, isBaseDocument, isLoading, evaluationResults]);

  if (!isValidSession) {
    return <Navigate to="/" />;
  }

  return (
    <>
      <Intro />
      {isLoading ? (
        <Box
          sx={{
            display: "flex",
            justifyContent: "center",
            alignItems: "center",
            mt: 8,
            mb: 8,
          }}
        >
          <CircularProgress size={60} />
        </Box>
      ) : error ? (
        <ErrorDisplay error={error} title="Error" />
      ) : (
        <Box
          sx={{
            border: `1px solid ${theme.palette.divider}`,
            borderRadius: 2,
            p: 4,
            bgcolor: "background.paper",
          }}
        >
          <Display sessionId={sessionId} results={evaluationResults} />
        </Box>
      )}
    </>
  );
}

export default EvaluationDisplayPage;