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

75.51% Statements 37/49
59.09% Branches 13/22
73.33% Functions 11/15
81.81% Lines 27/33

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 871x       1x   1x           1x 1x                     3x 1x         1x     1x 1x 1x 1x   1x 1x               1x 1x   1x 1x               1x 1x   1x 1x                 1x       1x 4x 1x       1x        
import { Component, OnDestroy, OnInit, ViewChild } from '@angular/core';
import { defer, uniq } from 'lodash-es';
import { autorun, IReactionDisposer, runInAction } from 'mobx';
import { MobxAngularModule } from 'mobx-angular';
import { RefListComponent } from '../../../component/ref/ref-list/ref-list.component';
import { HasChanges } from '../../../guard/pending-changes.guard';
import { Page } from '../../../model/page';
import { Ref } from '../../../model/ref';
import { AdminService } from '../../../service/admin.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';
 
@Component({
  selector: 'app-ref-sources',
  templateUrl: './sources.component.html',
  styleUrls: ['./sources.component.scss'],
  imports: [
    MobxAngularModule,
    RefListComponent,
  ],
})
export class RefSourcesComponent implements OnInit, OnDestroy, HasChanges {
  private disposers: IReactionDisposer[] = [];
 
  @ViewChild('list')
  list?: RefListComponent;
 
  page: Page<Ref> = Page.of([]);
 
  constructor(
    private mod: ModService,
    public admin: AdminService,
    public store: Store,
    public query: QueryStore,
  ) {
    query.clear();
    runInAction(() => store.view.defaultSort = ['published']);
  }
 
  saveChanges() {
    return !this.list || this.list.saveChanges();
  }
 
  ngOnInit(): void {
    this.disposers.push(autorun(() => {
      this.page = Page.of(this.sources.map(url => ({ url })) || []);
    }));
    this.disposers.push(autorun(() => {
      const args = getArgs(
        '',
        this.store.view.sort,
        this.store.view.filter,
        this.store.view.search,
        this.store.view.pageNumber,
        this.store.view.pageSize,
      );
      args.sources = this.store.view.url;
      defer(() => this.query.setArgs(args));
    }));
    this.disposers.push(autorun(() => {
      Eif (!this.query.page) return;
      for (let i = 0; i < this.sources.length; i ++) {
        if (this.page.content[i].created) continue;
        const url = this.sources[i];
        const existing = this.query.page.content.find(r => r.url === url);
        if (existing) this.page.content[i] = existing;
      }
    }));
    // TODO: set title for bare reposts
    this.disposers.push(autorun(() => this.mod.setTitle($localize`Sources: ` + getTitle(this.store.view.ref))));
  }
 
  ngOnDestroy() {
    this.query.close();
    for (const dispose of this.disposers) dispose();
    this.disposers.length = 0;
  }
 
  get sources() {
    return uniq(this.store.view.ref?.sources).filter(s => s != this.store.view.url);
  }
 
}