Skip to content

Commit

Permalink
Fix file selection clearing on file changes
Browse files Browse the repository at this point in the history
  • Loading branch information
mtsgrd committed Nov 5, 2024
1 parent a5e9974 commit 8726ffd
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 18 deletions.
3 changes: 1 addition & 2 deletions apps/desktop/src/lib/utils/selectFilesInList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,7 @@ export function selectFilesInList(
if (selectedFileIds.length === 1 && isAlreadySelected) {
fileIdSelection.clear();
} else {
fileIdSelection.clear();
fileIdSelection.add(file, commit?.id);
fileIdSelection.set(file, commit?.id);
}
}
}
58 changes: 42 additions & 16 deletions apps/desktop/src/lib/vbranches/fileIdSelection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,16 +95,26 @@ export class FileIdSelection implements Readable<string[]> {
/**
* Called when subscriber count goes from 1 -> 0.
*/
async setup() {
this.unsubscribeLocalFiles = this.uncommittedFiles.subscribe(() => {
this.clear();
private async setup() {
this.unsubscribeLocalFiles = this.uncommittedFiles.subscribe((localFiles) => {
if (this.currentCommitId !== undefined) {
// Selections from a commit are unaffected by uncommitted files.
return;
}
const localFilenames = localFiles.map((f) => f.path);
const removedFiles = this.selection.filter(
(s) => !localFilenames.includes(unstringifyFileKey(s))
);
if (removedFiles.length > 0) {
this.removeMany(removedFiles);
}
});
}

/**
* Called when subscriber count goes from 0 -> 1.
*/
teardown() {
private teardown() {
this.unsubscribeLocalFiles?.();
this.clear();
}
Expand Down Expand Up @@ -132,11 +142,11 @@ export class FileIdSelection implements Readable<string[]> {
}
}

add(file: AnyFile, commitId?: string) {
async add(file: AnyFile, commitId?: string) {
this.selectMany([file], commitId);
}

selectMany(files: AnyFile[], commitId?: string) {
async selectMany(files: AnyFile[], commitId?: string) {
if (this.selection.length > 0 && commitId !== this.currentCommitId) {
throw 'Selecting files from multiple commits not allowed.';
}
Expand All @@ -149,39 +159,55 @@ export class FileIdSelection implements Readable<string[]> {
}
}
}
await this.reloadFiles();
this.emit();
this.reloadFiles();
}

async set(file: AnyFile, commitId?: string) {
this.clearInternal();
this.add(file, commitId);
}

has(fileId: string, commitId?: string) {
return this.selection.includes(stringifyKey(fileId, commitId));
}

remove(fileId: string, commitId?: string) {
const strFileKey = stringifyKey(fileId, commitId);
this.selection = this.selection.filter((key) => key !== strFileKey);
if (commitId) {
this.remoteFiles.delete(strFileKey);
async remove(fileId: string, commitId?: string) {
await this.removeMany([stringifyKey(fileId, commitId)]);
}

async removeMany(keys: string[]) {
this.selection = this.selection.filter((key) => !keys.includes(key));
for (const key of keys) {
const parsedKey = parseFileKey(key);
if (parsedKey.commitId) {
this.remoteFiles.delete(stringifyKey(parsedKey.fileId, parsedKey.commitId));
}
}
if (this.selection.length === 0) {
this.clear();
} else {
this.reloadFiles();
await this.reloadFiles();
this.emit();
}
}

clear() {
this.clearInternal();
this.emit();
}

// Used internally for to bypass emitting new values.
private clearInternal() {
this.selection = [];
this.remoteFiles.clear();
this.currentCommitId = undefined;
this.selectedFile.set(undefined);
this.emit();
}

clearExcept(fileId: string, commitId?: string) {
async clearExcept(fileId: string, commitId?: string) {
this.selection = [stringifyKey(fileId, commitId)];
this.reloadFiles();
await this.reloadFiles();
this.emit();
}

Expand Down

0 comments on commit 8726ffd

Please sign in to comment.