All files / applications/go-by-transit/src/component/feature-flags FeatureFlags.tsx

91.66% Statements 11/12
75% Branches 3/4
100% Functions 4/4
100% Lines 9/9

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                            9x     81x   81x 79x 79x     81x               634x 634x 634x    
import React, {
  createContext,
  ReactNode,
  useContext,
  useEffect,
  useState,
} from "react";
import { clearWalkingCache } from "../../api/mapbox/walking";
 
interface FeatureFlags {
  walkingEnabled: boolean;
  setWalkingEnabled: (enabled: boolean) => void;
}
 
const FeatureFlagsContext = createContext<FeatureFlags | undefined>(undefined);
 
export function FeatureFlagsProvider({ children }: { children: ReactNode }) {
  const [walkingEnabled, setWalkingEnabled] = useState(false);
 
  useEffect(() => {
    if (!walkingEnabled) clearWalkingCache();
    return () => clearWalkingCache();
  }, [walkingEnabled]);
 
  return (
    <FeatureFlagsContext.Provider value={{ setWalkingEnabled, walkingEnabled }}>
      {children}
    </FeatureFlagsContext.Provider>
  );
}
 
export function useFeatureFlags(): FeatureFlags {
  const flags = useContext(FeatureFlagsContext);
  Iif (!flags) throw new Error("Feature flags require FeatureFlagsProvider.");
  return flags;
}