1 import type { ReactNode } from 'react';
3 import { c } from 'ttag';
5 import type { IconName } from '@proton/components';
14 } from '@proton/components';
15 import { SHARE_EXTERNAL_INVITATION_STATE } from '@proton/shared/lib/drive/constants';
16 import { SHARE_MEMBER_PERMISSIONS, SHARE_URL_PERMISSIONS } from '@proton/shared/lib/drive/permissions';
18 export const MenuItem = ({
29 <DropdownMenuButton className="text-left flex justify-space-between items-center flex-nowrap" onClick={onClick}>
30 <span className="flex items-center flex-nowrap mr-14">
31 {iconName && <Icon name={iconName} className="mr-2" />}
34 {isSelected ? <Icon name="checkmark" /> : null}
38 export const permissionsOptions = [SHARE_MEMBER_PERMISSIONS.VIEWER, SHARE_MEMBER_PERMISSIONS.EDITOR];
39 export const shareUrlPermissionsOptions = [SHARE_URL_PERMISSIONS.VIEWER, SHARE_URL_PERMISSIONS.EDITOR];
42 selectedPermissions: number;
43 onChangePermissions: (permissions: number) => void;
44 onRemoveAccess?: () => void;
45 onCopyShareInviteLink?: () => void;
46 onResendInvitationEmail?: () => void;
47 externalInvitationState?: SHARE_EXTERNAL_INVITATION_STATE;
50 autocompleteOptions?: boolean;
51 publicSharingOptions?: boolean;
54 export const PermissionsDropdownMenu = ({
59 onCopyShareInviteLink,
60 onResendInvitationEmail,
61 externalInvitationState,
63 autocompleteOptions = false,
64 publicSharingOptions = false,
66 const { anchorRef, isOpen, toggle, close } = usePopperAnchor<HTMLButtonElement>();
68 const externalInvitationStateLabels: { [key in SHARE_EXTERNAL_INVITATION_STATE]: string } = {
69 [SHARE_EXTERNAL_INVITATION_STATE.PENDING]: c('Status').t`Pending`,
70 [SHARE_EXTERNAL_INVITATION_STATE.USER_REGISTERED]: c('Status').t`Accepted`,
73 const memberPermissionsLabels = {
74 [SHARE_MEMBER_PERMISSIONS.VIEWER]: c('Label').t`Viewer`,
75 [SHARE_MEMBER_PERMISSIONS.EDITOR]: c('Label').t`Editor`,
78 const memberPermissionsIcons: { [key in number]: IconName } = {
79 [SHARE_MEMBER_PERMISSIONS.VIEWER]: 'eye',
80 [SHARE_MEMBER_PERMISSIONS.EDITOR]: 'pencil',
83 const memberChangePermissionsLabels = {
84 [SHARE_MEMBER_PERMISSIONS.VIEWER]: c('Label').t`Make viewer`,
85 [SHARE_MEMBER_PERMISSIONS.EDITOR]: c('Label').t`Make editor`,
88 const publicSharingPermissionsLabels = {
89 [SHARE_URL_PERMISSIONS.VIEWER]: c('Label').t`Viewer`,
90 [SHARE_URL_PERMISSIONS.EDITOR]: c('Label').t`Editor`,
93 const getPermissionsOptionLabel = (permissions: number) => {
94 if (autocompleteOptions) {
95 return memberPermissionsLabels[permissions];
96 } else if (publicSharingPermissionsLabels) {
97 return publicSharingPermissionsLabels[permissions];
98 } else if (permissions === selectedPermissions) {
99 return externalInvitationState
100 ? `${memberPermissionsLabels[permissions]} (${externalInvitationStateLabels[externalInvitationState]})`
101 : memberPermissionsLabels[permissions];
103 return memberChangePermissionsLabels[permissions];
111 externalInvitationState === SHARE_EXTERNAL_INVITATION_STATE.PENDING
112 ? c('Tooltip').t`We have sent them an invite to access the item.`
118 className="self-center"
127 {externalInvitationState
128 ? externalInvitationStateLabels[externalInvitationState]
129 : memberPermissionsLabels[selectedPermissions]}
132 <Dropdown isOpen={isOpen} anchorRef={anchorRef} onClose={close}>
134 {(publicSharingOptions ? shareUrlPermissionsOptions : permissionsOptions).map((permissions) => {
135 const label = getPermissionsOptionLabel(permissions);
139 isSelected={permissions === selectedPermissions}
140 iconName={memberPermissionsIcons[permissions]}
142 onClick={() => onChangePermissions(permissions)}
146 {onResendInvitationEmail &&
147 (!externalInvitationState ||
148 externalInvitationState === SHARE_EXTERNAL_INVITATION_STATE.PENDING) && (
150 iconName="paper-plane-horizontal"
151 label={c('Action').t`Resend invite`}
152 onClick={onResendInvitationEmail}
155 {onCopyShareInviteLink && (
158 label={c('Action').t`Copy invite link`}
159 onClick={onCopyShareInviteLink}
163 <MenuItem iconName="cross-big" label={c('Action').t`Remove access`} onClick={onRemoveAccess} />