Merge branch 'INDA-330-pii-update' into 'main'
[ProtonMail-WebClient.git] / applications / mail / src / app / hooks / useSelectAll.tsx
blob6a27bebf3f268430af85801c0a1e5473d82c34bd
1 import { useCallback, useMemo } from 'react';
3 import getBoldFormattedText from '@proton/components/helpers/getBoldFormattedText';
4 import { useFolders, useLabels } from '@proton/mail';
5 import { useConversationCounts } from '@proton/mail/counts/conversationCounts';
6 import { useMessageCounts } from '@proton/mail/counts/messageCounts';
7 import { useFlag } from '@proton/unleash';
9 import { getLocationElementsCount } from 'proton-mail/helpers/elements';
10 import { isConversationMode } from 'proton-mail/helpers/mailSettings';
11 import {
12     getSelectAllBannerText,
13     getSelectAllBannerTextWithLocation,
14     getSelectAllButtonText,
15 } from 'proton-mail/helpers/selectAll';
16 import useMailModel from 'proton-mail/hooks/useMailModel';
17 import { useMailDispatch, useMailSelector } from 'proton-mail/store/hooks';
18 import { layoutActions } from 'proton-mail/store/layout/layoutSlice';
19 import { selectSelectAll } from 'proton-mail/store/layout/layoutSliceSelectors';
21 interface Props {
22     labelID: string;
25 export const useSelectAll = ({ labelID }: Props) => {
26     const mailSettings = useMailModel('MailSettings');
27     const mailPageSize = mailSettings.PageSize;
28     const [conversationCounts] = useConversationCounts();
29     const [messageCounts] = useMessageCounts();
30     const isConversation = isConversationMode(labelID, mailSettings);
31     const [labels = []] = useLabels();
32     const [folders = []] = useFolders();
33     const dispatch = useMailDispatch();
34     const selectAll = useMailSelector(selectSelectAll);
35     const selectAllAvailable = useFlag('SelectAll');
37     const locationCount = useMemo(() => {
38         return getLocationElementsCount(labelID, conversationCounts || [], messageCounts || [], isConversation);
39     }, [conversationCounts, messageCounts, labelID]);
41     const getBannerText = useCallback(() => {
42         return getBoldFormattedText(getSelectAllBannerText(isConversation, selectAll ? locationCount : mailPageSize));
43     }, [isConversation, locationCount, mailPageSize, selectAll]);
45     const getBannerTextWithLocation = useCallback(() => {
46         return getSelectAllBannerTextWithLocation(
47             isConversation,
48             selectAll ? locationCount : mailPageSize,
49             labelID,
50             labels,
51             folders
52         );
53     }, [isConversation, locationCount, mailPageSize, selectAll, labelID, labels, folders]);
55     const getButtonText = useCallback(() => {
56         return getSelectAllButtonText(selectAll, locationCount, labelID, labels, folders);
57     }, [selectAll, locationCount, labelID, labels, folders]);
59     const setSelectAll = (value: boolean) => {
60         dispatch(layoutActions.setSelectAll(value));
61     };
63     return {
64         selectAll,
65         setSelectAll,
66         locationCount,
67         getBannerText,
68         getBannerTextWithLocation,
69         getButtonText,
70         selectAllAvailable,
71     };