- Add option to sort sidebar shelf and library
- Move user settings to a separate table - Make settings tabs' elements more consistent
This commit is contained in:
committed by
Aditya Chandel
parent
4cb82d2ba7
commit
5d1dd1576c
@@ -4,16 +4,19 @@ import {AsyncPipe, NgForOf, NgIf} from '@angular/common';
|
||||
import {MenuModule} from 'primeng/menu';
|
||||
import {LibraryService} from '../../../book/service/library.service';
|
||||
import {Observable, of} from 'rxjs';
|
||||
import {map} from 'rxjs/operators';
|
||||
import {filter, map} from 'rxjs/operators';
|
||||
import {ShelfService} from '../../../book/service/shelf.service';
|
||||
import {BookService} from '../../../book/service/book.service';
|
||||
import {LibraryShelfMenuService} from '../../../book/service/library-shelf-menu.service';
|
||||
import {AppVersion, VersionService} from '../../../core/service/version.service';
|
||||
import {DialogService, DynamicDialogRef} from 'primeng/dynamicdialog';
|
||||
import {VersionChangelogDialogComponent} from './version-changelog-dialog/version-changelog-dialog.component';
|
||||
import {AppSettingsService} from '../../../core/service/app-settings.service';
|
||||
import {UserService} from '../../../settings/user-management/user.service';
|
||||
|
||||
@Component({
|
||||
selector: 'app-menu',
|
||||
standalone: true,
|
||||
imports: [AppMenuitemComponent, NgIf, NgForOf, MenuModule, AsyncPipe],
|
||||
templateUrl: './app.menu.component.html',
|
||||
})
|
||||
@@ -31,41 +34,92 @@ export class AppMenuComponent implements OnInit {
|
||||
private versionService = inject(VersionService);
|
||||
private libraryShelfMenuService = inject(LibraryShelfMenuService);
|
||||
private dialogService = inject(DialogService);
|
||||
private userService = inject(UserService);
|
||||
|
||||
librarySortField: 'name' | 'id' = 'name';
|
||||
librarySortOrder: 'asc' | 'desc' = 'desc';
|
||||
shelfSortField: 'name' | 'id' = 'name';
|
||||
shelfSortOrder: 'asc' | 'desc' = 'asc';
|
||||
|
||||
ngOnInit(): void {
|
||||
|
||||
this.versionService.getVersion().subscribe((data) => {
|
||||
this.versionInfo = data;
|
||||
});
|
||||
|
||||
this.libraryMenu$ = this.libraryService.libraryState$.pipe(
|
||||
map((state) => [
|
||||
this.userService.userData$.pipe(
|
||||
filter(settings => !!settings))
|
||||
.subscribe(user => {
|
||||
if (user?.userSettings.sidebarLibrarySorting) {
|
||||
this.librarySortField = this.validateSortField(user.userSettings.sidebarLibrarySorting.field);
|
||||
this.librarySortOrder = this.validateSortOrder(user.userSettings.sidebarLibrarySorting.order);
|
||||
}
|
||||
if (user?.userSettings.sidebarShelfSorting) {
|
||||
this.shelfSortField = this.validateSortField(user.userSettings.sidebarShelfSorting.field);
|
||||
this.shelfSortOrder = this.validateSortOrder(user.userSettings.sidebarShelfSorting.order);
|
||||
}
|
||||
this.initMenus();
|
||||
});
|
||||
|
||||
this.homeMenu$ = this.bookService.bookState$.pipe(
|
||||
map((bookState) => [
|
||||
{
|
||||
label: 'Library',
|
||||
label: 'Home',
|
||||
separator: false,
|
||||
items: state.libraries?.map((library) => ({
|
||||
menu: this.libraryShelfMenuService.initializeLibraryMenuItems(library),
|
||||
label: library.name,
|
||||
type: 'Library',
|
||||
icon: 'pi pi-' + library.icon,
|
||||
routerLink: [`/library/${library.id}/books`],
|
||||
bookCount$: this.libraryService.getBookCount(library.id ?? 0),
|
||||
})) || [],
|
||||
items: [
|
||||
{
|
||||
label: 'Dashboard',
|
||||
icon: 'pi pi-fw pi-home',
|
||||
routerLink: ['/dashboard'],
|
||||
},
|
||||
{
|
||||
label: 'All Books',
|
||||
type: 'All Books',
|
||||
icon: 'pi pi-fw pi-book',
|
||||
routerLink: ['/all-books'],
|
||||
bookCount$: of(bookState.books ? bookState.books.length : 0),
|
||||
},
|
||||
],
|
||||
},
|
||||
])
|
||||
);
|
||||
}
|
||||
|
||||
private initMenus(): void {
|
||||
this.libraryMenu$ = this.libraryService.libraryState$.pipe(
|
||||
map((state) => {
|
||||
const libraries = state.libraries ?? [];
|
||||
const sortedLibraries = this.sortArray(libraries, this.librarySortField, this.librarySortOrder);
|
||||
|
||||
return [
|
||||
{
|
||||
label: 'Library',
|
||||
separator: false,
|
||||
items: sortedLibraries.map((library) => ({
|
||||
menu: this.libraryShelfMenuService.initializeLibraryMenuItems(library),
|
||||
label: library.name,
|
||||
type: 'Library',
|
||||
icon: 'pi pi-' + library.icon,
|
||||
routerLink: [`/library/${library.id}/books`],
|
||||
bookCount$: this.libraryService.getBookCount(library.id ?? 0),
|
||||
})),
|
||||
},
|
||||
];
|
||||
})
|
||||
);
|
||||
|
||||
this.shelfMenu$ = this.shelfService.shelfState$.pipe(
|
||||
map((state) => {
|
||||
const shelves = state.shelves?.map((shelf) => ({
|
||||
const shelves = state.shelves ?? [];
|
||||
const sortedShelves = this.sortArray(shelves, this.shelfSortField, this.shelfSortOrder);
|
||||
|
||||
const shelfItems = sortedShelves.map((shelf) => ({
|
||||
menu: this.libraryShelfMenuService.initializeShelfMenuItems(shelf),
|
||||
label: shelf.name,
|
||||
type: 'Shelf',
|
||||
icon: 'pi pi-' + shelf.icon,
|
||||
routerLink: [`/shelf/${shelf.id}/books`],
|
||||
bookCount$: this.shelfService.getBookCount(shelf.id ?? 0),
|
||||
})) || [];
|
||||
}));
|
||||
|
||||
const unshelvedItem = {
|
||||
label: 'Unshelved',
|
||||
@@ -79,32 +133,7 @@ export class AppMenuComponent implements OnInit {
|
||||
{
|
||||
label: 'Shelves',
|
||||
separator: false,
|
||||
items: [unshelvedItem, ...shelves],
|
||||
},
|
||||
];
|
||||
})
|
||||
);
|
||||
|
||||
this.homeMenu$ = this.bookService.bookState$.pipe(
|
||||
map((bookState) => {
|
||||
return [
|
||||
{
|
||||
label: 'Home',
|
||||
separator: false,
|
||||
items: [
|
||||
{
|
||||
label: 'Dashboard',
|
||||
icon: 'pi pi-fw pi-home',
|
||||
routerLink: ['/dashboard'],
|
||||
},
|
||||
{
|
||||
label: 'All Books',
|
||||
type: 'All Books',
|
||||
icon: 'pi pi-fw pi-book',
|
||||
routerLink: ['/all-books'],
|
||||
bookCount$: of(bookState.books ? bookState.books.length : 0),
|
||||
},
|
||||
],
|
||||
items: [unshelvedItem, ...shelfItems],
|
||||
},
|
||||
];
|
||||
})
|
||||
@@ -127,10 +156,32 @@ export class AppMenuComponent implements OnInit {
|
||||
|
||||
getVersionUrl(version: string | undefined): string {
|
||||
if (!version) return '#';
|
||||
if (version.startsWith('v')) {
|
||||
return `https://github.com/adityachandelgit/BookLore/releases/tag/${version}`;
|
||||
} else {
|
||||
return `https://github.com/adityachandelgit/BookLore/commit/${version}`;
|
||||
}
|
||||
return version.startsWith('v')
|
||||
? `https://github.com/adityachandelgit/BookLore/releases/tag/${version}`
|
||||
: `https://github.com/adityachandelgit/BookLore/commit/${version}`;
|
||||
}
|
||||
|
||||
private sortArray<T>(array: T[], field: 'name' | 'id', order: 'asc' | 'desc'): T[] {
|
||||
return [...array].sort((a, b) => {
|
||||
const aVal = (a as any)[field] ?? '';
|
||||
const bVal = (b as any)[field] ?? '';
|
||||
let comparison = 0;
|
||||
|
||||
if (typeof aVal === 'string' && typeof bVal === 'string') {
|
||||
comparison = aVal.localeCompare(bVal);
|
||||
} else if (typeof aVal === 'number' && typeof bVal === 'number') {
|
||||
comparison = aVal - bVal;
|
||||
}
|
||||
|
||||
return order === 'asc' ? comparison : -comparison;
|
||||
});
|
||||
}
|
||||
|
||||
private validateSortField(field: string): 'name' | 'id' {
|
||||
return field === 'id' ? 'id' : 'name';
|
||||
}
|
||||
|
||||
private validateSortOrder(order: string): 'asc' | 'desc' {
|
||||
return order === 'desc' ? 'desc' : 'asc';
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user