| 1 | /** |
| 2 | * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | * |
| 4 | * This source code is licensed under the MIT license found in the |
| 5 | * LICENSE file in the root directory of this source tree. |
| 6 | */ |
| 7 | |
| 8 | import {Button} from 'isl-components/Button'; |
| 9 | import {Icon} from 'isl-components/Icon'; |
| 10 | import {isMac} from 'isl-components/OperatingSystem'; |
| 11 | import {Tooltip} from 'isl-components/Tooltip'; |
| 12 | import {useAtom} from 'jotai'; |
| 13 | import {useContextMenu} from 'shared/ContextMenu'; |
| 14 | import {Row} from './ComponentUtils'; |
| 15 | import {t, T} from './i18n'; |
| 16 | import {configBackedAtom} from './jotaiUtils'; |
| 17 | |
| 18 | export type ChangedFilesDisplayType = 'short' | 'fullPaths' | 'tree' | 'fish'; |
| 19 | |
| 20 | export const defaultChangedFilesDisplayType: ChangedFilesDisplayType = 'short'; |
| 21 | |
| 22 | export const changedFilesDisplayType = configBackedAtom<ChangedFilesDisplayType>( |
| 23 | 'isl.changedFilesDisplayType', |
| 24 | defaultChangedFilesDisplayType, |
| 25 | ); |
| 26 | |
| 27 | type ChangedFileDisplayTypeOption = {icon: string; label: JSX.Element}; |
| 28 | const ChangedFileDisplayTypeOptions: Record<ChangedFilesDisplayType, ChangedFileDisplayTypeOption> = |
| 29 | { |
| 30 | short: {icon: 'list-selection', label: <T>Short file names</T>}, |
| 31 | fullPaths: {icon: 'menu', label: <T>Full file paths</T>}, |
| 32 | tree: {icon: 'list-tree', label: <T>Tree</T>}, |
| 33 | fish: {icon: 'whole-word', label: <T>One-letter directories</T>}, |
| 34 | }; |
| 35 | const entries = Object.entries(ChangedFileDisplayTypeOptions) as Array< |
| 36 | [ChangedFilesDisplayType, ChangedFileDisplayTypeOption] |
| 37 | >; |
| 38 | |
| 39 | export function ChangedFileDisplayTypePicker() { |
| 40 | const [displayType, setDisplayType] = useAtom(changedFilesDisplayType); |
| 41 | |
| 42 | const actions = entries.map(([type, options]) => ({ |
| 43 | label: ( |
| 44 | <Row> |
| 45 | <Icon icon={displayType === type ? 'check' : 'blank'} slot="start" /> |
| 46 | <Icon icon={options.icon} slot="start" /> |
| 47 | {options.label} |
| 48 | </Row> |
| 49 | ), |
| 50 | onClick: () => setDisplayType(type), |
| 51 | })); |
| 52 | const contextMenu = useContextMenu(() => actions); |
| 53 | |
| 54 | return ( |
| 55 | <Tooltip |
| 56 | title={t( |
| 57 | isMac |
| 58 | ? 'Change how file paths are displayed.\n\nTip: Hold the alt key to quickly see full file paths.' |
| 59 | : 'Change how file paths are displayed.\n\nTip: Hold the ctrl key to quickly see full file paths.', |
| 60 | )}> |
| 61 | <Button |
| 62 | icon |
| 63 | className="changed-file-display-type-picker" |
| 64 | data-testid="changed-file-display-type-picker" |
| 65 | onClick={contextMenu}> |
| 66 | <Icon icon={ChangedFileDisplayTypeOptions[displayType].icon} /> |
| 67 | </Button> |
| 68 | </Tooltip> |
| 69 | ); |
| 70 | } |
| 71 | |