| 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 {DOCUMENTATION_DELAY, Tooltip} from 'isl-components/Tooltip'; |
| 11 | import {useAtomValue} from 'jotai'; |
| 12 | import {getChangedFilesForHash} from './ChangedFilesWithFetching'; |
| 13 | import {codeReviewProvider, diffSummary} from './codeReview/CodeReviewInfo'; |
| 14 | import {t, T} from './i18n'; |
| 15 | import {UncommitOperation} from './operations/Uncommit'; |
| 16 | import {useRunOperation} from './operationsState'; |
| 17 | import platform from './platform'; |
| 18 | import {dagWithPreviews} from './previews'; |
| 19 | |
| 20 | export function UncommitButton() { |
| 21 | const dag = useAtomValue(dagWithPreviews); |
| 22 | const headCommit = dag.resolve('.'); |
| 23 | |
| 24 | const provider = useAtomValue(codeReviewProvider); |
| 25 | const diff = useAtomValue(diffSummary(headCommit?.diffId)); |
| 26 | const isClosed = provider != null && diff.value != null && provider?.isDiffClosed(diff.value); |
| 27 | |
| 28 | const runOperation = useRunOperation(); |
| 29 | if (!headCommit) { |
| 30 | return null; |
| 31 | } |
| 32 | |
| 33 | const hasChildren = dag.children(headCommit?.hash).size > 0; |
| 34 | |
| 35 | if (isClosed) { |
| 36 | return null; |
| 37 | } |
| 38 | return ( |
| 39 | <Tooltip |
| 40 | delayMs={DOCUMENTATION_DELAY} |
| 41 | title={ |
| 42 | hasChildren |
| 43 | ? t( |
| 44 | 'Go back to the previous commit, but keep the changes by skipping updating files in the working copy. Note: the original commit will not be deleted because it has children.', |
| 45 | ) |
| 46 | : t( |
| 47 | 'Hide this commit, but keep its changes as uncommitted changes, as if you never ran commit.', |
| 48 | ) |
| 49 | }> |
| 50 | <Button |
| 51 | onClick={async e => { |
| 52 | e.stopPropagation(); |
| 53 | const [confirmed, changedFilesResult] = await Promise.all([ |
| 54 | platform.confirm( |
| 55 | t('Are you sure you want to Uncommit?'), |
| 56 | hasChildren |
| 57 | ? t( |
| 58 | 'Uncommitting will not hide the original commit because it has children, but will move to the parent commit and keep its changes as uncommitted changes.', |
| 59 | ) |
| 60 | : t( |
| 61 | 'Uncommitting will hide this commit, but keep its changes as uncommitted changes, as if you never ran commit.', |
| 62 | ), |
| 63 | ), |
| 64 | getChangedFilesForHash(headCommit.hash), |
| 65 | ]); |
| 66 | if (!confirmed) { |
| 67 | return; |
| 68 | } |
| 69 | const changedFiles = |
| 70 | changedFilesResult.value?.filesSample ?? |
| 71 | headCommit.filePathsSample.map(path => ({ |
| 72 | path, |
| 73 | // In the event of a failure, just guess at it being Modified. This is just for the UI preview. |
| 74 | status: 'M', |
| 75 | })); |
| 76 | runOperation(new UncommitOperation(headCommit, changedFiles)); |
| 77 | }} |
| 78 | icon |
| 79 | data-testid="uncommit-button"> |
| 80 | <Icon icon="debug-step-out" slot="start" /> |
| 81 | <T>Uncommit</T> |
| 82 | </Button> |
| 83 | </Tooltip> |
| 84 | ); |
| 85 | } |
| 86 | |