All files / frontend/src/store localizationStore.ts

0% Statements 0/55
0% Branches 0/1
0% Functions 0/1
0% Lines 0/55

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 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97                                                                                                                                                                                                 
import { create } from 'zustand';
import type { LocalizationResult } from '@/services/api/types';
import { inferenceService } from '@/services/api';
import type { PredictionRequest, PredictionResponse } from '@/services/api/inference';
 
interface LocalizationState {
    // Data
    recentLocalizations: LocalizationResult[];
    currentPrediction: PredictionResponse | null;
 
    // UI State
    isLoading: boolean;
    isPredicting: boolean;
    error: string | null;
    selectedResultId: string | null;
 
    // Actions
    setLoading: (loading: boolean) => void;
    setPredicting: (predicting: boolean) => void;
    setError: (error: string | null) => void;
    setSelectedResult: (id: string | null) => void;
 
    // API Actions
    fetchRecentLocalizations: (limit?: number) => Promise<void>;
    predictLocalization: (request: PredictionRequest) => Promise<PredictionResponse>;
    clearCurrentPrediction: () => void;
    refreshData: () => Promise<void>;
}
 
export const useLocalizationStore = create<LocalizationState>((set, get) => ({
    // Initial state
    recentLocalizations: [],
    currentPrediction: null,
    isLoading: false,
    isPredicting: false,
    error: null,
    selectedResultId: null,
 
    // Basic setters
    setLoading: (loading) => set({ isLoading: loading }),
    setPredicting: (predicting) => set({ isPredicting: predicting }),
    setError: (error) => set({ error }),
    setSelectedResult: (id) => set({ selectedResultId: id }),
 
    // API Actions
    fetchRecentLocalizations: async (limit = 10) => {
        set({ isLoading: true, error: null });
 
        try {
            const localizations = await inferenceService.getRecentLocalizations(limit);
 
            set({
                recentLocalizations: localizations,
                error: null,
            });
        } catch (error) {
            const errorMessage = error instanceof Error ? error.message : 'Failed to fetch localizations';
            set({ error: errorMessage });
            console.error('Failed to fetch recent localizations:', error);
        } finally {
            set({ isLoading: false });
        }
    },
 
    predictLocalization: async (request: PredictionRequest) => {
        set({ isPredicting: true, error: null });
 
        try {
            const prediction = await inferenceService.predictLocalization(request);
 
            set({
                currentPrediction: prediction,
                error: null,
            });
 
            // Refresh recent localizations to include the new one
            await get().fetchRecentLocalizations();
 
            return prediction;
        } catch (error) {
            const errorMessage = error instanceof Error ? error.message : 'Prediction failed';
            set({ error: errorMessage });
            console.error('Prediction failed:', error);
            throw error;
        } finally {
            set({ isPredicting: false });
        }
    },
 
    clearCurrentPrediction: () => {
        set({ currentPrediction: null });
    },
 
    refreshData: async () => {
        await get().fetchRecentLocalizations();
    },
}));