| 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, Hash} from './types'; |
| b69ab31 | | | 9 | |
| b69ab31 | | | 10 | import {Button} from 'isl-components/Button'; |
| b69ab31 | | | 11 | import {Icon} from 'isl-components/Icon'; |
| b69ab31 | | | 12 | import {Tooltip} from 'isl-components/Tooltip'; |
| b69ab31 | | | 13 | import {atom, useAtomValue} from 'jotai'; |
| b69ab31 | | | 14 | import {useCallback} from 'react'; |
| b69ab31 | | | 15 | import {editedCommitMessages} from './CommitInfoView/CommitInfoState'; |
| b69ab31 | | | 16 | import { |
| b69ab31 | | | 17 | applyEditedFields, |
| b69ab31 | | | 18 | commitMessageFieldsSchema, |
| b69ab31 | | | 19 | commitMessageFieldsToString, |
| b69ab31 | | | 20 | mergeManyCommitMessageFields, |
| b69ab31 | | | 21 | parseCommitMessageFields, |
| b69ab31 | | | 22 | } from './CommitInfoView/CommitMessageFields'; |
| b69ab31 | | | 23 | import {T, t} from './i18n'; |
| b69ab31 | | | 24 | import {readAtom, writeAtom} from './jotaiUtils'; |
| b69ab31 | | | 25 | import { |
| b69ab31 | | | 26 | FOLD_COMMIT_PREVIEW_HASH_PREFIX, |
| b69ab31 | | | 27 | FoldOperation, |
| b69ab31 | | | 28 | getFoldRangeCommitHash, |
| b69ab31 | | | 29 | } from './operations/FoldOperation'; |
| b69ab31 | | | 30 | import {operationBeingPreviewed, useRunPreviewedOperation} from './operationsState'; |
| b69ab31 | | | 31 | import {type Dag, dagWithPreviews} from './previews'; |
| b69ab31 | | | 32 | import {selectedCommits} from './selection'; |
| b69ab31 | | | 33 | import {firstOfIterable} from './utils'; |
| b69ab31 | | | 34 | |
| b69ab31 | | | 35 | /** |
| b69ab31 | | | 36 | * If the selected commits are linear, contiguous, and non-branching, they may be folded together. |
| b69ab31 | | | 37 | * This selector gives the range of commits that can be folded, if any, |
| b69ab31 | | | 38 | * so a button may be shown to do the fold. |
| b69ab31 | | | 39 | */ |
| b69ab31 | | | 40 | export const foldableSelection = atom(get => { |
| b69ab31 | | | 41 | const selection = get(selectedCommits); |
| b69ab31 | | | 42 | if (selection.size < 2) { |
| b69ab31 | | | 43 | return undefined; |
| b69ab31 | | | 44 | } |
| b69ab31 | | | 45 | const dag = get(dagWithPreviews); |
| b69ab31 | | | 46 | const foldable = getFoldableRange(selection, dag); |
| b69ab31 | | | 47 | return foldable; |
| b69ab31 | | | 48 | }); |
| b69ab31 | | | 49 | |
| b69ab31 | | | 50 | /** |
| b69ab31 | | | 51 | * Given a set of selected commits, order them into an array from bottom to top. |
| b69ab31 | | | 52 | * If commits are not contiguous, returns undefined. |
| b69ab31 | | | 53 | * This selection must be linear and contiguous: no branches out are allowed. |
| b69ab31 | | | 54 | * This constitutes a set of commits that may be "folded"/combined into a single commit via `sl fold`. |
| b69ab31 | | | 55 | */ |
| b69ab31 | | | 56 | export function getFoldableRange(selection: Set<Hash>, dag: Dag): Array<CommitInfo> | undefined { |
| b69ab31 | | | 57 | const set = dag.present(selection); |
| b69ab31 | | | 58 | if (set.size <= 1) { |
| b69ab31 | | | 59 | return undefined; |
| b69ab31 | | | 60 | } |
| b69ab31 | | | 61 | const head = dag.heads(set); |
| b69ab31 | | | 62 | if ( |
| b69ab31 | | | 63 | head.size !== 1 || |
| b69ab31 | | | 64 | dag.roots(set).size !== 1 || |
| b69ab31 | | | 65 | dag.merge(set).size > 0 || |
| b69ab31 | | | 66 | dag.public_(set).size > 0 || |
| b69ab31 | | | 67 | // only head can have other children |
| b69ab31 | | | 68 | dag.children(set.subtract(head)).subtract(set).size > 0 |
| b69ab31 | | | 69 | ) { |
| b69ab31 | | | 70 | return undefined; |
| b69ab31 | | | 71 | } |
| b69ab31 | | | 72 | return dag.getBatch(dag.sortAsc(set, {gap: false})); |
| b69ab31 | | | 73 | } |
| b69ab31 | | | 74 | |
| b69ab31 | | | 75 | export function FoldButton({commit}: {commit?: CommitInfo}) { |
| b69ab31 | | | 76 | const foldable = useAtomValue(foldableSelection); |
| b69ab31 | | | 77 | const onClick = useCallback(() => { |
| b69ab31 | | | 78 | if (foldable == null) { |
| b69ab31 | | | 79 | return; |
| b69ab31 | | | 80 | } |
| b69ab31 | | | 81 | const schema = readAtom(commitMessageFieldsSchema); |
| b69ab31 | | | 82 | if (schema == null) { |
| b69ab31 | | | 83 | return; |
| b69ab31 | | | 84 | } |
| b69ab31 | | | 85 | const messageFields = mergeManyCommitMessageFields( |
| b69ab31 | | | 86 | schema, |
| b69ab31 | | | 87 | foldable.map(commit => parseCommitMessageFields(schema, commit.title, commit.description)), |
| b69ab31 | | | 88 | ); |
| b69ab31 | | | 89 | const message = commitMessageFieldsToString(schema, messageFields); |
| b69ab31 | | | 90 | writeAtom(operationBeingPreviewed, new FoldOperation(foldable, message)); |
| b69ab31 | | | 91 | writeAtom(selectedCommits, new Set([getFoldRangeCommitHash(foldable, /* isPreview */ true)])); |
| b69ab31 | | | 92 | }, [foldable]); |
| b69ab31 | | | 93 | if (foldable == null || (commit != null && foldable?.[0]?.hash !== commit.hash)) { |
| b69ab31 | | | 94 | return null; |
| b69ab31 | | | 95 | } |
| b69ab31 | | | 96 | return ( |
| b69ab31 | | | 97 | <Tooltip title={t('Combine selected commits into one commit')}> |
| b69ab31 | | | 98 | <Button onClick={onClick}> |
| b69ab31 | | | 99 | <Icon icon="fold" slot="start" /> |
| b69ab31 | | | 100 | <T replace={{$count: foldable.length}}>Combine $count commits</T> |
| b69ab31 | | | 101 | </Button> |
| b69ab31 | | | 102 | </Tooltip> |
| b69ab31 | | | 103 | ); |
| b69ab31 | | | 104 | } |
| b69ab31 | | | 105 | |
| b69ab31 | | | 106 | /** |
| b69ab31 | | | 107 | * Make a new copy of the FoldOperation with the latest edited message for the combined preview. |
| b69ab31 | | | 108 | * This allows running the fold operation to use the newly typed message. |
| b69ab31 | | | 109 | */ |
| b69ab31 | | | 110 | export function updateFoldedMessageWithEditedMessage(): FoldOperation | undefined { |
| b69ab31 | | | 111 | const beingPreviewed = readAtom(operationBeingPreviewed); |
| b69ab31 | | | 112 | if (beingPreviewed != null && beingPreviewed instanceof FoldOperation) { |
| b69ab31 | | | 113 | const range = beingPreviewed.getFoldRange(); |
| b69ab31 | | | 114 | const combinedHash = getFoldRangeCommitHash(range, /* isPreview */ true); |
| b69ab31 | | | 115 | const [existingTitle, existingMessage] = beingPreviewed.getFoldedMessage(); |
| b69ab31 | | | 116 | const editedMessage = readAtom(editedCommitMessages(combinedHash)); |
| b69ab31 | | | 117 | |
| b69ab31 | | | 118 | const schema = readAtom(commitMessageFieldsSchema); |
| b69ab31 | | | 119 | if (schema == null) { |
| b69ab31 | | | 120 | return undefined; |
| b69ab31 | | | 121 | } |
| b69ab31 | | | 122 | |
| b69ab31 | | | 123 | const old = parseCommitMessageFields(schema, existingTitle, existingMessage); |
| b69ab31 | | | 124 | const message = editedMessage == null ? old : applyEditedFields(old, editedMessage); |
| b69ab31 | | | 125 | |
| b69ab31 | | | 126 | const newMessage = commitMessageFieldsToString(schema, message); |
| b69ab31 | | | 127 | |
| b69ab31 | | | 128 | return new FoldOperation(range, newMessage); |
| b69ab31 | | | 129 | } |
| b69ab31 | | | 130 | } |
| b69ab31 | | | 131 | |
| b69ab31 | | | 132 | export function useRunFoldPreview(): [cancel: () => unknown, run: () => unknown] { |
| b69ab31 | | | 133 | const handlePreviewedOperation = useRunPreviewedOperation(); |
| b69ab31 | | | 134 | const run = useCallback(() => { |
| b69ab31 | | | 135 | const foldOperation = updateFoldedMessageWithEditedMessage(); |
| b69ab31 | | | 136 | if (foldOperation == null) { |
| b69ab31 | | | 137 | return; |
| b69ab31 | | | 138 | } |
| b69ab31 | | | 139 | handlePreviewedOperation(/* isCancel */ false, foldOperation); |
| b69ab31 | | | 140 | // select the optimistic commit instead of the preview commit |
| b69ab31 | | | 141 | writeAtom(selectedCommits, last => |
| b69ab31 | | | 142 | last.size === 1 && firstOfIterable(last.values())?.startsWith(FOLD_COMMIT_PREVIEW_HASH_PREFIX) |
| b69ab31 | | | 143 | ? new Set([getFoldRangeCommitHash(foldOperation.getFoldRange(), /* isPreview */ false)]) |
| b69ab31 | | | 144 | : last, |
| b69ab31 | | | 145 | ); |
| b69ab31 | | | 146 | }, [handlePreviewedOperation]); |
| b69ab31 | | | 147 | return [ |
| b69ab31 | | | 148 | () => { |
| b69ab31 | | | 149 | handlePreviewedOperation(/* isCancel */ true); |
| b69ab31 | | | 150 | }, |
| b69ab31 | | | 151 | run, |
| b69ab31 | | | 152 | ]; |
| b69ab31 | | | 153 | } |