| 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 {Button} from 'isl-components/Button'; |
| b69ab31 | | | 11 | import {Checkbox} from 'isl-components/Checkbox'; |
| b69ab31 | | | 12 | import {Divider} from 'isl-components/Divider'; |
| b69ab31 | | | 13 | import {TextField} from 'isl-components/TextField'; |
| b69ab31 | | | 14 | import {Tooltip} from 'isl-components/Tooltip'; |
| b69ab31 | | | 15 | import {useAtom, useAtomValue} from 'jotai'; |
| b69ab31 | | | 16 | import {useState} from 'react'; |
| b69ab31 | | | 17 | import {useAutofocusRef} from 'shared/hooks'; |
| b69ab31 | | | 18 | import {nullthrows} from 'shared/utils'; |
| b69ab31 | | | 19 | import {Commit} from './Commit'; |
| b69ab31 | | | 20 | import {FlexSpacer} from './ComponentUtils'; |
| b69ab31 | | | 21 | import {codeReviewProvider} from './codeReview/CodeReviewInfo'; |
| b69ab31 | | | 22 | import {submitAsDraft, SubmitAsDraftCheckbox} from './codeReview/DraftCheckbox'; |
| b69ab31 | | | 23 | import {publishWhenReady, PublishWhenReadyCheckbox} from './codeReview/PublishWhenReadyCheckbox'; |
| b69ab31 | | | 24 | import {t, T} from './i18n'; |
| b69ab31 | | | 25 | import {configBackedAtom, readAtom} from './jotaiUtils'; |
| b69ab31 | | | 26 | import {CommitPreview} from './previews'; |
| b69ab31 | | | 27 | import {useModal} from './useModal'; |
| b69ab31 | | | 28 | |
| b69ab31 | | | 29 | import './ConfirmSubmitStack.css'; |
| b69ab31 | | | 30 | |
| b69ab31 | | | 31 | export const confirmShouldSubmitEnabledAtom = configBackedAtom<boolean>( |
| b69ab31 | | | 32 | 'isl.show-stack-submit-confirmation', |
| b69ab31 | | | 33 | true, |
| b69ab31 | | | 34 | ); |
| b69ab31 | | | 35 | |
| b69ab31 | | | 36 | export type SubmitConfirmationReponse = |
| b69ab31 | | | 37 | | {submitAsDraft: boolean; updateMessage?: string; publishWhenReady?: boolean} |
| b69ab31 | | | 38 | | undefined; |
| b69ab31 | | | 39 | |
| b69ab31 | | | 40 | type SubmitType = 'submit' | 'submit-all' | 'resubmit'; |
| b69ab31 | | | 41 | |
| b69ab31 | | | 42 | export function shouldShowSubmitStackConfirmation(): boolean { |
| b69ab31 | | | 43 | const provider = readAtom(codeReviewProvider); |
| b69ab31 | | | 44 | const shouldShowConfirmation = readAtom(confirmShouldSubmitEnabledAtom); |
| b69ab31 | | | 45 | return ( |
| b69ab31 | | | 46 | shouldShowConfirmation === true && |
| b69ab31 | | | 47 | // if you can't submit as draft, no need to show the interstitial |
| b69ab31 | | | 48 | provider?.supportSubmittingAsDraft != null |
| b69ab31 | | | 49 | ); |
| b69ab31 | | | 50 | } |
| b69ab31 | | | 51 | |
| b69ab31 | | | 52 | /** |
| b69ab31 | | | 53 | * Show a modal to confirm if you want to bulk submit a given stack of commits. |
| b69ab31 | | | 54 | * Allows you to set if you want to submit as a draft or not, |
| b69ab31 | | | 55 | * and provide an update message. |
| b69ab31 | | | 56 | * |
| b69ab31 | | | 57 | * If your code review provider does not support submitting as draft, |
| b69ab31 | | | 58 | * this function returns true immediately. |
| b69ab31 | | | 59 | */ |
| b69ab31 | | | 60 | export function useShowConfirmSubmitStack() { |
| b69ab31 | | | 61 | const showModal = useModal(); |
| b69ab31 | | | 62 | |
| b69ab31 | | | 63 | return async (mode: SubmitType, stack: Array<CommitInfo>) => { |
| b69ab31 | | | 64 | if (!shouldShowSubmitStackConfirmation()) { |
| b69ab31 | | | 65 | const draft = readAtom(submitAsDraft); |
| b69ab31 | | | 66 | return {submitAsDraft: draft ?? false}; |
| b69ab31 | | | 67 | } |
| b69ab31 | | | 68 | |
| b69ab31 | | | 69 | const provider = readAtom(codeReviewProvider); |
| b69ab31 | | | 70 | |
| b69ab31 | | | 71 | const replace = { |
| b69ab31 | | | 72 | $numCommits: String(stack.length), |
| b69ab31 | | | 73 | $cmd: nullthrows(provider).submitCommandName(), |
| b69ab31 | | | 74 | }; |
| b69ab31 | | | 75 | const title = |
| b69ab31 | | | 76 | mode === 'submit' |
| b69ab31 | | | 77 | ? t('Submitting $numCommits commits for review with $cmd', {replace}) |
| b69ab31 | | | 78 | : mode === 'resubmit' |
| b69ab31 | | | 79 | ? t('Submitting new versions of $numCommits commits for review with $cmd', {replace}) |
| b69ab31 | | | 80 | : t('Submitting all $numCommits commits in this stack for review with $cmd', {replace}); |
| b69ab31 | | | 81 | const response = await showModal<SubmitConfirmationReponse>({ |
| b69ab31 | | | 82 | type: 'custom', |
| b69ab31 | | | 83 | title, |
| b69ab31 | | | 84 | component: ({returnResultAndDismiss}) => ( |
| b69ab31 | | | 85 | <ConfirmModalContent stack={stack} returnResultAndDismiss={returnResultAndDismiss} /> |
| b69ab31 | | | 86 | ), |
| b69ab31 | | | 87 | }); |
| b69ab31 | | | 88 | return response; |
| b69ab31 | | | 89 | }; |
| b69ab31 | | | 90 | } |
| b69ab31 | | | 91 | |
| b69ab31 | | | 92 | function ConfirmModalContent({ |
| b69ab31 | | | 93 | stack, |
| b69ab31 | | | 94 | returnResultAndDismiss, |
| b69ab31 | | | 95 | }: { |
| b69ab31 | | | 96 | stack: Array<CommitInfo>; |
| b69ab31 | | | 97 | returnResultAndDismiss: (value: SubmitConfirmationReponse) => unknown; |
| b69ab31 | | | 98 | }) { |
| b69ab31 | | | 99 | const [showSubmitConfirmation, setShowSubmitConfirmation] = useAtom( |
| b69ab31 | | | 100 | confirmShouldSubmitEnabledAtom, |
| b69ab31 | | | 101 | ); |
| b69ab31 | | | 102 | const shouldSubmitAsDraft = useAtomValue(submitAsDraft); |
| b69ab31 | | | 103 | const shouldPublishWhenReady = useAtomValue(publishWhenReady); |
| b69ab31 | | | 104 | const [updateMessage, setUpdateMessage] = useState(''); |
| b69ab31 | | | 105 | const commitsWithDiffs = stack.filter(commit => commit.diffId != null); |
| b69ab31 | | | 106 | |
| b69ab31 | | | 107 | const submitRef = useAutofocusRef<HTMLButtonElement>(); |
| b69ab31 | | | 108 | |
| b69ab31 | | | 109 | const provider = useAtomValue(codeReviewProvider); |
| b69ab31 | | | 110 | return ( |
| b69ab31 | | | 111 | <div className="confirm-submit-stack" data-testid="confirm-submit-stack"> |
| b69ab31 | | | 112 | <div className="confirm-submit-stack-content"> |
| b69ab31 | | | 113 | <div className="commit-list"> |
| b69ab31 | | | 114 | {stack.map(commit => ( |
| b69ab31 | | | 115 | <Commit |
| b69ab31 | | | 116 | key={commit.hash} |
| b69ab31 | | | 117 | commit={commit} |
| b69ab31 | | | 118 | hasChildren={false} |
| b69ab31 | | | 119 | previewType={CommitPreview.NON_ACTIONABLE_COMMIT} |
| b69ab31 | | | 120 | /> |
| b69ab31 | | | 121 | ))} |
| b69ab31 | | | 122 | </div> |
| b69ab31 | | | 123 | {provider?.supportsUpdateMessage !== true || commitsWithDiffs.length === 0 ? null : ( |
| b69ab31 | | | 124 | <TextField |
| b69ab31 | | | 125 | value={updateMessage} |
| b69ab31 | | | 126 | data-testid="submit-update-message-input" |
| b69ab31 | | | 127 | onChange={e => setUpdateMessage(e.currentTarget.value)}> |
| b69ab31 | | | 128 | Update Message |
| b69ab31 | | | 129 | </TextField> |
| b69ab31 | | | 130 | )} |
| b69ab31 | | | 131 | <SubmitAsDraftCheckbox commitsToBeSubmit={stack} /> |
| b69ab31 | | | 132 | <PublishWhenReadyCheckbox /> |
| b69ab31 | | | 133 | </div> |
| b69ab31 | | | 134 | <Divider /> |
| b69ab31 | | | 135 | <div className="use-modal-buttons"> |
| b69ab31 | | | 136 | <Tooltip |
| b69ab31 | | | 137 | placement="bottom" |
| b69ab31 | | | 138 | title={t( |
| b69ab31 | | | 139 | "Don't show this confirmation next time you submit a stack. " + |
| b69ab31 | | | 140 | 'Your last setting will control if it is submitted as a draft. ' + |
| b69ab31 | | | 141 | 'You can change this from settings.', |
| b69ab31 | | | 142 | )}> |
| b69ab31 | | | 143 | <Checkbox |
| b69ab31 | | | 144 | checked={!showSubmitConfirmation} |
| b69ab31 | | | 145 | onChange={checked => setShowSubmitConfirmation(!checked)}> |
| b69ab31 | | | 146 | <T>Don't show again</T> |
| b69ab31 | | | 147 | </Checkbox> |
| b69ab31 | | | 148 | </Tooltip> |
| b69ab31 | | | 149 | <FlexSpacer /> |
| b69ab31 | | | 150 | <Button onClick={() => returnResultAndDismiss(undefined)}> |
| b69ab31 | | | 151 | <T>Cancel</T> |
| b69ab31 | | | 152 | </Button> |
| b69ab31 | | | 153 | <Button |
| b69ab31 | | | 154 | ref={submitRef} |
| b69ab31 | | | 155 | primary |
| b69ab31 | | | 156 | onClick={() => |
| b69ab31 | | | 157 | returnResultAndDismiss({ |
| b69ab31 | | | 158 | submitAsDraft: shouldSubmitAsDraft, |
| b69ab31 | | | 159 | updateMessage: updateMessage || undefined, |
| b69ab31 | | | 160 | publishWhenReady: shouldPublishWhenReady, |
| b69ab31 | | | 161 | }) |
| b69ab31 | | | 162 | }> |
| b69ab31 | | | 163 | <T>Submit</T> |
| b69ab31 | | | 164 | </Button> |
| b69ab31 | | | 165 | </div> |
| b69ab31 | | | 166 | </div> |
| b69ab31 | | | 167 | ); |
| b69ab31 | | | 168 | } |