| b69ab31 | | | 1 | /** |
| b69ab31 | | | 2 | * Copyright (c) Meta Platforms, Inc. and affiliates. |
| b69ab31 | | | 3 | * |
| b69ab31 | | | 4 | * This source code is licensed under the MIT license found in the |
| b69ab31 | | | 5 | * LICENSE file in the root directory of this source tree. |
| b69ab31 | | | 6 | */ |
| b69ab31 | | | 7 | |
| b69ab31 | | | 8 | import type {Operation} from './operations/Operation'; |
| b69ab31 | | | 9 | |
| b69ab31 | | | 10 | import {Button} from 'isl-components/Button'; |
| b69ab31 | | | 11 | import {ButtonDropdown} from 'isl-components/ButtonDropdown'; |
| b69ab31 | | | 12 | import {Icon} from 'isl-components/Icon'; |
| b69ab31 | | | 13 | import {DOCUMENTATION_DELAY, Tooltip} from 'isl-components/Tooltip'; |
| b69ab31 | | | 14 | import {useAtom, useAtomValue} from 'jotai'; |
| b69ab31 | | | 15 | import {fetchStableLocations} from './BookmarksData'; |
| b69ab31 | | | 16 | import {Internal} from './Internal'; |
| b69ab31 | | | 17 | import {t, T} from './i18n'; |
| b69ab31 | | | 18 | import {configBackedAtom} from './jotaiUtils'; |
| b69ab31 | | | 19 | import {PullOperation} from './operations/PullOperation'; |
| b69ab31 | | | 20 | import {useRunOperation} from './operationsState'; |
| b69ab31 | | | 21 | import {uncommittedChangesWithPreviews, useMostRecentPendingOperation} from './previews'; |
| b69ab31 | | | 22 | |
| b69ab31 | | | 23 | import './PullButton.css'; |
| b69ab31 | | | 24 | |
| b69ab31 | | | 25 | const DEFAULT_PULL_BUTTON = { |
| b69ab31 | | | 26 | id: 'pull', |
| b69ab31 | | | 27 | label: <T>Pull</T>, |
| b69ab31 | | | 28 | getOperation: () => new PullOperation(), |
| b69ab31 | | | 29 | isRunning: (op: Operation) => op instanceof PullOperation, |
| b69ab31 | | | 30 | tooltip: t('Fetch latest repository and branch information from remote.'), |
| b69ab31 | | | 31 | allowWithUncommittedChanges: true, |
| b69ab31 | | | 32 | }; |
| b69ab31 | | | 33 | const pullButtonChoiceKey = configBackedAtom<string>( |
| b69ab31 | | | 34 | 'isl.pull-button-choice', |
| b69ab31 | | | 35 | DEFAULT_PULL_BUTTON.id, |
| b69ab31 | | | 36 | ); |
| b69ab31 | | | 37 | |
| b69ab31 | | | 38 | export type PullButtonOption = { |
| b69ab31 | | | 39 | id: string; |
| b69ab31 | | | 40 | label: React.ReactNode; |
| b69ab31 | | | 41 | getOperation: () => Operation; |
| b69ab31 | | | 42 | isRunning: (op: Operation) => boolean; |
| b69ab31 | | | 43 | tooltip: string; |
| b69ab31 | | | 44 | allowWithUncommittedChanges: boolean; |
| b69ab31 | | | 45 | }; |
| b69ab31 | | | 46 | |
| b69ab31 | | | 47 | export function PullButton() { |
| b69ab31 | | | 48 | const runOperation = useRunOperation(); |
| b69ab31 | | | 49 | |
| b69ab31 | | | 50 | const pullButtonOptions: Array<PullButtonOption> = []; |
| b69ab31 | | | 51 | pullButtonOptions.push(DEFAULT_PULL_BUTTON, ...(Internal.additionalPullOptions ?? [])); |
| b69ab31 | | | 52 | |
| b69ab31 | | | 53 | const [dropdownChoiceKey, setDropdownChoiceKey] = useAtom(pullButtonChoiceKey); |
| b69ab31 | | | 54 | const currentChoice = |
| b69ab31 | | | 55 | pullButtonOptions.find(option => option.id === dropdownChoiceKey) ?? pullButtonOptions[0]; |
| b69ab31 | | | 56 | |
| b69ab31 | | | 57 | const trackedChanges = useAtomValue(uncommittedChangesWithPreviews).filter( |
| b69ab31 | | | 58 | change => change.status !== '?', |
| b69ab31 | | | 59 | ); |
| b69ab31 | | | 60 | const hasUncommittedChanges = trackedChanges.length > 0; |
| b69ab31 | | | 61 | |
| b69ab31 | | | 62 | const disabledFromUncommittedChanges = |
| b69ab31 | | | 63 | currentChoice.allowWithUncommittedChanges === false && hasUncommittedChanges; |
| b69ab31 | | | 64 | |
| b69ab31 | | | 65 | let tooltip = |
| b69ab31 | | | 66 | currentChoice.tooltip + |
| b69ab31 | | | 67 | (disabledFromUncommittedChanges == false |
| b69ab31 | | | 68 | ? '' |
| b69ab31 | | | 69 | : '\n\n' + t('Disabled due to uncommitted changes.')); |
| b69ab31 | | | 70 | const pendingOperation = useMostRecentPendingOperation(); |
| b69ab31 | | | 71 | const isRunningPull = pendingOperation != null && currentChoice.isRunning(pendingOperation); |
| b69ab31 | | | 72 | if (isRunningPull) { |
| b69ab31 | | | 73 | tooltip += '\n\n' + t('Pull is already running.'); |
| b69ab31 | | | 74 | } |
| b69ab31 | | | 75 | |
| b69ab31 | | | 76 | return ( |
| b69ab31 | | | 77 | <Tooltip placement="bottom" delayMs={DOCUMENTATION_DELAY} title={tooltip}> |
| b69ab31 | | | 78 | <div className="pull-info"> |
| b69ab31 | | | 79 | {pullButtonOptions.length > 1 ? ( |
| b69ab31 | | | 80 | <ButtonDropdown |
| b69ab31 | | | 81 | buttonDisabled={!!isRunningPull || disabledFromUncommittedChanges} |
| b69ab31 | | | 82 | options={pullButtonOptions} |
| b69ab31 | | | 83 | onClick={() => { |
| b69ab31 | | | 84 | runOperation(currentChoice.getOperation()); |
| b69ab31 | | | 85 | fetchStableLocations(); |
| b69ab31 | | | 86 | }} |
| b69ab31 | | | 87 | onChangeSelected={choice => setDropdownChoiceKey(choice.id)} |
| b69ab31 | | | 88 | selected={currentChoice} |
| b69ab31 | | | 89 | icon={<Icon slot="start" icon={isRunningPull ? 'loading' : 'repo'} />} |
| b69ab31 | | | 90 | /> |
| b69ab31 | | | 91 | ) : ( |
| b69ab31 | | | 92 | <Button |
| b69ab31 | | | 93 | disabled={!!isRunningPull} |
| b69ab31 | | | 94 | onClick={() => { |
| b69ab31 | | | 95 | runOperation(new PullOperation()); |
| b69ab31 | | | 96 | fetchStableLocations(); |
| b69ab31 | | | 97 | }}> |
| b69ab31 | | | 98 | <Icon slot="start" icon={isRunningPull ? 'loading' : 'cloud-download'} /> |
| b69ab31 | | | 99 | <T>Pull</T> |
| b69ab31 | | | 100 | </Button> |
| b69ab31 | | | 101 | )} |
| b69ab31 | | | 102 | </div> |
| b69ab31 | | | 103 | </Tooltip> |
| b69ab31 | | | 104 | ); |
| b69ab31 | | | 105 | } |