Skip to content

Commit

Permalink
refactor: Throw error for invalid LD setup
Browse files Browse the repository at this point in the history
  • Loading branch information
Joel Anton committed May 31, 2024
1 parent 5c3ab75 commit 024b3fc
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion libs/shared-web/src/hooks/useFeatureFlags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,20 @@ function isFlagMissingInLdResponse(key: FeatureFlagsKeysEnum, keysFromLunchDarkl

export const useFeatureFlag = (key: FeatureFlagsKeysEnum): boolean => {
const shouldUseLaunchDarkly = checkShouldUseLaunchDarkly();
let flagValue = undefined;

/** We knowingly break the rule of hooks here to avoid making any LaunchDarkly calls when it is disabled */
const flagValue = shouldUseLaunchDarkly ? useFlags()[key] : undefined;
if (shouldUseLaunchDarkly) {
const flags = useFlags();
if (!flags || Object.keys(flags).length === 0) {
throw new Error(
'No flags were returned by LaunchDarkly, Please ensure that the app is wrapped in an LDProvider!'
);
}

flagValue = flags[key];
}

e2eExplicitFlagAssertion(key);
const fallbackValue = false;
const value = FEATURE_FLAGS[key];
Expand Down

0 comments on commit 024b3fc

Please sign in to comment.