File size: 1,209 Bytes
0bfe2e3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
'use client';
import React, {
  createContext,
  useContext,
  useState,
  useEffect,
  ReactNode,
} from 'react';
import { StatusResponse } from '@aiostreams/core';

type StatusContextType = {
  status: StatusResponse | null;
  loading: boolean;
  error: string | null;
};

const StatusContext = createContext<StatusContextType>({
  status: null,
  loading: true,
  error: null,
});

export const useStatus = () => useContext(StatusContext);

const baseUrl = process.env.NEXT_PUBLIC_BACKEND_BASE_URL || '/api/v1';

export function StatusProvider({ children }: { children: ReactNode }) {
  const [status, setStatus] = useState<StatusResponse | null>(null);
  const [loading, setLoading] = useState(true);
  const [error, setError] = useState<string | null>(null);

  useEffect(() => {
    fetch(`${baseUrl}/status`)
      .then((res) => {
        if (!res.ok) throw new Error('Failed to fetch status');
        return res.json();
      })
      .then((data) => setStatus(data.data))
      .catch((err) => setError(err.message))
      .finally(() => setLoading(false));
  }, []);

  return (
    <StatusContext.Provider value={{ status, loading, error }}>
      {children}
    </StatusContext.Provider>
  );
}