Files
smartguide/webapp/src/app/modules/person-interesting/person-interesting.component.ts
T

228 lines
6.2 KiB
TypeScript

import {
Component,
DestroyRef,
OnInit,
computed,
inject,
input,
output,
signal,
} from '@angular/core';
import { takeUntilDestroyed } from '@angular/core/rxjs-interop';
import { MatIconModule } from '@angular/material/icon';
import { MatProgressSpinnerModule } from '@angular/material/progress-spinner';
import { CommonModule } from '@angular/common';
import { ActivatedRoute, Router } from '@angular/router';
import { TopicCount } from '../../api';
import { TopicChipComponent } from '../../shared/ui/topic-chip/topic-chip.component';
import { TopicLegendComponent } from '../../shared/ui/topic-legend/topic-legend.component';
import { topicHierarchyColor } from '../../resolvers/utils/topic-colors';
import {
buildTopicLegendGroups,
getTopicLegendFieldId,
getTopicLegendSubfieldId,
TopicLegendGroup,
TopicLegendSubfieldSelection,
toggleTopicLegendFieldSelection,
toggleTopicLegendSubfieldSelection,
} from '../../resolvers/utils/topic-legend';
interface InterestTag {
id: string;
label: string;
count: number;
description: string;
color: string;
subfieldId: string;
subfieldName: string;
subfieldDescription: string;
fieldId: string;
fieldName: string;
fieldDescription: string;
domainId: string;
domainName: string;
domainDescription: string;
}
const MAX_TAGS = 5;
@Component({
selector: 'app-person-interesting',
imports: [
CommonModule,
MatIconModule,
MatProgressSpinnerModule,
TopicChipComponent,
TopicLegendComponent,
],
templateUrl: './person-interesting.component.html',
styleUrl: './person-interesting.component.scss',
})
export class PersonInterestingComponent implements OnInit {
private static readonly legendExpandedQueryParam =
'interesting_legend_expanded';
private readonly route = inject(ActivatedRoute);
private readonly router = inject(Router);
private readonly destroyRef = inject(DestroyRef);
readonly topics = input<TopicCount[] | null>(null);
readonly loading = input<boolean>(false);
readonly legendExpanded = signal(false);
readonly activeLegendFieldIds = signal<ReadonlySet<string>>(new Set());
readonly activeLegendSubfieldIds = signal<ReadonlySet<string>>(new Set());
readonly tagClick = output<{
id: string;
name: string;
fieldName?: string;
domainName?: string;
}>();
readonly tags = computed<InterestTag[]>(() => {
const topicsList = this.topics() ?? [];
const tags: InterestTag[] = [];
for (const topic of topicsList) {
const label = (topic.name ?? '').trim();
if (!label) continue;
tags.push({
id: topic.id ?? '',
label,
count: topic.count ?? 0,
description: (topic.description ?? '').trim(),
color: topicHierarchyColor(topic),
subfieldId: topic.subfield_id ?? '',
subfieldName: (topic.subfield_name ?? '').trim(),
subfieldDescription: (topic.subfield_description ?? '').trim(),
fieldId: topic.field_id ?? '',
fieldName: (topic.field_name ?? '').trim(),
fieldDescription: (topic.field_description ?? '').trim(),
domainId: topic.domain_id ?? '',
domainName: (topic.domain_name ?? '').trim(),
domainDescription: (topic.domain_description ?? '').trim(),
});
}
return tags.slice(0, MAX_TAGS).sort((a, b) => b.count - a.count);
});
readonly legendGroups = computed(() =>
buildTopicLegendGroups(
this.tags().map((tag) => ({
id: tag.id,
name: tag.label,
subfield_id: tag.subfieldId,
subfield_name: tag.subfieldName,
subfield_description: tag.subfieldDescription,
field_id: tag.fieldId,
field_name: tag.fieldName,
field_description: tag.fieldDescription,
domain_id: tag.domainId,
domain_name: tag.domainName,
domain_description: tag.domainDescription,
})),
),
);
ngOnInit(): void {
this.route.queryParamMap
.pipe(takeUntilDestroyed(this.destroyRef))
.subscribe((queryParams) => {
this.legendExpanded.set(
this.parseBooleanQueryParam(
queryParams.get(
PersonInterestingComponent.legendExpandedQueryParam,
),
false,
),
);
});
}
onTagClick(tag: InterestTag): void {
if (!tag.id) return;
this.tagClick.emit({
id: tag.id,
name: tag.label,
fieldName: tag.fieldName || undefined,
domainName: tag.domainName || undefined,
});
}
onLegendExpandedChange(expanded: boolean): void {
this.legendExpanded.set(expanded);
void this.router.navigate([], {
relativeTo: this.route,
queryParams: {
[PersonInterestingComponent.legendExpandedQueryParam]: expanded
? 'true'
: null,
},
queryParamsHandling: 'merge',
replaceUrl: true,
});
}
onLegendFieldToggle(group: TopicLegendGroup): void {
const selection = toggleTopicLegendFieldSelection(
this.activeLegendFieldIds(),
this.activeLegendSubfieldIds(),
group,
);
this.activeLegendFieldIds.set(selection.fieldIds);
this.activeLegendSubfieldIds.set(selection.subfieldIds);
}
onLegendSubfieldToggle(selection: TopicLegendSubfieldSelection): void {
const nextSelection = toggleTopicLegendSubfieldSelection(
this.activeLegendFieldIds(),
this.activeLegendSubfieldIds(),
selection,
);
this.activeLegendFieldIds.set(nextSelection.fieldIds);
this.activeLegendSubfieldIds.set(nextSelection.subfieldIds);
}
isTagActive(tag: InterestTag): boolean {
const fieldLegendId = getTopicLegendFieldId({
field_id: tag.fieldId,
field_name: tag.fieldName,
});
const subfieldLegendId = getTopicLegendSubfieldId({
subfield_id: tag.subfieldId,
subfield_name: tag.subfieldName,
});
return (
this.activeLegendFieldIds().has(fieldLegendId) ||
(subfieldLegendId.length > 0 &&
this.activeLegendSubfieldIds().has(subfieldLegendId))
);
}
private parseBooleanQueryParam(
value: string | null,
fallback: boolean,
): boolean {
if (value === null) {
return fallback;
}
if (value === 'true' || value === '1') {
return true;
}
if (value === 'false' || value === '0') {
return false;
}
return fallback;
}
}