1 import isNumber from 'lodash/isNumber';
3 import type { NEWSLETTER_SUBSCRIPTIONS } from '../constants';
4 import { NEWSLETTER_SUBSCRIPTIONS_BITS, NEWSLETTER_SUBSCRIPTIONS_BY_BITS } from '../constants';
5 import { hasBit } from './bitset';
7 export type NewsletterSubscriptionUpdateData = Partial<Record<NEWSLETTER_SUBSCRIPTIONS, boolean>>;
10 * if we have new value, we return it, else we return old value, if it is undefined we return null
12 const isProductNewsEnabled = (
13 flag: NEWSLETTER_SUBSCRIPTIONS_BITS,
14 currentNews: NewsletterSubscriptionUpdateData | number,
15 updatedNews?: NewsletterSubscriptionUpdateData | number
17 const strFlag = NEWSLETTER_SUBSCRIPTIONS_BY_BITS[flag];
18 const currentValue = isNumber(currentNews) ? hasBit(currentNews, flag) : currentNews[strFlag];
19 const updatedValue = isNumber(updatedNews) ? hasBit(updatedNews, flag) : updatedNews?.[strFlag];
20 return updatedValue ?? currentValue ?? null;
24 * If one of the product newsletter (Inbox/Drive/Pass/VPN) is enabled, then returns true
26 export const isGlobalFeatureNewsEnabled = (
27 currentNews: NewsletterSubscriptionUpdateData | number,
28 updatedNews?: NewsletterSubscriptionUpdateData | number
31 isProductNewsEnabled(NEWSLETTER_SUBSCRIPTIONS_BITS.INBOX_NEWS, currentNews, updatedNews) ||
32 isProductNewsEnabled(NEWSLETTER_SUBSCRIPTIONS_BITS.DRIVE_NEWS, currentNews, updatedNews) ||
33 isProductNewsEnabled(NEWSLETTER_SUBSCRIPTIONS_BITS.PASS_NEWS, currentNews, updatedNews) ||
34 isProductNewsEnabled(NEWSLETTER_SUBSCRIPTIONS_BITS.VPN_NEWS, currentNews, updatedNews)