All files / app/page/ref/errors errors.component.ts

80.85% Statements 38/47
75% Branches 15/20
53.33% Functions 8/15
78.94% Lines 30/38

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 942x             2x                     2x 2x 2x                   3x   1x       1x         1x 1x 1x 1x 1x 1x 1x 1x   1x 1x 1x 1x 1x 2x               2x 2x     1x 1x 1x                                   1x 3x 1x        
import { Component, ViewChild, ChangeDetectionStrategy } from '@angular/core';
import { takeUntilDestroyed } from '@angular/core/rxjs-interop';
import { defer } from 'lodash-es';
import { autorun, IReactionDisposer, runInAction } from 'mobx';
import { MobxAngularModule } from 'mobx-angular';
import { catchError, filter, of, Subject, Subscription, switchMap } from 'rxjs';
import { tap } from 'rxjs/operators';
import { RefListComponent } from '../../../component/ref/ref-list/ref-list.component';
import { HasChanges } from '../../../guard/pending-changes.guard';
import { Ref } from '../../../model/ref';
import { AdminService } from '../../../service/admin.service';
import { RefService } from '../../../service/api/ref.service';
import { StompService } from '../../../service/api/stomp.service';
import { BookmarkService } from '../../../service/bookmark.service';
import { ConfigService } from '../../../service/config.service';
import { ModService } from '../../../service/mod.service';
import { QueryStore } from '../../../store/query';
import { Store } from '../../../store/store';
import { getTitle } from '../../../util/format';
import { getArgs } from '../../../util/query';
import { hasTag, updateMetadata } from '../../../util/tag';
 
@Component({
  selector: 'app-ref-errors',
  templateUrl: './errors.component.html',
  styleUrl: './errors.component.scss',
  host: { 'class': 'errors' },
  changeDetection: ChangeDetectionStrategy.Eager,
  imports: [MobxAngularModule, RefListComponent]
})
export class RefErrorsComponent implements HasChanges {
 
  private disposers: IReactionDisposer[] = [];
  @ViewChild('list')
  list?: RefListComponent;
 
  newRefs$ = new Subject<Ref | undefined>();
 
  private watch?: Subscription;
 
  constructor(
    public config: ConfigService,
    private mod: ModService,
    public admin: AdminService,
    public store: Store,
    public query: QueryStore,
    private stomp: StompService,
    private refs: RefService,
    private bookmarks: BookmarkService,
  ) {
    query.clear();
    runInAction(() => store.view.defaultSort = ['published']);
    Eif (!this.store.view.filter.length) bookmarks.filters = ['query/' + (store.account.origin || '*')];
    const untilDestroyed = takeUntilDestroyed<Ref | undefined>();
    this.disposers.push(autorun(() => {
      const args = getArgs(
        '+plugin/log:!plugin/delete',
        this.store.view.sort,
        this.store.view.filter,
        this.store.view.search,
        this.store.view.pageNumber,
        this.store.view.pageSize,
      );
      args.responses = this.store.view.url;
      defer(() => this.query.setArgs(args));
    }));
    // TODO: set title for bare reposts
    this.disposers.push(autorun(() => this.mod.setTitle($localize`Errors: ` + getTitle(this.store.view.ref))));
    this.disposers.push(autorun(() => {
      Iif (this.store.view.url && this.config.websockets) {
        this.watch?.unsubscribe();
        this.watch = this.stomp.watchResponse(this.store.view.url).pipe(
          switchMap(url => this.refs.getCurrent(url)),
          tap(ref => runInAction(() => updateMetadata(this.store.view.ref!, ref))),
          filter(ref => hasTag('+plugin/log', ref)),
          catchError(err => of(undefined)),
          untilDestroyed,
        ).subscribe(ref => this.newRefs$.next(ref));
      }
    }));
  }
 
  saveChanges() {
    return !this.list || this.list.saveChanges();
  }
 
  ngOnDestroy() {
    this.query.close();
    for (const dispose of this.disposers) dispose();
    this.disposers.length = 0;
  }
 
}