addons/isl/src/operations/AddOperation.tsblame
View source
b69ab311/**
b69ab312 * Copyright (c) Meta Platforms, Inc. and affiliates.
b69ab313 *
b69ab314 * This source code is licensed under the MIT license found in the
b69ab315 * LICENSE file in the root directory of this source tree.
b69ab316 */
b69ab317
b69ab318import type {
b69ab319 ApplyUncommittedChangesPreviewsFuncType,
b69ab3110 UncommittedChangesPreviewContext,
b69ab3111} from '../previews';
b69ab3112import type {RepoRelativePath, UncommittedChanges} from '../types';
b69ab3113
b69ab3114import {Operation} from './Operation';
b69ab3115
b69ab3116export class AddOperation extends Operation {
b69ab3117 constructor(private filePath: RepoRelativePath) {
b69ab3118 super('AddOperation');
b69ab3119 }
b69ab3120
b69ab3121 static opName = 'Add';
b69ab3122
b69ab3123 getArgs() {
b69ab3124 return [
b69ab3125 'add',
b69ab3126 {
b69ab3127 type: 'repo-relative-file' as const,
b69ab3128 path: this.filePath,
b69ab3129 },
b69ab3130 ];
b69ab3131 }
b69ab3132
b69ab3133 makeOptimisticUncommittedChangesApplier?(
b69ab3134 context: UncommittedChangesPreviewContext,
b69ab3135 ): ApplyUncommittedChangesPreviewsFuncType | undefined {
b69ab3136 if (
b69ab3137 context.uncommittedChanges.some(
b69ab3138 change => change.path === this.filePath && change.status !== '?',
b69ab3139 )
b69ab3140 ) {
b69ab3141 return undefined;
b69ab3142 }
b69ab3143
b69ab3144 const func: ApplyUncommittedChangesPreviewsFuncType = (changes: UncommittedChanges) => {
b69ab3145 return changes.map(change =>
b69ab3146 change.path === this.filePath ? {path: change.path, status: 'A'} : change,
b69ab3147 );
b69ab3148 };
b69ab3149 return func;
b69ab3150 }
b69ab3151}