import { Component, computed, effect, inject, input, output, signal, } from '@angular/core'; import { Topic, TopicsV2Service } from '../../../api'; import { MatChipsModule } from '@angular/material/chips'; import { ComponentTooltipDirective } from '../../../directives/component-tooltip.directive'; import { topicHierarchyColor } from '../../../resolvers/utils/topic-colors'; import { MatIconModule } from '@angular/material/icon'; import { AppTopicTooltipComponent } from '../app-topic-tooltip/app-topic-tooltip.component'; import { SkTextDirective } from '../../../custom-directives/sk-text.directive'; @Component({ host: { '[style.--topic-color]': 'color()', '[style.--border-width]': 'isSelected() ? "3px" : "1px"', '[class.is-clickable]': 'isClickable()', '[class.is-selected]': 'isSelected()', '[class.is-mini-size]': 'isMiniSize()', }, selector: 'app-topic-chip-2', imports: [ MatChipsModule, ComponentTooltipDirective, MatIconModule, SkTextDirective, ], templateUrl: './app-topic-chip.component.html', styleUrl: './app-topic-chip.component.scss', }) export class AppTopicChipComponent { readonly AppTopicTooltipComponent = AppTopicTooltipComponent; private readonly topicsService = inject(TopicsV2Service); topic = input.required(); rawOaTopicId = computed(() => { return this.stripOpenAlexId(this.topic()?.id); }); topicId = input(undefined); _topic = signal(undefined); size = input<'normal' | 'mini'>('normal'); isMiniSize = computed(() => this.size() === 'mini'); isSelected = input(false); isClickable = input(false); handleChipClick = output(); showIcon = input(false); icon = input('topic'); showCount = input(false); count = input(0); constructor() { effect(() => { const topic = this.topic(); const topicId = this.topicId(); if (!topic && !topicId) { this._topic.set(undefined); } if (topic) { this._topic.set(topic); } else if (topicId) { this.topicsService.getTopicById(topicId).subscribe((fetchedTopic) => { this._topic.set(fetchedTopic); }); } }); } color = computed(() => { const topic = this.topic(); return topicHierarchyColor({ id: topic.id ?? undefined, name: topic.display_name ?? undefined, field_id: topic.field?.id ?? undefined, field_name: topic.field?.display_name ?? undefined, domain_id: topic.domain?.id ?? undefined, domain_name: topic.domain?.display_name ?? undefined, }) .replace('hsl(', '') .replace(')', ''); }); _onChipClick(event: MouseEvent | KeyboardEvent) { event.stopPropagation(); const topic = this.topic(); if (this.isClickable() && this.handleChipClick && topic) { this.handleChipClick.emit(topic); } } stripOpenAlexId(id: string | null | undefined): string | undefined { if (!id) { return undefined; } return id.replace('https://openalex.org/', ''); } }