Merge branch 'feat/inda-383-daily-stat' into 'main'
[ProtonMail-WebClient.git] / applications / drive / src / app / components / sections / Drive / DriveContextMenu.tsx
blobd513954d12d5bfb3668ddc92fed1d1d30b314703
1 import { useMemo } from 'react';
3 import { ContextSeparator } from '@proton/components';
4 import type { SHARE_MEMBER_PERMISSIONS} from '@proton/shared/lib/drive/permissions';
5 import { getCanAdmin, getCanWrite } from '@proton/shared/lib/drive/permissions';
6 import { isPreviewAvailable } from '@proton/shared/lib/helpers/preview';
8 import type { DecryptedLink } from '../../../store';
9 import { useDriveSharingFlags } from '../../../store';
10 import { useOpenInDocs } from '../../../store/_documents';
11 import type { ContextMenuProps } from '../../FileBrowser/interface';
12 import { useDetailsModal } from '../../modals/DetailsModal';
13 import { useFilesDetailsModal } from '../../modals/FilesDetailsModal';
14 import { useMoveToFolderModal } from '../../modals/MoveToFolderModal/MoveToFolderModal';
15 import { useRenameModal } from '../../modals/RenameModal';
16 import { useRevisionsModal } from '../../modals/RevisionsModal/RevisionsModal';
17 import { useLinkSharingModal } from '../../modals/ShareLinkModal/ShareLinkModal';
18 import {
19     CopyLinkButton,
20     DetailsButton,
21     DownloadButton,
22     OpenInDocsButton,
23     PreviewButton,
24     RenameButton,
25     RevisionsButton,
26     ShareLinkButton,
27 } from '../ContextMenu';
28 import { ItemContextMenu } from '../ContextMenu/ItemContextMenu';
29 import ShareLinkButtonLEGACY from '../ContextMenu/buttons/_legacy/ShareLinkButtonLEGACY';
30 import { MoveToFolderButton, MoveToTrashButton } from './ContextMenuButtons';
32 export function DriveItemContextMenu({
33     permissions,
34     shareId,
35     selectedLinks,
36     anchorRef,
37     isOpen,
38     position,
39     open,
40     close,
41     children,
42     isActiveLinkReadOnly,
43 }: ContextMenuProps & {
44     permissions: SHARE_MEMBER_PERMISSIONS;
45     shareId: string;
46     selectedLinks: DecryptedLink[];
47     isActiveLinkReadOnly?: boolean;
48 }) {
49     const selectedLink = selectedLinks.length > 0 ? selectedLinks[0] : undefined;
50     const isOnlyOneItem = selectedLinks.length === 1 && !!selectedLink;
51     const isOnlyOneFileItem = isOnlyOneItem && selectedLink.isFile;
53     const isEditor = useMemo(() => getCanWrite(permissions), [permissions]);
54     const isAdmin = useMemo(() => getCanAdmin(permissions), [permissions]);
56     const hasPreviewAvailable =
57         isOnlyOneFileItem && selectedLink.mimeType && isPreviewAvailable(selectedLink.mimeType, selectedLink.size);
58     const hasLink = isOnlyOneItem && selectedLink.shareUrl && !selectedLink.shareUrl.isExpired && !selectedLink.trashed;
59     const [detailsModal, showDetailsModal] = useDetailsModal();
60     const [filesDetailsModal, showFilesDetailsModal] = useFilesDetailsModal();
61     const [moveToFolderModal, showMoveToFolderModal] = useMoveToFolderModal();
62     const [renameModal, showRenameModal] = useRenameModal();
63     const [linkSharingModal, showLinkSharingModal] = useLinkSharingModal();
65     const [revisionsModal, showRevisionsModal] = useRevisionsModal();
66     const { isSharingInviteAvailable } = useDriveSharingFlags();
68     const { showOpenInDocs } = useOpenInDocs(selectedLink);
70     const ShareLinkButtonComponent = isSharingInviteAvailable ? ShareLinkButton : ShareLinkButtonLEGACY;
72     return (
73         <>
74             <ItemContextMenu isOpen={isOpen} open={open} close={close} position={position} anchorRef={anchorRef}>
75                 {hasPreviewAvailable && (
76                     <PreviewButton shareId={selectedLink.rootShareId} linkId={selectedLink.linkId} close={close} />
77                 )}
78                 {isOnlyOneFileItem && showOpenInDocs && (
79                     <OpenInDocsButton selectedBrowserItem={selectedLink} close={close} />
80                 )}
81                 {(hasPreviewAvailable || (isOnlyOneFileItem && showOpenInDocs)) && <ContextSeparator />}
82                 <DownloadButton selectedBrowserItems={selectedLinks} close={close} />
83                 {isAdmin && hasLink && (
84                     <CopyLinkButton shareId={selectedLink.rootShareId} linkId={selectedLink.linkId} close={close} />
85                 )}
86                 {isAdmin && isOnlyOneItem && (
87                     <ShareLinkButtonComponent
88                         shareId={shareId}
89                         showLinkSharingModal={showLinkSharingModal}
90                         link={selectedLink}
91                         close={close}
92                     />
93                 )}
94                 <ContextSeparator />
95                 {isEditor && !isActiveLinkReadOnly ? (
96                     <MoveToFolderButton
97                         shareId={shareId}
98                         selectedLinks={selectedLinks}
99                         showMoveToFolderModal={showMoveToFolderModal}
100                         close={close}
101                     />
102                 ) : null}
103                 {isEditor && isOnlyOneItem && !isActiveLinkReadOnly && (
104                     <RenameButton showRenameModal={showRenameModal} link={selectedLink} close={close} />
105                 )}
106                 <DetailsButton
107                     selectedBrowserItems={selectedLinks}
108                     showDetailsModal={showDetailsModal}
109                     showFilesDetailsModal={showFilesDetailsModal}
110                     close={close}
111                 />
112                 {isEditor && <ContextSeparator />}
113                 {isEditor && isOnlyOneFileItem && (
114                     <>
115                         <RevisionsButton
116                             selectedLink={selectedLink}
117                             showRevisionsModal={showRevisionsModal}
118                             close={close}
119                         />
120                         <ContextSeparator />
121                     </>
122                 )}
123                 {isEditor && <MoveToTrashButton selectedLinks={selectedLinks} close={close} />}
124                 {children}
125             </ItemContextMenu>
126             {filesDetailsModal}
127             {detailsModal}
128             {moveToFolderModal}
129             {renameModal}
130             {linkSharingModal}
131             {revisionsModal}
132         </>
133     );