From 11e59f758883dbb1afc97136efd36e29aed6c6f4 Mon Sep 17 00:00:00 2001 From: Aditya Chandel <8075870+adityachandelgit@users.noreply.github.com> Date: Sun, 26 Oct 2025 11:51:06 -0600 Subject: [PATCH] Add configurability to dashboard scrollers with support for magic shelves (#1453) --- .../custom/BookLoreUserTransformer.java | 6 +- .../booklore/model/dto/BookLoreUser.java | 25 +++ .../model/dto/settings/UserSettingKey.java | 1 + .../app/features/book/service/sort.service.ts | 1 + .../dashboard-scroller.component.html | 13 +- .../dashboard-scroller.component.scss | 9 + .../dashboard-scroller.component.ts | 8 +- .../dashboard-settings.component.html | 141 +++++++++++++ .../dashboard-settings.component.scss | 166 +++++++++++++++ .../dashboard-settings.component.ts | 179 ++++++++++++++++ .../main-dashboard.component.html | 35 ++-- .../main-dashboard.component.scss | 32 +++ .../main-dashboard.component.ts | 196 ++++++++++++++---- .../models/dashboard-config.model.ts | 32 +++ .../services/dashboard-config.service.ts | 41 ++++ .../component/magic-shelf-component.html | 2 +- .../service/book-rule-evaluator.service.ts | 16 ++ .../settings/user-management/user.service.ts | 8 +- .../services/dialog-launcher.service.ts | 8 +- 19 files changed, 842 insertions(+), 77 deletions(-) create mode 100644 booklore-ui/src/app/features/dashboard/components/dashboard-settings/dashboard-settings.component.html create mode 100644 booklore-ui/src/app/features/dashboard/components/dashboard-settings/dashboard-settings.component.scss create mode 100644 booklore-ui/src/app/features/dashboard/components/dashboard-settings/dashboard-settings.component.ts create mode 100644 booklore-ui/src/app/features/dashboard/models/dashboard-config.model.ts create mode 100644 booklore-ui/src/app/features/dashboard/services/dashboard-config.service.ts diff --git a/booklore-api/src/main/java/com/adityachandel/booklore/mapper/custom/BookLoreUserTransformer.java b/booklore-api/src/main/java/com/adityachandel/booklore/mapper/custom/BookLoreUserTransformer.java index 2f7832bae..9b03d85d8 100644 --- a/booklore-api/src/main/java/com/adityachandel/booklore/mapper/custom/BookLoreUserTransformer.java +++ b/booklore-api/src/main/java/com/adityachandel/booklore/mapper/custom/BookLoreUserTransformer.java @@ -62,10 +62,8 @@ public class BookLoreUserTransformer { case SIDEBAR_LIBRARY_SORTING -> userSettings.setSidebarLibrarySorting(objectMapper.readValue(value, SidebarSortOption.class)); case SIDEBAR_SHELF_SORTING -> userSettings.setSidebarShelfSorting(objectMapper.readValue(value, SidebarSortOption.class)); case ENTITY_VIEW_PREFERENCES -> userSettings.setEntityViewPreferences(objectMapper.readValue(value, BookLoreUser.UserSettings.EntityViewPreferences.class)); - case TABLE_COLUMN_PREFERENCE -> userSettings.setTableColumnPreference( - objectMapper.readValue(value, new TypeReference<>() { - }) - ); + case TABLE_COLUMN_PREFERENCE -> userSettings.setTableColumnPreference(objectMapper.readValue(value, new TypeReference<>() {})); + case DASHBOARD_CONFIG -> userSettings.setDashboardConfig(objectMapper.readValue(value, BookLoreUser.UserSettings.DashboardConfig.class)); } } else { switch (settingKey) { diff --git a/booklore-api/src/main/java/com/adityachandel/booklore/model/dto/BookLoreUser.java b/booklore-api/src/main/java/com/adityachandel/booklore/model/dto/BookLoreUser.java index 74f04b20c..e0d3d114f 100644 --- a/booklore-api/src/main/java/com/adityachandel/booklore/model/dto/BookLoreUser.java +++ b/booklore-api/src/main/java/com/adityachandel/booklore/model/dto/BookLoreUser.java @@ -52,6 +52,7 @@ public class BookLoreUser { public String filterSortingMode; public String metadataCenterViewMode; public boolean koReaderEnabled; + public DashboardConfig dashboardConfig; @Data @Builder @@ -162,5 +163,29 @@ public class BookLoreUser { Global, Individual } } + + @Data + @Builder + @AllArgsConstructor + @NoArgsConstructor + public static class DashboardConfig { + private List scrollers; + } + + @Data + @Builder + @AllArgsConstructor + @NoArgsConstructor + public static class ScrollerConfig { + private String id; + private String type; + private String title; + private boolean enabled; + private int order; + private int maxItems; + private Long magicShelfId; + private String sortField; + private String sortDirection; + } } } diff --git a/booklore-api/src/main/java/com/adityachandel/booklore/model/dto/settings/UserSettingKey.java b/booklore-api/src/main/java/com/adityachandel/booklore/model/dto/settings/UserSettingKey.java index 2735c68d7..0b1faf593 100644 --- a/booklore-api/src/main/java/com/adityachandel/booklore/model/dto/settings/UserSettingKey.java +++ b/booklore-api/src/main/java/com/adityachandel/booklore/model/dto/settings/UserSettingKey.java @@ -13,6 +13,7 @@ public enum UserSettingKey { SIDEBAR_SHELF_SORTING("sidebarShelfSorting", true), ENTITY_VIEW_PREFERENCES("entityViewPreferences", true), TABLE_COLUMN_PREFERENCE("tableColumnPreference", true), + DASHBOARD_CONFIG("dashboardConfig", true), FILTER_SORTING_MODE("filterSortingMode", false), METADATA_CENTER_VIEW_MODE("metadataCenterViewMode", false); diff --git a/booklore-ui/src/app/features/book/service/sort.service.ts b/booklore-ui/src/app/features/book/service/sort.service.ts index 6fe9a7ca9..e2080e768 100644 --- a/booklore-ui/src/app/features/book/service/sort.service.ts +++ b/booklore-ui/src/app/features/book/service/sort.service.ts @@ -24,6 +24,7 @@ export class SortService { publisher: (book) => book.metadata?.publisher || null, pageCount: (book) => book.metadata?.pageCount || null, rating: (book) => book.metadata?.rating || null, + personalRating: (book) => book.metadata?.personalRating || null, reviewCount: (book) => book.metadata?.reviewCount || null, amazonRating: (book) => book.metadata?.amazonRating || null, amazonReviewCount: (book) => book.metadata?.amazonReviewCount || null, diff --git a/booklore-ui/src/app/features/dashboard/components/dashboard-scroller/dashboard-scroller.component.html b/booklore-ui/src/app/features/dashboard/components/dashboard-scroller/dashboard-scroller.component.html index a1cc548c3..1c6c5774a 100644 --- a/booklore-ui/src/app/features/dashboard/components/dashboard-scroller/dashboard-scroller.component.html +++ b/booklore-ui/src/app/features/dashboard/components/dashboard-scroller/dashboard-scroller.component.html @@ -1,14 +1,17 @@
-

{{ title }}

+

+ {{ title }} + @if (isMagicShelf) { + + } +

@if (!books || books.length === 0) {
-

- {{ bookListType === 'lastRead' ? "You haven't read any books yet! Start your reading journey." : "No books have been added to BookLore yet! Add your first book to get started." }} -

+

No books found for this scroller.

} @@ -19,7 +22,7 @@ [infiniteScrollThrottle]="50" #scrollContainer [horizontal]="true"> - @for (book of books; track book) { + @for (book of books; track book.id) {
diff --git a/booklore-ui/src/app/features/dashboard/components/dashboard-scroller/dashboard-scroller.component.scss b/booklore-ui/src/app/features/dashboard/components/dashboard-scroller/dashboard-scroller.component.scss index 0ffa179e1..ee046706c 100644 --- a/booklore-ui/src/app/features/dashboard/components/dashboard-scroller/dashboard-scroller.component.scss +++ b/booklore-ui/src/app/features/dashboard/components/dashboard-scroller/dashboard-scroller.component.scss @@ -146,6 +146,15 @@ } } +.magic-shelf-icon { + margin-left: 0.5rem; + color: var(--primary-color) !important; + background: none !important; + -webkit-background-clip: unset !important; + -webkit-text-fill-color: var(--primary-color) !important; + background-clip: unset !important; +} + @media (max-width: 767px) { .dashboard-scroller-card { diff --git a/booklore-ui/src/app/features/dashboard/components/dashboard-scroller/dashboard-scroller.component.ts b/booklore-ui/src/app/features/dashboard/components/dashboard-scroller/dashboard-scroller.component.ts index 2c59536c7..25c5aae88 100644 --- a/booklore-ui/src/app/features/dashboard/components/dashboard-scroller/dashboard-scroller.component.ts +++ b/booklore-ui/src/app/features/dashboard/components/dashboard-scroller/dashboard-scroller.component.ts @@ -1,9 +1,10 @@ -import {Component, ElementRef, Input, OnChanges, OnInit, ViewChild} from '@angular/core'; +import {Component, ElementRef, Input, ViewChild} from '@angular/core'; import {BookCardComponent} from '../../../book/components/book-browser/book-card/book-card.component'; import {InfiniteScrollDirective} from 'ngx-infinite-scroll'; import {ProgressSpinnerModule} from 'primeng/progressspinner'; import {Book} from '../../../book/model/book.model'; +import {ScrollerType} from '../../models/dashboard-config.model'; @Component({ selector: 'app-dashboard-scroller', @@ -18,9 +19,10 @@ import {Book} from '../../../book/model/book.model'; }) export class DashboardScrollerComponent { - @Input() bookListType: 'lastRead' | null = null; - @Input() title: string = 'Last Read Books'; + @Input() bookListType: ScrollerType | null = null; + @Input() title!: string; @Input() books!: Book[] | null; + @Input() isMagicShelf: boolean = false; @ViewChild('scrollContainer') scrollContainer!: ElementRef; } diff --git a/booklore-ui/src/app/features/dashboard/components/dashboard-settings/dashboard-settings.component.html b/booklore-ui/src/app/features/dashboard/components/dashboard-settings/dashboard-settings.component.html new file mode 100644 index 000000000..f1927464e --- /dev/null +++ b/booklore-ui/src/app/features/dashboard/components/dashboard-settings/dashboard-settings.component.html @@ -0,0 +1,141 @@ +
+
+

+ + Dashboard Configuration +

+

+ Customize your dashboard by adding up to 5 scrollers. Each scroller can display different book collections. + Choose from Continue Reading, Recently Added, Random Discovery, or create custom views using Magic Shelves. + Enable/disable, reorder, and configure the number of items shown in each scroller. +

+
+ +
+ + +
+ + @for (scroller of config.scrollers; track scroller.id; let i = $index) { +
+
+
+ +
+ +
+
+ + + +
+ +
+ + + +
+ +
+ + + +
+ +
+ + + +
+ +
+ + + +
+
+ +
+ + + + + + +
+
+
+ } + + +
diff --git a/booklore-ui/src/app/features/dashboard/components/dashboard-settings/dashboard-settings.component.scss b/booklore-ui/src/app/features/dashboard/components/dashboard-settings/dashboard-settings.component.scss new file mode 100644 index 000000000..144417361 --- /dev/null +++ b/booklore-ui/src/app/features/dashboard/components/dashboard-settings/dashboard-settings.component.scss @@ -0,0 +1,166 @@ +.dashboard-settings { + width: 1000px; + max-width: 1200px; + min-height: 300px; + padding: 2rem 1rem 0 1rem; + margin: 0 auto; + + @media (max-width: 768px) { + width: 100%; + max-width: 100%; + padding: 3rem 1rem 0 1rem; + box-sizing: border-box; + } +} + +.scroller-item { + border: var(--card-border); + transition: all 0.2s; + border-color: var(--p-content-border-color); + background: var(--p-content-background); + padding: 1rem; + margin-bottom: 0.75rem; + border-radius: 0.375rem; + + @media (max-width: 768px) { + padding: 0.75rem !important; + } +} + +.scroller-content { + display: flex; + align-items: flex-end; + gap: 0.75rem; + + @media (max-width: 768px) { + flex-direction: column; + align-items: stretch; + gap: 1rem; + } +} + +.scroller-controls-left { + display: flex; + align-items: center; + padding-bottom: 0.65rem; + + @media (max-width: 768px) { + padding-bottom: 0; + align-self: flex-start; + } +} + +.scroller-controls-right { + display: flex; + gap: 0.5rem; + padding-bottom: 0.25rem; + + @media (max-width: 768px) { + padding-bottom: 0; + align-self: flex-end; + } +} + +.scroller-header { + display: flex; + align-items: center; + justify-content: space-between; + gap: 1rem; + margin-bottom: 1rem; + padding-bottom: 1rem; + border-bottom: 1px solid var(--p-content-border-color); + + @media (max-width: 768px) { + flex-direction: column; + align-items: flex-start; + gap: 0.75rem; + } +} + +.scroller-fields { + display: grid; + grid-template-columns: minmax(120px, 1fr) minmax(150px, 1.5fr) minmax(120px, 1fr) minmax(100px, 1fr) 90px; + gap: 0.75rem; + flex: 1; + + @media (max-width: 768px) { + grid-template-columns: 1fr; + gap: 0.75rem; + } + + @media (max-width: 640px) { + gap: 0.5rem; + } +} + +.scroller-field { + label { + display: block; + margin-bottom: 0.5rem; + } +} + +label { + color: var(--text-color-secondary); + font-size: 0.875rem; + font-weight: 500; +} + +.footer-actions { + display: flex; + justify-content: flex-end; + gap: 0.75rem; + margin-top: 2rem; + padding-top: 1.5rem; + border-top: 1px solid var(--p-content-border-color); + + @media (max-width: 768px) { + flex-wrap: wrap; + justify-content: stretch; + + ::ng-deep button { + flex: 1; + } + } +} + +.settings-info { + border: 1px solid var(--p-primary-300); + margin-bottom: 2rem; + padding: 0.75rem; + border-radius: 0.375rem; + + @media (max-width: 768px) { + padding: 0.75rem !important; + } +} + +.settings-info-title { + color: var(--p-primary-600); + font-size: 1.125rem; + font-weight: 600; + margin-bottom: 0.5rem; + + i { + margin-right: 0.25rem; + } + + @media (max-width: 768px) { + font-size: 1rem; + } +} + +.settings-info-text { + margin: 0; + line-height: 1.5; + font-size: 0.875rem; + color: var(--text-color-secondary); + + @media (max-width: 768px) { + font-size: 0.813rem; + } +} + +.add-scroller-wrapper { + margin-bottom: 1rem; +} diff --git a/booklore-ui/src/app/features/dashboard/components/dashboard-settings/dashboard-settings.component.ts b/booklore-ui/src/app/features/dashboard/components/dashboard-settings/dashboard-settings.component.ts new file mode 100644 index 000000000..481d1cee9 --- /dev/null +++ b/booklore-ui/src/app/features/dashboard/components/dashboard-settings/dashboard-settings.component.ts @@ -0,0 +1,179 @@ +import {Component, inject, OnInit} from '@angular/core'; +import {CommonModule} from '@angular/common'; +import {FormsModule} from '@angular/forms'; +import {DynamicDialogRef} from 'primeng/dynamicdialog'; +import {ButtonModule} from 'primeng/button'; +import {CheckboxModule} from 'primeng/checkbox'; +import {InputTextModule} from 'primeng/inputtext'; +import {SelectModule} from 'primeng/select'; +import {InputNumberModule} from 'primeng/inputnumber'; +import {DashboardConfig, ScrollerConfig, ScrollerType} from '../../models/dashboard-config.model'; +import {DashboardConfigService} from '../../services/dashboard-config.service'; +import {MagicShelfService} from '../../../magic-shelf/service/magic-shelf.service'; +import {map} from 'rxjs/operators'; + +export const MAX_SCROLLERS = 5; +export const DEFAULT_MAX_ITEMS = 20; +export const MIN_ITEMS = 10; +export const MAX_ITEMS = 20; + +@Component({ + selector: 'app-dashboard-settings', + standalone: true, + imports: [ + CommonModule, + FormsModule, + ButtonModule, + CheckboxModule, + InputTextModule, + SelectModule, + InputNumberModule + ], + templateUrl: './dashboard-settings.component.html', + styleUrls: ['./dashboard-settings.component.scss'] +}) +export class DashboardSettingsComponent implements OnInit { + private configService = inject(DashboardConfigService); + private dialogRef = inject(DynamicDialogRef); + private magicShelfService = inject(MagicShelfService); + + config!: DashboardConfig; + + availableScrollerTypes = [ + {label: 'Continue Reading', value: ScrollerType.LAST_READ}, + {label: 'Recently Added', value: ScrollerType.LATEST_ADDED}, + {label: 'Discover Something New', value: ScrollerType.RANDOM}, + {label: 'Magic Shelf', value: ScrollerType.MAGIC_SHELF} + ]; + + magicShelves$ = this.magicShelfService.shelvesState$.pipe( + map(state => (state.shelves || []).map(shelf => ({ + label: shelf.name, + value: shelf.id! + }))) + ); + + sortFieldOptions = [ + {label: 'Title', value: 'title'}, + {label: 'Title + Series', value: 'titleSeries'}, + {label: 'Date Added', value: 'addedOn'}, + {label: 'Author', value: 'author'}, + {label: 'Personal Rating', value: 'personalRating'}, + {label: 'Publisher', value: 'publisher'}, + {label: 'Published Date', value: 'publishedDate'}, + {label: 'Last Read', value: 'lastReadTime'}, + {label: 'Pages', value: 'pageCount'} + ]; + + sortDirectionOptions = [ + {label: 'Ascending', value: 'asc'}, + {label: 'Descending', value: 'desc'} + ]; + + private magicShelvesMap = new Map(); + + readonly MIN_ITEMS = MIN_ITEMS; + readonly MAX_ITEMS = MAX_ITEMS; + + ngOnInit(): void { + this.configService.config$.subscribe(config => { + this.config = JSON.parse(JSON.stringify(config)); + }); + + this.magicShelfService.shelvesState$.subscribe(state => { + this.magicShelvesMap.clear(); + (state.shelves || []).forEach(shelf => { + if (shelf.id) { + this.magicShelvesMap.set(shelf.id, shelf.name); + } + }); + }); + } + + getScrollerTitle(scroller: ScrollerConfig): string { + if (scroller.type === ScrollerType.MAGIC_SHELF && scroller.magicShelfId) { + return this.magicShelvesMap.get(scroller.magicShelfId) || 'Magic Shelf'; + } + + switch (scroller.type) { + case ScrollerType.LAST_READ: + return 'Continue Reading'; + case ScrollerType.LATEST_ADDED: + return 'Recently Added'; + case ScrollerType.RANDOM: + return 'Discover Something New'; + default: + return 'Scroller'; + } + } + + addScroller(): void { + if (this.config.scrollers.length >= MAX_SCROLLERS) { + return; + } + const newId = (Math.max(...this.config.scrollers.map((s: ScrollerConfig) => parseInt(s.id)), 0) + 1).toString(); + this.config.scrollers.push({ + id: newId, + type: ScrollerType.LATEST_ADDED, + title: '', + enabled: true, + order: this.config.scrollers.length + 1, + maxItems: DEFAULT_MAX_ITEMS + }); + } + + removeScroller(index: number): void { + if (this.config.scrollers.length <= 1) { + return; + } + this.config.scrollers.splice(index, 1); + this.updateOrder(); + } + + onScrollerTypeChange(scroller: ScrollerConfig): void { + if (scroller.type === ScrollerType.MAGIC_SHELF) { + scroller.magicShelfId = undefined; + } else { + delete scroller.magicShelfId; + } + } + + moveUp(index: number): void { + if (index > 0) { + [this.config.scrollers[index], this.config.scrollers[index - 1]] = + [this.config.scrollers[index - 1], this.config.scrollers[index]]; + this.updateOrder(); + } + } + + moveDown(index: number): void { + if (index < this.config.scrollers.length - 1) { + [this.config.scrollers[index], this.config.scrollers[index + 1]] = + [this.config.scrollers[index + 1], this.config.scrollers[index]]; + this.updateOrder(); + } + } + + private updateOrder(): void { + this.config.scrollers.forEach((scroller, index) => { + scroller.order = index + 1; + }); + } + + save(): void { + this.config.scrollers.forEach(scroller => { + scroller.title = this.getScrollerTitle(scroller); + }); + this.configService.saveConfig(this.config); + this.dialogRef.close(); + } + + cancel(): void { + this.dialogRef.close(); + } + + resetToDefault(): void { + this.configService.resetToDefault(); + this.dialogRef.close(); + } +} diff --git a/booklore-ui/src/app/features/dashboard/components/main-dashboard/main-dashboard.component.html b/booklore-ui/src/app/features/dashboard/components/main-dashboard/main-dashboard.component.html index 8d91d2313..93e9eea9b 100644 --- a/booklore-ui/src/app/features/dashboard/components/main-dashboard/main-dashboard.component.html +++ b/booklore-ui/src/app/features/dashboard/components/main-dashboard/main-dashboard.component.html @@ -38,21 +38,28 @@ }
} @else { - - +
+ + +
- - - - - + @if (dashboardConfig$ | async; as config) { + @for (scroller of config.scrollers; track scroller.id) { + @if (scroller.enabled) { + + + } + } + } } diff --git a/booklore-ui/src/app/features/dashboard/components/main-dashboard/main-dashboard.component.scss b/booklore-ui/src/app/features/dashboard/components/main-dashboard/main-dashboard.component.scss index 614a05216..0578322d0 100644 --- a/booklore-ui/src/app/features/dashboard/components/main-dashboard/main-dashboard.component.scss +++ b/booklore-ui/src/app/features/dashboard/components/main-dashboard/main-dashboard.component.scss @@ -79,3 +79,35 @@ ::ng-deep .p-dialog-mask { backdrop-filter: blur(3px); } + +.dashboard-settings-button { + position: fixed; + top: 6rem; + right: 2rem; + z-index: 100; + + ::ng-deep .p-button { + box-shadow: 0 4px 12px rgba(0, 0, 0, 0.3); + backdrop-filter: blur(10px); + background: rgba(var(--p-surface-900-rgb), 0.8); + border: 1px solid var(--p-content-border-color); + + &:hover { + background: rgba(var(--p-surface-800-rgb), 0.9); + transform: scale(1.05); + transition: all 0.2s ease; + } + } +} + +@media (max-width: 768px) { + .dashboard-settings-button { + top: 5rem; + right: 1rem; + + ::ng-deep .p-button { + width: 2.5rem; + height: 2.5rem; + } + } +} diff --git a/booklore-ui/src/app/features/dashboard/components/main-dashboard/main-dashboard.component.ts b/booklore-ui/src/app/features/dashboard/components/main-dashboard/main-dashboard.component.ts index c69112e27..51aae0ebf 100644 --- a/booklore-ui/src/app/features/dashboard/components/main-dashboard/main-dashboard.component.ts +++ b/booklore-ui/src/app/features/dashboard/components/main-dashboard/main-dashboard.component.ts @@ -1,9 +1,8 @@ import {Component, inject, OnInit} from '@angular/core'; -import {LibraryCreatorComponent} from '../../../library-creator/library-creator.component'; -import {DialogService, DynamicDialogRef} from 'primeng/dynamicdialog'; +import {DynamicDialogRef} from 'primeng/dynamicdialog'; import {LibraryService} from '../../../book/service/library.service'; import {Observable} from 'rxjs'; -import {map} from 'rxjs/operators'; +import {map, shareReplay, switchMap} from 'rxjs/operators'; import {Button} from 'primeng/button'; import {AsyncPipe} from '@angular/common'; import {DashboardScrollerComponent} from '../dashboard-scroller/dashboard-scroller.component'; @@ -12,6 +11,18 @@ import {BookState} from '../../../book/model/state/book-state.model'; import {Book, ReadStatus} from '../../../book/model/book.model'; import {UserService} from '../../../settings/user-management/user.service'; import {ProgressSpinner} from 'primeng/progressspinner'; +import {TooltipModule} from 'primeng/tooltip'; +import {DashboardConfigService} from '../../services/dashboard-config.service'; +import {ScrollerConfig, ScrollerType} from '../../models/dashboard-config.model'; +import {DashboardSettingsComponent} from '../dashboard-settings/dashboard-settings.component'; +import {MagicShelfService} from '../../../magic-shelf/service/magic-shelf.service'; +import {BookRuleEvaluatorService} from '../../../magic-shelf/service/book-rule-evaluator.service'; +import {GroupRule} from '../../../magic-shelf/component/magic-shelf-component'; +import {DialogLauncherService} from '../../../../shared/services/dialog-launcher.service'; +import {SortService} from '../../../book/service/sort.service'; +import {SortDirection, SortOption} from '../../../book/model/sort.model'; + +const DEFAULT_MAX_ITEMS = 20; @Component({ selector: 'app-main-dashboard', @@ -21,70 +32,171 @@ import {ProgressSpinner} from 'primeng/progressspinner'; Button, DashboardScrollerComponent, AsyncPipe, - ProgressSpinner + ProgressSpinner, + TooltipModule ], - providers: [DialogService], standalone: true }) export class MainDashboardComponent implements OnInit { ref: DynamicDialogRef | undefined | null; private bookService = inject(BookService); - private dialogService = inject(DialogService); + private dialogLauncher = inject(DialogLauncherService); protected userService = inject(UserService); + private dashboardConfigService = inject(DashboardConfigService); + private magicShelfService = inject(MagicShelfService); + private ruleEvaluatorService = inject(BookRuleEvaluatorService); + private sortService = inject(SortService); bookState$ = this.bookService.bookState$; + dashboardConfig$ = this.dashboardConfigService.config$; - lastReadBooks$: Observable | undefined; - latestAddedBooks$: Observable | undefined; - randomBooks$: Observable | undefined; + private scrollerBooksCache = new Map>(); isLibrariesEmpty$: Observable = inject(LibraryService).libraryState$.pipe( map(state => !state.libraries || state.libraries.length === 0) ); + ScrollerType = ScrollerType; + ngOnInit(): void { + this.dashboardConfig$.subscribe(() => { + this.scrollerBooksCache.clear(); + }); + } - this.lastReadBooks$ = this.bookService.bookState$.pipe( - map((state: BookState) => ( - (state.books || []).filter(book => book.lastReadTime && (book.readStatus === ReadStatus.READING || book.readStatus === ReadStatus.RE_READING || book.readStatus === ReadStatus.PAUSED)) - .sort((a, b) => { - const aTime = new Date(a.lastReadTime!).getTime(); - const bTime = new Date(b.lastReadTime!).getTime(); - return bTime - aTime; - }) - .slice(0, 25) - )) - ); + private getLastReadBooks(maxItems: number, sortBy?: string): Observable { + return this.bookService.bookState$.pipe( + map((state: BookState) => { + let books = (state.books || []).filter(book => + book.lastReadTime && + (book.readStatus === ReadStatus.READING || + book.readStatus === ReadStatus.RE_READING || + book.readStatus === ReadStatus.PAUSED) + ); - this.latestAddedBooks$ = this.bookService.bookState$.pipe( - map((state: BookState) => ( - (state.books || []) - .filter(book => book.addedOn) - .sort((a, b) => { - const aTime = new Date(a.addedOn!).getTime(); - const bTime = new Date(b.addedOn!).getTime(); - return bTime - aTime; - }) - .slice(0, 25) - )) - ); + books = books.sort((a, b) => { + const aTime = new Date(a.lastReadTime!).getTime(); + const bTime = new Date(b.lastReadTime!).getTime(); + return bTime - aTime; + }); - this.randomBooks$ = this.bookService.bookState$.pipe( - map((state: BookState) => this.getRandomBooks(state.books || [], 15)) + return books.slice(0, maxItems); + }) ); } - private getRandomBooks(books: Book[], count: number): Book[] { - const shuffled = books.sort(() => 0.5 - Math.random()); - return shuffled.slice(0, count); + private getLatestAddedBooks(maxItems: number, sortBy?: string): Observable { + return this.bookService.bookState$.pipe( + map((state: BookState) => { + let books = (state.books || []).filter(book => book.addedOn); + + books = books.sort((a, b) => { + const aTime = new Date(a.addedOn!).getTime(); + const bTime = new Date(b.addedOn!).getTime(); + return bTime - aTime; + }); + + return books.slice(0, maxItems); + }) + ); + } + + private getRandomBooks(maxItems: number, sortBy?: string): Observable { + return this.bookService.bookState$.pipe( + map((state: BookState) => { + return this.shuffleBooks(state.books || [], maxItems); + }) + ); + } + + private getMagicShelfBooks(shelfId: number, maxItems?: number, sortBy?: string): Observable { + return this.magicShelfService.getShelf(shelfId).pipe( + switchMap((shelf) => { + if (!shelf) return this.bookService.bookState$.pipe(map(() => [])); + + let group: GroupRule; + try { + group = JSON.parse(shelf.filterJson); + } catch (e) { + console.error('Invalid filter JSON', e); + return this.bookService.bookState$.pipe(map(() => [])); + } + + return this.bookService.bookState$.pipe( + map((state: BookState) => { + let filteredBooks = (state.books || []).filter((book) => + this.ruleEvaluatorService.evaluateGroup(book, group) + ); + + return maxItems ? filteredBooks.slice(0, maxItems) : filteredBooks; + }) + ); + }) + ); + } + + getBooksForScroller(config: ScrollerConfig): Observable { + if (!this.scrollerBooksCache.has(config.id)) { + let books$: Observable; + + switch (config.type) { + case ScrollerType.LAST_READ: + books$ = this.getLastReadBooks(config.maxItems || DEFAULT_MAX_ITEMS); + break; + case ScrollerType.LATEST_ADDED: + books$ = this.getLatestAddedBooks(config.maxItems || DEFAULT_MAX_ITEMS); + break; + case ScrollerType.RANDOM: + books$ = this.getRandomBooks(config.maxItems || DEFAULT_MAX_ITEMS); + break; + case ScrollerType.MAGIC_SHELF: + books$ = this.getMagicShelfBooks(config.magicShelfId!, config.maxItems).pipe( + map(books => { + if (config.sortField && config.sortDirection) { + const sortOption = this.createSortOption(config.sortField, config.sortDirection); + return this.sortService.applySort(books, sortOption); + } + return books; + }) + ); + break; + default: + books$ = this.bookService.bookState$.pipe(map(() => [])); + } + + this.scrollerBooksCache.set(config.id, books$.pipe(shareReplay(1))); + } + + return this.scrollerBooksCache.get(config.id)!; + } + + private createSortOption(field: string, direction: string): SortOption { + return { + field: field, + direction: direction === 'asc' ? SortDirection.ASCENDING : SortDirection.DESCENDING, + label: '' + }; + } + + private shuffleBooks(books: Book[], maxItems: number): Book[] { + const shuffled = [...books]; + for (let i = shuffled.length - 1; i > 0; i--) { + const j = Math.floor(Math.random() * (i + 1)); + [shuffled[i], shuffled[j]] = [shuffled[j], shuffled[i]]; + } + return shuffled.slice(0, maxItems); + } + + openDashboardSettings(): void { + this.ref = this.dialogLauncher.open({ + component: DashboardSettingsComponent, + header: 'Configure Dashboard', + showHeader: false + }); } createNewLibrary() { - this.ref = this.dialogService.open(LibraryCreatorComponent, { - header: 'Create New Library', - modal: true, - closable: true - }); + this.dialogLauncher.openLibraryCreatorDialog(); } } diff --git a/booklore-ui/src/app/features/dashboard/models/dashboard-config.model.ts b/booklore-ui/src/app/features/dashboard/models/dashboard-config.model.ts new file mode 100644 index 000000000..46d120508 --- /dev/null +++ b/booklore-ui/src/app/features/dashboard/models/dashboard-config.model.ts @@ -0,0 +1,32 @@ +import {DEFAULT_MAX_ITEMS} from '../components/dashboard-settings/dashboard-settings.component'; + +export enum ScrollerType { + LAST_READ = 'lastRead', + LATEST_ADDED = 'latestAdded', + RANDOM = 'random', + MAGIC_SHELF = 'magicShelf' +} + +export interface ScrollerConfig { + id: string; + type: ScrollerType; + title: string; + enabled: boolean; + order: number; + maxItems: number; + magicShelfId?: number; + sortField?: string; + sortDirection?: string; +} + +export interface DashboardConfig { + scrollers: ScrollerConfig[]; +} + +export const DEFAULT_DASHBOARD_CONFIG: DashboardConfig = { + scrollers: [ + {id: '1', type: ScrollerType.LAST_READ, title: 'Continue Reading', enabled: true, order: 1, maxItems: DEFAULT_MAX_ITEMS}, + {id: '2', type: ScrollerType.LATEST_ADDED, title: 'Recently Added', enabled: true, order: 2, maxItems: DEFAULT_MAX_ITEMS}, + {id: '3', type: ScrollerType.RANDOM, title: 'Discover Something New', enabled: true, order: 3, maxItems: DEFAULT_MAX_ITEMS} + ] +}; diff --git a/booklore-ui/src/app/features/dashboard/services/dashboard-config.service.ts b/booklore-ui/src/app/features/dashboard/services/dashboard-config.service.ts new file mode 100644 index 000000000..cb6ce733a --- /dev/null +++ b/booklore-ui/src/app/features/dashboard/services/dashboard-config.service.ts @@ -0,0 +1,41 @@ +import {Injectable} from '@angular/core'; +import {BehaviorSubject, Observable} from 'rxjs'; +import {DashboardConfig, DEFAULT_DASHBOARD_CONFIG} from '../models/dashboard-config.model'; +import {UserService} from '../../settings/user-management/user.service'; +import {filter, take} from 'rxjs/operators'; + +@Injectable({ + providedIn: 'root' +}) +export class DashboardConfigService { + private configSubject = new BehaviorSubject(DEFAULT_DASHBOARD_CONFIG); + + public config$: Observable = this.configSubject.asObservable(); + + constructor(private userService: UserService) { + this.userService.userState$ + .pipe( + filter(userState => !!userState?.user && userState.loaded), + take(1) + ) + .subscribe(userState => { + const dashboardConfig = userState.user?.userSettings?.dashboardConfig as DashboardConfig; + if (dashboardConfig) { + this.configSubject.next(dashboardConfig); + } + }); + } + + saveConfig(config: DashboardConfig): void { + this.configSubject.next(config); + + const user = this.userService.getCurrentUser(); + if (user) { + this.userService.updateUserSetting(user.id, 'dashboardConfig', config); + } + } + + resetToDefault(): void { + this.saveConfig(DEFAULT_DASHBOARD_CONFIG); + } +} diff --git a/booklore-ui/src/app/features/magic-shelf/component/magic-shelf-component.html b/booklore-ui/src/app/features/magic-shelf/component/magic-shelf-component.html index 2b97bb457..8f1e4efd2 100644 --- a/booklore-ui/src/app/features/magic-shelf/component/magic-shelf-component.html +++ b/booklore-ui/src/app/features/magic-shelf/component/magic-shelf-component.html @@ -93,7 +93,7 @@ } @else {
- +
} } @else if (ruleCtrl.get('field')?.value === 'readStatus') { diff --git a/booklore-ui/src/app/features/magic-shelf/service/book-rule-evaluator.service.ts b/booklore-ui/src/app/features/magic-shelf/service/book-rule-evaluator.service.ts index 47d403c25..c1744b547 100644 --- a/booklore-ui/src/app/features/magic-shelf/service/book-rule-evaluator.service.ts +++ b/booklore-ui/src/app/features/magic-shelf/service/book-rule-evaluator.service.ts @@ -90,21 +90,37 @@ export class BookRuleEvaluatorService { return value !== ruleVal; case 'contains': + if (Array.isArray(value)) { + if (typeof ruleVal !== 'string') return false; + return value.some(v => String(v).includes(ruleVal)); + } if (typeof value !== 'string') return false; if (typeof ruleVal !== 'string') return false; return value.includes(ruleVal); case 'does_not_contain': + if (Array.isArray(value)) { + if (typeof ruleVal !== 'string') return true; + return value.every(v => !String(v).includes(ruleVal)); + } if (typeof value !== 'string') return true; if (typeof ruleVal !== 'string') return true; return !value.includes(ruleVal); case 'starts_with': + if (Array.isArray(value)) { + if (typeof ruleVal !== 'string') return false; + return value.some(v => String(v).startsWith(ruleVal)); + } if (typeof value !== 'string') return false; if (typeof ruleVal !== 'string') return false; return value.startsWith(ruleVal); case 'ends_with': + if (Array.isArray(value)) { + if (typeof ruleVal !== 'string') return false; + return value.some(v => String(v).endsWith(ruleVal)); + } if (typeof value !== 'string') return false; if (typeof ruleVal !== 'string') return false; return value.endsWith(ruleVal); diff --git a/booklore-ui/src/app/features/settings/user-management/user.service.ts b/booklore-ui/src/app/features/settings/user-management/user.service.ts index 715906e2e..4b56535e9 100644 --- a/booklore-ui/src/app/features/settings/user-management/user.service.ts +++ b/booklore-ui/src/app/features/settings/user-management/user.service.ts @@ -5,6 +5,7 @@ import {API_CONFIG} from '../../../core/config/api-config'; import {Library} from '../../book/model/library.model'; import {catchError, distinctUntilChanged, finalize, shareReplay, tap} from 'rxjs/operators'; import {AuthService} from '../../../shared/service/auth.service'; +import {DashboardConfig} from '../../dashboard/models/dashboard-config.model'; export interface EntityViewPreferences { global: EntityViewPreference; @@ -130,6 +131,7 @@ export interface UserSettings { metadataCenterViewMode: 'route' | 'dialog'; entityViewPreferences: EntityViewPreferences; tableColumnPreference?: TableColumnPreference[]; + dashboardConfig?: DashboardConfig; koReaderEnabled: boolean; } @@ -218,17 +220,17 @@ export class UserService { private fetchMyself(): Observable { return this.http.get(`${this.userUrl}/me`).pipe( - tap(user => this.userStateSubject.next({ user, loaded: true, error: null })), + tap(user => this.userStateSubject.next({user, loaded: true, error: null})), catchError(err => { const curr = this.userStateSubject.value; - this.userStateSubject.next({ user: curr.user, loaded: true, error: err.message }); + this.userStateSubject.next({user: curr.user, loaded: true, error: err.message}); throw err; }) ); } public setInitialUser(user: User): void { - this.userStateSubject.next({ user, loaded: true, error: null }); + this.userStateSubject.next({user, loaded: true, error: null}); } getCurrentUser(): User | null { diff --git a/booklore-ui/src/app/shared/services/dialog-launcher.service.ts b/booklore-ui/src/app/shared/services/dialog-launcher.service.ts index 071a9648a..209654db8 100644 --- a/booklore-ui/src/app/shared/services/dialog-launcher.service.ts +++ b/booklore-ui/src/app/shared/services/dialog-launcher.service.ts @@ -13,16 +13,14 @@ export class DialogLauncherService { dialogService = inject(DialogService); - open(options: { component: any; header: string; top?: string; width?: string }): DynamicDialogRef | null { + open(options: { component: any; header: string; top?: string; width?: string; showHeader?: boolean }): DynamicDialogRef | null { const isMobile = window.innerWidth <= 768; - const {component, header, top, width} = options; + const {component, header, top, width, showHeader = true} = options; return this.dialogService.open(component, { header, + showHeader, modal: true, closable: true, - contentStyle: { - overflowY: 'hidden', - }, style: { position: 'absolute', ...(top ? {top} : {}),