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

97.22% Statements 35/36
93.75% Branches 15/16
100% Functions 8/8
100% Lines 34/34

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 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 1041x 1x 1x 1x 1x   1x 1x                 1x 12x 12x 12x     12x   14x       14x 11x   3x         12x   33x 3x         12x 10x   9x   10x 10x 10x 10x     12x 12x   12x 11x 11x 1x   10x 10x                                                                              
import { ErrorComponent } from '../../../../shared/components/error/error.component';
import { ArticlePageService } from '../../services/article-page.service';
import { ChangeDetectionStrategy, Component, computed, inject } from '@angular/core';
import { takeUntilDestroyed, toSignal } from '@angular/core/rxjs-interop';
import { ActivatedRoute, NavigationEnd, Router, RouterOutlet } from '@angular/router';
import { ArticleVersion } from '@drevo-web/shared';
import { TabGroup, TabsGroupComponent } from '@drevo-web/ui';
import { filter, map } from 'rxjs';
 
@Component({
    selector: 'app-article',
    imports: [ErrorComponent, TabsGroupComponent, RouterOutlet],
    templateUrl: './article.component.html',
    styleUrl: './article.component.scss',
    changeDetection: ChangeDetectionStrategy.OnPush,
})
export class ArticleComponent {
    private readonly pageService = inject(ArticlePageService);
    private readonly route = inject(ActivatedRoute);
    private readonly router = inject(Router);
 
    constructor() {
        this.route.data
            .pipe(
                map(data => data['article'] as ArticleVersion | undefined),
                takeUntilDestroyed()
            )
            .subscribe(article => {
                if (article) {
                    this.pageService.setArticle(article);
                } else {
                    this.pageService.setError('Ошибка загрузки статьи');
                }
            });
    }
 
    private readonly url = toSignal(
        this.router.events.pipe(
            filter((e): e is NavigationEnd => e instanceof NavigationEnd),
            map(e => e.urlAfterRedirects)
        ),
        { initialValue: this.router.url }
    );
 
    private readonly articleTabActive = computed(() => {
        const path = this.router
            .parseUrl(this.url())
            .root.children['primary']?.segments.map(s => s.path)
            .join('/');
        const id = this.pageService.articleId();
        Iif (!id) return false;
        const base = `articles/${id}`;
        return path === base || path?.startsWith(`${base}/version/`);
    });
 
    readonly article = this.pageService.article;
    readonly error = this.pageService.error;
 
    readonly tabGroups = computed<TabGroup[]>(() => {
        const id = this.pageService.articleId();
        if (!id) {
            return [];
        }
        const base = `/articles/${id}`;
        return [
            {
                items: [
                    {
                        label: 'Статья',
                        route: base,
                        icon: 'article',
                        isActive: this.articleTabActive,
                    },
                    {
                        label: 'Новости',
                        route: `${base}/news`,
                        icon: 'newspaper',
                    },
                    {
                        label: 'Обсуждение',
                        route: `${base}/forum`,
                        icon: 'forum',
                    },
                ],
            },
            {
                items: [
                    {
                        label: 'Версии',
                        route: `${base}/history`,
                        icon: 'history',
                    },
                    {
                        label: 'Кто ссылается',
                        route: `${base}/linkedhere`,
                        icon: 'link',
                    },
                ],
                align: 'end',
            },
        ];
    });
}