| 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 { |
| b69ab31 | | | 9 | ApplyUncommittedChangesPreviewsFuncType, |
| b69ab31 | | | 10 | UncommittedChangesPreviewContext, |
| b69ab31 | | | 11 | } from '../previews'; |
| b69ab31 | | | 12 | import type {RepoRelativePath, UncommittedChanges} from '../types'; |
| b69ab31 | | | 13 | |
| b69ab31 | | | 14 | import {Operation} from './Operation'; |
| b69ab31 | | | 15 | |
| b69ab31 | | | 16 | export class AddOperation extends Operation { |
| b69ab31 | | | 17 | constructor(private filePath: RepoRelativePath) { |
| b69ab31 | | | 18 | super('AddOperation'); |
| b69ab31 | | | 19 | } |
| b69ab31 | | | 20 | |
| b69ab31 | | | 21 | static opName = 'Add'; |
| b69ab31 | | | 22 | |
| b69ab31 | | | 23 | getArgs() { |
| b69ab31 | | | 24 | return [ |
| b69ab31 | | | 25 | 'add', |
| b69ab31 | | | 26 | { |
| b69ab31 | | | 27 | type: 'repo-relative-file' as const, |
| b69ab31 | | | 28 | path: this.filePath, |
| b69ab31 | | | 29 | }, |
| b69ab31 | | | 30 | ]; |
| b69ab31 | | | 31 | } |
| b69ab31 | | | 32 | |
| b69ab31 | | | 33 | makeOptimisticUncommittedChangesApplier?( |
| b69ab31 | | | 34 | context: UncommittedChangesPreviewContext, |
| b69ab31 | | | 35 | ): ApplyUncommittedChangesPreviewsFuncType | undefined { |
| b69ab31 | | | 36 | if ( |
| b69ab31 | | | 37 | context.uncommittedChanges.some( |
| b69ab31 | | | 38 | change => change.path === this.filePath && change.status !== '?', |
| b69ab31 | | | 39 | ) |
| b69ab31 | | | 40 | ) { |
| b69ab31 | | | 41 | return undefined; |
| b69ab31 | | | 42 | } |
| b69ab31 | | | 43 | |
| b69ab31 | | | 44 | const func: ApplyUncommittedChangesPreviewsFuncType = (changes: UncommittedChanges) => { |
| b69ab31 | | | 45 | return changes.map(change => |
| b69ab31 | | | 46 | change.path === this.filePath ? {path: change.path, status: 'A'} : change, |
| b69ab31 | | | 47 | ); |
| b69ab31 | | | 48 | }; |
| b69ab31 | | | 49 | return func; |
| b69ab31 | | | 50 | } |
| b69ab31 | | | 51 | } |