diff --git a/webapp/src/app/modules/person-funding/person-funding.component.html b/webapp/src/app/modules/person-funding/person-funding.component.html index 7c59a864..075e1e2b 100644 --- a/webapp/src/app/modules/person-funding/person-funding.component.html +++ b/webapp/src/app/modules/person-funding/person-funding.component.html @@ -77,7 +77,7 @@ diff --git a/webapp/src/app/modules/person-funding/person-funding.component.ts b/webapp/src/app/modules/person-funding/person-funding.component.ts index bc8446dd..d736bd1f 100644 --- a/webapp/src/app/modules/person-funding/person-funding.component.ts +++ b/webapp/src/app/modules/person-funding/person-funding.component.ts @@ -15,6 +15,7 @@ export class PersonFundingComponent implements OnInit { private router = inject(Router); private personsApi = inject(PersonsOpenApiService); private initialSelectedFunderKey: string | null = null; + private publicationUrlCache = new Map(); funders: any[] = []; filteredFunders: any[] = []; @@ -124,6 +125,12 @@ export class PersonFundingComponent implements OnInit { return funder.publication_ids[this.activeFilter] ?? []; } + async openPublication(event: MouseEvent, publicationId: string): Promise { + event.preventDefault(); + const url = await this.resolvePreferredPublicationUrl(publicationId); + window.open(url, '_blank', 'noopener'); + } + getPositionLabel(position: string): string { const labels: Record = { first: 'Erstautor', @@ -149,4 +156,42 @@ export class PersonFundingComponent implements OnInit { } return allIds.size; } + + private async resolvePreferredPublicationUrl(publicationId: string): Promise { + const cached = this.publicationUrlCache.get(publicationId); + if (cached) { + return cached; + } + + const fallbackUrl = publicationId; + const workId = publicationId.replace('https://openalex.org/', '').trim(); + if (!workId) { + return fallbackUrl; + } + + try { + const response = await fetch(`https://api.openalex.org/works/${encodeURIComponent(workId)}`); + if (!response.ok) { + return fallbackUrl; + } + + const work = await response.json(); + const pdfUrl = + work?.primary_location?.pdf_url ?? + work?.best_oa_location?.pdf_url ?? + null; + + const htmlUrl = + work?.primary_location?.landing_page_url ?? + work?.best_oa_location?.landing_page_url ?? + work?.open_access?.oa_url ?? + null; + + const resolvedUrl = pdfUrl || htmlUrl || fallbackUrl; + this.publicationUrlCache.set(publicationId, resolvedUrl); + return resolvedUrl; + } catch { + return fallbackUrl; + } + } }