File size: 2,746 Bytes
bd6511e
 
 
 
 
 
 
 
 
 
 
cb7c9ef
bd6511e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"use client";

import useSWR from 'swr';
import { Button } from "@nextui-org/react";
import { IconSpinner } from '@/app/components/ui/icons';
import Header from "@/app/components/header";
import Main from "@/app/components/ui/main-container";

// Define the API endpoint
const healthcheck_api = process.env.NEXT_PUBLIC_HEALTHCHECK_API;

const StatusPage = () => {
  // Use SWR hook to fetch data with caching and revalidation
  const { data, error, isValidating, mutate } = useSWR(healthcheck_api, async (url) => {
    try {
      // Fetch the data
      const response = await fetch(url);
      if (!response.ok) {
        throw new Error(response.statusText || 'Unknown Error');
      }
      const data = await response.json();
      return data;
    } catch (error: any) {
      console.error('Error fetching Backend API Status:', error.message);
      throw error;
    }
  }, {
    revalidateOnFocus: true, // Revalidate when the window gains focus
    revalidateIfStale: true, // Revalidate if the data is stale
    refreshInterval: 60000, // Revalidate every 60 seconds
  });
  if (error) {
    console.error('[status] Error fetching Backend API Status:', error.message);
  }

  const apiStatus = error ? '❌' : 'βœ…';
  const apiResponse = error ? '❌' : JSON.stringify(data, null, 2);

  const checkApiStatus = async () => {
    try {
      // Invalidate the cache and trigger a revalidation
      mutate();
    } catch (error: any) {
      console.error('Error fetching Backend API Status:', error.message);
    }
  };

  return (
    <Main>
      <Header />
      <div className="rounded-xl shadow-xl p-4 mb-8 max-w-5xl w-full">
        <div className="max-w-2xl space-y-2 p-4">
          <h1 className="text-xl font-bold">Backend API Status</h1>
          <p>
            <span className="font-bold">Status: </span>
            <span>{isValidating ? (
              <IconSpinner className="inline ml-2 animate-spin" />
            ) : apiStatus}</span>
          </p>
          <p><span className="font-bold">Response Data: </span>{isValidating ? (
            <IconSpinner className="inline ml-2 animate-spin" />
          ) : apiResponse}</p>
          <Button
            onClick={checkApiStatus}
            disabled={isValidating}  // Disable the button when isValidating is true
            className="flex text-center items-center text-l disabled:bg-orange-400 bg-blue-500 text-white px-6 py-3 rounded-md font-bold transition duration-300 ease-in-out transform hover:scale-105"
          >
            {isValidating ? (
              <IconSpinner className="mr-2 animate-spin" />
            ) : null}
            Refresh Status
          </Button>
        </div>
      </div>
    </Main>
  );
};

export default StatusPage;