All files / frontend/src/store sessionStore.ts

82.75% Statements 120/145
64.44% Branches 29/45
100% Functions 13/13
82.75% Lines 120/145

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 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 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252              1x                 1x                                                                                                           1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x   1x 19x 19x 19x 19x 19x 19x 19x 19x   18x 18x 18x 18x 18x 18x 18x 19x 5x 5x 5x 5x 19x   1x 3x 3x 3x 2x 3x 1x 1x 1x 1x 3x   1x 2x 2x 2x 1x     1x   1x 1x 1x 1x 1x 1x 1x 2x   1x 2x 2x     2x     2x 1x 1x 2x       2x   1x 1x 1x     1x     1x     1x       1x   1x 1x 1x     1x     1x     1x       1x   1x 1x 1x     1x     1x     1x       1x   1x 1x 1x 1x 1x       1x   1x 2x 2x 2x 2x       2x   1x 1x 1x     1x   1x 1x       1x   1x 2x 2x 2x   1x 2x 2x 2x   1x 1x  
 
/**
 * Session Store
 * 
 * Manages recording session state and operations
 */
 
import { create } from 'zustand';
import type {
    RecordingSession,
    RecordingSessionWithDetails,
    RecordingSessionCreate,
    SessionAnalytics,
    KnownSource,
    KnownSourceCreate,
} from '@/services/api/session';
import { sessionService } from '@/services/api';
 
interface SessionStore {
    sessions: RecordingSessionWithDetails[];
    currentSession: RecordingSessionWithDetails | null;
    knownSources: KnownSource[];
    analytics: SessionAnalytics | null;
 
    isLoading: boolean;
    error: string | null;
 
    // Pagination
    currentPage: number;
    totalSessions: number;
    perPage: number;
 
    // Filters
    statusFilter: string | null;
    approvalFilter: string | null;
 
    // Actions
    fetchSessions: (params?: {
        page?: number;
        per_page?: number;
        status?: string;
        approval_status?: string;
    }) => Promise<void>;
 
    fetchSession: (sessionId: number) => Promise<void>;
 
    createSession: (session: RecordingSessionCreate) => Promise<RecordingSession>;
 
    updateSessionStatus: (
        sessionId: number,
        status: string,
        celeryTaskId?: string
    ) => Promise<void>;
 
    approveSession: (sessionId: number) => Promise<void>;
    rejectSession: (sessionId: number) => Promise<void>;
 
    deleteSession: (sessionId: number) => Promise<void>;
 
    fetchAnalytics: () => Promise<void>;
 
    fetchKnownSources: () => Promise<void>;
    createKnownSource: (source: KnownSourceCreate) => Promise<KnownSource>;
 
    setStatusFilter: (status: string | null) => void;
    setApprovalFilter: (approval: string | null) => void;
 
    clearError: () => void;
}
 
export const useSessionStore = create<SessionStore>((set, get) => ({
    sessions: [],
    currentSession: null,
    knownSources: [],
    analytics: null,
    isLoading: false,
    error: null,
    currentPage: 1,
    totalSessions: 0,
    perPage: 20,
    statusFilter: null,
    approvalFilter: null,
 
    fetchSessions: async (params) => {
        set({ isLoading: true, error: null });
        try {
            const response = await sessionService.listSessions({
                page: params?.page || get().currentPage,
                per_page: params?.per_page || get().perPage,
                status: params?.status || get().statusFilter || undefined,
                approval_status: params?.approval_status || get().approvalFilter || undefined,
            });
 
            set({
                sessions: response.sessions,
                totalSessions: response.total,
                currentPage: response.page,
                perPage: response.per_page,
                isLoading: false,
            });
        } catch (error) {
            const errorMessage = error instanceof Error ? error.message : 'Failed to fetch sessions';
            set({ error: errorMessage, isLoading: false });
            console.error('Session fetch error:', error);
        }
    },
 
    fetchSession: async (sessionId: number) => {
        set({ isLoading: true, error: null });
        try {
            const session = await sessionService.getSession(sessionId);
            set({ currentSession: session, isLoading: false });
        } catch (error) {
            const errorMessage = error instanceof Error ? error.message : 'Failed to fetch session';
            set({ error: errorMessage, isLoading: false });
            console.error('Session fetch error:', error);
        }
    },
 
    createSession: async (session: RecordingSessionCreate) => {
        set({ isLoading: true, error: null });
        try {
            const newSession = await sessionService.createSession(session);
            set({ isLoading: false });
 
            // Refresh session list
            await get().fetchSessions();
 
            return newSession;
        } catch (error) {
            const errorMessage = error instanceof Error ? error.message : 'Failed to create session';
            set({ error: errorMessage, isLoading: false });
            console.error('Session creation error:', error);
            throw error;
        }
    },
 
    updateSessionStatus: async (sessionId: number, status: string, celeryTaskId?: string) => {
        try {
            await sessionService.updateSessionStatus(sessionId, status, celeryTaskId);
 
            // Refresh session list
            await get().fetchSessions();
 
            // Refresh current session if it's the one being updated
            if (get().currentSession?.id === sessionId) {
                await get().fetchSession(sessionId);
            }
        } catch (error) {
            console.error('Session status update error:', error);
            throw error;
        }
    },
 
    approveSession: async (sessionId: number) => {
        try {
            await sessionService.updateSessionApproval(sessionId, 'approved');
 
            // Refresh session list
            await get().fetchSessions();
 
            // Refresh current session if it's the one being approved
            if (get().currentSession?.id === sessionId) {
                await get().fetchSession(sessionId);
            }
        } catch (error) {
            console.error('Session approval error:', error);
            throw error;
        }
    },
 
    rejectSession: async (sessionId: number) => {
        try {
            await sessionService.updateSessionApproval(sessionId, 'rejected');
 
            // Refresh session list
            await get().fetchSessions();
 
            // Refresh current session if it's the one being rejected
            if (get().currentSession?.id === sessionId) {
                await get().fetchSession(sessionId);
            }
        } catch (error) {
            console.error('Session rejection error:', error);
            throw error;
        }
    },
 
    deleteSession: async (sessionId: number) => {
        try {
            await sessionService.deleteSession(sessionId);
 
            // Refresh session list
            await get().fetchSessions();
 
            // Clear current session if it was deleted
            if (get().currentSession?.id === sessionId) {
                set({ currentSession: null });
            }
        } catch (error) {
            console.error('Session deletion error:', error);
            throw error;
        }
    },
 
    fetchAnalytics: async () => {
        try {
            const analytics = await sessionService.getSessionAnalytics();
            set({ analytics });
        } catch (error) {
            console.error('Analytics fetch error:', error);
            // Don't set error for analytics, it's not critical
        }
    },
 
    fetchKnownSources: async () => {
        try {
            const sources = await sessionService.listKnownSources();
            set({ knownSources: sources });
        } catch (error) {
            console.error('Known sources fetch error:', error);
            // Don't set error for known sources, it's not critical
        }
    },
 
    createKnownSource: async (source: KnownSourceCreate) => {
        try {
            const newSource = await sessionService.createKnownSource(source);
 
            // Refresh known sources list
            await get().fetchKnownSources();
 
            return newSource;
        } catch (error) {
            console.error('Known source creation error:', error);
            throw error;
        }
    },
 
    setStatusFilter: (status: string | null) => {
        set({ statusFilter: status, currentPage: 1 });
        get().fetchSessions();
    },
 
    setApprovalFilter: (approval: string | null) => {
        set({ approvalFilter: approval, currentPage: 1 });
        get().fetchSessions();
    },
 
    clearError: () => set({ error: null }),
}));