Fix file selection after restoring selections from state

Signed-off-by: trivernis <trivernis@protonmail.com>
pull/15/head
trivernis 3 years ago
parent 69f3af1fa5
commit c6d4750013
Signed by: Trivernis
GPG Key ID: DFFFCC2C7A02DB45

@ -4,6 +4,7 @@ import {FilesTabState} from "../../../models/state/FilesTabState";
import {RepositoryMetadata} from "../../../../api/api-types/repo";
import {RepositoryService} from "../../../services/repository/repository.service";
import {TabCategory} from "../../../models/state/TabCategory";
import {take} from "rxjs";
@Component({
selector: "app-files-tab",
@ -19,6 +20,8 @@ export class FilesTabComponent implements OnInit {
selectedFiles: File[] = [];
public metadata?: RepositoryMetadata;
private preselectedCd?: string;
constructor(
repoService: RepositoryService,
) {
@ -28,6 +31,12 @@ export class FilesTabComponent implements OnInit {
async ngOnInit() {
this.state.files.subscribe(files => this.files = files);
this.state.loading.subscribe(loading => this.contentLoading = loading);
this.state.files.pipe(take(2)).subscribe(async files => {
await this.handlePreselection(this.preselectedCd, files);
});
this.state.selectedCD.pipe(take(2)).subscribe(async (cd) => {
await this.handlePreselection(cd, this.files);
});
}
async onFileSelect(files: File[]) {
@ -37,7 +46,6 @@ export class FilesTabComponent implements OnInit {
} else {
this.state.selectedCD.next(undefined);
}
console.debug(this.selectedFiles);
}
public getStateSelectedFile(): File | undefined {
@ -61,4 +69,19 @@ export class FilesTabComponent implements OnInit {
public onImportFiles(): void {
this.state.category = TabCategory.Import;
}
private async handlePreselection(cd: string | undefined, files: File[]) {
console.log(cd, files);
this.preselectedCd = cd;
if (cd && files.length > 0) {
const file = files.find(f => f.cd === cd);
if (file) {
console.debug("firing select");
this.preselectedCd = undefined;
await this.onFileSelect([file]);
}
}
}
}

@ -52,8 +52,8 @@ export class FilesTabState extends TabState implements SaveState<FilesTabSaveSta
this.filters = new BehaviorSubject(new SearchFilters(state.filters ?? []));
this.sortingPreset = new BehaviorSubject(new SortingPreset(state.sortingPreset));
this.mode = new BehaviorSubject(state.mode ?? "grid");
this.selectedCD = new BehaviorSubject(state.selectedCd);
this.files = new BehaviorSubject((state.files ?? []).map(mapNew(File)));
this.selectedCD = new BehaviorSubject(state.selectedCd);
this.subscribe();
}

Loading…
Cancel
Save