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