File size: 3,950 Bytes
5a81b95
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/**
 * useBackendData - Hook for fetching real data from backend endpoints
 * Provides auto-refresh and caching capabilities
 */

import { useState, useEffect, useCallback } from 'react';
import { API_URL } from '@/config/api';

export interface BackendDataOptions {
  endpoint: string;
  refreshInterval?: number; // ms, 0 = no auto-refresh
  enabled?: boolean;
}

export interface BackendDataResult<T> {
  data: T | null;
  isLoading: boolean;
  error: Error | null;
  refresh: () => Promise<void>;
  lastUpdated: Date | null;
}

export function useBackendData<T = unknown>(
  options: BackendDataOptions
): BackendDataResult<T> {
  const { endpoint, refreshInterval = 30000, enabled = true } = options;
  const [data, setData] = useState<T | null>(null);
  const [isLoading, setIsLoading] = useState(false);
  const [error, setError] = useState<Error | null>(null);
  const [lastUpdated, setLastUpdated] = useState<Date | null>(null);

  const fetchData = useCallback(async () => {
    if (!enabled) return;

    setIsLoading(true);
    setError(null);

    try {
      const response = await fetch(`${API_URL}${endpoint}`);
      if (!response.ok) {
        throw new Error(`HTTP ${response.status}: ${response.statusText}`);
      }
      const result = await response.json();
      setData(result);
      setLastUpdated(new Date());
    } catch (err) {
      setError(err instanceof Error ? err : new Error(String(err)));
    } finally {
      setIsLoading(false);
    }
  }, [endpoint, enabled]);

  useEffect(() => {
    fetchData();

    if (refreshInterval > 0 && enabled) {
      const interval = setInterval(fetchData, refreshInterval);
      return () => clearInterval(interval);
    }
  }, [fetchData, refreshInterval, enabled]);

  return {
    data,
    isLoading,
    error,
    refresh: fetchData,
    lastUpdated
  };
}

// Specific hooks for common data types

export interface HealthData {
  status: string;
  timestamp: string;
  services: Record<string, { healthy: boolean; latency?: number }>;
}

export function useHealthData(refreshInterval = 10000) {
  return useBackendData<HealthData>({
    endpoint: '/health',
    refreshInterval
  });
}

export interface GraphStats {
  nodes: number;
  relationships: number;
  labels: string[];
}

export function useGraphStats(refreshInterval = 30000) {
  return useBackendData<GraphStats>({
    endpoint: '/api/graph/stats',
    refreshInterval
  });
}

export interface MCPTool {
  name: string;
  description: string;
  inputSchema: Record<string, unknown>;
}

export function useMCPTools(refreshInterval = 60000) {
  return useBackendData<{ tools: MCPTool[] }>({
    endpoint: '/api/mcp/tools',
    refreshInterval
  });
}

export interface HyperEvent {
  id: string;
  timestamp: string;
  level: string;
  category: string;
  message: string;
  data?: Record<string, unknown>;
}

export function useHyperEvents(limit = 50, refreshInterval = 5000) {
  return useBackendData<HyperEvent[]>({
    endpoint: `/api/hyper/events?limit=${limit}`,
    refreshInterval
  });
}

export interface HealingStatus {
  healthy: boolean;
  services: Record<string, boolean>;
  lastCheck: string;
}

export function useHealingStatus(refreshInterval = 15000) {
  return useBackendData<HealingStatus>({
    endpoint: '/api/healing/status',
    refreshInterval
  });
}

export interface KnowledgeStats {
  totalPatterns: number;
  categories: Record<string, number>;
  sources: Record<string, number>;
}

export function useKnowledgeStats(refreshInterval = 60000) {
  return useBackendData<KnowledgeStats>({
    endpoint: '/api/healing/knowledge/stats',
    refreshInterval
  });
}

export interface IngestionStatus {
  running: boolean;
  lastRun: string;
  stats: Record<string, number>;
}

export function useIngestionStatus(refreshInterval = 30000) {
  return useBackendData<IngestionStatus>({
    endpoint: '/api/ingestion/status',
    refreshInterval
  });
}

export default useBackendData;