Merge branch 'feat/inda-383-daily-stat' into 'main'
[ProtonMail-WebClient.git] / packages / components / hooks / assistant / useAssistantFeatureEnabled.ts
blob8ef280f8409dfa0b10192c1d2a10f2623923a4c0
1 import { selectOrganization, selectUser } from '@proton/account';
2 import { useIsOrganizationBeforeBackfill } from '@proton/components/containers/payments/subscription/assistant/useIsOrganizationBeforeBackfill';
3 import useScribePaymentsEnabled from '@proton/components/containers/payments/subscription/assistant/useScribePaymentsEnabled';
4 import { isScribeSupported } from '@proton/components/helpers/assistant';
5 import { baseUseSelector } from '@proton/react-redux-store';
6 import { useFlag } from '@proton/unleash';
8 const useAssistantFeatureEnabled = () => {
9     const accessToAssistant = useFlag('ComposerAssistant');
10     const scribePaymentsEnabled = useScribePaymentsEnabled();
11     const isOrganizationBeforeBackfill = useIsOrganizationBeforeBackfill();
13     const user = baseUseSelector(selectUser)?.value;
14     const userHasScribeSeat = !!user?.NumAI;
16     const organization = baseUseSelector(selectOrganization)?.value;
17     const organizationScribeEnabled = !!organization?.Settings.ShowScribeWritingAssistant || !!user?.isAdmin;
18     const planSupportsScribe = isScribeSupported(organization, user);
20     const paymentsEnabled = accessToAssistant && !isOrganizationBeforeBackfill && scribePaymentsEnabled;
22     const enabled =
23         accessToAssistant &&
24         !isOrganizationBeforeBackfill &&
25         // you can't see anything Scribe related if the payments can't support you buying it
26         // but if you have a seat you can still use it
27         (scribePaymentsEnabled || userHasScribeSeat) &&
28         // user can't enter scribe trial if the organization plan doesn't support it
29         planSupportsScribe &&
30         // If org admin disabled scribe to sub users, do not show the feature
31         organizationScribeEnabled;
33     return {
34         /**
35          * If Scribe can be purchased then it should be true. This flag doesn't take organization's plan into account
36          * because free users should be able to buy Scribe too.
37          */
38         paymentsEnabled,
39         /**
40          * This flag controls if the configuration for scribe is available. Example: user management panel can
41          * have an upsell for scribe addon. In this case we need to check both things: payments are possible and
42          * scribe is supported by the organization's plan.
43          * Kill switch takes precedence
44          */
45         enabled: enabled,
46     };
49 export default useAssistantFeatureEnabled;