All files / src/FeatureFlags FeatureFlagProvider.tsx

50% Statements 4/8
100% Branches 0/0
33.33% Functions 1/3
42.85% Lines 3/7

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                        520x           23110x   520x                                                              
import { showMessage } from "@common/Toast";
import flagsmith from "flagsmith";
import { FlagsmithProvider } from "flagsmith/react";
import { ReactElement, ReactNode, createContext, useContext } from "react";
import { TFlagSmithState, useGetFlags } from "./useGetFlags";
 
type TFeatureFlagContext = {
  flags: Map<string, boolean>;
  flagsmithState: TFlagSmithState | undefined;
  isLoading: boolean;
};
 
export const FeatureFlagContext = createContext<TFeatureFlagContext>({
  flags: new Map(),
  flagsmithState: undefined,
  isLoading: true,
});
 
export const useFeatureFlagContext = () => useContext(FeatureFlagContext);
 
const FeatureFlagProvider = ({ children }: { children: ReactElement }) => {
  const { flags, isLoading, flagsmithState } = useGetFlags();
 
  const onError = ({ message }: { message: string }) => {
    showMessage("Error", message);
  };
  return (
    <FeatureFlagContext.Provider
      value={{
        flags,
        flagsmithState,
        isLoading,
      }}
    >
      <FlagsmithProvider
        options={{
          environmentID: process.env.VITE_ENVIRONMENT_ID_FLAG!,
          // enableLogs: true,
          api: process.env.VITE_GIVE_FLAGS_API,
          onError: onError,
        }}
        // we can do this because flagsmith is probably a singleton
        flagsmith={flagsmith}
      >
        {children}
      </FlagsmithProvider>
    </FeatureFlagContext.Provider>
  );
};
 
export default FeatureFlagProvider;