Merge branch 'INDA-330-pii-update' into 'main'
[ProtonMail-WebClient.git] / applications / mail / src / app / hooks / actions / markAs / useMarkAs.tsx
blob268a052dfa5a2fa6635575dacfbc36b1a5426e93
1 import { useCallback } from 'react';
3 import type { MARK_AS_STATUS } from '@proton/shared/lib/mail/constants';
5 import { useMarkAllAs } from 'proton-mail/hooks/actions/markAs/useMarkAllAs';
6 import { useMarkSelectionAs } from 'proton-mail/hooks/actions/markAs/useMarkSelectionAs';
8 import { isMessage as testIsMessage } from '../../../helpers/elements';
9 import type { Element } from '../../../models/element';
11 export interface MarkAsParams {
12     elements: Element[];
13     labelID?: string;
14     status: MARK_AS_STATUS;
15     silent?: boolean;
16     selectAll?: boolean;
17     onCheckAll?: (check: boolean) => void;
19 export const useMarkAs = () => {
20     const markSelectionAs = useMarkSelectionAs();
21     const { markAllAs, selectAllMarkModal } = useMarkAllAs();
23     const markAs = useCallback(
24         async ({ elements, labelID = '', status, silent = true, selectAll, onCheckAll }: MarkAsParams) => {
25             if (!elements.length) {
26                 return;
27             }
29             const isMessage = testIsMessage(elements[0]);
31             if (selectAll) {
32                 await markAllAs({ isMessage, labelID, status, onCheckAll });
33             } else {
34                 void markSelectionAs({
35                     elements,
36                     labelID,
37                     status,
38                     silent,
39                     isMessage,
40                 });
41             }
42         },
43         [markAllAs, markSelectionAs]
44     );
46     return { markAs, selectAllMarkModal };