1.3 KB52 lines
Blame
1/**
2 * Copyright (c) Meta Platforms, Inc. and affiliates.
3 *
4 * This source code is licensed under the MIT license found in the
5 * LICENSE file in the root directory of this source tree.
6 */
7
8import type {
9 ApplyUncommittedChangesPreviewsFuncType,
10 UncommittedChangesPreviewContext,
11} from '../previews';
12import type {RepoRelativePath, UncommittedChanges} from '../types';
13
14import {Operation} from './Operation';
15
16export class AddOperation extends Operation {
17 constructor(private filePath: RepoRelativePath) {
18 super('AddOperation');
19 }
20
21 static opName = 'Add';
22
23 getArgs() {
24 return [
25 'add',
26 {
27 type: 'repo-relative-file' as const,
28 path: this.filePath,
29 },
30 ];
31 }
32
33 makeOptimisticUncommittedChangesApplier?(
34 context: UncommittedChangesPreviewContext,
35 ): ApplyUncommittedChangesPreviewsFuncType | undefined {
36 if (
37 context.uncommittedChanges.some(
38 change => change.path === this.filePath && change.status !== '?',
39 )
40 ) {
41 return undefined;
42 }
43
44 const func: ApplyUncommittedChangesPreviewsFuncType = (changes: UncommittedChanges) => {
45 return changes.map(change =>
46 change.path === this.filePath ? {path: change.path, status: 'A'} : change,
47 );
48 };
49 return func;
50 }
51}
52