1 import { useEffect, useMemo } from 'react';
2 import * as React from 'react';
4 import { ContextMenu, ContextSeparator } from '@proton/components';
5 import type { SHARE_MEMBER_PERMISSIONS } from '@proton/shared/lib/drive/permissions';
6 import { getCanAdmin, getCanWrite } from '@proton/shared/lib/drive/permissions';
8 import { useActiveShare } from '../../../hooks/drive/useActiveShare';
9 import { useActions, useDriveSharingFlags, useFileUploadInput, useFolderUploadInput } from '../../../store';
10 import { useDocumentActions, useDriveDocsFeatureFlag } from '../../../store/_documents';
11 import type { ContextMenuProps } from '../../FileBrowser/interface';
12 import { useCreateFileModal } from '../../modals/CreateFileModal';
13 import { useCreateFolderModal } from '../../modals/CreateFolderModal';
14 import { useFileSharingModal } from '../../modals/SelectLinkToShareModal/SelectLinkToShareModal';
15 import { useLinkSharingModal } from '../../modals/ShareLinkModal/ShareLinkModal';
16 import { ShareFileButton } from '../ContextMenu/buttons';
17 import ShareFileButtonLEGACY from '../ContextMenu/buttons/_legacy/ShareFileButtonLEGACY';
18 import useIsEditEnabled from '../useIsEditEnabled';
19 import { CreateNewFileButton, CreateNewFolderButton, UploadFileButton, UploadFolderButton } from './ContextMenuButtons';
20 import CreateNewDocumentButton from './ContextMenuButtons/CreateNewDocumentButton';
22 export function FolderContextMenu({
31 }: ContextMenuProps & {
33 permissions: SHARE_MEMBER_PERMISSIONS;
34 isActiveLinkReadOnly?: boolean;
42 const isEditEnabled = useIsEditEnabled();
44 const { activeFolder } = useActiveShare();
45 const { createFolder } = useActions();
48 handleClick: fileClick,
49 handleChange: fileChange,
50 } = useFileUploadInput(activeFolder.shareId, activeFolder.linkId);
52 inputRef: folderInput,
53 handleClick: folderClick,
54 handleChange: folderChange,
55 } = useFolderUploadInput(activeFolder.shareId, activeFolder.linkId);
56 const [createFolderModal, showCreateFolderModal] = useCreateFolderModal();
57 const [createFileModal, showCreateFileModal] = useCreateFileModal();
58 const [fileSharingModal, showFileSharingModal] = useFileSharingModal();
59 const [linkSharingModal, showLinkSharingModal] = useLinkSharingModal();
60 const { isSharingInviteAvailable } = useDriveSharingFlags();
62 const isEditor = useMemo(() => getCanWrite(permissions), [permissions]);
63 const isAdmin = useMemo(() => getCanAdmin(permissions), [permissions]);
65 const ShareFileButtonComponent = isSharingInviteAvailable ? ShareFileButton : ShareFileButtonLEGACY;
67 const { createDocument } = useDocumentActions();
68 const { isDocsEnabled } = useDriveDocsFeatureFlag();
70 // All actions in this context menu needs editor permissions
75 // ContextMenu is removed from DOM when any action is executed but inputs
76 // need to stay rendered so onChange handler can work.
79 <input multiple type="file" ref={fileInput} className="hidden" onChange={fileChange} />
80 <input type="file" ref={folderInput} className="hidden" onChange={folderChange} />
85 <ContextMenu isOpen={isOpen} close={close} position={position} anchorRef={anchorRef}>
86 {!isActiveLinkReadOnly && (
87 <CreateNewFolderButton
89 action={() => showCreateFolderModal({ folder: activeFolder, createFolder })}
92 {isDocsEnabled && !isActiveLinkReadOnly && (
93 <CreateNewDocumentButton
97 shareId: activeFolder.shareId,
98 parentLinkId: activeFolder.linkId,
103 {isEditEnabled && !isActiveLinkReadOnly && (
104 <CreateNewFileButton close={close} action={() => showCreateFileModal({})} />
106 {!isActiveLinkReadOnly && <ContextSeparator />}
107 {!isActiveLinkReadOnly ? (
109 <UploadFileButton close={close} onClick={fileClick} />
110 <UploadFolderButton close={close} onClick={folderClick} />
113 {isAdmin && !isActiveLinkReadOnly && <ContextSeparator />}
115 <ShareFileButtonComponent
118 showFileSharingModal={showFileSharingModal}
119 showLinkSharingModal={showLinkSharingModal}