All files / src/hooks useFeatureFlags.ts

100% Statements 5/5
75% Branches 3/4
100% Functions 2/2
100% Lines 5/5

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                          3x         85x     33x 2x           85x    
import { useQuery } from '@tanstack/react-query';
import apiClient from '@/services/api/apiClient';
import type { components } from '@/types/generated/event-app-settings-api.types';
 
// Wire type — generated from docs/api/event-app-settings-api.openapi.yml (single source of truth).
type FeatureFlagsResponse = components['schemas']['FeatureFlagsResponse'];
 
// Normalized, all-fields-present shape the app consumes (derived from the wire type, so it
// cannot drift from the contract).
interface FeatureFlags {
  aiContentEnabled: boolean;
}
 
const DEFAULT_FLAGS: FeatureFlags = {
  aiContentEnabled: false,
};
 
export function useFeatureFlags(): FeatureFlags {
  const { data } = useQuery<FeatureFlags>({
    queryKey: ['feature-flags'],
    queryFn: async () => {
      const response = await apiClient.get<FeatureFlagsResponse>('/public/settings/features');
      return { aiContentEnabled: response.data.aiContentEnabled ?? false };
    },
    staleTime: 5 * 60 * 1000, // 5 minutes — feature flags don't change often
    gcTime: 10 * 60 * 1000,
  });
 
  return data ?? DEFAULT_FLAGS;
}