All files / app/features/history/pages/history-page history.component.ts

100% Statements 33/33
100% Branches 22/22
100% Functions 6/6
100% Lines 29/29

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 641x 1x 1x 1x 1x 1x   1x                           1x 9x 9x 9x 9x   13x   9x 12x   3x 3x   2x 8x 8x         9x 10x 1x 1x           8x   2x   2x   2x   2x        
import { AuthService } from '../../../../services/auth/auth.service';
import { HistoryCounts, HistoryCountsService } from '../../../../services/counts/history-counts.service';
import { ChangeDetectionStrategy, Component, computed, effect, inject } from '@angular/core';
import { toSignal } from '@angular/core/rxjs-interop';
import { RouterOutlet } from '@angular/router';
import { TabItem, TabsComponent } from '@drevo-web/ui';
 
const BASE_TABS: readonly TabItem[] = [
    { label: 'Статьи', route: '/history/articles', testId: 'history-tab-articles' },
    { label: 'Новости', route: '/history/news', testId: 'history-tab-news' },
    { label: 'Сообщения', route: '/history/forum', testId: 'history-tab-forum' },
    { label: 'Иллюстрации', route: '/history/pictures', testId: 'history-tab-pictures' },
];
 
@Component({
    selector: 'app-history',
    imports: [TabsComponent, RouterOutlet],
    templateUrl: './history.component.html',
    styleUrl: './history.component.scss',
    changeDetection: ChangeDetectionStrategy.OnPush,
})
export class HistoryComponent {
    private readonly authService = inject(AuthService);
    private readonly historyCountsService = inject(HistoryCountsService);
    private readonly user = toSignal(this.authService.user$);
    private countsLoaded = false;
 
    readonly canModerate = computed(() => this.user()?.permissions.canModerate ?? false);
 
    readonly tabs = computed<TabItem[]>(() => {
        if (!this.canModerate()) return [...BASE_TABS];
 
        const counts = this.historyCountsService.counts();
        if (!counts) return [...BASE_TABS];
 
        return BASE_TABS.map(tab => {
            const badge = this.getBadge(tab.route, counts);
            return badge > 0 ? { ...tab, badge } : tab;
        });
    });
 
    constructor() {
        effect(() => {
            if (this.canModerate() && !this.countsLoaded) {
                this.countsLoaded = true;
                this.historyCountsService.loadCounts();
            }
        });
    }
 
    private getBadge(route: string, counts: HistoryCounts): number {
        switch (route) {
            case '/history/articles':
                return counts.pendingArticles;
            case '/history/news':
                return counts.pendingNews;
            case '/history/pictures':
                return counts.pendingPictures;
            default:
                return 0;
        }
    }
}