Remove payments API routing initialization
[ProtonMail-WebClient.git] / packages / components / containers / offers / hooks / useOfferFlags.ts
blobca81d6d2746ffaf5f24a56368348549c739cfcb4
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>(
11         FeatureCode.Offers
12     );
13     const {
14         feature: userFlag,
15         loading: userFlagLoading,
16         update: userFlagUpdate,
17     } = useFeature<OfferUserFeatureCodeValue>(config.featureCode);
19     const userFlagValue = userFlag?.Value || Default;
21     return {
22         loading: globalFlagLoading || userFlagLoading,
23         isActive: globalFlag?.Value?.[config.ID] === true && !hasBit(userFlagValue, Hide),
24         isVisited: hasBit(userFlagValue, Visited),
25         handleHide: () => {
26             const nextValue = setBit(userFlagValue, Hide);
27             if (nextValue === userFlagValue) {
28                 return;
29             }
31             return userFlagUpdate(nextValue);
32         },
33         handleVisit: () => {
34             const nextValue = setBit(userFlagValue, Visited);
35             if (nextValue === userFlagValue) {
36                 return;
37             }
39             return userFlagUpdate(nextValue);
40         },
41     };
44 export default useOfferFlags;