All files / app/features/article/components/article-versions article-versions.component.ts

97.61% Statements 41/42
83.33% Branches 10/12
92.3% Functions 12/13
97.14% Lines 34/35

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 791x       1x 1x   1x 1x 1x                     1x 11x 11x 11x   11x 14x 17x 13x   11x   11x 11x       11x 11x   11x       11x               12x 12x   12x 1x 11x 10x   1x 2x 1x           2x 1x         1x 1x      
import {
    ArticleHistoryService,
    HistoryFilter,
} from '../../../../services/articles/article-history/article-history.service';
import { ArticleHistoryListComponent } from '../../../../shared/components/article-history-list/article-history-list.component';
import { FiltersSidePanelComponent } from '../../../../shared/components/filters/filters-side-panel/filters-side-panel.component';
import { FilterEntry } from '../../../../shared/models/filter.model';
import { ArticlePageService } from '../../services/article-page.service';
import { ChangeDetectionStrategy, Component, computed, HostListener, inject, OnInit, signal } from '@angular/core';
import { Router } from '@angular/router';
import { ArticleHistoryItem } from '@drevo-web/shared';
 
@Component({
    selector: 'app-article-versions',
    imports: [ArticleHistoryListComponent, FiltersSidePanelComponent],
    templateUrl: './article-versions.component.html',
    styleUrl: './article-versions.component.scss',
    changeDetection: ChangeDetectionStrategy.OnPush,
    providers: [ArticleHistoryService],
})
export class ArticleVersionsComponent implements OnInit {
    private readonly service = inject(ArticleHistoryService);
    private readonly articlePageService = inject(ArticlePageService);
    private readonly router = inject(Router);
 
    private readonly _selectedVersionIds = signal<readonly number[]>([]);
    readonly selectedVersionIds = computed(() => new Set(this._selectedVersionIds()));
    readonly selectedCount = computed(() => this._selectedVersionIds().length);
    readonly canCompare = computed(() => this.selectedCount() === 2);
 
    readonly activeFilter = this.service.activeFilter;
 
    readonly filters = computed<readonly FilterEntry<HistoryFilter>[]>(() => {
        const entries: FilterEntry<HistoryFilter>[] = [
            { key: 'all', label: 'Все' },
            { key: 'unchecked', label: 'Непроверенные' },
        ];
        Eif (this.service.isAuthenticated()) {
            entries.push({ key: 'my', label: 'Мои' });
        }
        return entries;
    });
 
    ngOnInit(): void {
        this.service.init({ articleId: this.articlePageService.articleId });
    }
 
    onFilterChange(filter: HistoryFilter): void {
        this.service.onFilterChange(filter);
    }
 
    onSelectItem(item: ArticleHistoryItem): void {
        const ids = this._selectedVersionIds();
        const index = ids.indexOf(item.versionId);
 
        if (index !== -1) {
            this._selectedVersionIds.set(ids.filter(id => id !== item.versionId));
        } else if (ids.length < 2) {
            this._selectedVersionIds.set([...ids, item.versionId]);
        } else {
            const minId = Math.min(...ids);
            const remaining = ids.find(id => id !== minId) ?? ids[0];
            this._selectedVersionIds.set([remaining, item.versionId]);
        }
    }
 
    @HostListener('document:keydown.escape')
    onEscapePress(): void {
        if (this.selectedCount() > 0) {
            this._selectedVersionIds.set([]);
        }
    }
 
    onCompare(): void {
        const [older, newer] = [...this._selectedVersionIds()].sort((a, b) => a - b);
        this.router.navigate(['/history/articles/diff', older, newer]);
    }
}