1- import { computed , Component , effect , inject , OnDestroy , OnInit , signal } from '@angular/core' ;
1+ import { computed , Component , inject , OnDestroy , OnInit , signal } from '@angular/core' ;
22import { ActivatedRoute , Router } from '@angular/router' ;
33import { UserService } from '../../../settings/user-management/user.service' ;
44import { Book , BookRecommendation } from '../../../book/model/book.model' ;
@@ -15,7 +15,8 @@ import {MetadataViewerComponent} from './metadata-viewer/metadata-viewer.compone
1515import { MetadataEditorComponent } from './metadata-editor/metadata-editor.component' ;
1616import { MetadataSearcherComponent } from './metadata-searcher/metadata-searcher.component' ;
1717import { SidecarViewerComponent } from './sidecar-viewer/sidecar-viewer.component' ;
18- import { injectQuery } from '@tanstack/angular-query-experimental' ;
18+ import { injectQuery , queryOptions } from '@tanstack/angular-query-experimental' ;
19+ import { bookRecommendationsQueryKey } from '../../../book/service/book-query-keys' ;
1920
2021@Component ( {
2122 selector : 'app-book-metadata-center' ,
@@ -64,33 +65,41 @@ export class BookMetadataCenterComponent implements OnInit, OnDestroy {
6465 return this . bookService . bookDetailQueryOptions ( bookId , true ) ;
6566 } ) ;
6667 readonly book = computed ( ( ) => this . bookQuery . data ( ) ?? null ) ;
67- private readonly fetchRecommendations = effect ( ( ) => {
68+ private readonly recommendationsQuery = injectQuery ( ( ) => {
6869 const bookId = this . currentBookId ( ) ;
69- if ( bookId == null ) {
70- this . recommendedBooks = [ ] ;
71- return ;
70+ const settings = this . appSettingsService . appSettings ( ) ;
71+
72+ if ( bookId == null || ! ( settings ?. similarBookRecommendation ?? false ) ) {
73+ return queryOptions ( {
74+ queryKey : bookRecommendationsQueryKey ( - 1 , 20 ) ,
75+ queryFn : async ( ) : Promise < BookRecommendation [ ] > => [ ] ,
76+ enabled : false ,
77+ } ) ;
7278 }
7379
74- this . fetchBookRecommendationsIfNeeded ( bookId ) ;
80+ return this . bookService . bookRecommendationsQueryOptions ( bookId , 20 ) ;
7581 } ) ;
76-
77- recommendedBooks : BookRecommendation [ ] = [ ] ;
82+ readonly recommendedBooks = computed ( ( ) =>
83+ [ ...( this . recommendationsQuery . data ( ) ?? [ ] ) ] . sort (
84+ ( a , b ) => ( b . similarityScore ?? 0 ) - ( a . similarityScore ?? 0 )
85+ )
86+ ) ;
7887 private _tab : string = 'view' ;
79- canEditMetadata : boolean = false ;
80- admin : boolean = false ;
81- private readonly syncUserPermissionsEffect = effect ( ( ) => {
88+ readonly canEditMetadata = computed ( ( ) => {
89+ const user = this . userService . currentUser ( ) ;
90+ return user ?. permissions ?. canEditMetadata ?? false ;
91+ } ) ;
92+ readonly admin = computed ( ( ) => {
8293 const user = this . userService . currentUser ( ) ;
83- if ( ! user ) return ;
84- this . canEditMetadata = user . permissions ?. canEditMetadata ?? false ;
85- this . admin = user . permissions ?. admin ?? false ;
94+ return user ?. permissions ?. admin ?? false ;
8695 } ) ;
8796 get isPhysical ( ) : boolean { return this . book ( ) ?. isPhysical ?? false ; }
88- isLocalStorage : boolean = true ;
97+ readonly isLocalStorage = computed ( ( ) => this . appSettingsService . appSettings ( ) ?. diskType === 'LOCAL' ) ;
8998 get canShowSidecarTab ( ) : boolean {
9099 const settings = this . appSettingsService . appSettings ( ) ;
91100 const sidecarEnabled = settings ?. metadataPersistenceSettings ?. sidecarSettings ?. enabled ?? false ;
92101
93- return ( this . admin || this . canEditMetadata ) && ! this . isPhysical && this . isLocalStorage && sidecarEnabled ;
102+ return ( this . admin ( ) || this . canEditMetadata ( ) ) && ! this . isPhysical && this . isLocalStorage ( ) && sidecarEnabled ;
94103 }
95104 private validTabs = [ 'view' , 'edit' , 'match' , 'sidecar' ] ;
96105
@@ -142,24 +151,6 @@ export class BookMetadataCenterComponent implements OnInit, OnDestroy {
142151 this . _tab = this . validTabs . includes ( tabParam ) ? tabParam : 'view' ;
143152 } ) ;
144153
145- const currentSettings = this . appSettingsService . appSettings ( ) ;
146- if ( currentSettings ) {
147- this . isLocalStorage = currentSettings . diskType === 'LOCAL' ;
148- }
149- }
150-
151- private fetchBookRecommendationsIfNeeded ( bookId : number ) : void {
152- const settings = this . appSettingsService . appSettings ( ) ;
153- if ( ! settings || ! ( settings . similarBookRecommendation ?? false ) ) {
154- return ;
155- }
156- this . bookService . getBookRecommendations ( bookId )
157- . pipe ( takeUntil ( this . destroy$ ) )
158- . subscribe ( recommendations => {
159- this . recommendedBooks = recommendations . sort (
160- ( a , b ) => ( b . similarityScore ?? 0 ) - ( a . similarityScore ?? 0 )
161- ) ;
162- } ) ;
163154 }
164155
165156 ngOnDestroy ( ) : void {
0 commit comments