addons/isl/src/UncommitButton.tsxblame
View source
b69ab311/**
b69ab312 * Copyright (c) Meta Platforms, Inc. and affiliates.
b69ab313 *
b69ab314 * This source code is licensed under the MIT license found in the
b69ab315 * LICENSE file in the root directory of this source tree.
b69ab316 */
b69ab317
b69ab318import {Button} from 'isl-components/Button';
b69ab319import {Icon} from 'isl-components/Icon';
b69ab3110import {DOCUMENTATION_DELAY, Tooltip} from 'isl-components/Tooltip';
b69ab3111import {useAtomValue} from 'jotai';
b69ab3112import {getChangedFilesForHash} from './ChangedFilesWithFetching';
b69ab3113import {codeReviewProvider, diffSummary} from './codeReview/CodeReviewInfo';
b69ab3114import {t, T} from './i18n';
b69ab3115import {UncommitOperation} from './operations/Uncommit';
b69ab3116import {useRunOperation} from './operationsState';
b69ab3117import platform from './platform';
b69ab3118import {dagWithPreviews} from './previews';
b69ab3119
b69ab3120export function UncommitButton() {
b69ab3121 const dag = useAtomValue(dagWithPreviews);
b69ab3122 const headCommit = dag.resolve('.');
b69ab3123
b69ab3124 const provider = useAtomValue(codeReviewProvider);
b69ab3125 const diff = useAtomValue(diffSummary(headCommit?.diffId));
b69ab3126 const isClosed = provider != null && diff.value != null && provider?.isDiffClosed(diff.value);
b69ab3127
b69ab3128 const runOperation = useRunOperation();
b69ab3129 if (!headCommit) {
b69ab3130 return null;
b69ab3131 }
b69ab3132
b69ab3133 const hasChildren = dag.children(headCommit?.hash).size > 0;
b69ab3134
b69ab3135 if (isClosed) {
b69ab3136 return null;
b69ab3137 }
b69ab3138 return (
b69ab3139 <Tooltip
b69ab3140 delayMs={DOCUMENTATION_DELAY}
b69ab3141 title={
b69ab3142 hasChildren
b69ab3143 ? t(
b69ab3144 '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.',
b69ab3145 )
b69ab3146 : t(
b69ab3147 'Hide this commit, but keep its changes as uncommitted changes, as if you never ran commit.',
b69ab3148 )
b69ab3149 }>
b69ab3150 <Button
b69ab3151 onClick={async e => {
b69ab3152 e.stopPropagation();
b69ab3153 const [confirmed, changedFilesResult] = await Promise.all([
b69ab3154 platform.confirm(
b69ab3155 t('Are you sure you want to Uncommit?'),
b69ab3156 hasChildren
b69ab3157 ? t(
b69ab3158 '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.',
b69ab3159 )
b69ab3160 : t(
b69ab3161 'Uncommitting will hide this commit, but keep its changes as uncommitted changes, as if you never ran commit.',
b69ab3162 ),
b69ab3163 ),
b69ab3164 getChangedFilesForHash(headCommit.hash),
b69ab3165 ]);
b69ab3166 if (!confirmed) {
b69ab3167 return;
b69ab3168 }
b69ab3169 const changedFiles =
b69ab3170 changedFilesResult.value?.filesSample ??
b69ab3171 headCommit.filePathsSample.map(path => ({
b69ab3172 path,
b69ab3173 // In the event of a failure, just guess at it being Modified. This is just for the UI preview.
b69ab3174 status: 'M',
b69ab3175 }));
b69ab3176 runOperation(new UncommitOperation(headCommit, changedFiles));
b69ab3177 }}
b69ab3178 icon
b69ab3179 data-testid="uncommit-button">
b69ab3180 <Icon icon="debug-step-out" slot="start" />
b69ab3181 <T>Uncommit</T>
b69ab3182 </Button>
b69ab3183 </Tooltip>
b69ab3184 );
b69ab3185}