1 import { FeatureCode, useFeature } from '@proton/features';
2 import { hasBit, setBit } from '@proton/shared/lib/helpers/bitset';
4 import type { OfferConfig, OfferGlobalFeatureCodeValue } from '../interface';
5 import { OfferUserFeatureCodeValue } from '../interface';
7 const { Default, Visited, Hide } = OfferUserFeatureCodeValue;
9 const useOfferFlags = (config: OfferConfig) => {
10 const { feature: globalFlag, loading: globalFlagLoading } = useFeature<OfferGlobalFeatureCodeValue>(
15 loading: userFlagLoading,
16 update: userFlagUpdate,
17 } = useFeature<OfferUserFeatureCodeValue>(config.featureCode);
19 const userFlagValue = userFlag?.Value || Default;
22 loading: globalFlagLoading || userFlagLoading,
23 isActive: globalFlag?.Value?.[config.ID] === true && !hasBit(userFlagValue, Hide),
24 isVisited: hasBit(userFlagValue, Visited),
26 const nextValue = setBit(userFlagValue, Hide);
27 if (nextValue === userFlagValue) {
31 return userFlagUpdate(nextValue);
34 const nextValue = setBit(userFlagValue, Visited);
35 if (nextValue === userFlagValue) {
39 return userFlagUpdate(nextValue);
44 export default useOfferFlags;