All files / hooks useAIStats.ts

100% Statements 27/27
100% Branches 2/2
100% Functions 4/4
100% Lines 25/25

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 441x 1x               1x 6x 6x 6x   6x 3x 3x 3x   3x 1x 1x 1x 1x     2x 1x 1x 1x 1x     1x 1x 1x     3x     6x    
import { useEffect, useState } from "react";
import { getAIUsageStats } from "../api/stats";
import { AIStats } from "../api/types/suggestion";
 
/**
 * Custom hook to fetch AI usage statistics.
 * @module Hooks
 * @returns { aiStats: AIStats[] | undefined, isLoading: boolean, error: string | null }
 */
export const useAIStats = () => {
  const [aiStats, setAIStats] = useState<AIStats[] | undefined>(undefined);
  const [isLoading, setIsLoading] = useState(true);
  const [error, setError] = useState<string | null>(null);
 
  useEffect(() => {
    const fetchAIStats = async () => {
      setIsLoading(true);
      const { data, error } = await getAIUsageStats();
 
      if (error) {
        setError(error);
        setAIStats(undefined);
        setIsLoading(false);
        return;
      }
 
      if (!data) {
        setError("No data found");
        setAIStats(undefined);
        setIsLoading(false);
        return;
      }
 
      setAIStats(data);
      setError(null);
      setIsLoading(false);
    };
 
    fetchAIStats();
  }, []);
 
  return { aiStats, isLoading, error };
};