From 95adfd735d658ff1ad8fcdd2086c64d5bbf74b6f Mon Sep 17 00:00:00 2001 From: Dennis Walker Date: Thu, 26 Jun 2025 04:11:06 +0600 Subject: [PATCH] Add icon in Storybook --- .../entity-logo/entity-logo.component.html | 4 +- .../entity-logo.component.stories.ts | 59 +++++++++++++++++++ .../lib/entity-logo/entity-logo.component.ts | 32 ++++++++-- 3 files changed, 90 insertions(+), 5 deletions(-) create mode 100644 libs/ui/src/lib/entity-logo/entity-logo.component.stories.ts diff --git a/libs/ui/src/lib/entity-logo/entity-logo.component.html b/libs/ui/src/lib/entity-logo/entity-logo.component.html index f0abad285..692fb1d3b 100644 --- a/libs/ui/src/lib/entity-logo/entity-logo.component.html +++ b/libs/ui/src/lib/entity-logo/entity-logo.component.html @@ -1,4 +1,6 @@ -@if (src) { +@if (useLogo) { + +} @else if (src) { ; + +type Story = StoryObj; + +export const Default: Story = { + args: { + dataSource: DataSource.MANUAL, + symbol: 'GHOST', + useLogo: true, + size: 'large' + } +}; + +export const WithUrl: Story = { + args: { + size: 'large', + tooltip: 'Ghostfolio', + url: 'https://ghostfol.io', + useLogo: true + } +}; diff --git a/libs/ui/src/lib/entity-logo/entity-logo.component.ts b/libs/ui/src/lib/entity-logo/entity-logo.component.ts index 7598fb4d5..4d2f56b26 100644 --- a/libs/ui/src/lib/entity-logo/entity-logo.component.ts +++ b/libs/ui/src/lib/entity-logo/entity-logo.component.ts @@ -1,17 +1,21 @@ import { CommonModule } from '@angular/common'; +import { HttpClient } from '@angular/common/http'; import { - CUSTOM_ELEMENTS_SCHEMA, ChangeDetectionStrategy, + ChangeDetectorRef, Component, Input, OnChanges } from '@angular/core'; import { DataSource } from '@prisma/client'; +import { of } from 'rxjs'; +import { catchError } from 'rxjs/operators'; + +import { GfLogoComponent } from '../logo/logo.component'; @Component({ changeDetection: ChangeDetectionStrategy.OnPush, - imports: [CommonModule], - schemas: [CUSTOM_ELEMENTS_SCHEMA], + imports: [CommonModule, GfLogoComponent], selector: 'gf-entity-logo', styleUrls: ['./entity-logo.component.scss'], templateUrl: './entity-logo.component.html' @@ -22,14 +26,34 @@ export class GfEntityLogoComponent implements OnChanges { @Input() symbol: string; @Input() tooltip: string; @Input() url: string; + @Input() useLogo = false; public src: string; + public constructor( + private readonly changeDetectorRef: ChangeDetectorRef, + private readonly http: HttpClient + ) {} + public ngOnChanges() { + if (this.useLogo) { + return; + } + if (this.dataSource && this.symbol) { this.src = `../api/v1/logo/${this.dataSource}/${this.symbol}`; } else if (this.url) { - this.src = `../api/v1/logo?url=${this.url}`; + this.http + .get<{ logoUrl: string }>(`../api/v1/logo?url=${this.url}`) + .pipe( + catchError(() => { + return of({ logoUrl: '' }); + }) + ) + .subscribe(({ logoUrl }) => { + this.src = logoUrl; + this.changeDetectorRef.markForCheck(); + }); } } }