| 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 {Operation} from '../operations/Operation'; |
| b69ab31 | | | 9 | import type {CommitInfo, DiffId} from '../types'; |
| b69ab31 | | | 10 | import type {CommitInfoMode, EditedMessage} from './CommitInfoState'; |
| b69ab31 | | | 11 | import type {CommitMessageFields, FieldConfig, FieldsBeingEdited} from './types'; |
| b69ab31 | | | 12 | |
| b69ab31 | | | 13 | import deepEqual from 'fast-deep-equal'; |
| b69ab31 | | | 14 | import {Badge} from 'isl-components/Badge'; |
| b69ab31 | | | 15 | import {Banner, BannerKind, BannerTooltip} from 'isl-components/Banner'; |
| b69ab31 | | | 16 | import {Button} from 'isl-components/Button'; |
| b69ab31 | | | 17 | import {Divider} from 'isl-components/Divider'; |
| b69ab31 | | | 18 | import {ErrorNotice} from 'isl-components/ErrorNotice'; |
| b69ab31 | | | 19 | import {Column} from 'isl-components/Flex'; |
| b69ab31 | | | 20 | import {Icon} from 'isl-components/Icon'; |
| b69ab31 | | | 21 | import {RadioGroup} from 'isl-components/Radio'; |
| b69ab31 | | | 22 | import {Subtle} from 'isl-components/Subtle'; |
| b69ab31 | | | 23 | import {Tooltip} from 'isl-components/Tooltip'; |
| b69ab31 | | | 24 | import {atom, useAtom, useAtomValue} from 'jotai'; |
| b69ab31 | | | 25 | import {useAtomCallback} from 'jotai/utils'; |
| b69ab31 | | | 26 | import {useCallback, useEffect, useMemo} from 'react'; |
| b69ab31 | | | 27 | import {ComparisonType} from 'shared/Comparison'; |
| b69ab31 | | | 28 | import {useContextMenu} from 'shared/ContextMenu'; |
| b69ab31 | | | 29 | import {usePrevious} from 'shared/hooks'; |
| b69ab31 | | | 30 | import {firstLine, notEmpty, nullthrows} from 'shared/utils'; |
| b69ab31 | | | 31 | import {tracker} from '../analytics'; |
| b69ab31 | | | 32 | import {ChangedFilesWithFetching} from '../ChangedFilesWithFetching'; |
| b69ab31 | | | 33 | import serverAPI from '../ClientToServerAPI'; |
| b69ab31 | | | 34 | import { |
| b69ab31 | | | 35 | allDiffSummaries, |
| b69ab31 | | | 36 | codeReviewProvider, |
| b69ab31 | | | 37 | latestCommitMessageFields, |
| b69ab31 | | | 38 | } from '../codeReview/CodeReviewInfo'; |
| b69ab31 | | | 39 | import {submitAsDraft, SubmitAsDraftCheckbox} from '../codeReview/DraftCheckbox'; |
| b69ab31 | | | 40 | import {showBranchingPrModal} from '../codeReview/github/BranchingPrModal'; |
| 6c9fcae | | | 41 | import {openCreateGroveRepoModal} from '../codeReview/grove/CreateGroveRepoBanner'; |
| b69ab31 | | | 42 | import {overrideDisabledSubmitModes} from '../codeReview/github/branchPrState'; |
| b69ab31 | | | 43 | import {publishWhenReady, PublishWhenReadyCheckbox} from '../codeReview/PublishWhenReadyCheckbox'; |
| b69ab31 | | | 44 | import {Commit} from '../Commit'; |
| b69ab31 | | | 45 | import {OpenComparisonViewButton} from '../ComparisonView/OpenComparisonViewButton'; |
| b69ab31 | | | 46 | import {Center} from '../ComponentUtils'; |
| b69ab31 | | | 47 | import {confirmNoBlockingDiagnostics} from '../Diagnostics'; |
| b69ab31 | | | 48 | import {FoldButton, useRunFoldPreview} from '../fold'; |
| b69ab31 | | | 49 | import {getCachedGeneratedFileStatuses, useGeneratedFileStatuses} from '../GeneratedFile'; |
| b69ab31 | | | 50 | import {t, T} from '../i18n'; |
| b69ab31 | | | 51 | import {IrrelevantCwdIcon} from '../icons/IrrelevantCwdIcon'; |
| b69ab31 | | | 52 | import {numPendingImageUploads} from '../ImageUpload'; |
| b69ab31 | | | 53 | import {readAtom, useAtomGet, writeAtom} from '../jotaiUtils'; |
| b69ab31 | | | 54 | import {Link} from '../Link'; |
| b69ab31 | | | 55 | import { |
| b69ab31 | | | 56 | messageSyncingEnabledState, |
| b69ab31 | | | 57 | messageSyncingOverrideState, |
| b69ab31 | | | 58 | updateRemoteMessage, |
| b69ab31 | | | 59 | } from '../messageSyncing'; |
| b69ab31 | | | 60 | import {OperationDisabledButton} from '../OperationDisabledButton'; |
| b69ab31 | | | 61 | import {AmendMessageOperation} from '../operations/AmendMessageOperation'; |
| b69ab31 | | | 62 | import {getAmendOperation} from '../operations/AmendOperation'; |
| b69ab31 | | | 63 | import {getCommitOperation} from '../operations/CommitOperation'; |
| b69ab31 | | | 64 | import {FOLD_COMMIT_PREVIEW_HASH_PREFIX} from '../operations/FoldOperation'; |
| b69ab31 | | | 65 | import {GhStackSubmitOperation} from '../operations/GhStackSubmitOperation'; |
| b69ab31 | | | 66 | import {PrSubmitOperation} from '../operations/PrSubmitOperation'; |
| b69ab31 | | | 67 | import {SetConfigOperation} from '../operations/SetConfigOperation'; |
| b69ab31 | | | 68 | import {useRunOperation} from '../operationsState'; |
| b69ab31 | | | 69 | import {useUncommittedSelection} from '../partialSelection'; |
| b69ab31 | | | 70 | import platform from '../platform'; |
| b69ab31 | | | 71 | import {CommitPreview, dagWithPreviews, uncommittedChangesWithPreviews} from '../previews'; |
| b69ab31 | | | 72 | import {repoRelativeCwd, useIsIrrelevantToCwd} from '../repositoryData'; |
| b69ab31 | | | 73 | import {selectedCommits} from '../selection'; |
| b69ab31 | | | 74 | import {authorString, commitByHash, latestHeadCommit, repositoryInfo} from '../serverAPIState'; |
| b69ab31 | | | 75 | import {SplitButton} from '../stackEdit/ui/SplitButton'; |
| b69ab31 | | | 76 | import {SubmitSelectionButton} from '../SubmitSelectionButton'; |
| b69ab31 | | | 77 | import {SubmitUpdateMessageInput} from '../SubmitUpdateMessageInput'; |
| b69ab31 | | | 78 | import {latestSuccessorUnlessExplicitlyObsolete} from '../successionUtils'; |
| b69ab31 | | | 79 | import {SuggestedRebaseButton} from '../SuggestedRebase'; |
| b69ab31 | | | 80 | import {showToast} from '../toast'; |
| b69ab31 | | | 81 | import {GeneratedStatus, succeedableRevset} from '../types'; |
| b69ab31 | | | 82 | import {UncommittedChanges} from '../UncommittedChanges'; |
| b69ab31 | | | 83 | import {confirmUnsavedFiles} from '../UnsavedFiles'; |
| b69ab31 | | | 84 | import {useModal} from '../useModal'; |
| b69ab31 | | | 85 | import {firstOfIterable} from '../utils'; |
| b69ab31 | | | 86 | import {CommitInfoField} from './CommitInfoField'; |
| b69ab31 | | | 87 | import { |
| b69ab31 | | | 88 | commitInfoViewCurrentCommits, |
| b69ab31 | | | 89 | commitMode, |
| b69ab31 | | | 90 | diffUpdateMessagesState, |
| b69ab31 | | | 91 | editedCommitMessages, |
| b69ab31 | | | 92 | forceNextCommitToEditAllFields, |
| b69ab31 | | | 93 | hasUnsavedEditedCommitMessage, |
| b69ab31 | | | 94 | unsavedFieldsBeingEdited, |
| b69ab31 | | | 95 | } from './CommitInfoState'; |
| b69ab31 | | | 96 | import { |
| b69ab31 | | | 97 | applyEditedFields, |
| b69ab31 | | | 98 | commitMessageFieldsSchema, |
| b69ab31 | | | 99 | commitMessageFieldsToString, |
| b69ab31 | | | 100 | editedMessageSubset, |
| b69ab31 | | | 101 | findEditedDiffNumber, |
| b69ab31 | | | 102 | findFieldsBeingEdited, |
| b69ab31 | | | 103 | parseCommitMessageFields, |
| b69ab31 | | | 104 | removeNoopEdits, |
| b69ab31 | | | 105 | } from './CommitMessageFields'; |
| b69ab31 | | | 106 | import {DiffStats, PendingDiffStats} from './DiffStats'; |
| b69ab31 | | | 107 | import {FillCommitMessage} from './FillCommitMessage'; |
| b69ab31 | | | 108 | import {CommitTitleByline, getFieldToAutofocus, Section, SmallCapsTitle} from './utils'; |
| b69ab31 | | | 109 | |
| b69ab31 | | | 110 | import {useFeatureFlagSync} from '../featureFlags'; |
| b69ab31 | | | 111 | import {AICodeReviewStatus} from '../firstPassCodeReview/AICodeReviewStatus'; |
| b69ab31 | | | 112 | import {AICodeReviewUpsell} from '../firstPassCodeReview/AICodeReviewUpsell'; |
| b69ab31 | | | 113 | import {Internal} from '../Internal'; |
| b69ab31 | | | 114 | import {confirmSuggestedEditsForFiles} from '../SuggestedEdits'; |
| b69ab31 | | | 115 | import './CommitInfoView.css'; |
| b69ab31 | | | 116 | |
| b69ab31 | | | 117 | export function CommitInfoSidebar() { |
| b69ab31 | | | 118 | const commitsToShow = useAtomValue(commitInfoViewCurrentCommits); |
| b69ab31 | | | 119 | |
| b69ab31 | | | 120 | if (commitsToShow == null) { |
| b69ab31 | | | 121 | return ( |
| b69ab31 | | | 122 | <div className="commit-info-view" data-testid="commit-info-view-loading"> |
| b69ab31 | | | 123 | <Center> |
| b69ab31 | | | 124 | <Icon icon="loading" /> |
| b69ab31 | | | 125 | </Center> |
| b69ab31 | | | 126 | </div> |
| b69ab31 | | | 127 | ); |
| b69ab31 | | | 128 | } else { |
| b69ab31 | | | 129 | if (commitsToShow.length > 1) { |
| b69ab31 | | | 130 | return <MultiCommitInfo selectedCommits={commitsToShow} />; |
| b69ab31 | | | 131 | } |
| b69ab31 | | | 132 | |
| b69ab31 | | | 133 | // only one commit selected |
| b69ab31 | | | 134 | return <CommitInfoDetails commit={commitsToShow[0]} />; |
| b69ab31 | | | 135 | } |
| b69ab31 | | | 136 | } |
| b69ab31 | | | 137 | |
| b69ab31 | | | 138 | export function MultiCommitInfo({selectedCommits}: {selectedCommits: Array<CommitInfo>}) { |
| b69ab31 | | | 139 | const commitsWithDiffs = selectedCommits.filter(commit => commit.diffId != null); |
| b69ab31 | | | 140 | return ( |
| b69ab31 | | | 141 | <div className="commit-info-view-multi-commit" data-testid="commit-info-view"> |
| b69ab31 | | | 142 | <strong className="commit-list-header"> |
| b69ab31 | | | 143 | <Icon icon="layers" size="M" /> |
| b69ab31 | | | 144 | <T replace={{$num: selectedCommits.length}}>$num Commits Selected</T> |
| b69ab31 | | | 145 | </strong> |
| b69ab31 | | | 146 | <Divider /> |
| b69ab31 | | | 147 | <div className="commit-list"> |
| b69ab31 | | | 148 | {selectedCommits.map(commit => ( |
| b69ab31 | | | 149 | <Commit |
| b69ab31 | | | 150 | key={commit.hash} |
| b69ab31 | | | 151 | commit={commit} |
| b69ab31 | | | 152 | hasChildren={false} |
| b69ab31 | | | 153 | previewType={CommitPreview.NON_ACTIONABLE_COMMIT} |
| b69ab31 | | | 154 | /> |
| b69ab31 | | | 155 | ))} |
| b69ab31 | | | 156 | </div> |
| b69ab31 | | | 157 | <div className="commit-info-actions-bar"> |
| b69ab31 | | | 158 | <div className="commit-info-actions-bar-right"> |
| b69ab31 | | | 159 | <SuggestedRebaseButton |
| b69ab31 | | | 160 | sources={selectedCommits.map(commit => succeedableRevset(commit.hash))} |
| b69ab31 | | | 161 | /> |
| b69ab31 | | | 162 | <FoldButton /> |
| b69ab31 | | | 163 | </div> |
| b69ab31 | | | 164 | {commitsWithDiffs.length === 0 ? null : ( |
| b69ab31 | | | 165 | <SubmitUpdateMessageInput commits={selectedCommits} /> |
| b69ab31 | | | 166 | )} |
| b69ab31 | | | 167 | <div className="commit-info-actions-bar-left"> |
| b69ab31 | | | 168 | <SubmitAsDraftCheckbox commitsToBeSubmit={selectedCommits} /> |
| b69ab31 | | | 169 | <PublishWhenReadyCheckbox /> |
| b69ab31 | | | 170 | </div> |
| b69ab31 | | | 171 | <div className="commit-info-actions-bar-right"> |
| b69ab31 | | | 172 | <SubmitSelectionButton /> |
| b69ab31 | | | 173 | </div> |
| b69ab31 | | | 174 | </div> |
| b69ab31 | | | 175 | </div> |
| b69ab31 | | | 176 | ); |
| b69ab31 | | | 177 | } |
| b69ab31 | | | 178 | |
| b69ab31 | | | 179 | function useFetchActiveDiffDetails(diffId?: string) { |
| b69ab31 | | | 180 | useEffect(() => { |
| b69ab31 | | | 181 | if (diffId != null) { |
| b69ab31 | | | 182 | serverAPI.postMessage({ |
| b69ab31 | | | 183 | type: 'fetchDiffSummaries', |
| b69ab31 | | | 184 | diffIds: [diffId], |
| b69ab31 | | | 185 | }); |
| b69ab31 | | | 186 | tracker.track('DiffFetchSource', {extras: {source: 'active_diff_details'}}); |
| b69ab31 | | | 187 | } |
| b69ab31 | | | 188 | }, [diffId]); |
| b69ab31 | | | 189 | } |
| b69ab31 | | | 190 | |
| b69ab31 | | | 191 | export function CommitInfoDetails({commit}: {commit: CommitInfo}) { |
| b69ab31 | | | 192 | const rollbackFeatureEnabled = useFeatureFlagSync(Internal.featureFlags?.ShowRollbackPlan); |
| b69ab31 | | | 193 | const aiCodeReviewUpsellEnabled = useFeatureFlagSync(Internal.featureFlags?.AICodeReviewUpsell); |
| b69ab31 | | | 194 | const aiFirstPassCodeReviewEnabled = useFeatureFlagSync( |
| b69ab31 | | | 195 | Internal.featureFlags?.AIFirstPassCodeReview, |
| b69ab31 | | | 196 | ); |
| b69ab31 | | | 197 | const [mode, setMode] = useAtom(commitMode); |
| b69ab31 | | | 198 | const isCommitMode = mode === 'commit'; |
| b69ab31 | | | 199 | const hashOrHead = isCommitMode ? 'head' : commit.hash; |
| b69ab31 | | | 200 | const [editedMessage, setEditedCommitMessage] = useAtom(editedCommitMessages(hashOrHead)); |
| b69ab31 | | | 201 | const uncommittedChanges = useAtomValue(uncommittedChangesWithPreviews); |
| b69ab31 | | | 202 | const selection = useUncommittedSelection(); |
| b69ab31 | | | 203 | const schema = useAtomValue(commitMessageFieldsSchema); |
| b69ab31 | | | 204 | |
| b69ab31 | | | 205 | const isFoldPreview = commit.hash.startsWith(FOLD_COMMIT_PREVIEW_HASH_PREFIX); |
| b69ab31 | | | 206 | const isOptimistic = |
| b69ab31 | | | 207 | useAtomValue(commitByHash(commit.hash)) == null && !isCommitMode && !isFoldPreview; |
| b69ab31 | | | 208 | |
| b69ab31 | | | 209 | const cwd = useAtomValue(repoRelativeCwd); |
| b69ab31 | | | 210 | const isIrrelevantToCwd = useIsIrrelevantToCwd(commit); |
| b69ab31 | | | 211 | |
| b69ab31 | | | 212 | const isPublic = commit.phase === 'public'; |
| b69ab31 | | | 213 | const isObsoleted = commit.successorInfo != null; |
| b69ab31 | | | 214 | const isAmendDisabled = mode === 'amend' && (isPublic || isObsoleted); |
| b69ab31 | | | 215 | |
| b69ab31 | | | 216 | const fieldsBeingEdited = useAtomValue(unsavedFieldsBeingEdited(hashOrHead)); |
| b69ab31 | | | 217 | const previousFieldsBeingEdited = usePrevious(fieldsBeingEdited, deepEqual); |
| b69ab31 | | | 218 | |
| b69ab31 | | | 219 | useFetchActiveDiffDetails(commit.diffId); |
| b69ab31 | | | 220 | |
| b69ab31 | | | 221 | const [forceEditAll, setForceEditAll] = useAtom(forceNextCommitToEditAllFields); |
| b69ab31 | | | 222 | |
| b69ab31 | | | 223 | useEffect(() => { |
| b69ab31 | | | 224 | if (isCommitMode && commit.isDot) { |
| b69ab31 | | | 225 | // no use resetting edited state for commit mode, where it's always being edited. |
| b69ab31 | | | 226 | return; |
| b69ab31 | | | 227 | } |
| b69ab31 | | | 228 | |
| b69ab31 | | | 229 | if (!forceEditAll) { |
| b69ab31 | | | 230 | // If the selected commit is changed, the fields being edited should slim down to only fields |
| b69ab31 | | | 231 | // that are meaningfully edited on the new commit. |
| b69ab31 | | | 232 | if (Object.keys(editedMessage).length > 0) { |
| b69ab31 | | | 233 | const trimmedEdits = removeNoopEdits(schema, parsedFields, editedMessage); |
| b69ab31 | | | 234 | if (Object.keys(trimmedEdits).length !== Object.keys(editedMessage).length) { |
| b69ab31 | | | 235 | setEditedCommitMessage(trimmedEdits); |
| b69ab31 | | | 236 | } |
| b69ab31 | | | 237 | } |
| b69ab31 | | | 238 | } |
| b69ab31 | | | 239 | setForceEditAll(false); |
| b69ab31 | | | 240 | |
| b69ab31 | | | 241 | // We only want to recompute this when the commit/mode changes. |
| b69ab31 | | | 242 | // we expect the edited message to change constantly. |
| b69ab31 | | | 243 | // eslint-disable-next-line react-hooks/exhaustive-deps |
| b69ab31 | | | 244 | }, [commit.hash, isCommitMode]); |
| b69ab31 | | | 245 | |
| b69ab31 | | | 246 | const parsedFields = useAtomValue(latestCommitMessageFields(hashOrHead)); |
| b69ab31 | | | 247 | |
| b69ab31 | | | 248 | const parentCommit = useAtomGet(dagWithPreviews, isCommitMode ? commit.hash : commit.parents[0]); |
| b69ab31 | | | 249 | const parentFields = |
| b69ab31 | | | 250 | parentCommit && parentCommit.phase !== 'public' |
| b69ab31 | | | 251 | ? parseCommitMessageFields(schema, parentCommit.title, parentCommit.description) |
| b69ab31 | | | 252 | : undefined; |
| b69ab31 | | | 253 | |
| b69ab31 | | | 254 | const provider = useAtomValue(codeReviewProvider); |
| b69ab31 | | | 255 | const startEditingField = (field: string) => { |
| b69ab31 | | | 256 | const original = parsedFields[field]; |
| b69ab31 | | | 257 | // If you start editing a tokenized field, add a blank token so you can write a new token instead of |
| b69ab31 | | | 258 | // modifying the last existing token. |
| b69ab31 | | | 259 | const fieldValue = Array.isArray(original) && original.at(-1) ? [...original, ''] : original; |
| b69ab31 | | | 260 | |
| b69ab31 | | | 261 | setEditedCommitMessage(last => ({ |
| b69ab31 | | | 262 | ...last, |
| b69ab31 | | | 263 | [field]: fieldValue, |
| b69ab31 | | | 264 | })); |
| b69ab31 | | | 265 | }; |
| b69ab31 | | | 266 | |
| b69ab31 | | | 267 | const fieldToAutofocus = getFieldToAutofocus( |
| b69ab31 | | | 268 | schema, |
| b69ab31 | | | 269 | fieldsBeingEdited, |
| b69ab31 | | | 270 | previousFieldsBeingEdited, |
| b69ab31 | | | 271 | ); |
| b69ab31 | | | 272 | |
| b69ab31 | | | 273 | const diffSummaries = useAtomValue(allDiffSummaries); |
| b69ab31 | | | 274 | const remoteTrackingBranch = provider?.getRemoteTrackingBranch( |
| b69ab31 | | | 275 | diffSummaries?.value, |
| b69ab31 | | | 276 | commit.diffId, |
| b69ab31 | | | 277 | ); |
| b69ab31 | | | 278 | |
| b69ab31 | | | 279 | const selectedFiles = uncommittedChanges.filter(f => |
| b69ab31 | | | 280 | selection.isFullyOrPartiallySelected(f.path), |
| b69ab31 | | | 281 | ); |
| b69ab31 | | | 282 | const selectedFilesLength = selectedFiles.length; |
| b69ab31 | | | 283 | return ( |
| b69ab31 | | | 284 | <div className="commit-info-view" data-testid="commit-info-view"> |
| b69ab31 | | | 285 | {!commit.isDot ? null : ( |
| b69ab31 | | | 286 | <div className="commit-info-view-toolbar-top" data-testid="commit-info-toolbar-top"> |
| b69ab31 | | | 287 | <Tooltip |
| b69ab31 | | | 288 | title={t( |
| b69ab31 | | | 289 | 'In Commit mode, you can edit the blank commit message for a new commit. \n\n' + |
| b69ab31 | | | 290 | 'In Amend mode, you can view and edit the commit message for the current head commit.', |
| b69ab31 | | | 291 | )}> |
| b69ab31 | | | 292 | <RadioGroup |
| b69ab31 | | | 293 | horizontal |
| b69ab31 | | | 294 | choices={[ |
| b69ab31 | | | 295 | {title: t('Commit'), value: 'commit'}, |
| b69ab31 | | | 296 | {title: t('Amend'), value: 'amend'}, |
| b69ab31 | | | 297 | ]} |
| b69ab31 | | | 298 | current={mode} |
| b69ab31 | | | 299 | onChange={setMode} |
| b69ab31 | | | 300 | /> |
| b69ab31 | | | 301 | </Tooltip> |
| b69ab31 | | | 302 | </div> |
| b69ab31 | | | 303 | )} |
| b69ab31 | | | 304 | {isCommitMode && <FillCommitMessage commit={commit} mode={mode} />} |
| b69ab31 | | | 305 | <div |
| b69ab31 | | | 306 | className="commit-info-view-main-content" |
| b69ab31 | | | 307 | // remount this if we change to commit mode |
| b69ab31 | | | 308 | key={mode}> |
| b69ab31 | | | 309 | {schema |
| b69ab31 | | | 310 | .filter(field => !isCommitMode || field.type !== 'read-only') |
| b69ab31 | | | 311 | .map(field => { |
| b69ab31 | | | 312 | if (!rollbackFeatureEnabled && field.type === 'custom') { |
| b69ab31 | | | 313 | return; |
| b69ab31 | | | 314 | } |
| b69ab31 | | | 315 | |
| b69ab31 | | | 316 | const setField = (newVal: string) => |
| b69ab31 | | | 317 | setEditedCommitMessage(val => ({ |
| b69ab31 | | | 318 | ...val, |
| b69ab31 | | | 319 | [field.key]: field.type === 'field' ? newVal.split(',') : newVal, |
| b69ab31 | | | 320 | })); |
| b69ab31 | | | 321 | |
| b69ab31 | | | 322 | let editedFieldValue = editedMessage?.[field.key]; |
| b69ab31 | | | 323 | if (editedFieldValue == null && isCommitMode) { |
| b69ab31 | | | 324 | // If the field is supposed to edited but not in the editedMessage, |
| b69ab31 | | | 325 | // it means we're loading from a blank slate. This is when we can load from the commit template. |
| b69ab31 | | | 326 | editedFieldValue = parsedFields[field.key]; |
| b69ab31 | | | 327 | } |
| b69ab31 | | | 328 | |
| b69ab31 | | | 329 | const parentVal = parentFields?.[field.key]; |
| b69ab31 | | | 330 | |
| b69ab31 | | | 331 | return ( |
| b69ab31 | | | 332 | <CommitInfoField |
| b69ab31 | | | 333 | key={field.key} |
| b69ab31 | | | 334 | field={field} |
| b69ab31 | | | 335 | content={parsedFields[field.key as keyof CommitMessageFields]} |
| b69ab31 | | | 336 | autofocus={fieldToAutofocus === field.key} |
| b69ab31 | | | 337 | readonly={isOptimistic || isAmendDisabled || isObsoleted} |
| b69ab31 | | | 338 | isBeingEdited={fieldsBeingEdited[field.key]} |
| b69ab31 | | | 339 | startEditingField={() => startEditingField(field.key)} |
| b69ab31 | | | 340 | editedField={editedFieldValue} |
| b69ab31 | | | 341 | setEditedField={setField} |
| b69ab31 | | | 342 | copyFromParent={ |
| b69ab31 | | | 343 | parentVal != null |
| b69ab31 | | | 344 | ? () => { |
| b69ab31 | | | 345 | tracker.track('CopyCommitFieldsFromParent'); |
| b69ab31 | | | 346 | const val = Array.isArray(parentVal) ? parentVal.join(',') : parentVal; |
| b69ab31 | | | 347 | setField(field.type === 'field' ? val + ',' : val); |
| b69ab31 | | | 348 | } |
| b69ab31 | | | 349 | : undefined |
| b69ab31 | | | 350 | } |
| b69ab31 | | | 351 | extra={ |
| b69ab31 | | | 352 | field.key === 'Title' ? ( |
| b69ab31 | | | 353 | <> |
| b69ab31 | | | 354 | {aiCodeReviewUpsellEnabled && |
| b69ab31 | | | 355 | Internal.aiCodeReview?.enabled && |
| b69ab31 | | | 356 | commit.isDot && |
| b69ab31 | | | 357 | !isPublic && ( |
| b69ab31 | | | 358 | <AICodeReviewUpsell |
| b69ab31 | | | 359 | isCommitMode={isCommitMode} |
| b69ab31 | | | 360 | hasUncommittedChanges={uncommittedChanges.length > 0} |
| b69ab31 | | | 361 | /> |
| b69ab31 | | | 362 | )} |
| b69ab31 | | | 363 | {!isCommitMode ? ( |
| b69ab31 | | | 364 | <> |
| b69ab31 | | | 365 | <CommitTitleByline commit={commit} /> |
| b69ab31 | | | 366 | {isFoldPreview && <FoldPreviewBanner />} |
| b69ab31 | | | 367 | <ShowingRemoteMessageBanner |
| b69ab31 | | | 368 | commit={commit} |
| b69ab31 | | | 369 | latestFields={parsedFields} |
| b69ab31 | | | 370 | editedCommitMessageKey={isCommitMode ? 'head' : commit.hash} |
| b69ab31 | | | 371 | /> |
| b69ab31 | | | 372 | {!isPublic && isIrrelevantToCwd ? ( |
| b69ab31 | | | 373 | <Tooltip |
| b69ab31 | | | 374 | title={ |
| b69ab31 | | | 375 | <T |
| b69ab31 | | | 376 | replace={{ |
| b69ab31 | | | 377 | $prefix: <pre>{commit.maxCommonPathPrefix}</pre>, |
| b69ab31 | | | 378 | $cwd: <pre>{cwd}</pre>, |
| b69ab31 | | | 379 | }}> |
| b69ab31 | | | 380 | This commit only contains files within: $prefix These are |
| b69ab31 | | | 381 | irrelevant to your current working directory: $cwd |
| b69ab31 | | | 382 | </T> |
| b69ab31 | | | 383 | }> |
| b69ab31 | | | 384 | <Banner kind={BannerKind.default}> |
| b69ab31 | | | 385 | <IrrelevantCwdIcon /> |
| b69ab31 | | | 386 | <div style={{paddingLeft: 'var(--halfpad)'}}> |
| b69ab31 | | | 387 | <T replace={{$cwd: <code>{cwd}</code>}}> |
| b69ab31 | | | 388 | All files in this commit are outside $cwd |
| b69ab31 | | | 389 | </T> |
| b69ab31 | | | 390 | </div> |
| b69ab31 | | | 391 | </Banner> |
| b69ab31 | | | 392 | </Tooltip> |
| b69ab31 | | | 393 | ) : null} |
| b69ab31 | | | 394 | </> |
| b69ab31 | | | 395 | ) : undefined} |
| b69ab31 | | | 396 | </> |
| b69ab31 | | | 397 | ) : undefined |
| b69ab31 | | | 398 | } |
| b69ab31 | | | 399 | /> |
| b69ab31 | | | 400 | ); |
| b69ab31 | | | 401 | })} |
| b69ab31 | | | 402 | {remoteTrackingBranch == null ? null : ( |
| b69ab31 | | | 403 | <Section> |
| b69ab31 | | | 404 | <SmallCapsTitle> |
| b69ab31 | | | 405 | <Icon icon="source-control"></Icon> |
| b69ab31 | | | 406 | <T>Remote Tracking Branch</T> |
| b69ab31 | | | 407 | </SmallCapsTitle> |
| b69ab31 | | | 408 | <div className="commit-info-tokenized-field"> |
| b69ab31 | | | 409 | <span className="token">{remoteTrackingBranch}</span> |
| b69ab31 | | | 410 | </div> |
| b69ab31 | | | 411 | </Section> |
| b69ab31 | | | 412 | )} |
| b69ab31 | | | 413 | <Divider /> |
| b69ab31 | | | 414 | {commit.isDot && !isAmendDisabled ? ( |
| b69ab31 | | | 415 | <Section data-testid="changes-to-amend"> |
| b69ab31 | | | 416 | <SmallCapsTitle> |
| b69ab31 | | | 417 | {isCommitMode ? <T>Changes to Commit</T> : <T>Changes to Amend</T>} |
| b69ab31 | | | 418 | <Badge> |
| b69ab31 | | | 419 | {selectedFilesLength === uncommittedChanges.length |
| b69ab31 | | | 420 | ? null |
| b69ab31 | | | 421 | : selectedFilesLength + '/'} |
| b69ab31 | | | 422 | {uncommittedChanges.length} |
| b69ab31 | | | 423 | </Badge> |
| b69ab31 | | | 424 | </SmallCapsTitle> |
| b69ab31 | | | 425 | {uncommittedChanges.length > 0 ? <PendingDiffStats /> : null} |
| b69ab31 | | | 426 | {uncommittedChanges.length === 0 ? ( |
| b69ab31 | | | 427 | <Subtle> |
| b69ab31 | | | 428 | {isCommitMode ? <T>No changes to commit</T> : <T>No changes to amend</T>} |
| b69ab31 | | | 429 | </Subtle> |
| b69ab31 | | | 430 | ) : ( |
| b69ab31 | | | 431 | <UncommittedChanges place={isCommitMode ? 'commit sidebar' : 'amend sidebar'} /> |
| b69ab31 | | | 432 | )} |
| b69ab31 | | | 433 | </Section> |
| b69ab31 | | | 434 | ) : null} |
| b69ab31 | | | 435 | {isCommitMode ? null : ( |
| b69ab31 | | | 436 | <Section data-testid="committed-changes"> |
| b69ab31 | | | 437 | <SmallCapsTitle> |
| b69ab31 | | | 438 | <T>Files Changed</T> |
| b69ab31 | | | 439 | <Badge>{commit.totalFileCount}</Badge> |
| b69ab31 | | | 440 | </SmallCapsTitle> |
| b69ab31 | | | 441 | {commit.phase !== 'public' ? <DiffStats commit={commit} /> : null} |
| b69ab31 | | | 442 | <div className="changed-file-list"> |
| b69ab31 | | | 443 | <div className="button-row"> |
| b69ab31 | | | 444 | <OpenComparisonViewButton |
| b69ab31 | | | 445 | comparison={{type: ComparisonType.Committed, hash: commit.hash}} |
| b69ab31 | | | 446 | /> |
| b69ab31 | | | 447 | <OpenAllFilesButton commit={commit} /> |
| b69ab31 | | | 448 | <SplitButton trackerEventName="SplitOpenFromSplitSuggestion" commit={commit} /> |
| b69ab31 | | | 449 | </div> |
| b69ab31 | | | 450 | <ChangedFilesWithFetching commit={commit} /> |
| b69ab31 | | | 451 | </div> |
| b69ab31 | | | 452 | </Section> |
| b69ab31 | | | 453 | )} |
| b69ab31 | | | 454 | </div> |
| b69ab31 | | | 455 | {!isAmendDisabled && ( |
| b69ab31 | | | 456 | <div className="commit-info-view-toolbar-bottom"> |
| b69ab31 | | | 457 | {isFoldPreview ? ( |
| b69ab31 | | | 458 | <FoldPreviewActions /> |
| b69ab31 | | | 459 | ) : ( |
| b69ab31 | | | 460 | <> |
| b69ab31 | | | 461 | {aiCodeReviewUpsellEnabled && aiFirstPassCodeReviewEnabled && <AICodeReviewStatus />} |
| b69ab31 | | | 462 | <ActionsBar |
| b69ab31 | | | 463 | commit={commit} |
| b69ab31 | | | 464 | latestMessage={parsedFields} |
| b69ab31 | | | 465 | editedMessage={editedMessage} |
| b69ab31 | | | 466 | fieldsBeingEdited={fieldsBeingEdited} |
| b69ab31 | | | 467 | isCommitMode={isCommitMode} |
| b69ab31 | | | 468 | setMode={setMode} |
| b69ab31 | | | 469 | /> |
| b69ab31 | | | 470 | </> |
| b69ab31 | | | 471 | )} |
| b69ab31 | | | 472 | </div> |
| b69ab31 | | | 473 | )} |
| b69ab31 | | | 474 | </div> |
| b69ab31 | | | 475 | ); |
| b69ab31 | | | 476 | } |
| b69ab31 | | | 477 | |
| b69ab31 | | | 478 | /** |
| b69ab31 | | | 479 | * No files are generated -> "Open all" button |
| b69ab31 | | | 480 | * All files are generated -> "Open all" button, with warning that they're all generated |
| b69ab31 | | | 481 | * Some files are generated -> "Open non-generated files" button |
| b69ab31 | | | 482 | */ |
| b69ab31 | | | 483 | function OpenAllFilesButton({commit}: {commit: CommitInfo}) { |
| b69ab31 | | | 484 | const paths = useMemo(() => commit.filePathsSample, [commit]); |
| b69ab31 | | | 485 | const statuses = useGeneratedFileStatuses(paths); |
| b69ab31 | | | 486 | const allAreGenerated = paths.every(file => statuses[file] === GeneratedStatus.Generated); |
| b69ab31 | | | 487 | const someAreGenerated = paths.some(file => statuses[file] === GeneratedStatus.Generated); |
| b69ab31 | | | 488 | return ( |
| b69ab31 | | | 489 | <Tooltip |
| b69ab31 | | | 490 | title={ |
| b69ab31 | | | 491 | someAreGenerated |
| b69ab31 | | | 492 | ? allAreGenerated |
| b69ab31 | | | 493 | ? t('Opens all files for editing.\nNote: All files are generated.') |
| b69ab31 | | | 494 | : t('Open all non-generated files for editing') |
| b69ab31 | | | 495 | : t('Open all files for editing') |
| b69ab31 | | | 496 | }> |
| b69ab31 | | | 497 | <Button |
| b69ab31 | | | 498 | icon |
| b69ab31 | | | 499 | onClick={() => { |
| b69ab31 | | | 500 | tracker.track('OpenAllFiles'); |
| b69ab31 | | | 501 | const statuses = getCachedGeneratedFileStatuses(commit.filePathsSample); |
| b69ab31 | | | 502 | const toOpen = allAreGenerated |
| b69ab31 | | | 503 | ? commit.filePathsSample |
| b69ab31 | | | 504 | : commit.filePathsSample.filter( |
| b69ab31 | | | 505 | file => statuses[file] == null || statuses[file] !== GeneratedStatus.Generated, |
| b69ab31 | | | 506 | ); |
| b69ab31 | | | 507 | platform.openFiles(toOpen); |
| b69ab31 | | | 508 | }}> |
| b69ab31 | | | 509 | <Icon icon="go-to-file" slot="start" /> |
| b69ab31 | | | 510 | {someAreGenerated && !allAreGenerated ? ( |
| b69ab31 | | | 511 | <T>Open Non-Generated Files</T> |
| b69ab31 | | | 512 | ) : ( |
| b69ab31 | | | 513 | <T>Open All Files</T> |
| b69ab31 | | | 514 | )} |
| b69ab31 | | | 515 | </Button> |
| b69ab31 | | | 516 | </Tooltip> |
| b69ab31 | | | 517 | ); |
| b69ab31 | | | 518 | } |
| b69ab31 | | | 519 | |
| b69ab31 | | | 520 | /** |
| b69ab31 | | | 521 | * Two parsed commit messages are considered unchanged if all the textareas (summary, test plan) are unchanged. |
| b69ab31 | | | 522 | * This avoids marking tiny changes like adding a reviewer as substatively changing the message. |
| b69ab31 | | | 523 | */ |
| b69ab31 | | | 524 | function areTextFieldsUnchanged( |
| b69ab31 | | | 525 | schema: Array<FieldConfig>, |
| b69ab31 | | | 526 | a: CommitMessageFields, |
| b69ab31 | | | 527 | b: CommitMessageFields, |
| b69ab31 | | | 528 | ) { |
| b69ab31 | | | 529 | for (const field of schema) { |
| b69ab31 | | | 530 | if (field.type === 'textarea') { |
| b69ab31 | | | 531 | if (a[field.key] !== b[field.key]) { |
| b69ab31 | | | 532 | return false; |
| b69ab31 | | | 533 | } |
| b69ab31 | | | 534 | } |
| b69ab31 | | | 535 | } |
| b69ab31 | | | 536 | return true; |
| b69ab31 | | | 537 | } |
| b69ab31 | | | 538 | |
| b69ab31 | | | 539 | function FoldPreviewBanner() { |
| b69ab31 | | | 540 | return ( |
| b69ab31 | | | 541 | <BannerTooltip |
| b69ab31 | | | 542 | tooltip={t( |
| b69ab31 | | | 543 | 'This is the commit message after combining these commits with the fold command. ' + |
| b69ab31 | | | 544 | 'You can edit this message before confirming and running fold.', |
| b69ab31 | | | 545 | )}> |
| b69ab31 | | | 546 | <Banner kind={BannerKind.green} icon={<Icon icon="info" />}> |
| b69ab31 | | | 547 | <T>Previewing result of combined commits</T> |
| b69ab31 | | | 548 | </Banner> |
| b69ab31 | | | 549 | </BannerTooltip> |
| b69ab31 | | | 550 | ); |
| b69ab31 | | | 551 | } |
| b69ab31 | | | 552 | |
| b69ab31 | | | 553 | function ShowingRemoteMessageBanner({ |
| b69ab31 | | | 554 | commit, |
| b69ab31 | | | 555 | latestFields, |
| b69ab31 | | | 556 | editedCommitMessageKey, |
| b69ab31 | | | 557 | }: { |
| b69ab31 | | | 558 | commit: CommitInfo; |
| b69ab31 | | | 559 | latestFields: CommitMessageFields; |
| b69ab31 | | | 560 | editedCommitMessageKey: string; |
| b69ab31 | | | 561 | }) { |
| b69ab31 | | | 562 | const provider = useAtomValue(codeReviewProvider); |
| b69ab31 | | | 563 | const schema = useAtomValue(commitMessageFieldsSchema); |
| b69ab31 | | | 564 | const runOperation = useRunOperation(); |
| b69ab31 | | | 565 | const syncingEnabled = useAtomValue(messageSyncingEnabledState); |
| b69ab31 | | | 566 | const syncingOverride = useAtomValue(messageSyncingOverrideState); |
| b69ab31 | | | 567 | |
| b69ab31 | | | 568 | const loadLocalMessage = useCallback(() => { |
| b69ab31 | | | 569 | const originalFields = parseCommitMessageFields(schema, commit.title, commit.description); |
| b69ab31 | | | 570 | const beingEdited = findFieldsBeingEdited(schema, originalFields, latestFields); |
| b69ab31 | | | 571 | |
| b69ab31 | | | 572 | writeAtom(editedCommitMessages(editedCommitMessageKey), () => |
| b69ab31 | | | 573 | editedMessageSubset(originalFields, beingEdited), |
| b69ab31 | | | 574 | ); |
| b69ab31 | | | 575 | }, [commit, editedCommitMessageKey, latestFields, schema]); |
| b69ab31 | | | 576 | |
| b69ab31 | | | 577 | const contextMenu = useContextMenu(() => { |
| b69ab31 | | | 578 | return [ |
| b69ab31 | | | 579 | { |
| b69ab31 | | | 580 | label: <T>Load local commit message instead</T>, |
| b69ab31 | | | 581 | onClick: loadLocalMessage, |
| b69ab31 | | | 582 | }, |
| b69ab31 | | | 583 | { |
| b69ab31 | | | 584 | label: <T>Sync local commit to match remote</T>, |
| b69ab31 | | | 585 | onClick: () => { |
| b69ab31 | | | 586 | runOperation( |
| b69ab31 | | | 587 | new AmendMessageOperation( |
| b69ab31 | | | 588 | latestSuccessorUnlessExplicitlyObsolete(commit), |
| b69ab31 | | | 589 | commitMessageFieldsToString(schema, latestFields), |
| b69ab31 | | | 590 | ), |
| b69ab31 | | | 591 | ); |
| b69ab31 | | | 592 | }, |
| b69ab31 | | | 593 | }, |
| b69ab31 | | | 594 | ]; |
| b69ab31 | | | 595 | }); |
| b69ab31 | | | 596 | |
| b69ab31 | | | 597 | if (!provider || (syncingOverride == null && !syncingEnabled)) { |
| b69ab31 | | | 598 | return null; |
| b69ab31 | | | 599 | } |
| b69ab31 | | | 600 | |
| b69ab31 | | | 601 | if (syncingOverride === false) { |
| b69ab31 | | | 602 | return ( |
| b69ab31 | | | 603 | <BannerTooltip |
| b69ab31 | | | 604 | tooltip={t( |
| b69ab31 | | | 605 | 'Message syncing with $provider has been temporarily disabled due to a failed sync.\n\n' + |
| b69ab31 | | | 606 | 'Your local commit message is shown instead.\n' + |
| b69ab31 | | | 607 | "Changes you make won't be automatically synced.\n\n" + |
| b69ab31 | | | 608 | 'Make sure to manually sync your message with $provider, then re-enable or restart ISL to start syncing again.', |
| b69ab31 | | | 609 | {replace: {$provider: provider.label}}, |
| b69ab31 | | | 610 | )}> |
| b69ab31 | | | 611 | <Banner |
| b69ab31 | | | 612 | icon={<Icon icon="warn" />} |
| b69ab31 | | | 613 | alwaysShowButtons |
| b69ab31 | | | 614 | kind={BannerKind.warning} |
| b69ab31 | | | 615 | buttons={ |
| b69ab31 | | | 616 | <Button |
| b69ab31 | | | 617 | icon |
| b69ab31 | | | 618 | onClick={() => { |
| b69ab31 | | | 619 | writeAtom(messageSyncingOverrideState, null); |
| b69ab31 | | | 620 | }}> |
| b69ab31 | | | 621 | <T>Show Remote Messages Instead</T> |
| b69ab31 | | | 622 | </Button> |
| b69ab31 | | | 623 | }> |
| b69ab31 | | | 624 | <T replace={{$provider: provider.label}}>Not syncing messages with $provider</T> |
| b69ab31 | | | 625 | </Banner> |
| b69ab31 | | | 626 | </BannerTooltip> |
| b69ab31 | | | 627 | ); |
| b69ab31 | | | 628 | } |
| b69ab31 | | | 629 | |
| b69ab31 | | | 630 | const originalFields = parseCommitMessageFields(schema, commit.title, commit.description); |
| b69ab31 | | | 631 | |
| b69ab31 | | | 632 | if (areTextFieldsUnchanged(schema, originalFields, latestFields)) { |
| b69ab31 | | | 633 | return null; |
| b69ab31 | | | 634 | } |
| b69ab31 | | | 635 | |
| b69ab31 | | | 636 | return ( |
| b69ab31 | | | 637 | <BannerTooltip |
| b69ab31 | | | 638 | tooltip={t( |
| b69ab31 | | | 639 | 'Viewing the newer commit message from $provider. This message will be used when your code is landed. You can also load the local message instead.', |
| b69ab31 | | | 640 | {replace: {$provider: provider.label}}, |
| b69ab31 | | | 641 | )}> |
| b69ab31 | | | 642 | <Banner |
| b69ab31 | | | 643 | icon={<Icon icon="info" />} |
| b69ab31 | | | 644 | alwaysShowButtons |
| b69ab31 | | | 645 | buttons={ |
| b69ab31 | | | 646 | <Button |
| b69ab31 | | | 647 | icon |
| b69ab31 | | | 648 | data-testid="message-sync-banner-context-menu" |
| b69ab31 | | | 649 | onClick={e => { |
| b69ab31 | | | 650 | contextMenu(e); |
| b69ab31 | | | 651 | }}> |
| b69ab31 | | | 652 | <Icon icon="ellipsis" /> |
| b69ab31 | | | 653 | </Button> |
| b69ab31 | | | 654 | }> |
| b69ab31 | | | 655 | <T replace={{$provider: provider.label}}>Showing latest commit message from $provider</T> |
| b69ab31 | | | 656 | </Banner> |
| b69ab31 | | | 657 | </BannerTooltip> |
| b69ab31 | | | 658 | ); |
| b69ab31 | | | 659 | } |
| b69ab31 | | | 660 | |
| b69ab31 | | | 661 | function FoldPreviewActions() { |
| b69ab31 | | | 662 | const [cancel, run] = useRunFoldPreview(); |
| b69ab31 | | | 663 | return ( |
| b69ab31 | | | 664 | <div className="commit-info-actions-bar" data-testid="commit-info-actions-bar"> |
| b69ab31 | | | 665 | <div className="commit-info-actions-bar-right"> |
| b69ab31 | | | 666 | <Button onClick={cancel}> |
| b69ab31 | | | 667 | <T>Cancel</T> |
| b69ab31 | | | 668 | </Button> |
| b69ab31 | | | 669 | <Button primary onClick={run}> |
| b69ab31 | | | 670 | <T>Run Combine</T> |
| b69ab31 | | | 671 | </Button> |
| b69ab31 | | | 672 | </div> |
| b69ab31 | | | 673 | </div> |
| b69ab31 | | | 674 | ); |
| b69ab31 | | | 675 | } |
| b69ab31 | | | 676 | |
| b69ab31 | | | 677 | const imageUploadsPendingAtom = atom(get => { |
| b69ab31 | | | 678 | return get(numPendingImageUploads(undefined)) > 0; |
| b69ab31 | | | 679 | }); |
| b69ab31 | | | 680 | |
| b69ab31 | | | 681 | function ActionsBar({ |
| b69ab31 | | | 682 | commit, |
| b69ab31 | | | 683 | latestMessage, |
| b69ab31 | | | 684 | editedMessage, |
| b69ab31 | | | 685 | fieldsBeingEdited, |
| b69ab31 | | | 686 | isCommitMode, |
| b69ab31 | | | 687 | setMode, |
| b69ab31 | | | 688 | }: { |
| b69ab31 | | | 689 | commit: CommitInfo; |
| b69ab31 | | | 690 | latestMessage: CommitMessageFields; |
| b69ab31 | | | 691 | editedMessage: EditedMessage; |
| b69ab31 | | | 692 | fieldsBeingEdited: FieldsBeingEdited; |
| b69ab31 | | | 693 | isCommitMode: boolean; |
| b69ab31 | | | 694 | setMode: (mode: CommitInfoMode) => unknown; |
| b69ab31 | | | 695 | }) { |
| b69ab31 | | | 696 | const isAnythingBeingEdited = Object.values(fieldsBeingEdited).some(Boolean); |
| b69ab31 | | | 697 | const uncommittedChanges = useAtomValue(uncommittedChangesWithPreviews); |
| b69ab31 | | | 698 | const selection = useUncommittedSelection(); |
| b69ab31 | | | 699 | const anythingToCommit = |
| b69ab31 | | | 700 | !selection.isNothingSelected() && |
| b69ab31 | | | 701 | ((!isCommitMode && isAnythingBeingEdited) || uncommittedChanges.length > 0); |
| b69ab31 | | | 702 | |
| b69ab31 | | | 703 | const provider = useAtomValue(codeReviewProvider); |
| b69ab31 | | | 704 | const schema = useAtomValue(commitMessageFieldsSchema); |
| b69ab31 | | | 705 | const headCommit = useAtomValue(latestHeadCommit); |
| b69ab31 | | | 706 | |
| b69ab31 | | | 707 | const messageSyncEnabled = useAtomValue(messageSyncingEnabledState); |
| b69ab31 | | | 708 | |
| b69ab31 | | | 709 | // after committing/amending, if you've previously selected the head commit, |
| b69ab31 | | | 710 | // we should show you the newly amended/committed commit instead of the old one. |
| b69ab31 | | | 711 | const deselectIfHeadIsSelected = useAtomCallback((get, set) => { |
| b69ab31 | | | 712 | if (!commit.isDot) { |
| b69ab31 | | | 713 | return; |
| b69ab31 | | | 714 | } |
| b69ab31 | | | 715 | const selected = get(selectedCommits); |
| b69ab31 | | | 716 | // only reset if selection exactly matches our expectation |
| b69ab31 | | | 717 | if (selected && selected.size === 1 && firstOfIterable(selected.values()) === commit.hash) { |
| b69ab31 | | | 718 | set(selectedCommits, new Set()); |
| b69ab31 | | | 719 | } |
| b69ab31 | | | 720 | }); |
| b69ab31 | | | 721 | |
| b69ab31 | | | 722 | const clearEditedCommitMessage = useCallback( |
| b69ab31 | | | 723 | async (skipConfirmation?: boolean) => { |
| b69ab31 | | | 724 | if (!skipConfirmation) { |
| b69ab31 | | | 725 | const hasUnsavedEdits = readAtom( |
| b69ab31 | | | 726 | hasUnsavedEditedCommitMessage(isCommitMode ? 'head' : commit.hash), |
| b69ab31 | | | 727 | ); |
| b69ab31 | | | 728 | if (hasUnsavedEdits) { |
| b69ab31 | | | 729 | const confirmed = await platform.confirm( |
| b69ab31 | | | 730 | t('Are you sure you want to discard your edited message?'), |
| b69ab31 | | | 731 | ); |
| b69ab31 | | | 732 | if (confirmed === false) { |
| b69ab31 | | | 733 | return; |
| b69ab31 | | | 734 | } |
| b69ab31 | | | 735 | } |
| b69ab31 | | | 736 | } |
| b69ab31 | | | 737 | |
| b69ab31 | | | 738 | // Delete the edited message atom (and delete from persisted storage) |
| b69ab31 | | | 739 | writeAtom(editedCommitMessages(isCommitMode ? 'head' : commit.hash), undefined); |
| b69ab31 | | | 740 | }, |
| b69ab31 | | | 741 | [commit.hash, isCommitMode], |
| b69ab31 | | | 742 | ); |
| b69ab31 | | | 743 | const doAmendOrCommit = () => { |
| b69ab31 | | | 744 | const updatedMessage = applyEditedFields(latestMessage, editedMessage); |
| b69ab31 | | | 745 | const message = commitMessageFieldsToString(schema, updatedMessage); |
| b69ab31 | | | 746 | const allFiles = uncommittedChanges.map(file => file.path); |
| b69ab31 | | | 747 | |
| b69ab31 | | | 748 | const operation = isCommitMode |
| b69ab31 | | | 749 | ? getCommitOperation(message, headCommit, selection.selection, allFiles) |
| b69ab31 | | | 750 | : getAmendOperation(message, headCommit, selection.selection, allFiles); |
| b69ab31 | | | 751 | |
| b69ab31 | | | 752 | selection.discardPartialSelections(); |
| b69ab31 | | | 753 | |
| b69ab31 | | | 754 | clearEditedCommitMessage(/* skip confirmation */ true); |
| b69ab31 | | | 755 | // reset to amend mode now that the commit has been made |
| b69ab31 | | | 756 | setMode('amend'); |
| b69ab31 | | | 757 | deselectIfHeadIsSelected(); |
| b69ab31 | | | 758 | |
| b69ab31 | | | 759 | return operation; |
| b69ab31 | | | 760 | }; |
| b69ab31 | | | 761 | |
| b69ab31 | | | 762 | const showOptionModal = useModal(); |
| b69ab31 | | | 763 | |
| b69ab31 | | | 764 | const codeReviewProviderName = provider?.label; |
| b69ab31 | | | 765 | |
| b69ab31 | | | 766 | const areImageUploadsOngoing = useAtomValue(imageUploadsPendingAtom); |
| b69ab31 | | | 767 | |
| b69ab31 | | | 768 | // Generally "Amend"/"Commit" for head commit, but if there's no changes while amending, just use "Amend message" |
| b69ab31 | | | 769 | const showCommitOrAmend = |
| b69ab31 | | | 770 | commit.isDot && (isCommitMode || anythingToCommit || !isAnythingBeingEdited); |
| b69ab31 | | | 771 | |
| b69ab31 | | | 772 | return ( |
| b69ab31 | | | 773 | <div className="commit-info-actions-bar" data-testid="commit-info-actions-bar"> |
| b69ab31 | | | 774 | {isCommitMode || commit.diffId == null ? null : ( |
| b69ab31 | | | 775 | <SubmitUpdateMessageInput commits={[commit]} /> |
| b69ab31 | | | 776 | )} |
| b69ab31 | | | 777 | <div className="commit-info-actions-bar-left"> |
| b69ab31 | | | 778 | <SubmitAsDraftCheckbox commitsToBeSubmit={isCommitMode ? [] : [commit]} /> |
| b69ab31 | | | 779 | <PublishWhenReadyCheckbox /> |
| b69ab31 | | | 780 | </div> |
| b69ab31 | | | 781 | <div className="commit-info-actions-bar-right"> |
| b69ab31 | | | 782 | {isAnythingBeingEdited && !isCommitMode ? ( |
| b69ab31 | | | 783 | <Button onClick={() => clearEditedCommitMessage()}> |
| b69ab31 | | | 784 | <T>Cancel</T> |
| b69ab31 | | | 785 | </Button> |
| b69ab31 | | | 786 | ) : null} |
| b69ab31 | | | 787 | |
| b69ab31 | | | 788 | {showCommitOrAmend ? ( |
| b69ab31 | | | 789 | <Tooltip |
| b69ab31 | | | 790 | title={ |
| b69ab31 | | | 791 | areImageUploadsOngoing |
| b69ab31 | | | 792 | ? t('Image uploads are still pending') |
| b69ab31 | | | 793 | : isCommitMode |
| b69ab31 | | | 794 | ? selection.isEverythingSelected() |
| b69ab31 | | | 795 | ? t('No changes to commit') |
| b69ab31 | | | 796 | : t('No selected changes to commit') |
| b69ab31 | | | 797 | : selection.isEverythingSelected() |
| b69ab31 | | | 798 | ? t('No changes to amend') |
| b69ab31 | | | 799 | : t('No selected changes to amend') |
| b69ab31 | | | 800 | } |
| b69ab31 | | | 801 | trigger={areImageUploadsOngoing || !anythingToCommit ? 'hover' : 'disabled'}> |
| b69ab31 | | | 802 | <OperationDisabledButton |
| b69ab31 | | | 803 | contextKey={isCommitMode ? 'commit' : 'amend'} |
| b69ab31 | | | 804 | disabled={!anythingToCommit || editedMessage == null || areImageUploadsOngoing} |
| b69ab31 | | | 805 | runOperation={async () => { |
| b69ab31 | | | 806 | if (!isCommitMode) { |
| b69ab31 | | | 807 | const updatedMessage = applyEditedFields(latestMessage, editedMessage); |
| b69ab31 | | | 808 | const stringifiedMessage = commitMessageFieldsToString(schema, updatedMessage); |
| b69ab31 | | | 809 | const diffId = findEditedDiffNumber(updatedMessage) ?? commit.diffId; |
| b69ab31 | | | 810 | // if there's a diff attached, we should also update the remote message |
| b69ab31 | | | 811 | if (messageSyncEnabled && diffId) { |
| b69ab31 | | | 812 | const shouldAbort = await tryToUpdateRemoteMessage( |
| b69ab31 | | | 813 | commit, |
| b69ab31 | | | 814 | diffId, |
| b69ab31 | | | 815 | stringifiedMessage, |
| b69ab31 | | | 816 | showOptionModal, |
| b69ab31 | | | 817 | 'amend', |
| b69ab31 | | | 818 | ); |
| b69ab31 | | | 819 | if (shouldAbort) { |
| b69ab31 | | | 820 | return; |
| b69ab31 | | | 821 | } |
| b69ab31 | | | 822 | } |
| b69ab31 | | | 823 | } |
| b69ab31 | | | 824 | |
| b69ab31 | | | 825 | { |
| b69ab31 | | | 826 | const shouldContinue = await confirmUnsavedFiles(); |
| b69ab31 | | | 827 | if (!shouldContinue) { |
| b69ab31 | | | 828 | return; |
| b69ab31 | | | 829 | } |
| b69ab31 | | | 830 | } |
| b69ab31 | | | 831 | |
| b69ab31 | | | 832 | { |
| b69ab31 | | | 833 | const shouldContinue = await confirmSuggestedEditsForFiles( |
| b69ab31 | | | 834 | isCommitMode ? 'commit' : 'amend', |
| b69ab31 | | | 835 | 'accept', |
| b69ab31 | | | 836 | selection.selection, |
| b69ab31 | | | 837 | ); |
| b69ab31 | | | 838 | if (!shouldContinue) { |
| b69ab31 | | | 839 | return; |
| b69ab31 | | | 840 | } |
| b69ab31 | | | 841 | } |
| b69ab31 | | | 842 | |
| b69ab31 | | | 843 | return doAmendOrCommit(); |
| b69ab31 | | | 844 | }}> |
| b69ab31 | | | 845 | {isCommitMode ? <T>Commit</T> : <T>Amend</T>} |
| b69ab31 | | | 846 | </OperationDisabledButton> |
| b69ab31 | | | 847 | </Tooltip> |
| b69ab31 | | | 848 | ) : ( |
| b69ab31 | | | 849 | <Tooltip |
| b69ab31 | | | 850 | title={ |
| b69ab31 | | | 851 | areImageUploadsOngoing |
| b69ab31 | | | 852 | ? t('Image uploads are still pending') |
| b69ab31 | | | 853 | : !isAnythingBeingEdited |
| b69ab31 | | | 854 | ? t('No message edits to amend') |
| b69ab31 | | | 855 | : messageSyncEnabled && commit.diffId != null |
| b69ab31 | | | 856 | ? t( |
| b69ab31 | | | 857 | 'Amend the commit message with the newly entered message, then sync that message up to $provider.', |
| b69ab31 | | | 858 | {replace: {$provider: codeReviewProviderName ?? 'remote'}}, |
| b69ab31 | | | 859 | ) |
| b69ab31 | | | 860 | : t('Amend the commit message with the newly entered message.') |
| b69ab31 | | | 861 | }> |
| b69ab31 | | | 862 | <OperationDisabledButton |
| b69ab31 | | | 863 | contextKey={`amend-message-${commit.hash}`} |
| b69ab31 | | | 864 | data-testid="amend-message-button" |
| b69ab31 | | | 865 | disabled={!isAnythingBeingEdited || editedMessage == null || areImageUploadsOngoing} |
| b69ab31 | | | 866 | runOperation={async () => { |
| b69ab31 | | | 867 | const updatedMessage = applyEditedFields(latestMessage, editedMessage); |
| b69ab31 | | | 868 | const stringifiedMessage = commitMessageFieldsToString(schema, updatedMessage); |
| b69ab31 | | | 869 | const diffId = findEditedDiffNumber(updatedMessage) ?? commit.diffId; |
| b69ab31 | | | 870 | // if there's a diff attached, we should also update the remote message |
| b69ab31 | | | 871 | if (messageSyncEnabled && diffId) { |
| b69ab31 | | | 872 | const shouldAbort = await tryToUpdateRemoteMessage( |
| b69ab31 | | | 873 | commit, |
| b69ab31 | | | 874 | diffId, |
| b69ab31 | | | 875 | stringifiedMessage, |
| b69ab31 | | | 876 | showOptionModal, |
| b69ab31 | | | 877 | 'amendMessage', |
| b69ab31 | | | 878 | ); |
| b69ab31 | | | 879 | if (shouldAbort) { |
| b69ab31 | | | 880 | return; |
| b69ab31 | | | 881 | } |
| b69ab31 | | | 882 | } |
| b69ab31 | | | 883 | |
| b69ab31 | | | 884 | const intendedAuthor = readAtom(authorString); |
| b69ab31 | | | 885 | const authorArg = |
| b69ab31 | | | 886 | intendedAuthor != null && commit.author !== intendedAuthor |
| b69ab31 | | | 887 | ? intendedAuthor |
| b69ab31 | | | 888 | : undefined; |
| b69ab31 | | | 889 | |
| b69ab31 | | | 890 | const operation = new AmendMessageOperation( |
| b69ab31 | | | 891 | latestSuccessorUnlessExplicitlyObsolete(commit), |
| b69ab31 | | | 892 | stringifiedMessage, |
| b69ab31 | | | 893 | authorArg, |
| b69ab31 | | | 894 | ); |
| b69ab31 | | | 895 | clearEditedCommitMessage(/* skip confirmation */ true); |
| b69ab31 | | | 896 | return operation; |
| b69ab31 | | | 897 | }}> |
| b69ab31 | | | 898 | <T>Amend Message</T> |
| b69ab31 | | | 899 | </OperationDisabledButton> |
| b69ab31 | | | 900 | </Tooltip> |
| b69ab31 | | | 901 | )} |
| b69ab31 | | | 902 | <SubmitButton |
| b69ab31 | | | 903 | commit={commit} |
| b69ab31 | | | 904 | getAmendOrCommitOperation={doAmendOrCommit} |
| b69ab31 | | | 905 | anythingToCommit={anythingToCommit} |
| b69ab31 | | | 906 | isAnythingBeingEdited={isAnythingBeingEdited} |
| b69ab31 | | | 907 | isCommitMode={isCommitMode} |
| b69ab31 | | | 908 | /> |
| b69ab31 | | | 909 | </div> |
| b69ab31 | | | 910 | </div> |
| b69ab31 | | | 911 | ); |
| b69ab31 | | | 912 | } |
| b69ab31 | | | 913 | |
| b69ab31 | | | 914 | function SubmitButton({ |
| b69ab31 | | | 915 | commit, |
| b69ab31 | | | 916 | getAmendOrCommitOperation, |
| b69ab31 | | | 917 | anythingToCommit, |
| b69ab31 | | | 918 | isAnythingBeingEdited, |
| b69ab31 | | | 919 | isCommitMode, |
| b69ab31 | | | 920 | }: { |
| b69ab31 | | | 921 | commit: CommitInfo; |
| b69ab31 | | | 922 | getAmendOrCommitOperation: () => Operation; |
| b69ab31 | | | 923 | anythingToCommit: boolean; |
| b69ab31 | | | 924 | isAnythingBeingEdited: boolean; |
| b69ab31 | | | 925 | isCommitMode: boolean; |
| b69ab31 | | | 926 | }) { |
| b69ab31 | | | 927 | const [repoInfo, setRepoInfo] = useAtom(repositoryInfo); |
| b69ab31 | | | 928 | const diffSummaries = useAtomValue(allDiffSummaries); |
| b69ab31 | | | 929 | const shouldSubmitAsDraft = useAtomValue(submitAsDraft); |
| b69ab31 | | | 930 | const shouldPublishWhenReady = useAtomValue(publishWhenReady); |
| b69ab31 | | | 931 | const [updateMessage, setUpdateMessage] = useAtom(diffUpdateMessagesState(commit.hash)); |
| b69ab31 | | | 932 | const provider = useAtomValue(codeReviewProvider); |
| b69ab31 | | | 933 | |
| b69ab31 | | | 934 | const codeReviewProviderType = repoInfo?.codeReviewSystem.type ?? 'unknown'; |
| b69ab31 | | | 935 | const canSubmitWithCodeReviewProvider = |
| b69ab31 | | | 936 | codeReviewProviderType !== 'none' && codeReviewProviderType !== 'unknown'; |
| b69ab31 | | | 937 | const submittable = |
| b69ab31 | | | 938 | diffSummaries.value && provider?.getSubmittableDiffs([commit], diffSummaries.value); |
| b69ab31 | | | 939 | const canSubmitIndividualDiffs = submittable && submittable.length > 0; |
| b69ab31 | | | 940 | |
| b69ab31 | | | 941 | const showOptionModal = useModal(); |
| b69ab31 | | | 942 | const forceEnableSubmit = useAtomValue(overrideDisabledSubmitModes); |
| b69ab31 | | | 943 | const submitDisabledReason = forceEnableSubmit ? undefined : provider?.submitDisabledReason?.(); |
| b69ab31 | | | 944 | const messageSyncEnabled = useAtomValue(messageSyncingEnabledState); |
| b69ab31 | | | 945 | const areImageUploadsOngoing = useAtomValue(imageUploadsPendingAtom); |
| b69ab31 | | | 946 | |
| b69ab31 | | | 947 | const runOperation = useRunOperation(); |
| b69ab31 | | | 948 | |
| b69ab31 | | | 949 | const selection = useUncommittedSelection(); |
| b69ab31 | | | 950 | |
| 6c9fcae | | | 951 | // When no code review system is configured, show a "Create Grove Repository" button |
| 6c9fcae | | | 952 | if (codeReviewProviderType === 'none') { |
| 6c9fcae | | | 953 | return ( |
| 6c9fcae | | | 954 | <Tooltip title={t('Create a repository on Grove to enable push and code review')} placement="top"> |
| 6c9fcae | | | 955 | <Button kind="primary" onClick={openCreateGroveRepoModal}> |
| 6c9fcae | | | 956 | <T>Create Grove Repository</T> |
| 6c9fcae | | | 957 | </Button> |
| 6c9fcae | | | 958 | </Tooltip> |
| 6c9fcae | | | 959 | ); |
| 6c9fcae | | | 960 | } |
| 6c9fcae | | | 961 | |
| b69ab31 | | | 962 | const isBranchingPREnabled = |
| b69ab31 | | | 963 | codeReviewProviderType === 'github' && repoInfo?.preferredSubmitCommand === 'push'; |
| b69ab31 | | | 964 | |
| b69ab31 | | | 965 | const disabledReason = areImageUploadsOngoing |
| b69ab31 | | | 966 | ? t('Image uploads are still pending') |
| b69ab31 | | | 967 | : submitDisabledReason |
| b69ab31 | | | 968 | ? submitDisabledReason |
| b69ab31 | | | 969 | : !canSubmitWithCodeReviewProvider |
| b69ab31 | | | 970 | ? t('No code review system found for this repository') |
| b69ab31 | | | 971 | : null; |
| b69ab31 | | | 972 | |
| b69ab31 | | | 973 | const getApplicableOperations = async (): Promise<Array<Operation> | undefined> => { |
| b69ab31 | | | 974 | const shouldContinue = await confirmUnsavedFiles(); |
| b69ab31 | | | 975 | if (!shouldContinue) { |
| b69ab31 | | | 976 | return; |
| b69ab31 | | | 977 | } |
| b69ab31 | | | 978 | |
| b69ab31 | | | 979 | if (!(await confirmNoBlockingDiagnostics(selection, isCommitMode ? undefined : commit))) { |
| b69ab31 | | | 980 | return; |
| b69ab31 | | | 981 | } |
| b69ab31 | | | 982 | |
| b69ab31 | | | 983 | let amendOrCommitOp; |
| b69ab31 | | | 984 | if (commit.isDot && anythingToCommit) { |
| b69ab31 | | | 985 | // TODO: we should also amend if there are pending commit message changes, and change the button |
| b69ab31 | | | 986 | // to amend message & submit. |
| b69ab31 | | | 987 | // Or just remove the submit button if you start editing since we'll update the remote message anyway... |
| b69ab31 | | | 988 | amendOrCommitOp = getAmendOrCommitOperation(); |
| b69ab31 | | | 989 | } |
| b69ab31 | | | 990 | |
| b69ab31 | | | 991 | if ( |
| b69ab31 | | | 992 | repoInfo?.type === 'success' && |
| b69ab31 | | | 993 | repoInfo.codeReviewSystem.type === 'github' && |
| b69ab31 | | | 994 | repoInfo.preferredSubmitCommand == null |
| b69ab31 | | | 995 | ) { |
| b69ab31 | | | 996 | const buttons = [t('Cancel') as 'Cancel', 'ghstack', 'pr'] as const; |
| b69ab31 | | | 997 | const cancel = buttons[0]; |
| b69ab31 | | | 998 | const answer = await showOptionModal({ |
| b69ab31 | | | 999 | type: 'confirm', |
| b69ab31 | | | 1000 | icon: 'warning', |
| b69ab31 | | | 1001 | title: t('Preferred Code Review command not yet configured'), |
| b69ab31 | | | 1002 | message: ( |
| b69ab31 | | | 1003 | <div className="commit-info-confirm-modal-paragraphs"> |
| b69ab31 | | | 1004 | <div> |
| b69ab31 | | | 1005 | <T replace={{$pr: <code>sl pr</code>, $ghstack: <code>sl ghstack</code>}}> |
| b69ab31 | | | 1006 | You can configure Sapling to use either $pr or $ghstack to submit for code review on |
| b69ab31 | | | 1007 | GitHub. |
| b69ab31 | | | 1008 | </T> |
| b69ab31 | | | 1009 | </div> |
| b69ab31 | | | 1010 | <div> |
| b69ab31 | | | 1011 | <T |
| b69ab31 | | | 1012 | replace={{ |
| b69ab31 | | | 1013 | $config: <code>github.preferred_submit_command</code>, |
| b69ab31 | | | 1014 | }}> |
| b69ab31 | | | 1015 | Each submit command has tradeoffs, due to how GitHub creates Pull Requests. This can |
| b69ab31 | | | 1016 | be controlled by the $config config. |
| b69ab31 | | | 1017 | </T> |
| b69ab31 | | | 1018 | </div> |
| b69ab31 | | | 1019 | <div> |
| b69ab31 | | | 1020 | <T>To continue, select a command to use to submit.</T> |
| b69ab31 | | | 1021 | </div> |
| b69ab31 | | | 1022 | <Link href="https://sapling-scm.com/docs/git/intro#pull-requests"> |
| b69ab31 | | | 1023 | <T>Learn More</T> |
| b69ab31 | | | 1024 | </Link> |
| b69ab31 | | | 1025 | </div> |
| b69ab31 | | | 1026 | ), |
| b69ab31 | | | 1027 | buttons, |
| b69ab31 | | | 1028 | }); |
| b69ab31 | | | 1029 | if (answer === cancel || answer == null) { |
| b69ab31 | | | 1030 | return; |
| b69ab31 | | | 1031 | } |
| b69ab31 | | | 1032 | const rememberConfigOp = new SetConfigOperation( |
| b69ab31 | | | 1033 | 'local', |
| b69ab31 | | | 1034 | 'github.preferred_submit_command', |
| b69ab31 | | | 1035 | answer, |
| b69ab31 | | | 1036 | ); |
| b69ab31 | | | 1037 | setRepoInfo(info => ({ |
| b69ab31 | | | 1038 | ...nullthrows(info), |
| b69ab31 | | | 1039 | preferredSubmitCommand: answer, |
| b69ab31 | | | 1040 | })); |
| b69ab31 | | | 1041 | // setRepoInfo updates `provider`, but we still have a stale reference in this callback. |
| b69ab31 | | | 1042 | // So this one time, we need to manually run the new submit command. |
| b69ab31 | | | 1043 | // Future submit calls can delegate to provider.submitOperation(); |
| b69ab31 | | | 1044 | const submitOp = |
| b69ab31 | | | 1045 | answer === 'ghstack' |
| b69ab31 | | | 1046 | ? new GhStackSubmitOperation({ |
| b69ab31 | | | 1047 | draft: shouldSubmitAsDraft, |
| b69ab31 | | | 1048 | }) |
| b69ab31 | | | 1049 | : answer === 'pr' |
| b69ab31 | | | 1050 | ? new PrSubmitOperation({ |
| b69ab31 | | | 1051 | draft: shouldSubmitAsDraft, |
| b69ab31 | | | 1052 | }) |
| b69ab31 | | | 1053 | : null; |
| b69ab31 | | | 1054 | |
| b69ab31 | | | 1055 | // TODO: account for branching PR |
| b69ab31 | | | 1056 | |
| b69ab31 | | | 1057 | return [amendOrCommitOp, rememberConfigOp, submitOp].filter(notEmpty); |
| b69ab31 | | | 1058 | } |
| b69ab31 | | | 1059 | |
| b69ab31 | | | 1060 | // Only do message sync if we're amending the local commit in some way. |
| b69ab31 | | | 1061 | // If we're just doing a submit, we expect the message to have been synced previously |
| b69ab31 | | | 1062 | // during another amend or amend message. |
| b69ab31 | | | 1063 | const shouldUpdateMessage = !isCommitMode && messageSyncEnabled && anythingToCommit; |
| b69ab31 | | | 1064 | |
| b69ab31 | | | 1065 | const submitOp = isBranchingPREnabled |
| b69ab31 | | | 1066 | ? null // branching PRs will show a follow-up modal which controls submitting |
| b69ab31 | | | 1067 | : nullthrows(provider).submitOperation( |
| b69ab31 | | | 1068 | commit.isDot ? [] : [commit], // [] means to submit the head commit |
| b69ab31 | | | 1069 | { |
| b69ab31 | | | 1070 | draft: shouldSubmitAsDraft, |
| b69ab31 | | | 1071 | updateFields: shouldUpdateMessage, |
| b69ab31 | | | 1072 | updateMessage: updateMessage || undefined, |
| b69ab31 | | | 1073 | publishWhenReady: shouldPublishWhenReady, |
| b69ab31 | | | 1074 | }, |
| b69ab31 | | | 1075 | ); |
| b69ab31 | | | 1076 | |
| b69ab31 | | | 1077 | // clear out the update message now that we've used it to submit |
| b69ab31 | | | 1078 | if (updateMessage) { |
| b69ab31 | | | 1079 | setUpdateMessage(''); |
| b69ab31 | | | 1080 | } |
| b69ab31 | | | 1081 | |
| b69ab31 | | | 1082 | return [amendOrCommitOp, submitOp].filter(notEmpty); |
| b69ab31 | | | 1083 | }; |
| b69ab31 | | | 1084 | |
| b69ab31 | | | 1085 | return (commit.isDot && (anythingToCommit || !isAnythingBeingEdited)) || |
| b69ab31 | | | 1086 | (!commit.isDot && |
| b69ab31 | | | 1087 | canSubmitIndividualDiffs && |
| b69ab31 | | | 1088 | // For non-head commits, "submit" doesn't update the message, which is confusing. |
| b69ab31 | | | 1089 | // Just hide the submit button so you're encouraged to "amend message" first. |
| b69ab31 | | | 1090 | !isAnythingBeingEdited) ? ( |
| b69ab31 | | | 1091 | <Tooltip |
| b69ab31 | | | 1092 | title={ |
| b69ab31 | | | 1093 | disabledReason ?? |
| 8d8e815 | | | 1094 | t('$action with $provider', { |
| 8d8e815 | | | 1095 | replace: { |
| 8d8e815 | | | 1096 | $action: provider?.submitButtonLabel ?? 'Submit', |
| 8d8e815 | | | 1097 | $provider: provider?.label ?? 'remote', |
| 8d8e815 | | | 1098 | }, |
| b69ab31 | | | 1099 | }) |
| b69ab31 | | | 1100 | } |
| b69ab31 | | | 1101 | placement="top"> |
| b69ab31 | | | 1102 | {isBranchingPREnabled ? ( |
| b69ab31 | | | 1103 | <Button |
| b69ab31 | | | 1104 | primary |
| b69ab31 | | | 1105 | disabled={disabledReason != null} |
| b69ab31 | | | 1106 | onClick={async () => { |
| b69ab31 | | | 1107 | try { |
| b69ab31 | | | 1108 | const operations = await getApplicableOperations(); |
| b69ab31 | | | 1109 | if (operations == null || operations.length === 0) { |
| b69ab31 | | | 1110 | return; |
| b69ab31 | | | 1111 | } |
| b69ab31 | | | 1112 | |
| b69ab31 | | | 1113 | for (const operation of operations) { |
| b69ab31 | | | 1114 | runOperation(operation); |
| b69ab31 | | | 1115 | } |
| b69ab31 | | | 1116 | const dag = readAtom(dagWithPreviews); |
| b69ab31 | | | 1117 | const topOfStack = commit.isDot && isCommitMode ? dag.resolve('.') : commit; |
| b69ab31 | | | 1118 | if (topOfStack == null) { |
| b69ab31 | | | 1119 | throw new Error('could not find commit to push'); |
| b69ab31 | | | 1120 | } |
| b69ab31 | | | 1121 | const pushOps = await showBranchingPrModal(topOfStack); |
| b69ab31 | | | 1122 | if (pushOps == null) { |
| b69ab31 | | | 1123 | return; |
| b69ab31 | | | 1124 | } |
| b69ab31 | | | 1125 | for (const pushOp of pushOps) { |
| b69ab31 | | | 1126 | runOperation(pushOp); |
| b69ab31 | | | 1127 | } |
| b69ab31 | | | 1128 | } catch (err) { |
| b69ab31 | | | 1129 | const error = err as Error; |
| b69ab31 | | | 1130 | showToast(<ErrorNotice error={error} title={<T>Failed to push commits</T>} />, { |
| b69ab31 | | | 1131 | durationMs: 10000, |
| b69ab31 | | | 1132 | }); |
| b69ab31 | | | 1133 | } |
| b69ab31 | | | 1134 | }}> |
| b69ab31 | | | 1135 | {commit.isDot && anythingToCommit ? ( |
| b69ab31 | | | 1136 | isCommitMode ? ( |
| b69ab31 | | | 1137 | <T>Commit and Push...</T> |
| b69ab31 | | | 1138 | ) : ( |
| b69ab31 | | | 1139 | <T>Amend and Push...</T> |
| b69ab31 | | | 1140 | ) |
| b69ab31 | | | 1141 | ) : ( |
| b69ab31 | | | 1142 | <T>Push...</T> |
| b69ab31 | | | 1143 | )} |
| b69ab31 | | | 1144 | </Button> |
| b69ab31 | | | 1145 | ) : ( |
| b69ab31 | | | 1146 | <OperationDisabledButton |
| b69ab31 | | | 1147 | kind="primary" |
| b69ab31 | | | 1148 | contextKey={`submit-${commit.isDot ? 'head' : commit.hash}`} |
| b69ab31 | | | 1149 | disabled={disabledReason != null} |
| b69ab31 | | | 1150 | runOperation={getApplicableOperations}> |
| b69ab31 | | | 1151 | {commit.isDot && anythingToCommit ? ( |
| b69ab31 | | | 1152 | isCommitMode ? ( |
| 8d8e815 | | | 1153 | <T replace={{$action: provider?.submitButtonLabel ?? 'Submit'}}> |
| 8d8e815 | | | 1154 | Commit and $action |
| 8d8e815 | | | 1155 | </T> |
| b69ab31 | | | 1156 | ) : ( |
| 8d8e815 | | | 1157 | <T replace={{$action: provider?.submitButtonLabel ?? 'Submit'}}> |
| 8d8e815 | | | 1158 | Amend and $action |
| 8d8e815 | | | 1159 | </T> |
| b69ab31 | | | 1160 | ) |
| b69ab31 | | | 1161 | ) : ( |
| 8d8e815 | | | 1162 | <T>{provider?.submitButtonLabel ?? 'Submit'}</T> |
| b69ab31 | | | 1163 | )} |
| b69ab31 | | | 1164 | </OperationDisabledButton> |
| b69ab31 | | | 1165 | )} |
| b69ab31 | | | 1166 | </Tooltip> |
| b69ab31 | | | 1167 | ) : null; |
| b69ab31 | | | 1168 | } |
| b69ab31 | | | 1169 | |
| b69ab31 | | | 1170 | async function tryToUpdateRemoteMessage( |
| b69ab31 | | | 1171 | commit: CommitInfo, |
| b69ab31 | | | 1172 | diffId: DiffId, |
| b69ab31 | | | 1173 | latestMessageString: string, |
| b69ab31 | | | 1174 | showOptionModal: ReturnType<typeof useModal>, |
| b69ab31 | | | 1175 | reason: 'amend' | 'amendMessage', |
| b69ab31 | | | 1176 | ): Promise<boolean> { |
| b69ab31 | | | 1177 | // TODO: we could skip the update if the new message matches the old one, |
| b69ab31 | | | 1178 | // which is possible when amending changes without changing the commit message |
| b69ab31 | | | 1179 | |
| b69ab31 | | | 1180 | let optedOutOfSync = false; |
| b69ab31 | | | 1181 | if (diffId !== commit.diffId) { |
| b69ab31 | | | 1182 | const buttons = [ |
| b69ab31 | | | 1183 | t('Cancel') as 'Cancel', |
| b69ab31 | | | 1184 | t('Use Remote Message'), |
| b69ab31 | | | 1185 | t('Sync New Message'), |
| b69ab31 | | | 1186 | ] as const; |
| b69ab31 | | | 1187 | const cancel = buttons[0]; |
| b69ab31 | | | 1188 | const syncButton = buttons[2]; |
| b69ab31 | | | 1189 | const answer = await showOptionModal({ |
| b69ab31 | | | 1190 | type: 'confirm', |
| b69ab31 | | | 1191 | icon: 'warning', |
| b69ab31 | | | 1192 | title: t('Sync message for newly attached Diff?'), |
| b69ab31 | | | 1193 | message: ( |
| b69ab31 | | | 1194 | <T> |
| b69ab31 | | | 1195 | You're changing the attached Diff for this commit. Would you like you sync your new local |
| b69ab31 | | | 1196 | message up to the remote Diff, or just use the existing remote message for this Diff? |
| b69ab31 | | | 1197 | </T> |
| b69ab31 | | | 1198 | ), |
| b69ab31 | | | 1199 | buttons, |
| b69ab31 | | | 1200 | }); |
| b69ab31 | | | 1201 | tracker.track('ConfirmSyncNewDiffNumber', { |
| b69ab31 | | | 1202 | extras: { |
| b69ab31 | | | 1203 | choice: answer, |
| b69ab31 | | | 1204 | }, |
| b69ab31 | | | 1205 | }); |
| b69ab31 | | | 1206 | if (answer === cancel || answer == null) { |
| b69ab31 | | | 1207 | return true; // abort |
| b69ab31 | | | 1208 | } |
| b69ab31 | | | 1209 | optedOutOfSync = answer !== syncButton; |
| b69ab31 | | | 1210 | } |
| b69ab31 | | | 1211 | if (!optedOutOfSync) { |
| b69ab31 | | | 1212 | const title = firstLine(latestMessageString); |
| b69ab31 | | | 1213 | const description = latestMessageString.slice(title.length); |
| b69ab31 | | | 1214 | // don't wait for the update mutation to go through, just let it happen in parallel with the metaedit |
| b69ab31 | | | 1215 | tracker |
| b69ab31 | | | 1216 | .operation('SyncDiffMessageMutation', 'SyncMessageError', {extras: {reason}}, () => |
| b69ab31 | | | 1217 | updateRemoteMessage(diffId, title, description), |
| b69ab31 | | | 1218 | ) |
| b69ab31 | | | 1219 | .catch(err => { |
| b69ab31 | | | 1220 | // Uh oh we failed to sync. Let's override all syncing so you can see your local changes |
| b69ab31 | | | 1221 | // and we don't get you stuck in a syncing loop. |
| b69ab31 | | | 1222 | |
| b69ab31 | | | 1223 | writeAtom(messageSyncingOverrideState, false); |
| b69ab31 | | | 1224 | |
| b69ab31 | | | 1225 | showToast( |
| b69ab31 | | | 1226 | <Banner kind={BannerKind.error}> |
| b69ab31 | | | 1227 | <Column alignStart> |
| b69ab31 | | | 1228 | <div> |
| b69ab31 | | | 1229 | <T>Failed to sync message to remote. Further syncing has been disabled.</T> |
| b69ab31 | | | 1230 | </div> |
| b69ab31 | | | 1231 | <div> |
| b69ab31 | | | 1232 | <T>Try manually syncing and restarting ISL.</T> |
| b69ab31 | | | 1233 | </div> |
| b69ab31 | | | 1234 | <div>{firstLine(err.message || err.toString())}</div> |
| b69ab31 | | | 1235 | </Column> |
| b69ab31 | | | 1236 | </Banner>, |
| b69ab31 | | | 1237 | { |
| b69ab31 | | | 1238 | durationMs: 20_000, |
| b69ab31 | | | 1239 | }, |
| b69ab31 | | | 1240 | ); |
| b69ab31 | | | 1241 | }); |
| b69ab31 | | | 1242 | } |
| b69ab31 | | | 1243 | return false; |
| b69ab31 | | | 1244 | } |