Merge branch 'feat/inda-383-daily-stat' into 'main'
[ProtonMail-WebClient.git] / packages / account / subscription / startListeningToPlanNameChange.ts
blobdd5252f9e29704ba81ea64afc6b8daec8b8680f2
1 import type { SharedStartListening } from '@proton/redux-shared-store-types';
2 import noop from '@proton/utils/noop';
4 import type { OrganizationState } from '../organization';
5 import { selectOrganization } from '../organization';
7 /**
8  * Listen to subscription changes and update unleash feature flags after change.
9  */
10 export const startListeningToPlanNameChange = (startListening: SharedStartListening<OrganizationState>) => {
11     startListening({
12         predicate: (_, currentState, previousState) => {
13             const oldValue = selectOrganization(previousState);
14             const newValue = selectOrganization(currentState);
15             return !!(oldValue.meta.fetchedAt && oldValue.value?.PlanName !== newValue.value?.PlanName);
16         },
17         effect: (_, listenerApi) => {
18             listenerApi.extra.unleashClient.stop();
19             listenerApi.extra.unleashClient.start().catch(noop);
20         },
21     });