fix(ui): convert book metadata suggestions to signals (#1583)
This commit is contained in:
+26
-35
@@ -1,4 +1,4 @@
|
||||
import {computed, Component, effect, inject, OnDestroy, OnInit, signal} from '@angular/core';
|
||||
import {computed, Component, inject, OnDestroy, OnInit, signal} from '@angular/core';
|
||||
import {ActivatedRoute, Router} from '@angular/router';
|
||||
import {UserService} from '../../../settings/user-management/user.service';
|
||||
import {Book, BookRecommendation} from '../../../book/model/book.model';
|
||||
@@ -15,7 +15,8 @@ import {MetadataViewerComponent} from './metadata-viewer/metadata-viewer.compone
|
||||
import {MetadataEditorComponent} from './metadata-editor/metadata-editor.component';
|
||||
import {MetadataSearcherComponent} from './metadata-searcher/metadata-searcher.component';
|
||||
import {SidecarViewerComponent} from './sidecar-viewer/sidecar-viewer.component';
|
||||
import {injectQuery} from '@tanstack/angular-query-experimental';
|
||||
import {injectQuery, queryOptions} from '@tanstack/angular-query-experimental';
|
||||
import {bookRecommendationsQueryKey} from '../../../book/service/book-query-keys';
|
||||
|
||||
@Component({
|
||||
selector: 'app-book-metadata-center',
|
||||
@@ -64,33 +65,41 @@ export class BookMetadataCenterComponent implements OnInit, OnDestroy {
|
||||
return this.bookService.bookDetailQueryOptions(bookId, true);
|
||||
});
|
||||
readonly book = computed(() => this.bookQuery.data() ?? null);
|
||||
private readonly fetchRecommendations = effect(() => {
|
||||
private readonly recommendationsQuery = injectQuery(() => {
|
||||
const bookId = this.currentBookId();
|
||||
if (bookId == null) {
|
||||
this.recommendedBooks = [];
|
||||
return;
|
||||
const settings = this.appSettingsService.appSettings();
|
||||
|
||||
if (bookId == null || !(settings?.similarBookRecommendation ?? false)) {
|
||||
return queryOptions({
|
||||
queryKey: bookRecommendationsQueryKey(-1, 20),
|
||||
queryFn: async (): Promise<BookRecommendation[]> => [],
|
||||
enabled: false,
|
||||
});
|
||||
}
|
||||
|
||||
this.fetchBookRecommendationsIfNeeded(bookId);
|
||||
return this.bookService.bookRecommendationsQueryOptions(bookId, 20);
|
||||
});
|
||||
|
||||
recommendedBooks: BookRecommendation[] = [];
|
||||
readonly recommendedBooks = computed(() =>
|
||||
[...(this.recommendationsQuery.data() ?? [])].sort(
|
||||
(a, b) => (b.similarityScore ?? 0) - (a.similarityScore ?? 0)
|
||||
)
|
||||
);
|
||||
private _tab: string = 'view';
|
||||
canEditMetadata: boolean = false;
|
||||
admin: boolean = false;
|
||||
private readonly syncUserPermissionsEffect = effect(() => {
|
||||
readonly canEditMetadata = computed(() => {
|
||||
const user = this.userService.currentUser();
|
||||
if (!user) return;
|
||||
this.canEditMetadata = user.permissions?.canEditMetadata ?? false;
|
||||
this.admin = user.permissions?.admin ?? false;
|
||||
return user?.permissions?.canEditMetadata ?? false;
|
||||
});
|
||||
readonly admin = computed(() => {
|
||||
const user = this.userService.currentUser();
|
||||
return user?.permissions?.admin ?? false;
|
||||
});
|
||||
get isPhysical(): boolean { return this.book()?.isPhysical ?? false; }
|
||||
isLocalStorage: boolean = true;
|
||||
readonly isLocalStorage = computed(() => this.appSettingsService.appSettings()?.diskType === 'LOCAL');
|
||||
get canShowSidecarTab(): boolean {
|
||||
const settings = this.appSettingsService.appSettings();
|
||||
const sidecarEnabled = settings?.metadataPersistenceSettings?.sidecarSettings?.enabled ?? false;
|
||||
|
||||
return (this.admin || this.canEditMetadata) && !this.isPhysical && this.isLocalStorage && sidecarEnabled;
|
||||
return (this.admin() || this.canEditMetadata()) && !this.isPhysical && this.isLocalStorage() && sidecarEnabled;
|
||||
}
|
||||
private validTabs = ['view', 'edit', 'match', 'sidecar'];
|
||||
|
||||
@@ -142,24 +151,6 @@ export class BookMetadataCenterComponent implements OnInit, OnDestroy {
|
||||
this._tab = this.validTabs.includes(tabParam) ? tabParam : 'view';
|
||||
});
|
||||
|
||||
const currentSettings = this.appSettingsService.appSettings();
|
||||
if (currentSettings) {
|
||||
this.isLocalStorage = currentSettings.diskType === 'LOCAL';
|
||||
}
|
||||
}
|
||||
|
||||
private fetchBookRecommendationsIfNeeded(bookId: number): void {
|
||||
const settings = this.appSettingsService.appSettings();
|
||||
if (!settings || !(settings.similarBookRecommendation ?? false)) {
|
||||
return;
|
||||
}
|
||||
this.bookService.getBookRecommendations(bookId)
|
||||
.pipe(takeUntil(this.destroy$))
|
||||
.subscribe(recommendations => {
|
||||
this.recommendedBooks = recommendations.sort(
|
||||
(a, b) => (b.similarityScore ?? 0) - (a.similarityScore ?? 0)
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
ngOnDestroy(): void {
|
||||
|
||||
Reference in New Issue
Block a user