File size: 1,871 Bytes
f555806
 
 
 
 
 
 
 
 
 
 
 
 
 
231dcea
f555806
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
231dcea
 
 
 
f555806
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
231dcea
f555806
 
 
 
 
 
 
 
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
import { useState, useEffect } from 'react';
import { apiClient } from '@/utils/api';
import useSettings from './useSettings';
import { useAuth } from '@/contexts/AuthContext';

interface HFJobStatus {
  id: string;
  status: string;
  message: string | null;
  created_at: string;
  flavor: string;
  url: string;
}

export function useHFJobStatus(hfJobId: string | null, hfJobNamespace?: string, refreshInterval = 30000) {
  const [status, setStatus] = useState<HFJobStatus | null>(null);
  const [loading, setLoading] = useState(false);
  const [error, setError] = useState<string | null>(null);
  const { settings } = useSettings();
  const { token: authToken } = useAuth();
  const token = authToken || settings.HF_TOKEN;

  useEffect(() => {
    if (!hfJobId || !token) return;

    const fetchStatus = async () => {
      setLoading(true);
      setError(null);

      try {
        const response = await apiClient.post('/api/hf-jobs', {
          action: 'checkStatus',
          token,
          jobConfig: {
            hf_job_id: hfJobId,
            hf_job_namespace: hfJobNamespace,
          },
        });

        if (response.data.status) {
          setStatus(response.data.status);
        }
      } catch (err: any) {
        setError(err.response?.data?.error || 'Failed to fetch status');
        console.error('HF Job status fetch error:', err);
      } finally {
        setLoading(false);
      }
    };

    // Initial fetch
    fetchStatus();

    // Set up periodic refresh for running jobs
    const interval = setInterval(fetchStatus, refreshInterval);

    return () => clearInterval(interval);
  }, [hfJobId, hfJobNamespace, token, refreshInterval]);

  return { status, loading, error, refetch: () => {
    if (hfJobId && token) {
      setError(null);
      // Trigger immediate refetch by setting a new effect dependency
    }
  }};
}