From b1895f05397ec16fd0ef6541b91fe58574818dfc Mon Sep 17 00:00:00 2001 From: Ivor Scott Date: Sun, 26 Nov 2023 21:15:03 +0100 Subject: [PATCH] feat: add feature flags support --- src/index.tsx | 5 ++++- src/services/FeatureService.ts | 28 ++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) create mode 100644 src/services/FeatureService.ts diff --git a/src/index.tsx b/src/index.tsx index b8c6613..8f66317 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -29,8 +29,11 @@ import { import { PinnedProvider } from "services/PinnedProvider"; import awsconfig from "./aws-exports"; +import { features } from "./services/FeatureService"; import { theme } from "./theme"; +features.applyOverrides(); + Amplify.configure({ ...awsconfig, userPoolId: process.env.REACT_APP_USER_POOL_ID, @@ -113,7 +116,7 @@ const App = withAuthenticator( ), - { hideSignUp: true, components } + { hideSignUp: features.flags["F001"], components } ); const container = document.getElementById("root"); diff --git a/src/services/FeatureService.ts b/src/services/FeatureService.ts new file mode 100644 index 0000000..2c8dffd --- /dev/null +++ b/src/services/FeatureService.ts @@ -0,0 +1,28 @@ +interface Features { + [id: string]: boolean; +} + +class FeatureService { + public flags: Features; + + constructor() { + this.flags = { + // F001 - Early Access Flow - Users can't sign up on their own + F001: true, + }; + } + + // applyOverrides reads feature flags from local storage and overrides + // the app's default set of enabled features. + applyOverrides() { + const flags = localStorage.getItem("flags"); + if (!flags) { + return; + } + + const overrides: Features = JSON.parse(flags); + this.flags = { ...this.flags, ...overrides }; + } +} + +export const features = new FeatureService();