All files / app/component/search search.component.ts

37.7% Statements 23/61
20.68% Branches 12/58
41.66% Functions 5/12
33.33% Lines 17/51

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 988x                                   8x   1x   1x 1x   1x     1x 1x 1x   1x 1x   1x           1x 1x                   1x                                   3x                                               3x      
import { Component, OnDestroy } from '@angular/core';
import { ReactiveFormsModule } from '@angular/forms';
import { NavigationEnd, Router } from '@angular/router';
import { debounce } from 'lodash-es';
import { autorun, IReactionDisposer, toJS } from 'mobx';
import { MobxAngularModule } from 'mobx-angular';
import { filter } from 'rxjs';
import { AdminService } from '../../service/admin.service';
import { Store } from '../../store/store';
import { View } from '../../store/view';
 
@Component({
  selector: 'app-search',
  templateUrl: './search.component.html',
  styleUrls: ['./search.component.scss'],
  host: { 'class': 'search form-group' },
  imports: [MobxAngularModule, ReactiveFormsModule]
})
export class SearchComponentI implements OnDestroy {
 
  private disposers: IReactionDisposer[] = [];
 
  searchValue = '';
  replace = false;
 
  private searchEvent = false;
 
  constructor(
    public router: Router,
    public store: Store,
    public admin: AdminService,
  ) {
    this.disposers.push(autorun(() => {
      this.searchValue = toJS(this.store.view.search) || '';
    }));
    router.events.pipe(
      filter(event => event instanceof NavigationEnd),
    ).subscribe(() => this.replace = false);
  }
 
  ngOnDestroy() {
    for (const dispose of this.disposers) dispose();
    this.disposers.length = 0;
  }
 
  change(target: HTMLInputElement) {
    this.searchValue = target.value || '';
    if (this.searchEvent) return;
    if (!this.store.account.config.liveSearch) return;
    this.debounceSearch();
  }
 
  debounceSearch = debounce(() => this.doSearch(), 400);
 
  search() {
    this.searchEvent = true;
    this.doSearch();
  }
 
  submit() {
    if (this.searchEvent) return;
    this.doSearch();
  }
 
  doSearch() {
    this.router.navigate([], { queryParams: { search: this.searchValue }, queryParamsHandling: 'merge', replaceUrl: this.replace });
    this.replace ||= !!this.searchValue;
  }
 
  viewName(view?: View) {
    switch (view) {
      case 'tag': return this.store.view.ext?.name || this.admin.getPlugin(this.store.view.tag)?.name || this.store.view.tag;
      case 'query': return $localize`query results`;
      case 'home': return this.store.account.signedIn ? $localize`subscriptions` : $localize`home page`;
      case 'all': return $localize`all`;
      case 'local': return $localize`local`;
      case 'inbox/all': return $localize`my inbox`;
      case 'inbox/sent': return $localize`sent by me`;
      case 'inbox/alarms': return $localize`alarms`;
      case 'inbox/dms': return $localize`direct messages`;
      case 'inbox/modlist': return $localize`unmoderated`;
      case 'inbox/reports': return $localize`flagged`;
      case 'inbox/ref': return this.admin.getPlugin(this.store.view.inboxTag)?.name || this.store.view.inboxTag;
      case 'ref/thread': return $localize`thread`;
      case 'ref/comments': return $localize`comments`;
      case 'ref/responses': return $localize`responses`;
      case 'ref/sources': return $localize`sources`;
      case 'ref/versions': return $localize`versions`;
      case 'ref/errors': return $localize`errors`;
      case 'settings/user': return $localize`permissions`;
      case 'settings/plugin': return $localize`plugins`;
      case 'settings/template': return $localize`templates`;
      case 'settings/ref': return this.admin.getPlugin(this.store.view.settingsTag)?.name || this.store.view.settingsTag;
    }
    return view || '';
  }
}