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';
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';
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(
48 selectAll ? locationCount : mailPageSize,
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));
68 getBannerTextWithLocation,