fix(fpa): emblemas clubes calendário FPA (BIT-25572) #27
No files matched your search
@@ -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<string | null> {
|
||||
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<string, unknown>)['URL_EMBLEMA_CASA']);
|
||||
return emblem || null;
|
||||
}
|
||||
|
||||
async function resolve_club_logos(club_ids: Iterable<string>): Promise<Map<string, string | null>> {
|
||||
const unique = [...new Set([...club_ids].filter(Boolean))];
|
||||
const logos = new Map<string, string | null>();
|
||||
|
||||
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<FpaAgendaGame[]> {
|
||||
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<FpaAgendaGame[]> {
|
||||
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,
|
||||
}));
|
||||
}
|
||||
@@ -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 {
|
||||
|
||||
@@ -87,8 +87,26 @@ const date_groups = [...by_date.entries()];
|
||||
|
||||
<div class="fpa-agenda__faceoff">
|
||||
<div class="fpa-agenda__side fpa-agenda__side--home">
|
||||
<span class="fpa-agenda__crest" aria-hidden="true">
|
||||
{team_initials(g.home_team)}
|
||||
<span
|
||||
class:list={[
|
||||
'fpa-agenda__crest',
|
||||
g.home_logo_url && 'fpa-agenda__crest--logo',
|
||||
]}
|
||||
aria-hidden="true"
|
||||
>
|
||||
{g.home_logo_url ? (
|
||||
<img
|
||||
class="fpa-agenda__crest-img"
|
||||
src={g.home_logo_url}
|
||||
alt=""
|
||||
loading="lazy"
|
||||
decoding="async"
|
||||
width="44"
|
||||
height="44"
|
||||
/>
|
||||
) : (
|
||||
team_initials(g.home_team)
|
||||
)}
|
||||
</span>
|
||||
<span class="fpa-agenda__team">{g.home_team}</span>
|
||||
</div>
|
||||
@@ -106,8 +124,26 @@ const date_groups = [...by_date.entries()];
|
||||
</div>
|
||||
|
||||
<div class="fpa-agenda__side fpa-agenda__side--away">
|
||||
<span class="fpa-agenda__crest" aria-hidden="true">
|
||||
{team_initials(g.away_team)}
|
||||
<span
|
||||
class:list={[
|
||||
'fpa-agenda__crest',
|
||||
g.away_logo_url && 'fpa-agenda__crest--logo',
|
||||
]}
|
||||
aria-hidden="true"
|
||||
>
|
||||
{g.away_logo_url ? (
|
||||
<img
|
||||
class="fpa-agenda__crest-img"
|
||||
src={g.away_logo_url}
|
||||
alt=""
|
||||
loading="lazy"
|
||||
decoding="async"
|
||||
width="44"
|
||||
height="44"
|
||||
/>
|
||||
) : (
|
||||
team_initials(g.away_team)
|
||||
)}
|
||||
</span>
|
||||
<span class="fpa-agenda__team">{g.away_team}</span>
|
||||
</div>
|
||||
@@ -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%,
|
||||
|
||||
@@ -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 {
|
||||
|
||||
Reference in new issue
Block a user