-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
204 additions
and
22 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { useContext } from 'react'; | ||
import invariant from 'tiny-invariant'; | ||
|
||
import { FeatureFlagsContext, FeatureFlagsContextType } from './provider'; | ||
import { FeatureFlagsType } from './types'; | ||
|
||
type UseFeatureFlagReturnType = { | ||
[key in keyof FeatureFlagsType]: boolean; | ||
} & { | ||
setFeatureFlag: (featureFlag: keyof FeatureFlagsType, value: boolean) => void; | ||
}; | ||
|
||
export const useFeatureFlag = ( | ||
flag: keyof FeatureFlagsType, | ||
): UseFeatureFlagReturnType => { | ||
const context = useContext(FeatureFlagsContext); | ||
invariant(context, 'Attempt to use `feature flag` outside of provider'); | ||
return { | ||
[flag]: context[flag], | ||
setFeatureFlag: context.setFeatureFlag, | ||
}; | ||
}; | ||
|
||
export const useFeatureFlags = (): FeatureFlagsContextType => { | ||
const context = useContext(FeatureFlagsContext); | ||
invariant(context, 'Attempt to use `feature flag` outside of provider'); | ||
return context; | ||
}; |
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,4 @@ | ||
export * from './hooks'; | ||
export * from './provider'; | ||
export * from './types'; | ||
export * from './utils'; |
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,59 @@ | ||
import { | ||
PropsWithChildren, | ||
useMemo, | ||
useState, | ||
useCallback, | ||
createContext, | ||
} from 'react'; | ||
import { useLocalStorage } from '@lido-sdk/react'; | ||
|
||
import { getFeatureFlagsDefault } from './utils'; | ||
import { FeatureFlagsType } from './types'; | ||
|
||
const STORAGE_FEATURE_FLAGS = 'lido-feature-flags'; | ||
|
||
const FEATURE_FLAGS_DEFAULT = getFeatureFlagsDefault(); | ||
|
||
export type FeatureFlagsContextType = FeatureFlagsType & { | ||
setFeatureFlag: (featureFlag: keyof FeatureFlagsType, value: boolean) => void; | ||
}; | ||
|
||
export const FeatureFlagsContext = | ||
createContext<FeatureFlagsContextType | null>(null); | ||
|
||
export const FeatureFlagsProvider = ({ children }: PropsWithChildren) => { | ||
const [featureFlagsLocalStorage, setFeatureFlagsLocalStorage] = | ||
useLocalStorage(STORAGE_FEATURE_FLAGS, FEATURE_FLAGS_DEFAULT); | ||
|
||
const [featureFlagsState, setFeatureFlagsState] = useState<FeatureFlagsType>( | ||
featureFlagsLocalStorage, | ||
); | ||
|
||
const setFeatureFlag = useCallback( | ||
(featureFlag: keyof FeatureFlagsType, value: boolean) => { | ||
setFeatureFlagsLocalStorage({ | ||
...featureFlagsState, | ||
[featureFlag]: value, | ||
}); | ||
setFeatureFlagsState({ | ||
...featureFlagsState, | ||
[featureFlag]: value, | ||
}); | ||
}, | ||
[featureFlagsState, setFeatureFlagsLocalStorage], | ||
); | ||
|
||
const contextValue = useMemo(() => { | ||
return { | ||
...FEATURE_FLAGS_DEFAULT, | ||
...featureFlagsState, | ||
setFeatureFlag: setFeatureFlag, | ||
}; | ||
}, [featureFlagsState, setFeatureFlag]); | ||
|
||
return ( | ||
<FeatureFlagsContext.Provider value={contextValue}> | ||
{children} | ||
</FeatureFlagsContext.Provider> | ||
); | ||
}; |
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,9 @@ | ||
// TODO: maybe do we wants use 'feature flags' for this? | ||
// export const FEATURE_FLAGS_PAGE_IS_ENABLED = 'featureFlagsPageIsEnabled'; | ||
export const RPC_SETTINGS_PAGE_ON_INFRA_IS_ENABLED = | ||
'rpcSettingsPageOnInfraIsEnabled'; | ||
|
||
export type FeatureFlagsType = { | ||
// [FEATURE_FLAGS_PAGE_IS_ENABLED]: boolean; | ||
[RPC_SETTINGS_PAGE_ON_INFRA_IS_ENABLED]: boolean; | ||
}; |
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,9 @@ | ||
import { FeatureFlagsType } from './types'; | ||
|
||
export const getFeatureFlagsDefault = (): FeatureFlagsType => { | ||
return { | ||
// TODO: maybe do we wants use 'feature flags' for this? | ||
// featureFlagsPageIsEnabled: true, | ||
rpcSettingsPageOnInfraIsEnabled: false, | ||
}; | ||
}; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import { FC } from 'react'; | ||
import { GetStaticProps } from 'next'; | ||
import { Block, Checkbox } from '@lidofinance/lido-ui'; | ||
|
||
import { config } from 'config'; | ||
import { | ||
useFeatureFlag, | ||
RPC_SETTINGS_PAGE_ON_INFRA_IS_ENABLED, | ||
} from 'config/feature-flags'; | ||
import { Layout } from 'shared/components'; | ||
import NoSSRWrapper from 'shared/components/no-ssr-wrapper'; | ||
|
||
const FeatureFlags: FC = () => { | ||
const { rpcSettingsPageOnInfraIsEnabled, setFeatureFlag } = useFeatureFlag( | ||
RPC_SETTINGS_PAGE_ON_INFRA_IS_ENABLED, | ||
); | ||
|
||
return ( | ||
<Layout title="Feature Flags"> | ||
<NoSSRWrapper> | ||
<br /> | ||
<Block> | ||
<Checkbox | ||
checked={rpcSettingsPageOnInfraIsEnabled} | ||
onChange={() => | ||
setFeatureFlag( | ||
RPC_SETTINGS_PAGE_ON_INFRA_IS_ENABLED, | ||
!rpcSettingsPageOnInfraIsEnabled, | ||
) | ||
} | ||
label="RPC settings page on infra" | ||
/> | ||
</Block> | ||
</NoSSRWrapper> | ||
</Layout> | ||
); | ||
}; | ||
|
||
export default FeatureFlags; | ||
|
||
export const getStaticProps: GetStaticProps = async () => { | ||
if (!config.featureFlagsPageIsEnabled) return { notFound: true }; | ||
|
||
return { props: {} }; | ||
}; |
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,23 +1,34 @@ | ||
import { FC } from 'react'; | ||
import { GetStaticProps } from 'next'; | ||
// import { GetStaticProps } from 'next'; | ||
|
||
import { getConfig } from 'config'; | ||
// import { config } from 'config'; | ||
import { | ||
RPC_SETTINGS_PAGE_ON_INFRA_IS_ENABLED, | ||
useFeatureFlag, | ||
} from 'config/feature-flags'; | ||
import { Layout } from 'shared/components'; | ||
import { SettingsForm } from 'features/settings/settings-form'; | ||
import NoSSRWrapper from 'shared/components/no-ssr-wrapper'; | ||
|
||
const Settings: FC = () => { | ||
const { rpcSettingsPageOnInfraIsEnabled } = useFeatureFlag( | ||
RPC_SETTINGS_PAGE_ON_INFRA_IS_ENABLED, | ||
); | ||
|
||
return ( | ||
<Layout title="Settings"> | ||
<SettingsForm /> | ||
<NoSSRWrapper> | ||
{rpcSettingsPageOnInfraIsEnabled && <SettingsForm />} | ||
{!rpcSettingsPageOnInfraIsEnabled && <>Settings Not Available!</>} | ||
</NoSSRWrapper> | ||
</Layout> | ||
); | ||
}; | ||
|
||
export default Settings; | ||
|
||
export const getStaticProps: GetStaticProps = async () => { | ||
const { ipfsMode } = getConfig(); | ||
if (!ipfsMode) return { notFound: true }; | ||
|
||
return { props: {} }; | ||
}; | ||
// export const getStaticProps: GetStaticProps = async () => { | ||
// if (!config.ipfsMode) return { notFound: true }; | ||
// | ||
// return { props: {} }; | ||
// }; |
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