| 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 {Kbd} from 'isl-components/Kbd'; |
| 11 | import {KeyCode, Modifier} from 'isl-components/KeyboardShortcuts'; |
| 12 | import {Tooltip} from 'isl-components/Tooltip'; |
| 13 | import {useCallback} from 'react'; |
| 14 | import {useCommand} from './ISLShortcuts'; |
| 15 | import {islDrawerState} from './drawerState'; |
| 16 | import {t, T} from './i18n'; |
| 17 | import {readAtom, writeAtom} from './jotaiUtils'; |
| 18 | import {dagWithPreviews} from './previews'; |
| 19 | import {selectedCommits} from './selection'; |
| 20 | |
| 21 | /** By default, "select all" selects draft, non-obsoleted commits. */ |
| 22 | function getSelectAllCommitHashSet(): Set<string> { |
| 23 | const dag = readAtom(dagWithPreviews); |
| 24 | return new Set( |
| 25 | dag |
| 26 | .nonObsolete(dag.draft()) |
| 27 | .toArray() |
| 28 | .filter(hash => !hash.startsWith('OPTIMISTIC')), |
| 29 | ); |
| 30 | } |
| 31 | |
| 32 | export function useSelectAllCommitsShortcut() { |
| 33 | const cb = useSelectAllCommits(); |
| 34 | useCommand('SelectAllCommits', cb); |
| 35 | } |
| 36 | |
| 37 | export function useSelectAllCommits() { |
| 38 | return useCallback(() => { |
| 39 | const draftCommits = getSelectAllCommitHashSet(); |
| 40 | writeAtom(selectedCommits, draftCommits); |
| 41 | // pop open sidebar so you can act on the bulk selection |
| 42 | writeAtom(islDrawerState, last => ({ |
| 43 | ...last, |
| 44 | right: { |
| 45 | ...last.right, |
| 46 | collapsed: false, |
| 47 | }, |
| 48 | })); |
| 49 | }, []); |
| 50 | } |
| 51 | |
| 52 | export function SelectAllButton({dismiss}: {dismiss: () => unknown}) { |
| 53 | const onClick = useSelectAllCommits(); |
| 54 | return ( |
| 55 | <Tooltip |
| 56 | title={t( |
| 57 | 'Select all draft commits. This allows more granular bulk manipulations in the sidebar.', |
| 58 | )}> |
| 59 | <Button |
| 60 | data-testid="select-all-button" |
| 61 | onClick={() => { |
| 62 | onClick(); |
| 63 | dismiss(); |
| 64 | }}> |
| 65 | <Icon icon="check-all" slot="start" /> |
| 66 | <T>Select all commits</T> |
| 67 | <Kbd keycode={KeyCode.A} modifiers={[Modifier.ALT]} /> |
| 68 | </Button> |
| 69 | </Tooltip> |
| 70 | ); |
| 71 | } |
| 72 | |