All files / web/src/providers skill-optimization-evaluation-runs.tsx

100% Statements 69/69
100% Branches 11/11
100% Functions 5/5
100% Lines 69/69

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      1x 1x   1x     1x 1x 1x 3x 1x 2x 1x                                           1x   1x   1x 25x 25x   25x 25x   25x 25x     25x 25x 25x 25x 25x 25x 25x 25x 25x 25x     25x 25x 2x 2x 2x 2x 25x 25x   25x 1x 1x 1x 25x   25x   25x 25x 25x 25x     25x 25x 25x 25x     25x 25x 25x   25x 25x 25x 25x   25x   1x 1x 27x 27x 2x 2x 2x 2x 25x 25x  
'use client';
 
import type { SkillOptimizationEvaluationRun } from '@shared/types/data/skill-optimization-evaluation-run';
import { useQuery, useQueryClient } from '@tanstack/react-query';
import { getSkillEvaluationRuns } from '@web/api/v1/super-agents/skills';
import type React from 'react';
import { createContext, useCallback, useContext, useState } from 'react';
 
// Query keys for React Query caching
export const skillOptimizationEvaluationRunQueryKeys = {
  all: ['skillOptimizationEvaluationRuns'] as const,
  lists: () =>
    [...skillOptimizationEvaluationRunQueryKeys.all, 'list'] as const,
  list: (skillId: string | null) =>
    [...skillOptimizationEvaluationRunQueryKeys.lists(), skillId] as const,
};
 
interface SkillOptimizationEvaluationRunsContextType {
  // Query state
  evaluationRuns: SkillOptimizationEvaluationRun[];
  isLoading: boolean;
  error: Error | null;
  refetch: () => void;
 
  // Skill ID and Log ID
  skillId: string | null;
  setSkillId: (skillId: string | null) => void;
  logId: string | null;
  setLogId: (logId: string | null) => void;
 
  // Helper functions
  getEvaluationRunsByClusterId: (
    clusterId: string,
  ) => SkillOptimizationEvaluationRun[];
  refreshEvaluationRuns: () => void;
}
 
const SkillOptimizationEvaluationRunsContext = createContext<
  SkillOptimizationEvaluationRunsContextType | undefined
>(undefined);
 
export const SkillOptimizationEvaluationRunsProvider = ({
  children,
}: {
  children: React.ReactNode;
}): React.ReactElement => {
  const queryClient = useQueryClient();
 
  const [skillId, setSkillId] = useState<string | null>(null);
  const [logId, setLogId] = useState<string | null>(null);
 
  // Evaluation runs query - only fetches for specific log if logId is set
  const {
    data: evaluationRuns = [],
    isLoading,
    error,
    refetch,
  } = useQuery({
    queryKey: ['evaluation-runs', skillId, logId],
    queryFn: () => getSkillEvaluationRuns(skillId!, logId ?? undefined),
    enabled: !!skillId,
  });
 
  // Helper functions
  const getEvaluationRunsByClusterId = useCallback(
    (clusterId: string): SkillOptimizationEvaluationRun[] => {
      return evaluationRuns.filter(
        (run: SkillOptimizationEvaluationRun) => run.cluster_id === clusterId,
      );
    },
    [evaluationRuns],
  );
 
  const refreshEvaluationRuns = useCallback(() => {
    queryClient.invalidateQueries({
      queryKey: skillOptimizationEvaluationRunQueryKeys.all,
    });
  }, [queryClient]);
 
  const contextValue: SkillOptimizationEvaluationRunsContextType = {
    // Query state
    evaluationRuns,
    isLoading,
    error,
    refetch,
 
    // Skill ID and Log ID
    skillId,
    setSkillId,
    logId,
    setLogId,
 
    // Helper functions
    getEvaluationRunsByClusterId,
    refreshEvaluationRuns,
  };
 
  return (
    <SkillOptimizationEvaluationRunsContext.Provider value={contextValue}>
      {children}
    </SkillOptimizationEvaluationRunsContext.Provider>
  );
};
 
export const useSkillOptimizationEvaluationRuns =
  (): SkillOptimizationEvaluationRunsContextType => {
    const context = useContext(SkillOptimizationEvaluationRunsContext);
    if (!context) {
      throw new Error(
        'useSkillOptimizationEvaluationRuns must be used within a SkillOptimizationEvaluationRunsProvider',
      );
    }
    return context;
  };