mirror of
https://github.com/Unleash/unleash.git
synced 2026-01-23 20:06:43 +01:00
chore: add a delayed evaluation of UI flags (#11091)
Adds a new hook for UI flag evaluation that returns an evaluation function. This solves a frequent pain point where we can't check if a flag is enabled conditionally (e.g. if we don't know if we have a flag yet) because it's implemented as a hook. The new implementation uses the same evaluation as the old one, but returns the eval function after calling the hook, thereby circumventing the issue and allowing you to eval a flag anywhere in the function body.
This commit is contained in:
parent
3e840b8e22
commit
21acef88df
@ -1,9 +1,17 @@
|
||||
import useUiConfig from 'hooks/api/getters/useUiConfig/useUiConfig';
|
||||
|
||||
type flags = ReturnType<typeof useUiConfig>['uiConfig']['flags'];
|
||||
type Flags = ReturnType<typeof useUiConfig>['uiConfig']['flags'];
|
||||
export type Flag = keyof Flags;
|
||||
|
||||
export const useUiFlag = <K extends keyof flags>(flag: K) => {
|
||||
export const useDelayedUiFlagEvaluation = (): (<K extends Flag>(
|
||||
flag: K,
|
||||
) => NonNullable<Flags[K]> | false) => {
|
||||
const { uiConfig } = useUiConfig();
|
||||
|
||||
return uiConfig?.flags?.[flag] || false;
|
||||
return (flag) => uiConfig?.flags?.[flag] || false;
|
||||
};
|
||||
|
||||
export const useUiFlag = <K extends Flag>(flag: K) => {
|
||||
const evaluate = useDelayedUiFlagEvaluation();
|
||||
return evaluate(flag);
|
||||
};
|
||||
|
||||
Loading…
Reference in New Issue
Block a user