From eb4da37692c66c834377a995f5d36e695e646044 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Alves?= Date: Thu, 18 Jun 2026 15:44:06 +0000 Subject: [PATCH] =?UTF-8?q?fix=20emblemas=20clubes=20calend=C3=A1rio=20FPA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fetch club logo URLs from FPA getClube API and render img elements in the FpaAgendaSection component; fallback to team initials when the URL is unavailable. Paperclip-Issue: BIT-25572 Paperclip-Run: 6f581074-e308-4a75-b6bb-7a08aee20136 --- bridge/src/services/fpa-agenda.ts | 62 ++++++++++++++++++- bridge/src/types.ts | 3 + .../institutional/FpaAgendaSection.astro | 60 ++++++++++++++++-- frontend/src/types/index.ts | 3 + 4 files changed, 121 insertions(+), 7 deletions(-) diff --git a/bridge/src/services/fpa-agenda.ts b/bridge/src/services/fpa-agenda.ts index 2ed601e..f6a2c7f 100644 --- a/bridge/src/services/fpa-agenda.ts +++ b/bridge/src/services/fpa-agenda.ts @@ -4,9 +4,17 @@ import type { FpaAgendaGame } from '../types.js'; const FPA_AGENDA_URL = process.env['FPA_AGENDA_URL'] ?? 'https://ws.fpa.pt/index.php/getAgendaJogosFAP/'; +const FPA_CLUBE_URL_BASE = + process.env['FPA_CLUBE_URL_BASE'] ?? 'https://ws.fpa.pt/index.php/getClube/'; + const FETCH_TIMEOUT_MS = parseInt(process.env['FPA_FETCH_TIMEOUT_MS'] ?? '20000', 10); const MAX_GAMES = Math.min(parseInt(process.env['FPA_AGENDA_MAX'] ?? '48', 10), 200); +interface ParsedFpaGame extends FpaAgendaGame { + home_club_id: string; + away_club_id: string; +} + function as_trimmed_string(value: unknown): string { if (value === null || value === undefined) { return ''; @@ -26,6 +34,42 @@ function sort_key(date: string, time: string): string { return `${date} ${time}`.padEnd(20, '0'); } +async function fetch_club_logo_url(club_id: string): Promise { + const res = await fetch(`${FPA_CLUBE_URL_BASE}${encodeURIComponent(club_id)}`, { + headers: { Accept: 'application/json' }, + signal: AbortSignal.timeout(FETCH_TIMEOUT_MS), + }); + + if (!res.ok) { + return null; + } + + const raw: unknown = await res.json(); + if (!Array.isArray(raw) || raw.length === 0 || raw[0] === null || typeof raw[0] !== 'object') { + return null; + } + + const emblem = as_trimmed_string((raw[0] as Record)['URL_EMBLEMA_CASA']); + return emblem || null; +} + +async function resolve_club_logos(club_ids: Iterable): Promise> { + const unique = [...new Set([...club_ids].filter(Boolean))]; + const logos = new Map(); + + await Promise.all( + unique.map(async (club_id) => { + try { + logos.set(club_id, await fetch_club_logo_url(club_id)); + } catch { + logos.set(club_id, null); + } + }), + ); + + return logos; +} + /** * Fetch and normalise FPA agenda JSON. Caller should wrap in cache (e.g. swr_fetch). * Throws on network/HTTP errors or invalid payload shape. @@ -45,7 +89,7 @@ export async function fetch_fpa_agenda(): Promise { throw new Error('FPA agenda: expected JSON array'); } - const games: FpaAgendaGame[] = []; + const games: ParsedFpaGame[] = []; for (const row of raw) { if (row === null || typeof row !== 'object') { @@ -70,9 +114,23 @@ export async function fetch_fpa_agenda(): Promise { venue: as_trimmed_string(o['LOCAL']) || '—', competition: as_trimmed_string(o['PROVA']) || '—', phase: as_trimmed_string(o['FASE']) || '', + home_logo_url: null, + away_logo_url: null, + home_club_id: as_trimmed_string(o['CLUBE_CASA_ID']), + away_club_id: as_trimmed_string(o['CLUBE_FORA_ID']), }); } games.sort((a, b) => sort_key(a.date, a.time).localeCompare(sort_key(b.date, b.time))); - return games.slice(0, MAX_GAMES); + const limited = games.slice(0, MAX_GAMES); + + const logos = await resolve_club_logos( + limited.flatMap((g) => [g.home_club_id, g.away_club_id]), + ); + + return limited.map(({ home_club_id, away_club_id, ...game }) => ({ + ...game, + home_logo_url: home_club_id ? logos.get(home_club_id) ?? null : null, + away_logo_url: away_club_id ? logos.get(away_club_id) ?? null : null, + })); } diff --git a/bridge/src/types.ts b/bridge/src/types.ts index 533c854..c3e7bcb 100644 --- a/bridge/src/types.ts +++ b/bridge/src/types.ts @@ -31,6 +31,9 @@ export interface FpaAgendaGame { venue: string; competition: string; phase: string; + /** Club emblem from FPA `getClube` (si.fpa.pt); null when unavailable. */ + home_logo_url: string | null; + away_logo_url: string | null; } export interface CategoryConfig { diff --git a/frontend/src/themes/institutional/FpaAgendaSection.astro b/frontend/src/themes/institutional/FpaAgendaSection.astro index c6fef06..8aa5f9d 100644 --- a/frontend/src/themes/institutional/FpaAgendaSection.astro +++ b/frontend/src/themes/institutional/FpaAgendaSection.astro @@ -87,8 +87,26 @@ const date_groups = [...by_date.entries()];
-
@@ -106,8 +124,26 @@ const date_groups = [...by_date.entries()];
-
@@ -410,9 +446,23 @@ const date_groups = [...by_date.entries()]; ); box-shadow: 0 4px 12px color-mix(in srgb, var(--color-primary) 25%, transparent); flex-shrink: 0; + overflow: hidden; } - .fpa-agenda__side--away .fpa-agenda__crest { + .fpa-agenda__crest--logo { + background: #fff; + border: 1px solid color-mix(in srgb, var(--color-border) 80%, #fff); + box-shadow: 0 2px 8px color-mix(in srgb, var(--color-primary) 12%, transparent); + } + + .fpa-agenda__crest-img { + width: 100%; + height: 100%; + object-fit: contain; + padding: 0.2rem; + } + + .fpa-agenda__side--away .fpa-agenda__crest:not(.fpa-agenda__crest--logo) { background: linear-gradient( 145deg, var(--color-secondary) 0%, diff --git a/frontend/src/types/index.ts b/frontend/src/types/index.ts index 29f6dc5..0e02b31 100644 --- a/frontend/src/types/index.ts +++ b/frontend/src/types/index.ts @@ -46,6 +46,9 @@ export interface FpaAgendaGame { venue: string; competition: string; phase: string; + /** Club emblem from FPA `getClube` (si.fpa.pt); null when unavailable. */ + home_logo_url?: string | null; + away_logo_url?: string | null; } export interface Article {