import { useAntToken } from '@/styles/useAntToken'; import React, { useMemo } from 'react'; import { Dropdown, Divider, MenuProps } from 'antd'; import { AppMaterialIcons } from '@/components/ui'; import { ShareRole } from '@/api/asset_interfaces'; import { DropdownLabel } from '@/components/ui/dropdown'; import { Text } from '@/components/ui'; import { useMemoizedFn } from 'ahooks'; export const AccessDropdown: React.FC<{ groupShare?: boolean; className?: string; showRemove?: boolean; shareLevel?: ShareRole | null; onChangeShareLevel?: (level: ShareRole | null) => void; }> = ({ shareLevel, showRemove = true, groupShare = false, className = '', onChangeShareLevel }) => { const token = useAntToken(); const disabled = useMemo(() => { return shareLevel === ShareRole.OWNER; }, [shareLevel]); const items = useMemo( () => groupShare ? [ ...standardItems, { label: , key: 'notShared' } ] : ([ ...standardItems, showRemove && { label: 'Remove', key: 'remove' } ].filter(Boolean) as { label: string | React.ReactNode; key: string; }[]), [groupShare, standardItems, showRemove] ); const selectedItem = useMemo( () => groupShare && !shareLevel ? items.find((v) => v.key === 'notShared')! : items.find((v) => v.key === shareLevel) || items[items.length - 1], [groupShare, shareLevel, items] ); const selectedLabel = useMemo(() => { if (!selectedItem) return 'No shared'; if (selectedItem.key === ShareRole.OWNER) return 'Full access'; if (selectedItem.key === ShareRole.EDITOR) return 'Can edit'; if (selectedItem.key === ShareRole.VIEWER) return 'Can view'; if (selectedItem.key === 'remove') return 'Remove'; if (selectedItem.key === 'notShared') return 'Not shared'; return selectedItem.label; }, [selectedItem, items]); const onSelectMenuItem = useMemoizedFn(({ key }: { key: string }) => { if (key === 'remove' || key === 'notShared') { onChangeShareLevel?.(null); } else { onChangeShareLevel?.(key as ShareRole); } }); const dropdownRender = useMemoizedFn((menu: React.ReactNode) => { return (
{React.cloneElement(menu as React.ReactElement, { style: { boxShadow: 'none' } })}
Sharing cannot override permissions set by your account admins.
); }); const memoizedMenu: MenuProps = useMemo(() => { return { items, selectable: true, defaultSelectedKeys: [items[0]?.key as string], selectedKeys: [selectedItem.key as string], onSelect: onSelectMenuItem }; }, [items, onSelectMenuItem, selectedItem]); return (
{selectedLabel}
{!disabled && }
); }; const standardItems = [ { label: , key: ShareRole.OWNER }, { label: , key: ShareRole.EDITOR }, { label: , key: ShareRole.VIEWER } ];