All files / frontend/src/store websdrStore.ts

0% Statements 0/48
100% Branches 1/1
100% Functions 1/1
0% Lines 0/48

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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                                                                                                                                                               
/**
 * WebSDR Store
 * 
 * Manages WebSDR receivers state and operations
 */
 
import { create } from 'zustand';
import type { WebSDRConfig, WebSDRHealthStatus } from '@/services/api/types';
import { webSDRService } from '@/services/api';
 
interface WebSDRStore {
    websdrs: WebSDRConfig[];
    healthStatus: Record<number, WebSDRHealthStatus>;
    isLoading: boolean;
    error: string | null;
    lastHealthCheck: Date | null;
    
    fetchWebSDRs: () => Promise<void>;
    checkHealth: () => Promise<void>;
    getActiveWebSDRs: () => WebSDRConfig[];
    getWebSDRById: (id: number) => WebSDRConfig | undefined;
    isWebSDROnline: (id: number) => boolean;
    
    refreshAll: () => Promise<void>;
}
 
export const useWebSDRStore = create<WebSDRStore>((set, get) => ({
    websdrs: [],
    healthStatus: {},
    isLoading: false,
    error: null,
    lastHealthCheck: null,
 
    fetchWebSDRs: async () => {
        set({ isLoading: true, error: null });
        try {
            const websdrs = await webSDRService.getWebSDRs();
            set({ websdrs, isLoading: false });
        } catch (error) {
            const errorMessage = error instanceof Error ? error.message : 'Failed to fetch WebSDRs';
            set({ error: errorMessage, isLoading: false });
            console.error('WebSDR fetch error:', error);
        }
    },
 
    checkHealth: async () => {
        try {
            const healthStatus = await webSDRService.checkWebSDRHealth();
            set({ 
                healthStatus, 
                lastHealthCheck: new Date(),
                error: null,
            });
        } catch (error) {
            console.error('Health check error:', error);
            // Don't set error for health checks, just log it
        }
    },
 
    getActiveWebSDRs: () => {
        return get().websdrs.filter(w => w.is_active);
    },
 
    getWebSDRById: (id: number) => {
        return get().websdrs.find(w => w.id === id);
    },
 
    isWebSDROnline: (id: number) => {
        const health = get().healthStatus[id];
        return health?.status === 'online';
    },
 
    refreshAll: async () => {
        await Promise.all([
            get().fetchWebSDRs(),
            get().checkHealth(),
        ]);
    },
}));