Skip to content

Open Source Wealth Management Software. Angular + NestJS + Prisma + Nx + TypeScript 🤍

2.5 KB
Raw Blame
import { internalRoutes } from '@ghostfolio/common/routes/routes';

import { FocusableOption } from '@angular/cdk/a11y';
import {
  ChangeDetectionStrategy,
  ChangeDetectorRef,
  Component,
  ElementRef,
  HostBinding,
  Input,
  OnChanges,
  ViewChild,
  inject,
  output
} from '@angular/core';
import { Params, RouterModule } from '@angular/router';

import { SearchMode } from '../enums/search-mode';
import {
  AssetSearchResultItem,
  SearchResultItem
} from '../interfaces/interfaces';

@Component({
  changeDetection: ChangeDetectionStrategy.OnPush,
  imports: [RouterModule],
  selector: 'gf-assistant-list-item',
  styleUrls: ['./assistant-list-item.scss'],
  templateUrl: './assistant-list-item.html'
})
export class GfAssistantListItemComponent
  implements FocusableOption, OnChanges
{
  @HostBinding('attr.tabindex') tabindex = -1;

  @Input() item: SearchResultItem;

  @ViewChild('link') public linkElement: ElementRef<HTMLAnchorElement>;

  public hasFocus = false;
  public queryParams: Params;
  public routerLink: string[];

  protected readonly clicked = output<void>();

  private readonly changeDetectorRef = inject(ChangeDetectorRef);

  @HostBinding('class.has-focus')
  public get getHasFocus() {
    return this.hasFocus;
  }

  public ngOnChanges() {
    if (this.item?.mode === SearchMode.ACCOUNT) {
      this.queryParams = {};
      this.routerLink = this.item.routerLink;
    } else if (this.item?.mode === SearchMode.ASSET_PROFILE) {
      this.queryParams = {};

      this.routerLink =
        internalRoutes.adminControl.subRoutes.marketData.subRoutes.update.routerLink(
          this.item.dataSource,
          this.item.symbol
        );
    } else if (this.item?.mode === SearchMode.HOLDING) {
      this.queryParams = {
        dataSource: this.item.dataSource,
        holdingDetailDialog: true,
        symbol: this.item.symbol
      };

      this.routerLink = [];
    } else if (this.item?.mode === SearchMode.QUICK_LINK) {
      this.queryParams = {};
      this.routerLink = this.item.routerLink;
    }
  }

  public focus() {
    this.hasFocus = true;

    this.changeDetectorRef.markForCheck();
  }

  public isAsset(item: SearchResultItem): item is AssetSearchResultItem {
    return (
      (item.mode === SearchMode.ASSET_PROFILE ||
        item.mode === SearchMode.HOLDING) &&
      !!item.dataSource &&
      !!item.symbol
    );
  }

  public onClick() {
    this.clicked.emit();
  }

  public removeFocus() {
    this.hasFocus = false;

    this.changeDetectorRef.markForCheck();
  }
}