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