1 import { useState } from 'react';
3 import { c } from 'ttag';
5 import { Button } from '@proton/atoms';
6 import { Dropdown, DropdownCaret, DropdownMenu, DropdownMenuButton, usePopperAnchor } from '@proton/components';
7 import { SORT_DIRECTION } from '@proton/shared/lib/constants';
8 import generateUID from '@proton/utils/generateUID';
10 import type { SortParams } from '../FileBrowser/interface';
12 export default function SortDropdown<T extends string>({
19 sortField: SortParams<T>['sortField'];
20 onSort?: (sortParams: SortParams<T>) => void;
23 const [uid] = useState(generateUID('dropdown'));
24 const { anchorRef, isOpen, toggle, close } = usePopperAnchor<HTMLButtonElement>();
26 const dropdownMenuButtons = sortFields?.map((sortFieldToCheck) => (
28 key={sortFieldToCheck}
29 className="flex flex-nowrap text-left"
30 onClick={() => onSort?.({ sortField: sortFieldToCheck, sortOrder: SORT_DIRECTION.ASC })}
31 aria-current={sortField === sortFieldToCheck}
33 {translateSortField(sortFieldToCheck)}
41 aria-describedby={uid}
43 aria-expanded={isOpen}
45 data-testid="toolbar-sort"
46 title={c('Title').t`Sort files/folders`}
51 <DropdownCaret isOpen={isOpen} className="expand-caret toolbar-icon my-auto" size={4} />
58 originalPlacement="bottom-start"
59 className="dropdown--no-max-size"
61 <DropdownMenu>{dropdownMenuButtons}</DropdownMenu>
67 export function translateSortField(sortField: string): string {
68 const translations: Record<string, string> = {
69 name: c('Label').t`Name`,
70 size: c('Label').t`Size`,
71 // Type is not used (in UI) at this moment, but users might have set
72 // it, so we keep the code, so it continue to work until user changes
73 // the sorting. Also, we might get it back at some point.
74 mimeType: c('Label').t`Type`,
75 fileModifyTime: c('Label').t`Modified`,
76 metaDataModifyTime: c('Label').t`Modified`,
77 linkCreateTime: c('Label').t`Created`,
78 linkExpireTime: c('Label').t`Expires`,
79 numAccesses: c('Label').t`# of downloads`,
80 sharedOn: c('Label').t`Shared by`,
81 sharedBy: c('Label').t`Shared on`,
82 trashed: c('Label').t`Deleted`,
84 return translations[sortField];