fix(sidebar): add the notification badge to the sidebar (#1299)

This commit is contained in:
Zach
2026-05-13 23:28:46 -04:00
committed by GitHub
parent ff5f054bd3
commit 168c915d57
5 changed files with 185 additions and 14 deletions
@@ -87,7 +87,7 @@
<button <button
type="button" type="button"
class="sidebar-icon-button sidebar-mobile-landscape-action" class="sidebar-icon-button sidebar-mobile-landscape-action sidebar-notification-button"
[attr.aria-label]="t('notifications')" [attr.aria-label]="t('notifications')"
[pTooltip]="t('notifications')" [pTooltip]="t('notifications')"
[tooltipDisabled]="!layoutService.isDesktop()" [tooltipDisabled]="!layoutService.isDesktop()"
@@ -96,7 +96,12 @@
[attr.aria-expanded]="notificationsOpen() ? 'true' : 'false'" [attr.aria-expanded]="notificationsOpen() ? 'true' : 'false'"
(click)="openSidebarOverlay($event, notificationsPopover, 'below')" (click)="openSidebarOverlay($event, notificationsPopover, 'below')"
> >
<i class="pi pi-bell" aria-hidden="true"></i> <span class="sidebar-notification-button-content">
<i class="pi pi-bell" aria-hidden="true"></i>
@if (shouldShowNotificationBadge) {
<span class="sidebar-notification-badge">{{ completedTaskCount }}</span>
}
</span>
</button> </button>
<p-popover #notificationsPopover appendTo="body" styleClass="sidebar-flyout-popover" (onShow)="notificationsOpen.set(true); applySidebarOverlayPosition(notificationsPopover)" (onHide)="notificationsOpen.set(false)"> <p-popover #notificationsPopover appendTo="body" styleClass="sidebar-flyout-popover" (onShow)="notificationsOpen.set(true); applySidebarOverlayPosition(notificationsPopover)" (onHide)="notificationsOpen.set(false)">
@@ -138,7 +143,7 @@
(click)="openSearch()" (click)="openSearch()"
> >
<i class="pi pi-search" aria-hidden="true"></i> <i class="pi pi-search" aria-hidden="true"></i>
<span>{{ t('search') }}</span> <span class="sidebar-action-label">{{ t('search') }}</span>
<kbd class="sidebar-search-shortcut" aria-hidden="true">{{ searchShortcutLabel }}</kbd> <kbd class="sidebar-search-shortcut" aria-hidden="true">{{ searchShortcutLabel }}</kbd>
</button> </button>
</div> </div>
@@ -166,7 +171,7 @@
[attr.aria-label]="t('add')" [attr.aria-label]="t('add')"
> >
<i class="pi pi-plus" aria-hidden="true"></i> <i class="pi pi-plus" aria-hidden="true"></i>
<span>{{ t('add') }}</span> <span class="sidebar-footer-action-label">{{ t('add') }}</span>
</button> </button>
<app-menu <app-menu
@@ -188,21 +193,26 @@
(click)="openUploadDialog()" (click)="openUploadDialog()"
> >
<i class="pi pi-upload" aria-hidden="true"></i> <i class="pi pi-upload" aria-hidden="true"></i>
<span>{{ t('uploadBook') }}</span> <span class="sidebar-footer-action-label">{{ t('uploadBook') }}</span>
</button> </button>
} }
<button <button
type="button" type="button"
class="sidebar-action-link sidebar-footer-action" class="sidebar-action-link sidebar-footer-action sidebar-notification-button"
[attr.aria-label]="t('notifications')" [attr.aria-label]="t('notifications')"
[pTooltip]="t('notifications')" [pTooltip]="t('notifications')"
[tooltipDisabled]="!layoutService.isDesktop()" [tooltipDisabled]="!layoutService.isDesktop()"
tooltipPosition="top" tooltipPosition="top"
(click)="openSidebarOverlay($event, notificationsPopover, layoutService.isDesktop() ? 'below' : 'above')" (click)="openSidebarOverlay($event, notificationsPopover, layoutService.isDesktop() ? 'below' : 'above')"
> >
<i class="pi pi-bell" aria-hidden="true"></i> <span class="sidebar-notification-button-content">
<span>{{ t('notifications') }}</span> <i class="pi pi-bell" aria-hidden="true"></i>
@if (shouldShowNotificationBadge) {
<span class="sidebar-notification-badge">{{ completedTaskCount }}</span>
}
</span>
<span class="sidebar-footer-action-label">{{ t('notifications') }}</span>
</button> </button>
</div> </div>
@@ -148,6 +148,38 @@
} }
} }
.sidebar-notification-button {
position: relative;
}
.sidebar-notification-button-content {
position: relative;
display: inline-flex;
align-items: center;
justify-content: center;
margin-left: 0;
overflow: visible;
white-space: normal;
}
.sidebar-notification-badge {
position: absolute;
top: -0.3rem;
right: -0.35rem;
min-width: 1rem;
height: 1rem;
padding: 0 0.25rem;
border-radius: var(--radius-pill);
background: var(--primary-color);
color: var(--primary-color-text);
font-size: 0.65rem;
font-weight: 700;
line-height: 1rem;
text-align: center;
pointer-events: none;
z-index: 1;
}
.sidebar-nav { .sidebar-nav {
flex: 1 1 auto; flex: 1 1 auto;
overflow-y: auto; overflow-y: auto;
@@ -219,7 +251,8 @@
flex-shrink: 0; flex-shrink: 0;
} }
span { .sidebar-action-label,
.sidebar-footer-action-label {
margin-left: var(--sidebar-row-label-offset); margin-left: var(--sidebar-row-label-offset);
overflow: hidden; overflow: hidden;
text-overflow: ellipsis; text-overflow: ellipsis;
@@ -241,7 +274,7 @@
padding-left: 0; padding-left: 0;
padding-right: 0; padding-right: 0;
span { .sidebar-footer-action-label {
display: none; display: none;
} }
@@ -454,7 +487,7 @@
} }
} }
.sidebar-footer-action span { .sidebar-footer-action .sidebar-footer-action-label {
display: inline-flex; display: inline-flex;
margin-left: 0; margin-left: 0;
font-size: var(--text-sm); font-size: var(--text-sm);
@@ -15,7 +15,10 @@ import { MagicShelfService } from '../../../features/magic-shelf/service/magic-s
import { SeriesDataService } from '../../../features/series-browser/service/series-data.service'; import { SeriesDataService } from '../../../features/series-browser/service/series-data.service';
import { UserService } from '../../../features/settings/user-management/user.service'; import { UserService } from '../../../features/settings/user-management/user.service';
import { CommandPaletteService } from '../../../features/command-palette/command-palette.service'; import { CommandPaletteService } from '../../../features/command-palette/command-palette.service';
import { BookdropFileService } from '../../../features/bookdrop/service/bookdrop-file.service';
import { AuthService } from '../../service/auth.service'; import { AuthService } from '../../service/auth.service';
import { MetadataBatchProgressNotification, MetadataBatchStatus } from '../../model/metadata-batch-progress.model';
import { MetadataProgressService } from '../../service/metadata-progress.service';
import { AppVersion, VersionService } from '../../service/version.service'; import { AppVersion, VersionService } from '../../service/version.service';
import { DialogLauncherService } from '../../services/dialog-launcher.service'; import { DialogLauncherService } from '../../services/dialog-launcher.service';
import { LayoutService } from '../layout.service'; import { LayoutService } from '../layout.service';
@@ -53,6 +56,9 @@ describe('AppSidebarComponent', () => {
let commandPaletteService: { open: ReturnType<typeof vi.fn> }; let commandPaletteService: { open: ReturnType<typeof vi.fn> };
let currentUser: WritableSignal<TestUser | null>; let currentUser: WritableSignal<TestUser | null>;
let versionInfo: BehaviorSubject<AppVersion>; let versionInfo: BehaviorSubject<AppVersion>;
let activeTasks$: BehaviorSubject<Record<string, MetadataBatchProgressNotification>>;
let progressUpdates$: BehaviorSubject<MetadataBatchProgressNotification>;
let hasPendingFiles$: BehaviorSubject<boolean>;
const sidebarCollapsed = signal(false); const sidebarCollapsed = signal(false);
const isDesktop = signal(true); const isDesktop = signal(true);
const layoutService = { const layoutService = {
@@ -69,6 +75,16 @@ describe('AppSidebarComponent', () => {
commandPaletteService = { open: vi.fn() }; commandPaletteService = { open: vi.fn() };
currentUser = signal<TestUser | null>(null); currentUser = signal<TestUser | null>(null);
versionInfo = new BehaviorSubject<AppVersion>({ current: '1.2.3', latest: '1.2.3' }); versionInfo = new BehaviorSubject<AppVersion>({ current: '1.2.3', latest: '1.2.3' });
activeTasks$ = new BehaviorSubject<Record<string, MetadataBatchProgressNotification>>({});
progressUpdates$ = new BehaviorSubject<MetadataBatchProgressNotification>({
taskId: 'initial',
completed: 0,
total: 1,
message: 'idle',
status: MetadataBatchStatus.COMPLETED,
review: false,
});
hasPendingFiles$ = new BehaviorSubject(false);
TestBed.configureTestingModule({ TestBed.configureTestingModule({
imports: [AppSidebarComponent, getTranslocoModule()], imports: [AppSidebarComponent, getTranslocoModule()],
@@ -96,6 +112,8 @@ describe('AppSidebarComponent', () => {
{ provide: CommandPaletteService, useValue: commandPaletteService }, { provide: CommandPaletteService, useValue: commandPaletteService },
{ provide: BookDialogHelperService, useValue: { openShelfCreatorDialog: vi.fn() } }, { provide: BookDialogHelperService, useValue: { openShelfCreatorDialog: vi.fn() } },
{ provide: AuthService, useValue: { logout: vi.fn() } }, { provide: AuthService, useValue: { logout: vi.fn() } },
{ provide: MetadataProgressService, useValue: { activeTasks$, progressUpdates$ } },
{ provide: BookdropFileService, useValue: { hasPendingFiles$ } },
{ provide: VersionService, useValue: { getVersion: vi.fn(() => versionInfo) } }, { provide: VersionService, useValue: { getVersion: vi.fn(() => versionInfo) } },
{ provide: LayoutService, useValue: layoutService }, { provide: LayoutService, useValue: layoutService },
{ provide: UserService, useValue: { currentUser } }, { provide: UserService, useValue: { currentUser } },
@@ -229,4 +247,61 @@ describe('AppSidebarComponent', () => {
expect(container.style.getPropertyValue('--sidebar-popover-top')).toBe('212px'); expect(container.style.getPropertyValue('--sidebar-popover-top')).toBe('212px');
expect(container.style.getPropertyValue('--sidebar-popover-left')).toBe(`${window.innerWidth - 128}px`); expect(container.style.getPropertyValue('--sidebar-popover-left')).toBe(`${window.innerWidth - 128}px`);
}); });
it('aggregates metadata tasks and pending bookdrop files into the sidebar badge count', () => {
const sidebar = component as unknown as {
completedTaskCount: number;
shouldShowNotificationBadge: boolean;
};
activeTasks$.next({
taskA: {
taskId: 'taskA',
completed: 1,
total: 2,
message: 'Scanning',
status: MetadataBatchStatus.COMPLETED,
review: false,
},
taskB: {
taskId: 'taskB',
completed: 2,
total: 2,
message: 'Importing',
status: MetadataBatchStatus.ERROR,
review: true,
},
});
hasPendingFiles$.next(true);
expect(sidebar.completedTaskCount).toBe(3);
expect(sidebar.shouldShowNotificationBadge).toBe(true);
});
it('hides the badge while metadata progress is actively running', () => {
const sidebar = component as unknown as {
shouldShowNotificationBadge: boolean;
};
activeTasks$.next({
taskA: {
taskId: 'taskA',
completed: 1,
total: 3,
message: 'Updating metadata',
status: MetadataBatchStatus.IN_PROGRESS,
review: false,
},
});
progressUpdates$.next({
taskId: 'taskA',
completed: 1,
total: 3,
message: 'Updating metadata',
status: MetadataBatchStatus.IN_PROGRESS,
review: false,
});
expect(sidebar.shouldShowNotificationBadge).toBe(false);
});
}); });
@@ -1,4 +1,4 @@
import { Component, Renderer2, RendererStyleFlags2, computed, inject, signal } from '@angular/core'; import { Component, DestroyRef, Renderer2, RendererStyleFlags2, computed, inject, signal } from '@angular/core';
import { Router, RouterLink } from '@angular/router'; import { Router, RouterLink } from '@angular/router';
import { AppSidebarSectionComponent } from './app.sidebar-section.component'; import { AppSidebarSectionComponent } from './app.sidebar-section.component';
import { MenuTrigger } from '@angular/aria/menu'; import { MenuTrigger } from '@angular/aria/menu';
@@ -30,7 +30,7 @@ import { AuthService } from '../../service/auth.service';
import { LayoutService } from '../layout.service'; import { LayoutService } from '../layout.service';
import { TranslocoDirective, TranslocoPipe, TranslocoService } from '@jsverse/transloco'; import { TranslocoDirective, TranslocoPipe, TranslocoService } from '@jsverse/transloco';
import { Tooltip } from 'primeng/tooltip'; import { Tooltip } from 'primeng/tooltip';
import { toSignal } from '@angular/core/rxjs-interop'; import { takeUntilDestroyed, toSignal } from '@angular/core/rxjs-interop';
import { NavItem, SidebarSection } from '../navigation/nav-item.model'; import { NavItem, SidebarSection } from '../navigation/nav-item.model';
import { buildCreateActionNavItems } from '../navigation/nav-catalog'; import { buildCreateActionNavItems } from '../navigation/nav-catalog';
import { import {
@@ -41,6 +41,9 @@ import {
buildToolsSection, buildToolsSection,
} from './sidebar-sections'; } from './sidebar-sections';
import { VersionService } from '../../service/version.service'; import { VersionService } from '../../service/version.service';
import { MetadataProgressService } from '../../service/metadata-progress.service';
import { BookdropFileService } from '../../../features/bookdrop/service/bookdrop-file.service';
import { MetadataBatchProgressNotification, MetadataBatchStatus } from '../../model/metadata-batch-progress.model';
const DOCUMENTATION_URL = 'https://grimmory.org/docs/getting-started'; const DOCUMENTATION_URL = 'https://grimmory.org/docs/getting-started';
@@ -125,6 +128,9 @@ export class AppSidebarComponent {
private readonly authorService = inject(AuthorService); private readonly authorService = inject(AuthorService);
private readonly t = inject(TranslocoService); private readonly t = inject(TranslocoService);
private readonly renderer = inject(Renderer2); private readonly renderer = inject(Renderer2);
private readonly destroyRef = inject(DestroyRef);
private readonly metadataProgressService = inject(MetadataProgressService);
private readonly bookdropFileService = inject(BookdropFileService);
readonly currentUser = this.userService.currentUser; readonly currentUser = this.userService.currentUser;
private readonly allAuthors = this.authorService.allAuthors; private readonly allAuthors = this.authorService.allAuthors;
@@ -207,17 +213,43 @@ export class AppSidebarComponent {
}); });
protected readonly notificationsOpen = signal(false); protected readonly notificationsOpen = signal(false);
protected progressHighlight = false;
protected completedTaskCount = 0;
protected hasPendingBookdropFiles = false;
protected readonly mobileMenuPositions = BELOW_ALIGN_LEFT; protected readonly mobileMenuPositions = BELOW_ALIGN_LEFT;
protected readonly aboveMenuPositions = ABOVE_ALIGN_LEFT; protected readonly aboveMenuPositions = ABOVE_ALIGN_LEFT;
protected readonly footerMenuPositions = computed(() => protected readonly footerMenuPositions = computed(() =>
this.layoutService.desktopSidebarCollapsed() ? BESIDE_RIGHT_BOTTOM : ABOVE_ALIGN_LEFT, this.layoutService.desktopSidebarCollapsed() ? BESIDE_RIGHT_BOTTOM : ABOVE_ALIGN_LEFT,
); );
private readonly latestTasks: Record<string, MetadataBatchProgressNotification> = {};
constructor() {
this.subscribeToMetadataProgress();
this.metadataProgressService.activeTasks$
.pipe(takeUntilDestroyed(this.destroyRef))
.subscribe((tasks) => {
this.replaceLatestTasks(tasks);
this.updateCompletedTaskCount();
});
this.bookdropFileService.hasPendingFiles$
.pipe(takeUntilDestroyed(this.destroyRef))
.subscribe((hasPending) => {
this.hasPendingBookdropFiles = hasPending;
this.updateCompletedTaskCount();
});
}
openSearch(): void { openSearch(): void {
this.commandPaletteService.open(); this.commandPaletteService.open();
} }
protected get shouldShowNotificationBadge(): boolean {
return this.completedTaskCount > 0 && !this.progressHighlight;
}
closeMobileSidebar(): void { closeMobileSidebar(): void {
this.layoutService.closeMobileSidebar(); this.layoutService.closeMobileSidebar();
} }
@@ -333,4 +365,25 @@ export class AppSidebarComponent {
} }
overlay.toggle(event); overlay.toggle(event);
} }
private subscribeToMetadataProgress(): void {
this.metadataProgressService.progressUpdates$
.pipe(takeUntilDestroyed(this.destroyRef))
.subscribe((progress) => {
this.progressHighlight = progress.status === MetadataBatchStatus.IN_PROGRESS;
});
}
private replaceLatestTasks(tasks: Record<string, MetadataBatchProgressNotification>): void {
for (const key of Object.keys(this.latestTasks)) {
delete this.latestTasks[key];
}
Object.assign(this.latestTasks, tasks);
}
private updateCompletedTaskCount(): void {
const metadataTaskCount = Object.keys(this.latestTasks).length;
const bookdropFileTaskCount = this.hasPendingBookdropFiles ? 1 : 0;
this.completedTaskCount = metadataTaskCount + bookdropFileTaskCount;
}
} }
@@ -118,7 +118,7 @@ body.layout-resizing-cursor {
.sidebar-item-chevron, .sidebar-item-chevron,
.entity-menu-button, .entity-menu-button,
.sidebar-item-end-content, .sidebar-item-end-content,
.sidebar-action-link span, .sidebar-footer-action-label,
.sidebar-user-trigger .sidebar-user-meta, .sidebar-user-trigger .sidebar-user-meta,
.sidebar-search-shortcut { .sidebar-search-shortcut {
display: none; display: none;