| 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 {tracker} from './analytics'; |
| 13 | import {codeReviewProvider, diffSummary} from './codeReview/CodeReviewInfo'; |
| 14 | import {submitAsDraft} from './codeReview/DraftCheckbox'; |
| 15 | import {publishWhenReady} from './codeReview/PublishWhenReadyCheckbox'; |
| 16 | import {t, T} from './i18n'; |
| 17 | import {readAtom} from './jotaiUtils'; |
| 18 | import {useRunOperation} from './operationsState'; |
| 19 | import {dagWithPreviews} from './previews'; |
| 20 | |
| 21 | export function SubmitSingleCommitButton() { |
| 22 | const dag = useAtomValue(dagWithPreviews); |
| 23 | const headCommit = dag.resolve('.'); |
| 24 | |
| 25 | const provider = useAtomValue(codeReviewProvider); |
| 26 | const diff = useAtomValue(diffSummary(headCommit?.diffId)); |
| 27 | const isClosed = provider != null && diff.value != null && provider?.isDiffClosed(diff.value); |
| 28 | |
| 29 | const runOperation = useRunOperation(); |
| 30 | |
| 31 | if (!headCommit || !provider) { |
| 32 | return null; |
| 33 | } |
| 34 | |
| 35 | const draftAncestors = dag.ancestors(headCommit.hash, {within: dag.draft()}); |
| 36 | const isSingleCommit = draftAncestors.size === 1; |
| 37 | const hasDiff = headCommit.diffId !== undefined; |
| 38 | |
| 39 | if (!isSingleCommit || isClosed || hasDiff) { |
| 40 | return null; |
| 41 | } |
| 42 | |
| 43 | const actionLabel = provider.submitButtonLabel ?? 'Submit'; |
| 44 | const tooltip = t('$action this commit with $cmd.', { |
| 45 | replace: {$action: actionLabel, $cmd: provider.submitCommandName()}, |
| 46 | }); |
| 47 | |
| 48 | return ( |
| 49 | <Tooltip delayMs={DOCUMENTATION_DELAY} title={tooltip}> |
| 50 | <Button |
| 51 | onClick={e => { |
| 52 | e.stopPropagation(); |
| 53 | tracker.track('SubmitSingleCommit'); |
| 54 | const draft = readAtom(submitAsDraft); |
| 55 | runOperation( |
| 56 | provider.submitOperation([], { |
| 57 | draft: draft ?? false, |
| 58 | publishWhenReady: readAtom(publishWhenReady), |
| 59 | }), |
| 60 | ); |
| 61 | }} |
| 62 | icon |
| 63 | data-testid="submit-button"> |
| 64 | <Icon icon="cloud-upload" slot="start" /> |
| 65 | <T>{provider.submitButtonLabel ?? 'Submit'}</T> |
| 66 | </Button> |
| 67 | </Tooltip> |
| 68 | ); |
| 69 | } |
| 70 | |