2.1 KB72 lines
Blame
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
8import {Button} from 'isl-components/Button';
9import {Icon} from 'isl-components/Icon';
10import {Kbd} from 'isl-components/Kbd';
11import {KeyCode, Modifier} from 'isl-components/KeyboardShortcuts';
12import {Tooltip} from 'isl-components/Tooltip';
13import {useCallback} from 'react';
14import {useCommand} from './ISLShortcuts';
15import {islDrawerState} from './drawerState';
16import {t, T} from './i18n';
17import {readAtom, writeAtom} from './jotaiUtils';
18import {dagWithPreviews} from './previews';
19import {selectedCommits} from './selection';
20
21/** By default, "select all" selects draft, non-obsoleted commits. */
22function 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
32export function useSelectAllCommitsShortcut() {
33 const cb = useSelectAllCommits();
34 useCommand('SelectAllCommits', cb);
35}
36
37export 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
52export 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