feat(ui): add enhanced user popover + rearrange sidebar (#1246)
Co-authored-by: brios <127139797+balazs-szucs@users.noreply.github.com>
This commit is contained in:
@@ -1,8 +1,10 @@
|
||||
import { Component, Renderer2, RendererStyleFlags2, computed, inject, signal } from '@angular/core';
|
||||
import { RouterLink } from '@angular/router';
|
||||
import { Router, RouterLink } from '@angular/router';
|
||||
import { AppSidebarSectionComponent } from './app.sidebar-section.component';
|
||||
import { MenuTrigger } from '@angular/aria/menu';
|
||||
import { Popover } from 'primeng/popover';
|
||||
import { CdkTrapFocus } from '@angular/cdk/a11y';
|
||||
import { CdkConnectedOverlay, CdkOverlayOrigin } from '@angular/cdk/overlay';
|
||||
import { BookDialogHelperService } from '../../../features/book/components/book-browser/book-dialog-helper.service';
|
||||
import { UnifiedNotificationBoxComponent } from '../../components/unified-notification-popover/unified-notification-popover-component';
|
||||
import { MenuComponent } from '../../components/menu/menu.component';
|
||||
@@ -29,13 +31,15 @@ import { TranslocoDirective, TranslocoPipe, TranslocoService } from '@jsverse/tr
|
||||
import { Tooltip } from 'primeng/tooltip';
|
||||
import { toSignal } from '@angular/core/rxjs-interop';
|
||||
import { NavItem, SidebarSection } from '../navigation/nav-item.model';
|
||||
import { buildQuickActionNavItems, findPageNavItem } from '../navigation/nav-catalog';
|
||||
import { buildCreateActionNavItems } from '../navigation/nav-catalog';
|
||||
import {
|
||||
buildHomeSection,
|
||||
buildLibrarySection,
|
||||
buildMagicShelfSection,
|
||||
buildShelfSection,
|
||||
buildToolsSection,
|
||||
} from './sidebar-sections';
|
||||
import { VersionService } from '../../service/version.service';
|
||||
|
||||
const DOCUMENTATION_URL = 'https://grimmory.org/docs/getting-started';
|
||||
|
||||
@@ -53,6 +57,34 @@ function detectSearchShortcut(userAgent: string): string {
|
||||
return /Mac|iPhone|iPad|iPod/i.test(userAgent) ? '⌘K' : 'Ctrl+K';
|
||||
}
|
||||
|
||||
type SemanticVersion = readonly [number, number, number];
|
||||
|
||||
function parseSemanticVersion(version: string | undefined): SemanticVersion | null {
|
||||
const match = /^v?(\d+)\.(\d+)\.(\d+)$/.exec(version?.trim() ?? '');
|
||||
return match ? [Number(match[1]), Number(match[2]), Number(match[3])] : null;
|
||||
}
|
||||
|
||||
function formatVersionLabel(version: string): string {
|
||||
const value = version.trim();
|
||||
if (!value) return 'unknown';
|
||||
const semanticVersion = parseSemanticVersion(value);
|
||||
return semanticVersion ? `v${semanticVersion.join('.')}` : value;
|
||||
}
|
||||
|
||||
function isSemanticVersion(version: string | undefined): boolean {
|
||||
return parseSemanticVersion(version) !== null;
|
||||
}
|
||||
|
||||
function isNewerVersion(latest: string | undefined, current: string | undefined): boolean {
|
||||
const latestVersion = parseSemanticVersion(latest);
|
||||
const currentVersion = parseSemanticVersion(current);
|
||||
if (!latestVersion || !currentVersion) return false;
|
||||
|
||||
if (latestVersion[0] !== currentVersion[0]) return latestVersion[0] > currentVersion[0];
|
||||
if (latestVersion[1] !== currentVersion[1]) return latestVersion[1] > currentVersion[1];
|
||||
return latestVersion[2] > currentVersion[2];
|
||||
}
|
||||
|
||||
@Component({
|
||||
selector: 'app-sidebar',
|
||||
imports: [
|
||||
@@ -65,6 +97,9 @@ function detectSearchShortcut(userAgent: string): string {
|
||||
TranslocoPipe,
|
||||
Tooltip,
|
||||
MenuTrigger,
|
||||
CdkTrapFocus,
|
||||
CdkConnectedOverlay,
|
||||
CdkOverlayOrigin,
|
||||
],
|
||||
templateUrl: './app.sidebar.component.html',
|
||||
styleUrl: './app.sidebar.component.scss',
|
||||
@@ -79,8 +114,10 @@ export class AppSidebarComponent {
|
||||
private readonly commandPaletteService = inject(CommandPaletteService);
|
||||
private readonly bookDialogHelperService = inject(BookDialogHelperService);
|
||||
private readonly authService = inject(AuthService);
|
||||
private readonly router = inject(Router);
|
||||
readonly layoutService = inject(LayoutService);
|
||||
private readonly userService = inject(UserService);
|
||||
private readonly versionService = inject(VersionService);
|
||||
private readonly magicShelfService = inject(MagicShelfService);
|
||||
private readonly seriesDataService = inject(SeriesDataService);
|
||||
private readonly authorService = inject(AuthorService);
|
||||
@@ -89,8 +126,26 @@ export class AppSidebarComponent {
|
||||
|
||||
readonly currentUser = this.userService.currentUser;
|
||||
private readonly allAuthors = this.authorService.allAuthors;
|
||||
private readonly activeLang = toSignal(this.t.langChanges$, { initialValue: this.t.getActiveLang() });
|
||||
protected readonly activeLang = toSignal(this.t.langChanges$, { initialValue: this.t.getActiveLang() });
|
||||
protected readonly versionInfo = toSignal(this.versionService.getVersion(), { initialValue: null });
|
||||
protected readonly appVersionLabel = computed(() => formatVersionLabel(this.versionInfo()?.current ?? '...'));
|
||||
protected readonly updateAvailable = computed(() => {
|
||||
const version = this.versionInfo();
|
||||
return isSemanticVersion(version?.current)
|
||||
&& isSemanticVersion(version?.latest)
|
||||
&& isNewerVersion(version?.latest, version?.current);
|
||||
});
|
||||
private readonly translate = (key: string): string => this.t.translate(key);
|
||||
protected readonly userPopoverOpen = signal(false);
|
||||
protected readonly userPopoverOrigin = signal<CdkOverlayOrigin | null>(null);
|
||||
protected readonly canAccessReadingStats = computed(() => {
|
||||
const user = this.currentUser();
|
||||
return !!user && (user.permissions.admin || user.permissions.canAccessUserStats);
|
||||
});
|
||||
protected readonly canUploadBooks = computed(() => {
|
||||
const user = this.currentUser();
|
||||
return !!user && (user.permissions.admin || user.permissions.canUpload);
|
||||
});
|
||||
|
||||
readonly searchShortcutLabel = detectSearchShortcut(
|
||||
typeof navigator !== 'undefined' ? navigator.userAgent : ''
|
||||
@@ -126,6 +181,7 @@ export class AppSidebarComponent {
|
||||
this.translate,
|
||||
{ menuItems: this.libraryShelfMenuService },
|
||||
),
|
||||
...buildToolsSection(this.translate, this.currentUser()?.permissions ?? {}),
|
||||
];
|
||||
});
|
||||
|
||||
@@ -134,14 +190,13 @@ export class AppSidebarComponent {
|
||||
const user = this.currentUser();
|
||||
if (!user) return [];
|
||||
|
||||
const actions = buildQuickActionNavItems(this.translate, user.permissions, {
|
||||
const actions = buildCreateActionNavItems(this.translate, user.permissions, {
|
||||
createLibrary: () => this.dialogLauncherService.openLibraryCreateDialog(),
|
||||
createShelf: () => this.bookDialogHelperService.openShelfCreatorDialog(),
|
||||
createMagicShelf: () => this.dialogLauncherService.openMagicShelfCreateDialog(),
|
||||
uploadBook: () => this.dialogLauncherService.openFileUploadDialog(),
|
||||
});
|
||||
const bookdrop = findPageNavItem('bookdrop', this.translate, user.permissions);
|
||||
return this.toMenuEntries(bookdrop ? [...actions, bookdrop] : actions);
|
||||
return this.toMenuEntries(actions);
|
||||
});
|
||||
|
||||
readonly userInitials = computed(() => {
|
||||
@@ -157,52 +212,6 @@ export class AppSidebarComponent {
|
||||
this.layoutService.desktopSidebarCollapsed() ? BESIDE_RIGHT_BOTTOM : ABOVE_ALIGN_LEFT,
|
||||
);
|
||||
|
||||
readonly userMenuItems = computed<MenuEntry[]>(() => {
|
||||
this.activeLang();
|
||||
const user = this.currentUser();
|
||||
if (!user) return [];
|
||||
|
||||
const items: MenuEntry[] = [];
|
||||
|
||||
if (!user.permissions.demoUser) {
|
||||
items.push({
|
||||
label: this.t.translate('layout.menu.account'),
|
||||
icon: 'pi pi-user',
|
||||
action: () => this.dialogLauncherService.openUserProfileDialog(),
|
||||
});
|
||||
}
|
||||
|
||||
items.push({
|
||||
label: this.t.translate('layout.menu.documentation'),
|
||||
icon: 'pi pi-info-circle',
|
||||
action: () => window.open(DOCUMENTATION_URL, '_blank', 'noopener,noreferrer'),
|
||||
});
|
||||
|
||||
items.push({ separator: true });
|
||||
|
||||
items.push({
|
||||
label: this.t.translate('layout.menu.logout'),
|
||||
icon: 'pi pi-sign-out',
|
||||
action: () => this.authService.logout(),
|
||||
});
|
||||
|
||||
return items;
|
||||
});
|
||||
|
||||
readonly moreMenuItems = computed<MenuEntry[]>(() => {
|
||||
this.activeLang();
|
||||
const user = this.currentUser();
|
||||
if (!user) return [];
|
||||
|
||||
const perms = user.permissions;
|
||||
return this.toMenuEntries([
|
||||
findPageNavItem('settings', this.translate, perms),
|
||||
findPageNavItem('libraryStats', this.translate, perms),
|
||||
findPageNavItem('readingStats', this.translate, perms),
|
||||
findPageNavItem('metadataManager', this.translate, perms),
|
||||
]);
|
||||
});
|
||||
|
||||
openSearch(): void {
|
||||
this.commandPaletteService.open();
|
||||
}
|
||||
@@ -211,6 +220,61 @@ export class AppSidebarComponent {
|
||||
this.layoutService.closeMobileSidebar();
|
||||
}
|
||||
|
||||
protected toggleUserPopover(origin: CdkOverlayOrigin): void {
|
||||
if (this.userPopoverOpen() && this.userPopoverOrigin() === origin) {
|
||||
this.closeUserPopover();
|
||||
return;
|
||||
}
|
||||
|
||||
this.userPopoverOrigin.set(origin);
|
||||
this.userPopoverOpen.set(true);
|
||||
}
|
||||
|
||||
protected closeUserPopover(): void {
|
||||
this.userPopoverOpen.set(false);
|
||||
}
|
||||
|
||||
protected openDocumentation(): void {
|
||||
window.open(DOCUMENTATION_URL, '_blank', 'noopener,noreferrer');
|
||||
this.closeUserPopover();
|
||||
}
|
||||
|
||||
protected openAccountSettings(): void {
|
||||
this.dialogLauncherService.openUserProfileDialog();
|
||||
this.closeUserPopover();
|
||||
}
|
||||
|
||||
protected openSettings(): void {
|
||||
this.router.navigate(['/settings']);
|
||||
this.closeUserPopover();
|
||||
}
|
||||
|
||||
protected openChangelogDialog(): void {
|
||||
this.dialogLauncherService.openVersionChangelogDialog();
|
||||
this.closeUserPopover();
|
||||
}
|
||||
|
||||
protected openReadingStats(): void {
|
||||
this.router.navigate(['/reading-stats']);
|
||||
this.closeUserPopover();
|
||||
}
|
||||
|
||||
protected openUploadDialog(): void {
|
||||
this.dialogLauncherService.openFileUploadDialog();
|
||||
}
|
||||
|
||||
protected logout(): void {
|
||||
this.authService.logout();
|
||||
this.closeUserPopover();
|
||||
}
|
||||
|
||||
protected onUserPopoverKeydown(event: KeyboardEvent): void {
|
||||
if (event.key === 'Escape') {
|
||||
event.preventDefault();
|
||||
this.closeUserPopover();
|
||||
}
|
||||
}
|
||||
|
||||
private readonly anchorByOverlay = new WeakMap<Popover, { trigger: HTMLElement; placement: 'above' | 'below' }>();
|
||||
|
||||
protected applySidebarOverlayPosition(overlay: Popover): void {
|
||||
|
||||
Reference in New Issue
Block a user