refactor(ui): switch library and shelf menu to app-menu (#2057)

This commit is contained in:
Alex
2026-07-30 20:47:24 +01:00
committed by GitHub
parent 134e97f028
commit 0acd8bcb1e
22 changed files with 631 additions and 845 deletions
@@ -28,19 +28,24 @@
}
</div>
@if (userService.currentUser(); as currentUser) {
@if (entityType !== EntityType.ALL_BOOKS && entityType !== EntityType.UNSHELVED &&
(currentUser.permissions.admin || currentUser.permissions.canManageLibrary)) {
<div class="entity-menu-wrapper">
<p-button
icon="pi pi-ellipsis-v"
@if (entityMenuTarget(); as menuTarget) {
<div class="entity-menu-wrapper">
<app-library-shelf-menu
#entityMenu
[target]="menuTarget"
[ariaLabel]="'common.moreActionsFor' | transloco: {label: menuTarget.entity.name}" />
@if (entityMenu.available()) {
<button
pButton
type="button"
[rounded]="true"
[text]="true"
(click)="entitymenu.toggle($event)">
</p-button>
<p-menu #entitymenu [model]="entityOptions()" [popup]="true" appendTo="body"></p-menu>
</div>
}
[attr.aria-label]="'common.moreActionsFor' | transloco: {label: menuTarget.entity.name}"
[appMenuTriggerFor]="entityMenu.menu()">
<i class="pi pi-ellipsis-v" aria-hidden="true"></i>
</button>
}
</div>
}
}
</div>
@@ -8,7 +8,6 @@ import {ConfirmationService, MessageService} from '@openng/optimus-ui/api';
import {PageTitleService} from '../../../../shared/service/page-title.service';
import {BookService} from '../../service/book.service';
import {BookMetadataManageService} from '../../service/book-metadata-manage.service';
import {LibraryShelfMenuService} from '../../service/library-shelf-menu.service';
import {Book} from '../../model/book.model';
import {SortDirection, SortOption} from '../../model/sort.model';
import {UserService} from '../../../settings/user-management/user.service';
@@ -294,14 +293,6 @@ function createHarness(options?: {
getMetadataMenuItems: vi.fn(() => []),
},
},
{
provide: LibraryShelfMenuService,
useValue: {
initializeLibraryMenuItems: vi.fn(() => []),
initializeMagicShelfMenuItems: vi.fn(() => []),
initializeShelfMenuItems: vi.fn(() => []),
},
},
{provide: PageTitleService, useValue: {setPageTitle: vi.fn()}},
{provide: LoadingService, useValue: {show: vi.fn(), hide: vi.fn()}},
{provide: LocalStorageService, useValue: {get: vi.fn(), set: vi.fn()}},
@@ -11,13 +11,15 @@ import {DynamicDialogRef} from '@openng/optimus-ui/dynamicdialog';
import {Library} from '../../model/library.model';
import {SortDirection, SortOption} from '../../model/sort.model';
import {Book} from '../../model/book.model';
import {LibraryShelfMenuService} from '../../service/library-shelf-menu.service';
import {
LibraryShelfMenuComponent,
type LibraryShelfMenuTarget,
} from '../library-shelf-menu/library-shelf-menu.component';
import {BookTableComponent} from './book-table/book-table.component';
import {Button} from '@openng/optimus-ui/button';
import {Button, ButtonDirective} from '@openng/optimus-ui/button';
import {NgClass} from '@angular/common';
import {BookCardComponent} from './book-card/book-card.component';
import {Menu} from '@openng/optimus-ui/menu';
import {InputText} from '@openng/optimus-ui/inputtext';
import {FormsModule} from '@angular/forms';
import {BookFilterComponent} from './book-filter/book-filter.component';
@@ -59,6 +61,7 @@ import {filterBooksByFilters} from './filters/sidebar-filter';
import {LayoutService} from '../../../../shared/layout/layout.service';
import {createGridDensity} from '../../../../shared/util/grid-density.util';
import {DeferredRenderState} from './deferred-render-state';
import {AppMenuTriggerDirective} from '../../../../shared/ui/menu/app-menu-trigger.directive';
export enum EntityType {
LIBRARY = 'Library',
@@ -81,9 +84,10 @@ const MOBILE_COLUMNS_STORAGE_KEY = 'mobileColumnsPreference';
styleUrls: ['./book-browser.component.scss'],
changeDetection: ChangeDetectionStrategy.OnPush,
imports: [
Button, BookCardComponent, Menu, InputText, FormsModule,
Button, ButtonDirective, BookCardComponent, InputText, FormsModule,
BookTableComponent, BookFilterComponent, Tooltip, NgClass, Popover,
Checkbox, Divider, MultiSelect, TieredMenu, Badge, MultiSortPopoverComponent, TranslocoDirective, TranslocoPipe, GridDensityButtonsComponent,
LibraryShelfMenuComponent, AppMenuTriggerDirective,
],
providers: [SeriesCollapseFilter],
})
@@ -105,7 +109,6 @@ export class BookBrowserComponent implements AfterViewInit {
private bookMetadataManageService = inject(BookMetadataManageService);
private dialogHelperService = inject(BookDialogHelperService);
private bookMenuService = inject(BookMenuService);
private libraryShelfMenuService = inject(LibraryShelfMenuService);
private pageTitle = inject(PageTitleService);
private loadingService = inject(LoadingService);
private bookNavigationService = inject(BookNavigationService);
@@ -187,19 +190,17 @@ export class BookBrowserComponent implements AfterViewInit {
const {entityId, entityType} = this.entityInfo();
return this.entityService.getEntity(entityId, entityType);
});
readonly entityOptions = computed<MenuItem[]>(() => {
readonly entityMenuTarget = computed<LibraryShelfMenuTarget | null>(() => {
const entity = this.entity();
if (!entity) {
return [];
if (entity?.id == null) return null;
if (this.entityService.isLibrary(entity)) {
return {type: 'library', entity: {...entity, id: entity.id}};
}
const actions = this.entityService.isLibrary(entity)
? this.libraryShelfMenuService.initializeLibraryMenuItems(entity)
: this.entityService.isMagicShelf(entity)
? this.libraryShelfMenuService.initializeMagicShelfMenuItems(entity)
: this.libraryShelfMenuService.initializeShelfMenuItems(entity);
return actions;
if (this.entityService.isMagicShelf(entity)) {
return {type: 'magicShelf', entity: {...entity, id: entity.id}};
}
return {type: 'shelf', entity: {...entity, id: entity.id}};
});
// Deferred pipeline: heavy filter/sort runs in a setTimeout so the page chrome
// and skeletons paint first, then real books replace them on the next task.
@@ -0,0 +1,82 @@
import {signal} from '@angular/core';
import {ComponentFixture, TestBed} from '@angular/core/testing';
import {provideRouter} from '@angular/router';
import {beforeEach, describe, expect, it} from 'vitest';
import {getTranslocoModule} from '../../../../core/testing/transloco-testing';
import {LayoutService} from '../../../../shared/layout/layout.service';
import {UserService} from '../../../settings/user-management/user.service';
import {LibraryShelfMenuService} from '../../service/library-shelf-menu.service';
import {LibraryShelfMenuComponent} from './library-shelf-menu.component';
describe('LibraryShelfMenuComponent', () => {
let fixture: ComponentFixture<LibraryShelfMenuComponent>;
const currentUser = signal({
id: 3,
permissions: {admin: false, canManageLibrary: true},
});
beforeEach(() => {
TestBed.configureTestingModule({
imports: [LibraryShelfMenuComponent, getTranslocoModule()],
providers: [
provideRouter([]),
{provide: UserService, useValue: {currentUser}},
{provide: LibraryShelfMenuService, useValue: {}},
{provide: LayoutService, useValue: {isDesktop: signal(true)}},
],
});
fixture = TestBed.createComponent(LibraryShelfMenuComponent);
});
it.each([
{
name: 'library manager',
user: {id: 3, permissions: {admin: false, canManageLibrary: true}},
target: {type: 'library', entity: {id: 7, name: 'Library', watch: true, paths: []}},
labels: ['Add Physical Book', 'Import ISBNs from File', 'Edit Library', 'Re-scan Library',
'Custom Fetch Metadata', 'Auto Fetch Metadata', 'Find Duplicates', 'Delete Library'],
disabled: 0,
available: true,
},
{
name: 'library reader',
user: {id: 3, permissions: {admin: false, canManageLibrary: false}},
target: {type: 'library', entity: {id: 7, name: 'Library', watch: true, paths: []}},
labels: [],
disabled: 0,
available: false,
},
{
name: 'public shelf reader',
user: {id: 3, permissions: {admin: false, canManageLibrary: false}},
target: {type: 'shelf', entity: {id: 11, name: 'Shelf', userId: 99, publicShelf: true}},
labels: ['Edit Shelf', 'Delete Shelf'],
disabled: 2,
available: true,
},
{
name: 'public magic-shelf reader',
user: {id: 3, permissions: {admin: false, canManageLibrary: false}},
target: {type: 'magicShelf', entity: {id: 13, name: 'Magic', filterJson: '{}', isPublic: true}},
labels: ['Edit Magic Shelf', 'Copy JSON', 'Delete Magic Shelf'],
disabled: 2,
available: true,
},
])('renders the $name state', async ({user, target, labels, disabled, available}) => {
currentUser.set(user);
fixture.componentRef.setInput('target', target);
fixture.componentRef.setInput('ariaLabel', 'Actions');
await fixture.whenStable();
fixture.componentInstance.menu().open(fixture.nativeElement);
await fixture.whenStable();
const menu = document.querySelector('app-menu[aria-label="Actions"]') as HTMLElement;
const renderedLabels = Array.from(menu.querySelectorAll('app-menu-item'))
.map(item => item.textContent?.trim());
expect(fixture.componentInstance.available()).toBe(available);
expect(renderedLabels).toEqual(labels);
expect(menu.querySelectorAll('app-menu-item[aria-disabled="true"]')).toHaveLength(disabled);
});
});
@@ -0,0 +1,157 @@
import {ChangeDetectionStrategy, Component, computed, inject, input, output, viewChild} from '@angular/core';
import {LucideTrash2} from '@lucide/angular';
import {TranslocoPipe} from '@jsverse/transloco';
import {AppMenuComponent} from '../../../../shared/ui/menu/app-menu.component';
import {AppMenuContentDirective} from '../../../../shared/ui/menu/app-menu-content.directive';
import {AppMenuItemComponent} from '../../../../shared/ui/menu/app-menu-item.component';
import {AppMenuSectionComponent} from '../../../../shared/ui/menu/app-menu-section.component';
import {AppMenuSeparatorComponent} from '../../../../shared/ui/menu/app-menu-separator.component';
import {MagicShelf} from '../../../magic-shelf/service/magic-shelf.service';
import {UserService} from '../../../settings/user-management/user.service';
import {Library} from '../../model/library.model';
import {Shelf} from '../../model/shelf.model';
import {LibraryShelfMenuService} from '../../service/library-shelf-menu.service';
type Persisted<T extends {id?: number | null}> = Omit<T, 'id'> & {id: number};
export type LibraryShelfMenuTarget =
| {type: 'library'; entity: Persisted<Library>}
| {type: 'shelf'; entity: Persisted<Shelf>}
| {type: 'magicShelf'; entity: Persisted<MagicShelf>};
@Component({
selector: 'app-library-shelf-menu',
standalone: true,
changeDetection: ChangeDetectionStrategy.OnPush,
host: {class: 'contents'},
imports: [
TranslocoPipe,
AppMenuComponent,
AppMenuContentDirective,
AppMenuItemComponent,
AppMenuSectionComponent,
AppMenuSeparatorComponent,
],
template: `
@let currentTarget = target();
<app-menu
[ariaLabel]="ariaLabel()"
(opened)="opened.emit()"
(closed)="closed.emit()">
<ng-template appMenuContent>
@if (available()) {
@switch (currentTarget.type) {
@case ('library') {
<app-menu-item (selected)="actions.addPhysicalBook(currentTarget.entity.id)">
{{ 'book.shelfMenuService.library.addPhysicalBook' | transloco }}
</app-menu-item>
<app-menu-item (selected)="actions.importIsbns(currentTarget.entity.id)">
{{ 'book.shelfMenuService.library.bulkIsbnImport' | transloco }}
</app-menu-item>
<app-menu-separator />
<app-menu-item
(selected)="actions.editLibrary(currentTarget.entity.id)">
{{ 'book.shelfMenuService.library.editLibrary' | transloco }}
</app-menu-item>
<app-menu-item (selected)="actions.rescanLibrary(currentTarget.entity)">
{{ 'book.shelfMenuService.library.rescanLibrary' | transloco }}
</app-menu-item>
<app-menu-separator />
<app-menu-item (selected)="actions.customFetchLibraryMetadata(currentTarget.entity.id)">
{{ 'book.shelfMenuService.library.customFetchMetadata' | transloco }}
</app-menu-item>
<app-menu-item (selected)="actions.autoFetchLibraryMetadata(currentTarget.entity.id)">
{{ 'book.shelfMenuService.library.autoFetchMetadata' | transloco }}
</app-menu-item>
<app-menu-item (selected)="actions.findLibraryDuplicates(currentTarget.entity.id)">
{{ 'book.shelfMenuService.library.findDuplicates' | transloco }}
</app-menu-item>
<app-menu-separator />
<app-menu-item
[icon]="trashIcon"
variant="destructive"
(selected)="actions.deleteLibrary(currentTarget.entity)">
{{ 'book.shelfMenuService.library.deleteLibrary' | transloco }}
</app-menu-item>
}
@case ('shelf') {
@if (currentTarget.entity.publicShelf || !canManageShelf()) {
<app-menu-section>
@if (currentTarget.entity.publicShelf) {
{{ 'book.shelfMenuService.shelf.publicShelfPrefix' | transloco }}
}
{{ (canManageShelf()
? 'book.shelfMenuService.shelf.optionsLabel'
: 'book.shelfMenuService.shelf.readOnly') | transloco }}
</app-menu-section>
}
<app-menu-item
[disabled]="!canManageShelf()"
(selected)="actions.editShelf(currentTarget.entity.id)">
{{ 'book.shelfMenuService.shelf.editShelf' | transloco }}
</app-menu-item>
<app-menu-separator />
<app-menu-item
[icon]="trashIcon"
[disabled]="!canManageShelf()"
variant="destructive"
(selected)="actions.deleteShelf(currentTarget.entity)">
{{ 'book.shelfMenuService.shelf.deleteShelf' | transloco }}
</app-menu-item>
}
@case ('magicShelf') {
<app-menu-item
[disabled]="!canManageMagicShelf()"
(selected)="actions.editMagicShelf(currentTarget.entity.id)">
{{ 'book.shelfMenuService.magicShelf.editMagicShelf' | transloco }}
</app-menu-item>
<app-menu-item (selected)="actions.copyMagicShelfJson(currentTarget.entity.filterJson)">
{{ 'book.shelfMenuService.magicShelf.exportJson' | transloco }}
</app-menu-item>
<app-menu-separator />
<app-menu-item
[icon]="trashIcon"
[disabled]="!canManageMagicShelf()"
variant="destructive"
(selected)="actions.deleteMagicShelf(currentTarget.entity)">
{{ 'book.shelfMenuService.magicShelf.deleteMagicShelf' | transloco }}
</app-menu-item>
}
}
}
</ng-template>
</app-menu>
`,
})
export class LibraryShelfMenuComponent {
readonly target = input.required<LibraryShelfMenuTarget>();
readonly ariaLabel = input.required<string>();
readonly opened = output<void>();
readonly closed = output<void>();
readonly menu = viewChild.required(AppMenuComponent);
private readonly currentUser = inject(UserService).currentUser;
protected readonly actions = inject(LibraryShelfMenuService);
protected readonly trashIcon = LucideTrash2.icon;
readonly available = computed(() => {
const user = this.currentUser();
if (!user) return false;
return this.target().type !== 'library'
|| user.permissions.admin
|| user.permissions.canManageLibrary;
});
protected readonly canManageShelf = computed(() => {
const target = this.target();
return target.type === 'shelf' && target.entity.userId === this.currentUser()?.id;
});
protected readonly canManageMagicShelf = computed(() => {
const target = this.target();
return target.type === 'magicShelf'
&& (!target.entity.isPublic || !!this.currentUser()?.permissions.admin);
});
}