saleor-dashboard/src/hooks/useFlags/flagsService/useServiceFlags.ts
poulch d5ed6fb202
Feature flags (#2961)
* [Feature Flags] Abstraction over flags provider (#2928)

* Remove useFlag hook

* [Feature Flags] GraphQL build multiple schemas (#2937)

* Build script

* Refactor build types script

* Remove old codegen.yml

* Clean feature flags in script

* Refactor schema path

* Restore useAuthProvider

* Update configuration file

* encapsulate details for feature flags provider

* Add proper env to flagsmith provider

* Remove flagsmith mocks

* Vite config define global variables

* Render flagmisth provider only when is used

* Keep name service agnostic

* Test with mocked flagsmith

* Use global FLAGS varaible for env flags

* Fix type issue with FLAGS

* Fix build issue

* Remove duplicate translations

* Fix typo

* Prepare for QA tests

* Remove test feature flag
2023-01-16 14:55:38 +01:00

27 lines
762 B
TypeScript

import { useFlags } from "flagsmith/react";
import snakeCase from "lodash/snakeCase";
import { FlagsResults } from "../types";
export const useServiceFlags = <T extends readonly string[]>(
flags: readonly [...T],
traits?: string[],
): FlagsResults<T> => {
const flagsmithFlags = useFlags(transformFlagsToSnakeCase(flags), traits);
return flags.reduce((acc, flag) => {
const flagName = snakeCase(flag);
if (flagsmithFlags[flagName]) {
acc[flag] = {
enabled: flagsmithFlags[flagName].enabled,
value: flagsmithFlags[flagName].value ?? "",
};
}
return acc;
}, {} as FlagsResults<T>);
};
function transformFlagsToSnakeCase(flags: readonly string[]): string[] {
return flags.map(flag => snakeCase(flag));
}