All files / web/src/providers skill-optimization-clusters.tsx

95.83% Statements 69/72
100% Branches 13/13
100% Functions 5/5
95.83% Lines 69/72

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      1x 1x 1x   1x                 1x 1x 1x 1x 19x 1x                                     1x 1x 1x   1x 17x 17x   17x 17x 17x   17x     17x 17x 17x 17x 17x 17x 17x 17x 17x 17x     17x 17x 3x 3x 3x 17x     17x 17x 2x 2x 2x 2x 17x 17x   17x       17x   17x   17x 17x 17x 17x 17x     17x 17x     17x 17x 17x   17x 17x 17x 17x   17x   1x 19x 19x 2x 2x 17x 17x  
'use client';
 
import type { SkillOptimizationCluster } from '@shared/types/data/skill-optimization-cluster';
import { useQuery, useQueryClient } from '@tanstack/react-query';
import { getSkillClusterStates as getSkillClusters } from '@web/api/v1/super-agents/skills';
import { useNavigation } from '@web/providers/navigation';
import type React from 'react';
import {
  createContext,
  useCallback,
  useContext,
  useMemo,
  useState,
} from 'react';
 
// Query keys for React Query caching
export const clusterQueryKeys = {
  all: ['skillOptimizationClusters'] as const,
  lists: () => [...clusterQueryKeys.all, 'list'] as const,
  list: (skillId: string | null) =>
    [...clusterQueryKeys.lists(), skillId] as const,
};
 
interface ClustersContextType {
  // Query state
  clusters: SkillOptimizationCluster[];
  selectedCluster?: SkillOptimizationCluster;
  isLoading: boolean;
  error: Error | null;
  refetch: () => void;
 
  // Skill ID
  skillId: string | null;
  setSkillId: (skillId: string | null) => void;
 
  // Helper functions
  getClusterById: (id: string) => SkillOptimizationCluster | undefined;
  refreshClusters: () => void;
}
 
const ClustersContext = createContext<ClustersContextType | undefined>(
  undefined,
);
 
export const SkillOptimizationClustersProvider = ({
  children,
}: {
  children: React.ReactNode;
}): React.ReactElement => {
  const queryClient = useQueryClient();
  const { navigationState } = useNavigation();
 
  const [skillId, setSkillId] = useState<string | null>(null);
 
  // Cluster states query
  const {
    data: clusters = [],
    isLoading,
    error,
    refetch,
  } = useQuery({
    queryKey: clusterQueryKeys.list(skillId),
    queryFn: () => getSkillClusters(skillId!),
    enabled: !!skillId, // Only fetch when we have skillId
  });
 
  // Resolve selectedCluster from navigationState.selectedClusterName
  const selectedCluster = useMemo(() => {
    if (!navigationState.selectedClusterName) return undefined;
    return clusters.find(
      (cluster) => cluster.name === navigationState.selectedClusterName,
    );
  }, [navigationState.selectedClusterName, clusters]);
 
  // Helper functions
  const getClusterById = useCallback(
    (id: string): SkillOptimizationCluster | undefined => {
      return clusters?.find(
        (cluster: SkillOptimizationCluster) => cluster.id === id,
      );
    },
    [clusters],
  );
 
  const refreshClusters = useCallback(() => {
    queryClient.invalidateQueries({
      queryKey: clusterQueryKeys.all,
    });
  }, [queryClient]);
 
  const contextValue: ClustersContextType = {
    // Query state
    clusters,
    selectedCluster,
    isLoading,
    error,
    refetch,
 
    // Skill ID
    skillId,
    setSkillId,
 
    // Helper functions
    getClusterById: getClusterById,
    refreshClusters: refreshClusters,
  };
 
  return (
    <ClustersContext.Provider value={contextValue}>
      {children}
    </ClustersContext.Provider>
  );
};
 
export const useSkillOptimizationClusters = (): ClustersContextType => {
  const context = useContext(ClustersContext);
  if (!context) {
    throw new Error('useClusters must be used within a ClustersProvider');
  }
  return context;
};