Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add possibility to override feature flags with environment variable FEATURE_FLAGS #1372

Merged
merged 5 commits into from
Oct 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/pretty-dingos-sing.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"live-mobile": patch
---

Feature flags: take into account env variable FEATURE_FLAGS to override feature flags
5 changes: 5 additions & 0 deletions .changeset/quiet-doors-cross.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"ledger-live-desktop": patch
---

Feature flags: take into account env variable FEATURE_FLAGS to override feature flags
6 changes: 6 additions & 0 deletions .changeset/silent-beers-carry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@ledgerhq/live-common": patch
"@ledgerhq/types-live": patch
---

Add FEATURE_FLAGS env variable to override feature flags
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { Feature, FeatureId } from "@ledgerhq/types-live";
import { getValue } from "firebase/remote-config";

import { formatFeatureId, useFirebaseRemoteConfig } from "./FirebaseRemoteConfig";
import { getEnv } from "@ledgerhq/live-common/env";

const checkFeatureFlagVersion = (feature: Feature) => {
if (
Expand Down Expand Up @@ -42,6 +43,17 @@ export const FirebaseFeatureFlagsProvider = ({ children }: Props): JSX.Element =
return checkFeatureFlagVersion(localOverrides[key]);
}

const envFlags = getEnv("FEATURE_FLAGS") as { [key in FeatureId]?: Feature } | undefined;
if (allowOverride && envFlags) {
const feature = envFlags[key];
if (feature)
return {
...feature,
overridesRemote: true,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if it's overriden by env, should remove be false? πŸ€”

Copy link
Contributor Author

@ofreyssinet-ledger ofreyssinet-ledger Sep 27, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It still overrides the remote value, if the name of the variable was different I would have done what you are suggesting

overriddenByEnv: true,
};
}

const value = getValue(remoteConfig, formatFeatureId(key));
const feature: Feature = JSON.parse(value.asString());

Expand All @@ -58,7 +70,8 @@ export const FirebaseFeatureFlagsProvider = ({ children }: Props): JSX.Element =
(key: FeatureId, value: Feature): void => {
const actualRemoteValue = getFeature(key, false);
if (!isEqual(actualRemoteValue, value)) {
const overridenValue = { ...value, overridesRemote: true };
const { overriddenByEnv, ...pureValue } = value; // eslint-disable-line
const overridenValue = { ...pureValue, overridesRemote: true };
setLocalOverrides(currentOverrides => ({ ...currentOverrides, [key]: overridenValue }));
} else {
setLocalOverrides(currentOverrides => ({ ...currentOverrides, [key]: undefined }));
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import React, { useCallback } from "react";
import ButtonV2 from "~/renderer/components/Button";
import { useFeatureFlags } from "@ledgerhq/live-common/featureFlags/index";
import { Text, Flex, Tag } from "@ledgerhq/react-ui";
import { FeatureId } from "@ledgerhq/types-live";
import { withV2StyleProvider } from "~/renderer/styles/StyleProvider";
import Box from "~/renderer/components/Box";
import FeatureFlagEdit from "./FeatureFlagEdit";

const OldButton = withV2StyleProvider(ButtonV2);

type Props = {
flagName: FeatureId;
focused?: boolean;
setFocusedName: (arg0: string | undefined) => void;
};

const FeatureFlagDetails: React.FC<Props> = props => {
const { flagName, focused, setFocusedName } = props;
const { getFeature } = useFeatureFlags();
const flagValue = getFeature(flagName as FeatureId);

const {
overriddenByEnv,
overridesRemote,
enabledOverriddenForCurrentLanguage,
enabledOverriddenForCurrentDesktopVersion,
} = flagValue || {};

const handleClick = useCallback(() => {
focused ? setFocusedName(undefined) : setFocusedName(flagName);
}, [focused, flagName, setFocusedName]);

if (!flagValue) return null;

return (
<>
<OldButton flexDirection="row" py={1} onClick={handleClick}>
<Flex flex={1} mr={3} alignItems="center">
<Box
bg={flagValue?.enabled ? "success.c100" : "error.c100"}
height={10}
width={10}
mr={2}
borderRadius={999}
/>
<Text mr={1}>{flagName}</Text>
{overriddenByEnv ? (
<Tag active mx={1} type="opacity" size="small">
overridden by env
</Tag>
) : overridesRemote ? (
<Tag active mx={1} type="opacity" size="small">
overridden locally
</Tag>
) : null}
{enabledOverriddenForCurrentLanguage ? (
<Tag active mx={1} type="outlinedOpacity" size="small">
disabled for current language
</Tag>
) : null}
{enabledOverriddenForCurrentDesktopVersion ? (
<Tag active mx={1} type="outlinedOpacity" size="small">
disabled for current version
</Tag>
) : null}
</Flex>
</OldButton>
{focused ? <FeatureFlagEdit flagName={flagName} flagValue={flagValue} /> : null}
</>
);
};

export default FeatureFlagDetails;
Loading