-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Migrated env feature flags to openfeature
- Loading branch information
1 parent
fedae9e
commit 25eddd9
Showing
36 changed files
with
570 additions
and
85 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 21 additions & 8 deletions
29
packages/commonwealth/client/scripts/helpers/feature-flags.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,25 @@ | ||
// As of 240205, we are moving away from the use of env vars for feature flags, | ||
// and towards the use of Unleash for all flag management. | ||
// This is our in memory provider setup. It does not automatically ensure your | ||
// feature flag is set on our Unleash instance (May not be available on prod). | ||
// | ||
// See knowledge_base/Feature-Flags.md for info. | ||
// See knowledge_base/Feature-Flags.md for more info. | ||
|
||
const buildFlag = (env: string) => { | ||
return { | ||
variants: { | ||
on: true, | ||
off: false, | ||
}, | ||
disabled: false, | ||
defaultVariant: env === 'true' ? 'on' : 'off', | ||
}; | ||
}; | ||
|
||
export const featureFlags = { | ||
proposalTemplates: process.env.FLAG_PROPOSAL_TEMPLATES === 'true', | ||
communityHomepage: process.env.FLAG_COMMUNITY_HOMEPAGE === 'true', | ||
newAdminOnboardingEnabled: process.env.FLAG_NEW_ADMIN_ONBOARDING === 'true', | ||
communityStake: process.env.FLAG_COMMUNITY_STAKE === 'true', | ||
newSignInModal: process.env.FLAG_NEW_SIGN_IN_MODAL === 'true', | ||
proposalTemplates: buildFlag(process.env.FLAG_PROPOSAL_TEMPLATES), | ||
communityHomepage: buildFlag(process.env.FLAG_COMMUNITY_HOMEPAGE), | ||
newAdminOnboarding: buildFlag(process.env.FLAG_NEW_ADMIN_ONBOARDING), | ||
communityStake: buildFlag(process.env.FLAG_COMMUNITY_STAKE), | ||
newSignInModal: buildFlag(process.env.FLAG_NEW_SIGN_IN_MODAL), | ||
}; | ||
|
||
export type AvailableFeatureFlag = keyof typeof featureFlags; |
61 changes: 61 additions & 0 deletions
61
packages/commonwealth/client/scripts/hooks/openFeature/OpenFeatureProvider.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/** | ||
* This file was grabbed from https://github.com/open-feature. There was an issue with the react-sdk npm deploy, | ||
* so I had copied the code | ||
*/ | ||
|
||
import { Client, OpenFeature } from '@openfeature/web-sdk'; | ||
import * as React from 'react'; | ||
|
||
type ClientOrClientName = | ||
| { | ||
/** | ||
* The name of the client. | ||
* @see OpenFeature.setProvider() and overloads. | ||
*/ | ||
clientName: string; | ||
/** | ||
* OpenFeature client to use. | ||
*/ | ||
client?: never; | ||
} | ||
| { | ||
/** | ||
* OpenFeature client to use. | ||
*/ | ||
client: Client; | ||
/** | ||
* The name of the client. | ||
* @see OpenFeature.setProvider() and overloads. | ||
*/ | ||
clientName?: never; | ||
}; | ||
|
||
type ProviderProps = { | ||
children?: React.ReactNode; | ||
} & ClientOrClientName; | ||
|
||
const Context = React.createContext<Client | undefined>(undefined); | ||
|
||
export const OpenFeatureProvider = ({ | ||
client, | ||
clientName, | ||
children, | ||
}: ProviderProps) => { | ||
if (!client) { | ||
client = OpenFeature.getClient(clientName); | ||
} | ||
|
||
return <Context.Provider value={client}>{children}</Context.Provider>; | ||
}; | ||
|
||
export const useOpenFeatureClient = () => { | ||
const client = React.useContext(Context); | ||
|
||
if (!client) { | ||
throw new Error( | ||
'No OpenFeature client available - components using OpenFeature must be wrapped with an <OpenFeatureProvider>', | ||
); | ||
} | ||
|
||
return client; | ||
}; |
Oops, something went wrong.