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

217 lines
6.5 KiB
TypeScript

import { Component, computed, inject, signal, DestroyRef } from '@angular/core';
import {
toSignal,
toObservable,
takeUntilDestroyed,
} from '@angular/core/rxjs-interop';
import { MatIconModule } from '@angular/material/icon';
import { MatProgressSpinner } from '@angular/material/progress-spinner';
import { MatTabsModule } from '@angular/material/tabs';
import {
ActivatedRoute,
ResolveEnd,
ResolveStart,
Router,
RouterEvent,
RouterModule,
RouterOutlet,
} from '@angular/router';
import { of, switchMap, finalize, map, catchError } from 'rxjs';
import { PersonInterestingComponent } from '../person-interesting/person-interesting.component';
import { PersonsService, WorksService } from '../../api';
import { LoadingComponent } from '../../shared/ui/loading/loading.component';
import { PersonBioResolved } from '../../resolvers/person-bio.resolver';
const PERSON_INCLUDE_TAGS =
'topics_full,topic_share,summary_stats,ids,last_known_institutions,' +
'sources,counts_by_year,works_api_url,dates,cited_by_count_boost';
@Component({
selector: 'app-person',
imports: [
MatIconModule,
MatTabsModule,
PersonInterestingComponent,
RouterOutlet,
RouterModule,
MatProgressSpinner,
LoadingComponent,
],
templateUrl: './person.component.html',
styleUrl: './person.component.scss',
})
export class PersonComponent {
private readonly router = inject(Router);
private readonly activatedRoute = inject(ActivatedRoute);
private readonly personsApi = inject(PersonsService);
private readonly worksApi = inject(WorksService);
private readonly destroyRef = inject(DestroyRef);
readonly params = toSignal(this.activatedRoute.paramMap);
readonly routerEvents = toSignal(this.router.events);
readonly currentUrl = computed(() => this.router.url);
// derive authorID directly from route
private readonly authorId = toSignal(
this.activatedRoute.paramMap.pipe(
map((params) => params.get('id') ?? null),
takeUntilDestroyed(this.destroyRef),
),
);
readonly isNetworkResolving = computed(() => {
const event = this.routerEvents();
if (
!(event instanceof RouterEvent) || // not a router event
!this.isResolverEvent(event) || // not a resolver event
!this.isNetworkRouterEvent(event) // not navigating to network route
) {
return false;
}
if (event instanceof ResolveStart) {
return true;
}
return false;
});
isResolverEvent(event: RouterEvent): boolean {
return event instanceof ResolveStart || event instanceof ResolveEnd;
}
isNetworkRouterEvent(event: RouterEvent): boolean {
return event.url.startsWith(`/person/${this.authorId()}/network`);
}
// loading states
readonly isPersonLoading = signal(false);
readonly isBioLoading = signal(false);
readonly person = toSignal(
this.activatedRoute.data.pipe(
map(({ personData }) => {
return personData?.results[0] ?? null;
}),
),
{ initialValue: null },
);
readonly personBio = toSignal(
this.activatedRoute.data.pipe(
switchMap((data) => {
const resolved = data['personBio'] as PersonBioResolved | undefined;
if (!resolved) {
this.isBioLoading.set(false);
return of(null);
}
this.isBioLoading.set(true);
return resolved.bio$.pipe(finalize(() => this.isBioLoading.set(false)));
}),
),
{ initialValue: null },
);
readonly displayName = computed(
() => this.person()?.display_name ?? 'Unbekannte Person',
);
readonly initials = computed(() => this.buildInitials(this.displayName()));
readonly primaryAffiliation = computed(
() => this.person()?.affiliations?.[0] ?? null,
);
readonly lastAffiliation = computed(() => {
let lastAffiliation: number | undefined = 0;
if (this.person()?.affiliations?.length != undefined) {
lastAffiliation = this.person()?.affiliations?.length;
}
return (
(lastAffiliation
? this.person()?.affiliations?.[lastAffiliation - 1]
: this.person()?.affiliations?.[0]) ?? null
);
});
readonly institution = computed(
() =>
this.lastAffiliation()?.institution?.display_name ??
'Unbekannte Institution',
);
readonly countryCode = computed(
() => this.lastAffiliation()?.institution?.country_code ?? null,
);
readonly worksCount = computed(() => this.person()?.works_count ?? null);
readonly citedByCount = computed(() => this.person()?.cited_by_count ?? null);
readonly orcid = computed(() => {
const rawOrcid = this.getId('orcid');
if (!rawOrcid) {
return null;
}
return rawOrcid.replace('https://orcid.org/', '');
});
readonly orcidUrl = computed(() =>
this.orcid() ? `https://orcid.org/${this.orcid()}` : null,
);
readonly worksApiUrl = computed(() => this.person()?.works_api_url ?? null);
readonly primaryTopicCounts = toSignal(
toObservable(this.authorId).pipe(
switchMap((id) => {
if (!id) return of([]);
return this.worksApi.getPrimaryTopicCountsByAuthor(id).pipe(
catchError((err) => {
console.error('Failed to load primary topic counts:', err);
return of([]);
}),
);
}),
),
{ initialValue: [] },
);
links = [
{ label: 'Übersicht', path: 'overview', disabled: false },
{ label: 'Projekte', path: 'projects', disabled: true },
{ label: 'Publikationen', path: 'publications', disabled: false },
{ label: 'Netzwerk', path: 'network', disabled: false },
{ label: 'Gründungen', path: 'foundings', disabled: true },
{ label: 'Förderungen', path: 'funding', disabled: false },
];
onInterestingTagClick(selection: { id: string; name: string }): void {
if (!selection.id) return;
void this.router.navigate(['publications'], {
relativeTo: this.activatedRoute,
queryParams: {
publicationPage: 1,
publicationTopicId: selection.id,
publicationTopicName: selection.name,
},
});
}
private getId(key: string): string | null {
const ids = this.person()?.ids as Record<string, string> | undefined;
if (!ids) {
return null;
}
const value = ids[key];
return value ? String(value) : null;
}
private buildInitials(name: string): string {
const parts = name.trim().split(/\s+/).filter(Boolean);
if (parts.length === 0) {
return '??';
}
if (parts.length === 1) {
return parts[0].slice(0, 2).toUpperCase();
}
return `${parts[0][0]}${parts[1][0]}`.toUpperCase();
}
}