| 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 {atom} from 'jotai'; |
| b69ab31 | | | 9 | import {minimalDisambiguousPaths} from 'shared/minimalDisambiguousPaths'; |
| b69ab31 | | | 10 | import {tracker} from './analytics'; |
| b69ab31 | | | 11 | import {File} from './ChangedFile'; |
| b69ab31 | | | 12 | import {Column, Row} from './ComponentUtils'; |
| b69ab31 | | | 13 | import {T, t} from './i18n'; |
| b69ab31 | | | 14 | import {Internal} from './Internal'; |
| b69ab31 | | | 15 | import {readAtom, writeAtom} from './jotaiUtils'; |
| b69ab31 | | | 16 | import type {PartialSelection} from './partialSelection'; |
| b69ab31 | | | 17 | import platform from './platform'; |
| b69ab31 | | | 18 | import {repoRootAtom} from './repositoryData'; |
| b69ab31 | | | 19 | import type {AbsolutePath, RepoRelativePath} from './types'; |
| b69ab31 | | | 20 | import {ChangedFileMode} from './types'; |
| b69ab31 | | | 21 | import {showModal} from './useModal'; |
| b69ab31 | | | 22 | import {registerDisposable} from './utils'; |
| b69ab31 | | | 23 | |
| b69ab31 | | | 24 | import './UncommittedChanges.css'; |
| b69ab31 | | | 25 | |
| b69ab31 | | | 26 | /** All known suggested edits, if applicable. |
| b69ab31 | | | 27 | * n.b. we get absolute paths from the suggested edits API */ |
| b69ab31 | | | 28 | const allSuggestedEdits = atom<Array<AbsolutePath>>([]); |
| b69ab31 | | | 29 | registerDisposable( |
| b69ab31 | | | 30 | allSuggestedEdits, |
| b69ab31 | | | 31 | platform.suggestedEdits?.onDidChangeSuggestedEdits(edits => { |
| b69ab31 | | | 32 | writeAtom(allSuggestedEdits, edits); |
| b69ab31 | | | 33 | }) ?? {dispose: () => {}}, |
| b69ab31 | | | 34 | import.meta.hot, |
| b69ab31 | | | 35 | ); |
| b69ab31 | | | 36 | |
| b69ab31 | | | 37 | /** Filter all known suggested edits to relevant repo-relative paths */ |
| b69ab31 | | | 38 | const currentSuggestedEdits = atom<Array<RepoRelativePath>>(get => { |
| b69ab31 | | | 39 | const allEdits = get(allSuggestedEdits); |
| b69ab31 | | | 40 | const repoRoot = get(repoRootAtom); |
| b69ab31 | | | 41 | return allEdits |
| b69ab31 | | | 42 | .filter(path => path.startsWith(repoRoot)) |
| b69ab31 | | | 43 | .map(path => path.slice(repoRoot.length + 1)); |
| b69ab31 | | | 44 | }); |
| b69ab31 | | | 45 | |
| b69ab31 | | | 46 | /** |
| b69ab31 | | | 47 | * If there are pending suggested edits as determined by the platform (typically suggestions from an AI), |
| b69ab31 | | | 48 | * and they intersect with the given files, |
| b69ab31 | | | 49 | * show a modal to confirm how to resolve those edits before proceeding. |
| b69ab31 | | | 50 | * Different operations may have a different behavior for resolving. |
| b69ab31 | | | 51 | * For example, `commit` should accept the edits before continuing, |
| b69ab31 | | | 52 | * but `revert` should reject the edits before continuing. |
| b69ab31 | | | 53 | * |
| b69ab31 | | | 54 | * We intentionally don't expose all possible ways of resolving edits for simplicity as a user. |
| b69ab31 | | | 55 | * We don't give any option to leave edits pending, because that should almost never be what you want. |
| b69ab31 | | | 56 | * |
| b69ab31 | | | 57 | * `source` is used for analytics purposes. |
| b69ab31 | | | 58 | */ |
| b69ab31 | | | 59 | export async function confirmSuggestedEditsForFiles( |
| b69ab31 | | | 60 | source: string, |
| b69ab31 | | | 61 | action: 'accept' | 'reject', |
| b69ab31 | | | 62 | files: PartialSelection | Array<RepoRelativePath>, |
| b69ab31 | | | 63 | ): Promise<boolean> { |
| b69ab31 | | | 64 | const suggestedEdits = readAtom(currentSuggestedEdits); |
| b69ab31 | | | 65 | if (suggestedEdits == null || suggestedEdits.length === 0) { |
| b69ab31 | | | 66 | return true; // nothing to warn about |
| b69ab31 | | | 67 | } |
| b69ab31 | | | 68 | |
| b69ab31 | | | 69 | const toWarnAbout = |
| b69ab31 | | | 70 | files == null |
| b69ab31 | | | 71 | ? suggestedEdits |
| b69ab31 | | | 72 | : Array.isArray(files) |
| b69ab31 | | | 73 | ? suggestedEdits.filter(filepath => files.includes(filepath)) |
| b69ab31 | | | 74 | : suggestedEdits.filter(filepath => files.isFullyOrPartiallySelected(filepath)); |
| b69ab31 | | | 75 | if (toWarnAbout.length === 0) { |
| b69ab31 | | | 76 | return true; // nothing to warn about |
| b69ab31 | | | 77 | } |
| b69ab31 | | | 78 | |
| b69ab31 | | | 79 | const buttons = [ |
| b69ab31 | | | 80 | t('Cancel'), |
| b69ab31 | | | 81 | action === 'accept' |
| b69ab31 | | | 82 | ? {label: t('Accept Edits and Continue'), primary: true} |
| b69ab31 | | | 83 | : {label: t('Discard Edits and Continue'), primary: true}, |
| b69ab31 | | | 84 | ]; |
| b69ab31 | | | 85 | const answer = await showModal({ |
| b69ab31 | | | 86 | type: 'confirm', |
| b69ab31 | | | 87 | buttons, |
| b69ab31 | | | 88 | title: Internal.PendingSuggestedEditsMessage ?? <T>You have pending suggested edits</T>, |
| b69ab31 | | | 89 | message: ( |
| b69ab31 | | | 90 | <Column alignStart> |
| b69ab31 | | | 91 | <Column alignStart> |
| b69ab31 | | | 92 | <SimpleChangedFilesList files={toWarnAbout} /> |
| b69ab31 | | | 93 | </Column> |
| b69ab31 | | | 94 | <Row> |
| b69ab31 | | | 95 | {action === 'accept' ? ( |
| b69ab31 | | | 96 | <T>Do you want to accept these suggested edits and continue?</T> |
| b69ab31 | | | 97 | ) : ( |
| b69ab31 | | | 98 | <T>Do you want to discard these suggested edits and continue?</T> |
| b69ab31 | | | 99 | )} |
| b69ab31 | | | 100 | </Row> |
| b69ab31 | | | 101 | </Column> |
| b69ab31 | | | 102 | ), |
| b69ab31 | | | 103 | }); |
| b69ab31 | | | 104 | tracker.track('WarnAboutSuggestedEdits', { |
| b69ab31 | | | 105 | extras: { |
| b69ab31 | | | 106 | source, |
| b69ab31 | | | 107 | answer: typeof answer === 'string' ? answer : answer?.label, |
| b69ab31 | | | 108 | }, |
| b69ab31 | | | 109 | }); |
| b69ab31 | | | 110 | |
| b69ab31 | | | 111 | switch (answer) { |
| b69ab31 | | | 112 | default: |
| b69ab31 | | | 113 | case buttons[0]: |
| b69ab31 | | | 114 | return false; |
| b69ab31 | | | 115 | case buttons[1]: { |
| b69ab31 | | | 116 | const fullEdits = readAtom(allSuggestedEdits); |
| b69ab31 | | | 117 | const absolutePaths = fullEdits.filter(path => |
| b69ab31 | | | 118 | toWarnAbout.find(filepath => path.endsWith(filepath)), |
| b69ab31 | | | 119 | ); |
| b69ab31 | | | 120 | platform.suggestedEdits?.resolveSuggestedEdits(action, absolutePaths); |
| b69ab31 | | | 121 | return true; |
| b69ab31 | | | 122 | } |
| b69ab31 | | | 123 | } |
| b69ab31 | | | 124 | } |
| b69ab31 | | | 125 | |
| b69ab31 | | | 126 | /** Simplified list of changed files, for rendering a list of files when we don't have the full context of the file. |
| b69ab31 | | | 127 | * Just pretend everything is modified and hide extra actions like opening diff views. |
| b69ab31 | | | 128 | */ |
| b69ab31 | | | 129 | function SimpleChangedFilesList({files}: {files: Array<string>}) { |
| b69ab31 | | | 130 | const disambiguated = minimalDisambiguousPaths(files); |
| b69ab31 | | | 131 | return ( |
| b69ab31 | | | 132 | <div className="changed-files-list-container"> |
| b69ab31 | | | 133 | <div className="changed-files-list"> |
| b69ab31 | | | 134 | {files.map((path, i) => ( |
| b69ab31 | | | 135 | <File |
| b69ab31 | | | 136 | file={{ |
| b69ab31 | | | 137 | label: disambiguated[i], |
| b69ab31 | | | 138 | path, |
| b69ab31 | | | 139 | tooltip: path, |
| b69ab31 | | | 140 | // These are wrong, but we don't have the full context of the file to know if it's added, removed, etc |
| b69ab31 | | | 141 | visualStatus: 'M', |
| b69ab31 | | | 142 | status: 'M', |
| b69ab31 | | | 143 | // Similar to the above, we assume it's a regular change |
| b69ab31 | | | 144 | // rather than a submodule update, which is unlikely to be suggested |
| b69ab31 | | | 145 | mode: ChangedFileMode.Regular, |
| b69ab31 | | | 146 | }} |
| b69ab31 | | | 147 | key={path} |
| b69ab31 | | | 148 | displayType="short" |
| b69ab31 | | | 149 | /> |
| b69ab31 | | | 150 | ))} |
| b69ab31 | | | 151 | </div> |
| b69ab31 | | | 152 | </div> |
| b69ab31 | | | 153 | ); |
| b69ab31 | | | 154 | } |