| 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 {MutableRefObject} from 'react'; |
| b69ab31 | | | 9 | import type {FieldsBeingEdited} from '../../CommitInfoView/types'; |
| b69ab31 | | | 10 | import type {CommitInfo} from '../../types'; |
| b69ab31 | | | 11 | |
| b69ab31 | | | 12 | import {Button} from 'isl-components/Button'; |
| b69ab31 | | | 13 | import {Divider} from 'isl-components/Divider'; |
| b69ab31 | | | 14 | import {Icon} from 'isl-components/Icon'; |
| b69ab31 | | | 15 | import {useAtomValue} from 'jotai'; |
| b69ab31 | | | 16 | import {useCallback} from 'react'; |
| b69ab31 | | | 17 | import {useAutofocusRef} from 'shared/hooks'; |
| b69ab31 | | | 18 | import {Commit} from '../../Commit'; |
| b69ab31 | | | 19 | import { |
| b69ab31 | | | 20 | editedCommitMessages, |
| b69ab31 | | | 21 | getDefaultEditedCommitMessage, |
| b69ab31 | | | 22 | unsavedFieldsBeingEdited, |
| b69ab31 | | | 23 | } from '../../CommitInfoView/CommitInfoState'; |
| b69ab31 | | | 24 | import {commitMessageFieldsSchema} from '../../CommitInfoView/CommitMessageFields'; |
| b69ab31 | | | 25 | import {FlexSpacer} from '../../ComponentUtils'; |
| b69ab31 | | | 26 | import {T, t} from '../../i18n'; |
| b69ab31 | | | 27 | import {readAtom, writeAtom} from '../../jotaiUtils'; |
| b69ab31 | | | 28 | import {CommitPreview} from '../../previews'; |
| b69ab31 | | | 29 | import {useModal} from '../../useModal'; |
| b69ab31 | | | 30 | |
| b69ab31 | | | 31 | import './ConfirmUnsavedEditsBeforeSplit.css'; |
| b69ab31 | | | 32 | |
| b69ab31 | | | 33 | type UnsavedEditConfirmKind = 'split' | 'edit_stack'; |
| b69ab31 | | | 34 | |
| b69ab31 | | | 35 | export function useConfirmUnsavedEditsBeforeSplit(): ( |
| b69ab31 | | | 36 | commits: Array<CommitInfo>, |
| b69ab31 | | | 37 | kind: UnsavedEditConfirmKind, |
| b69ab31 | | | 38 | ) => Promise<boolean> { |
| b69ab31 | | | 39 | const showModal = useModal(); |
| b69ab31 | | | 40 | const showConfirmation = useCallback( |
| b69ab31 | | | 41 | async (commits: Array<CommitInfo>, kind: UnsavedEditConfirmKind): Promise<boolean> => { |
| b69ab31 | | | 42 | const editedCommits = commits |
| b69ab31 | | | 43 | .map(commit => [commit, readAtom(unsavedFieldsBeingEdited(commit.hash))]) |
| b69ab31 | | | 44 | .filter(([_, f]) => f != null) as Array<[CommitInfo, FieldsBeingEdited]>; |
| b69ab31 | | | 45 | if (editedCommits.some(([_, f]) => Object.values(f).some(Boolean))) { |
| b69ab31 | | | 46 | const continueWithSplit = await showModal<boolean>({ |
| b69ab31 | | | 47 | type: 'custom', |
| b69ab31 | | | 48 | component: ({returnResultAndDismiss}) => ( |
| b69ab31 | | | 49 | <PreSplitUnsavedEditsConfirmationModal |
| b69ab31 | | | 50 | kind={kind} |
| b69ab31 | | | 51 | editedCommits={editedCommits} |
| b69ab31 | | | 52 | returnResultAndDismiss={returnResultAndDismiss} |
| b69ab31 | | | 53 | /> |
| b69ab31 | | | 54 | ), |
| b69ab31 | | | 55 | title: |
| b69ab31 | | | 56 | kind === 'split' |
| b69ab31 | | | 57 | ? t('Save edits before splitting?') |
| b69ab31 | | | 58 | : t('Save edits before editing stack?'), |
| b69ab31 | | | 59 | }); |
| b69ab31 | | | 60 | return continueWithSplit === true; |
| b69ab31 | | | 61 | } |
| b69ab31 | | | 62 | return true; |
| b69ab31 | | | 63 | }, |
| b69ab31 | | | 64 | [showModal], |
| b69ab31 | | | 65 | ); |
| b69ab31 | | | 66 | |
| b69ab31 | | | 67 | return (commits: Array<CommitInfo>, kind: UnsavedEditConfirmKind) => { |
| b69ab31 | | | 68 | return showConfirmation(commits, kind); |
| b69ab31 | | | 69 | }; |
| b69ab31 | | | 70 | } |
| b69ab31 | | | 71 | |
| b69ab31 | | | 72 | function PreSplitUnsavedEditsConfirmationModal({ |
| b69ab31 | | | 73 | kind, |
| b69ab31 | | | 74 | editedCommits, |
| b69ab31 | | | 75 | returnResultAndDismiss, |
| b69ab31 | | | 76 | }: { |
| b69ab31 | | | 77 | kind: UnsavedEditConfirmKind; |
| b69ab31 | | | 78 | editedCommits: Array<[CommitInfo, FieldsBeingEdited]>; |
| b69ab31 | | | 79 | returnResultAndDismiss: (continueWithSplit: boolean) => unknown; |
| b69ab31 | | | 80 | }) { |
| b69ab31 | | | 81 | const schema = useAtomValue(commitMessageFieldsSchema); |
| b69ab31 | | | 82 | |
| b69ab31 | | | 83 | const resetEditedCommitMessage = useCallback((commit: CommitInfo) => { |
| b69ab31 | | | 84 | writeAtom(editedCommitMessages(commit.hash), getDefaultEditedCommitMessage()); |
| b69ab31 | | | 85 | }, []); |
| b69ab31 | | | 86 | |
| b69ab31 | | | 87 | const commitsWithUnsavedEdits = editedCommits.filter(([_, fields]) => |
| b69ab31 | | | 88 | Object.values(fields).some(Boolean), |
| b69ab31 | | | 89 | ); |
| b69ab31 | | | 90 | |
| b69ab31 | | | 91 | const saveButtonRef = useAutofocusRef(); |
| b69ab31 | | | 92 | |
| b69ab31 | | | 93 | return ( |
| b69ab31 | | | 94 | <div className="confirm-unsaved-edits-pre-split" data-testid="confirm-unsaved-edits-pre-split"> |
| b69ab31 | | | 95 | <> |
| b69ab31 | | | 96 | <div> |
| b69ab31 | | | 97 | <T count={commitsWithUnsavedEdits.length}> |
| b69ab31 | | | 98 | {kind === 'split' |
| b69ab31 | | | 99 | ? 'confirmUnsavedEditsBeforeSplit' |
| b69ab31 | | | 100 | : 'confirmUnsavedEditsBeforeEditStack'} |
| b69ab31 | | | 101 | </T> |
| b69ab31 | | | 102 | </div> |
| b69ab31 | | | 103 | <div className="commits-with-unsaved-changes"> |
| b69ab31 | | | 104 | {commitsWithUnsavedEdits.map(([commit, fields]) => ( |
| b69ab31 | | | 105 | <div className="commit-row" key={commit.hash}> |
| b69ab31 | | | 106 | <Commit |
| b69ab31 | | | 107 | commit={commit} |
| b69ab31 | | | 108 | hasChildren={false} |
| b69ab31 | | | 109 | previewType={CommitPreview.NON_ACTIONABLE_COMMIT} |
| b69ab31 | | | 110 | /> |
| b69ab31 | | | 111 | <span key={`${commit.hash}-fields`} className="byline"> |
| b69ab31 | | | 112 | <T |
| b69ab31 | | | 113 | replace={{ |
| b69ab31 | | | 114 | $commitTitle: commit.title, |
| b69ab31 | | | 115 | $fields: ( |
| b69ab31 | | | 116 | <> |
| b69ab31 | | | 117 | {Object.entries(fields) |
| b69ab31 | | | 118 | .filter(([, value]) => value) |
| b69ab31 | | | 119 | .map(([field]) => { |
| b69ab31 | | | 120 | const icon = schema.find(f => f.key === field)?.icon; |
| b69ab31 | | | 121 | return ( |
| b69ab31 | | | 122 | <span key={field} className="field-name"> |
| b69ab31 | | | 123 | {icon && <Icon icon={icon} />} |
| b69ab31 | | | 124 | {field} |
| b69ab31 | | | 125 | </span> |
| b69ab31 | | | 126 | ); |
| b69ab31 | | | 127 | })} |
| b69ab31 | | | 128 | </> |
| b69ab31 | | | 129 | ), |
| b69ab31 | | | 130 | }}> |
| b69ab31 | | | 131 | unsaved changes to $fields |
| b69ab31 | | | 132 | </T> |
| b69ab31 | | | 133 | </span> |
| b69ab31 | | | 134 | </div> |
| b69ab31 | | | 135 | ))} |
| b69ab31 | | | 136 | </div> |
| b69ab31 | | | 137 | <Divider /> |
| b69ab31 | | | 138 | <div className="use-modal-buttons"> |
| b69ab31 | | | 139 | <FlexSpacer /> |
| b69ab31 | | | 140 | <Button onClick={() => returnResultAndDismiss(false)}> |
| b69ab31 | | | 141 | <T>Cancel</T> |
| b69ab31 | | | 142 | </Button> |
| b69ab31 | | | 143 | <Button |
| b69ab31 | | | 144 | onClick={() => { |
| b69ab31 | | | 145 | for (const [commit] of editedCommits) { |
| b69ab31 | | | 146 | resetEditedCommitMessage(commit); |
| b69ab31 | | | 147 | } |
| b69ab31 | | | 148 | returnResultAndDismiss(true); // continue with split |
| b69ab31 | | | 149 | }}> |
| b69ab31 | | | 150 | <T>Discard Edits</T> |
| b69ab31 | | | 151 | </Button> |
| b69ab31 | | | 152 | <Button |
| b69ab31 | | | 153 | ref={saveButtonRef as MutableRefObject<null>} |
| b69ab31 | | | 154 | primary |
| b69ab31 | | | 155 | onClick={() => { |
| b69ab31 | | | 156 | // Unsaved edits will be automatically loaded by the split as the commits' text |
| b69ab31 | | | 157 | returnResultAndDismiss(true); // continue with split |
| b69ab31 | | | 158 | }}> |
| b69ab31 | | | 159 | <T>Save Edits</T> |
| b69ab31 | | | 160 | </Button> |
| b69ab31 | | | 161 | </div> |
| b69ab31 | | | 162 | </> |
| b69ab31 | | | 163 | </div> |
| b69ab31 | | | 164 | ); |
| b69ab31 | | | 165 | } |