| 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 type {CommitInfo} from './types'; |
| b69ab31 | | | 9 | |
| b69ab31 | | | 10 | import {Tooltip} from 'isl-components/Tooltip'; |
| b69ab31 | | | 11 | import {atom, useAtomValue} from 'jotai'; |
| b69ab31 | | | 12 | import {HighlightCommitsWhileHovering} from './HighlightedCommits'; |
| b69ab31 | | | 13 | import {OperationDisabledButton} from './OperationDisabledButton'; |
| b69ab31 | | | 14 | import {multiSubmitUpdateMessage} from './SubmitUpdateMessageInput'; |
| b69ab31 | | | 15 | import {allDiffSummaries, codeReviewProvider} from './codeReview/CodeReviewInfo'; |
| b69ab31 | | | 16 | import {submitAsDraft} from './codeReview/DraftCheckbox'; |
| b69ab31 | | | 17 | import {publishWhenReady} from './codeReview/PublishWhenReadyCheckbox'; |
| b69ab31 | | | 18 | import {t, T} from './i18n'; |
| b69ab31 | | | 19 | import {readAtom, writeAtom} from './jotaiUtils'; |
| b69ab31 | | | 20 | import {dagWithPreviews} from './previews'; |
| b69ab31 | | | 21 | import {selectedCommits} from './selection'; |
| b69ab31 | | | 22 | |
| b69ab31 | | | 23 | /** |
| b69ab31 | | | 24 | * If the selected commits are submittable by the review provider, |
| b69ab31 | | | 25 | * they may be submit. |
| b69ab31 | | | 26 | */ |
| b69ab31 | | | 27 | export const submittableSelection = atom(get => { |
| b69ab31 | | | 28 | const selection = get(selectedCommits); |
| b69ab31 | | | 29 | if (selection.size < 2) { |
| b69ab31 | | | 30 | return undefined; |
| b69ab31 | | | 31 | } |
| b69ab31 | | | 32 | const provider = get(codeReviewProvider); |
| b69ab31 | | | 33 | const diffSummaries = get(allDiffSummaries); |
| b69ab31 | | | 34 | |
| b69ab31 | | | 35 | if (provider == null || diffSummaries == null) { |
| b69ab31 | | | 36 | return undefined; |
| b69ab31 | | | 37 | } |
| b69ab31 | | | 38 | |
| b69ab31 | | | 39 | const dag = get(dagWithPreviews); |
| b69ab31 | | | 40 | const commits = dag.getBatch(dag.sortAsc(dag.present(selection))); |
| b69ab31 | | | 41 | const submittable = |
| b69ab31 | | | 42 | (diffSummaries.value != null |
| b69ab31 | | | 43 | ? provider?.getSubmittableDiffs(commits, diffSummaries.value) |
| b69ab31 | | | 44 | : undefined) ?? []; |
| b69ab31 | | | 45 | |
| b69ab31 | | | 46 | return submittable; |
| b69ab31 | | | 47 | }); |
| b69ab31 | | | 48 | |
| b69ab31 | | | 49 | /** |
| b69ab31 | | | 50 | * Button to submit the selected commits, if applicable. |
| b69ab31 | | | 51 | * If `commit` is provided, only render the button if the commit is the bottom of the selected range. |
| b69ab31 | | | 52 | * If `commit` is null, always show the button if there are commits to submit. |
| b69ab31 | | | 53 | */ |
| b69ab31 | | | 54 | export function SubmitSelectionButton({commit}: {commit?: CommitInfo}) { |
| b69ab31 | | | 55 | const submittable = useAtomValue(submittableSelection); |
| b69ab31 | | | 56 | const provider = useAtomValue(codeReviewProvider); |
| b69ab31 | | | 57 | |
| b69ab31 | | | 58 | if ( |
| b69ab31 | | | 59 | provider == null || |
| b69ab31 | | | 60 | submittable == null || |
| b69ab31 | | | 61 | submittable.length < 2 || |
| b69ab31 | | | 62 | // show the button on the bottom commit of the submittable selection, if showing the button on a commit. |
| b69ab31 | | | 63 | (commit != null && submittable?.[0]?.hash !== commit.hash) |
| b69ab31 | | | 64 | ) { |
| b69ab31 | | | 65 | return null; |
| b69ab31 | | | 66 | } |
| b69ab31 | | | 67 | |
| b69ab31 | | | 68 | return ( |
| b69ab31 | | | 69 | <Tooltip |
| b69ab31 | | | 70 | title={t('Submit $count selected commits for code review with $provider', { |
| b69ab31 | | | 71 | replace: { |
| b69ab31 | | | 72 | $count: String(submittable.length), |
| b69ab31 | | | 73 | $provider: provider.label ?? 'remote', |
| b69ab31 | | | 74 | }, |
| b69ab31 | | | 75 | })}> |
| b69ab31 | | | 76 | <HighlightCommitsWhileHovering toHighlight={submittable}> |
| b69ab31 | | | 77 | <OperationDisabledButton |
| b69ab31 | | | 78 | runOperation={() => { |
| b69ab31 | | | 79 | const updateMessage = readAtom(multiSubmitUpdateMessage(submittable)); |
| b69ab31 | | | 80 | // clear update message on submit |
| b69ab31 | | | 81 | writeAtom(multiSubmitUpdateMessage(submittable), ''); |
| b69ab31 | | | 82 | return provider.submitOperation(submittable, { |
| b69ab31 | | | 83 | draft: readAtom(submitAsDraft), |
| b69ab31 | | | 84 | updateMessage: updateMessage || undefined, |
| b69ab31 | | | 85 | publishWhenReady: readAtom(publishWhenReady), |
| b69ab31 | | | 86 | }); |
| b69ab31 | | | 87 | }} |
| b69ab31 | | | 88 | contextKey={`submit-selection-${submittable[0].hash}`}> |
| 8d8e815 | | | 89 | <T replace={{$count: submittable.length, $action: provider.submitButtonLabel ?? 'Submit'}}> |
| 8d8e815 | | | 90 | $action $count commits |
| 8d8e815 | | | 91 | </T> |
| b69ab31 | | | 92 | </OperationDisabledButton> |
| b69ab31 | | | 93 | </HighlightCommitsWhileHovering> |
| b69ab31 | | | 94 | </Tooltip> |
| b69ab31 | | | 95 | ); |
| b69ab31 | | | 96 | } |