Skip to content

Instantly share code, notes, and snippets.

@mannyyang
Last active May 6, 2020 07:31
Show Gist options
  • Select an option

  • Save mannyyang/32b290f9f6375706bda2ba5903fc2e20 to your computer and use it in GitHub Desktop.

Select an option

Save mannyyang/32b290f9f6375706bda2ba5903fc2e20 to your computer and use it in GitHub Desktop.
/**
* Feature Flags
*
* Settings to turn on a feature. Currently, Flags can be set through either a
* query param or from local storage.
*/
import env from '@beam-australia/react-env';
import UrlParse from 'url-parse';
const LOCAL_STORAGE_KEY = 'orFeatureFlag';
const QUERY_PARAM_KEY = 'ff';
const featureDisabled = (feature, allowOnDev = true) => (
(allowOnDev && env('NODE_ENV') !== 'production') || feature
);
const url = new UrlParse(window.location.href, true);
const queryParams = url.query;
function getFlagsFromLocalStorage() {
let lStorage = {};
try {
lStorage = localStorage.getItem(LOCAL_STORAGE_KEY)
? JSON.parse(localStorage.getItem(LOCAL_STORAGE_KEY))
: {};
} catch (e) {
lStorage = {};
}
return lStorage;
}
function getFlagState(envVar, param) {
return (
// Check environment variables
env(envVar) === 'true'
// Check query params
|| (QUERY_PARAM_KEY in queryParams && queryParams.ff.indexOf(param) >= 0)
// Check local storage
|| getFlagsFromLocalStorage()[param]
|| false
);
}
const featureFlags = {
assetGroupsV2: getFlagState('IS_ASSET_GROUPS_V2', 'assetGroupsV2'),
deploymentApp: getFlagState('IS_DEPLOYMENT_APP', 'deploymentApp'),
discoveryAccountsApp: getFlagState('IS_DISCOVERY_ACCOUNTS_APP', 'discoveryAccountsApp'),
exploreV2: getFlagState('IS_EXPLORER_V2', 'exploreV2'),
exploreSingleAsset: getFlagState('IS_EXPLORE_SINGLE_ASSET', 'exploreSingleAsset'),
};
export {
LOCAL_STORAGE_KEY,
getFlagsFromLocalStorage,
featureDisabled,
featureFlags,
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment