Merge branch 'feat/inda-383-daily-stat' into 'main'
[ProtonMail-WebClient.git] / applications / drive / src / app / components / sections / DrivePublic / DrivePublicContextMenu.tsx
blob6222e92201c4a06a2e6b7a038e1e227318d782e8
1 import { ContextSeparator } from '@proton/components';
2 import { isProtonDocument } from '@proton/shared/lib/helpers/mimetype';
3 import { isPreviewAvailable } from '@proton/shared/lib/helpers/preview';
5 import type { DecryptedLink } from '../../../store';
6 import type { ContextMenuProps } from '../../FileBrowser/interface';
7 import { useRenameModal } from '../../modals/RenameModal';
8 import { ItemContextMenu } from '../ContextMenu/ItemContextMenu';
9 import { DownloadButton, MoveToTrashButton, OpenInDocsButton, PreviewButton, RenameButton } from './ContextMenuButtons';
10 import { usePublicLinkOwnerInfo } from './utils/usePublicLinkOwnerInfo';
12 export function DrivePublicContextMenu({
13     selectedLinks,
14     anchorRef,
15     isOpen,
16     position,
17     open,
18     openPreview,
19     openInDocs,
20     close,
21     children,
22     isActiveLinkReadOnly,
23 }: ContextMenuProps & {
24     shareId: string;
25     selectedLinks: DecryptedLink[];
26     isActiveLinkReadOnly?: boolean;
27     openPreview: (item: DecryptedLink) => void;
28     openInDocs?: (linkId: string) => void;
29 }) {
30     const { isCreator, isLastEditor } = usePublicLinkOwnerInfo(selectedLinks);
31     const selectedLink = selectedLinks.length > 0 ? selectedLinks[0] : undefined;
32     const isOnlyOneItem = selectedLinks.length === 1 && !!selectedLink;
33     const isOnlyOneFileItem = isOnlyOneItem && selectedLink.isFile;
34     const hasPreviewAvailable =
35         isOnlyOneFileItem && selectedLink.mimeType && isPreviewAvailable(selectedLink.mimeType, selectedLink.size);
36     const [publicRenameModal, showPublicRenameModal] = useRenameModal();
37     const isDocument = isProtonDocument(selectedLink?.mimeType || '');
39     const showPreviewButton = hasPreviewAvailable;
40     const showOpenInDocsButton = isOnlyOneFileItem && isDocument && openInDocs;
42     return (
43         <>
44             <ItemContextMenu isOpen={isOpen} open={open} close={close} position={position} anchorRef={anchorRef}>
45                 {showPreviewButton && <PreviewButton link={selectedLink} openPreview={openPreview} close={close} />}
46                 {showOpenInDocsButton && (
47                     <OpenInDocsButton
48                         openInDocs={openInDocs}
49                         linkId={selectedLink.linkId}
50                         mimeType={selectedLink.mimeType}
51                         close={close}
52                     />
53                 )}
54                 {(showPreviewButton || showOpenInDocsButton) && <ContextSeparator />}
55                 <DownloadButton selectedBrowserItems={selectedLinks} close={close} />
56                 {isOnlyOneItem && !isActiveLinkReadOnly && isLastEditor && (
57                     <>
58                         <ContextSeparator />
59                         <RenameButton showRenameModal={showPublicRenameModal} link={selectedLink} close={close} />
60                     </>
61                 )}
63                 {isCreator && isLastEditor && (
64                     <>
65                         <ContextSeparator />
66                         <MoveToTrashButton selectedLinks={selectedLinks} close={close} />
67                     </>
68                 )}
69                 {children}
70             </ItemContextMenu>
71             {publicRenameModal}
72         </>
73     );