/* * File: usePredictions.ts * Description: Custom hook for managing AI predictions state and actions * Design & Developed by Tech4Biz Solutions * Copyright (c) Spurrin Innovations. All rights reserved. */ import { useEffect, useCallback } from 'react'; import { useAppDispatch, useAppSelector } from '../../../store/hooks'; import { fetchAllPredictions, setActiveTab, setSearchQuery, clearErrors, clearSearch, selectActiveTab, selectSearchQuery, selectPredictionsWithFeedback, selectPredictionsWithoutFeedback, selectIsLoading, selectError, selectCurrentPredictions, selectCurrentLoadingState, selectCurrentError, } from '../redux/predictionsSlice'; import { selectUser } from '../../Auth/redux'; import type { PredictionTabType } from '../types/predictions'; // ============================================================================ // USE PREDICTIONS HOOK // ============================================================================ /** * usePredictions Hook * * Purpose: Manage AI predictions state and provide actions * * Features: * - Fetch all predictions from single API call * - Frontend filtering for feedback status * - Manage active tab state * - Handle search functionality * - Provide loading and error states * - Auto-fetch data when needed */ export const usePredictions = () => { const dispatch = useAppDispatch(); // ============================================================================ // SELECTORS // ============================================================================ const activeTab = useAppSelector(selectActiveTab); const searchQuery = useAppSelector(selectSearchQuery); // Performance optimization: Load both datasets upfront const predictionsWithFeedback = useAppSelector(selectPredictionsWithFeedback); const predictionsWithoutFeedback = useAppSelector(selectPredictionsWithoutFeedback); const isLoading = useAppSelector(selectIsLoading); const error = useAppSelector(selectError); // Performance optimization: Direct dataset selection - no filtering needed const currentPredictions = useAppSelector(selectCurrentPredictions); const currentLoadingState = useAppSelector(selectCurrentLoadingState); const currentError = useAppSelector(selectCurrentError); // Get authentication token from auth store const authToken = useAppSelector(selectUser)?.access_token; // ============================================================================ // ACTIONS // ============================================================================ /** * Switch Active Tab * * Purpose: Change between feedback tabs * Performance: Instant switching - no filtering needed as both datasets are pre-loaded */ const switchTab = useCallback((tab: PredictionTabType) => { dispatch(setActiveTab(tab)); }, [dispatch]); /** * Update Search Query * * Purpose: Update search query for filtering predictions */ const updateSearchQuery = useCallback((query: string) => { dispatch(setSearchQuery(query)); }, [dispatch]); /** * Clear Search * * Purpose: Clear search query */ const clearSearchQuery = useCallback(() => { dispatch(clearSearch()); }, [dispatch]); /** * Clear Errors * * Purpose: Clear error states */ const clearErrorStates = useCallback(() => { dispatch(clearErrors()); }, [dispatch]); /** * Refresh Predictions * * Purpose: Refresh all predictions data */ const refreshPredictions = useCallback(() => { if (!authToken) return; dispatch(fetchAllPredictions(authToken)); }, [dispatch, authToken]); /** * Fetch All Predictions * * Purpose: Fetch all predictions from API */ const fetchPredictions = useCallback(() => { if (!authToken) return; dispatch(fetchAllPredictions(authToken)); }, [dispatch, authToken]); // ============================================================================ // EFFECTS // ============================================================================ /** * Auto-fetch data when component mounts or token changes * Performance optimization: Load both datasets upfront to eliminate tab switching delays */ useEffect(() => { if (!authToken) return; // Performance optimization: Always fetch if either dataset is empty // This ensures both datasets are loaded upfront for instant tab switching if (predictionsWithFeedback.length === 0 || predictionsWithoutFeedback.length === 0) { dispatch(fetchAllPredictions(authToken)); } }, [authToken, predictionsWithFeedback.length, predictionsWithoutFeedback.length, dispatch]); // ============================================================================ // RETURN VALUES // ============================================================================ return { // State activeTab, searchQuery, predictionsWithFeedback, predictionsWithoutFeedback, currentPredictions, // Loading states isLoading, currentLoadingState, // Error states error, currentError, // Actions switchTab, updateSearchQuery, clearSearchQuery, clearErrorStates, refreshPredictions, fetchPredictions, // Constants authToken }; }; /* * End of File: usePredictions.ts * Design & Developed by Tech4Biz Solutions * Copyright (c) Spurrin Innovations. All rights reserved. */