diff --git a/frontend/src/lib/ui-i18n.ts b/frontend/src/lib/ui-i18n.ts index 8ca078e..c02e259 100644 --- a/frontend/src/lib/ui-i18n.ts +++ b/frontend/src/lib/ui-i18n.ts @@ -53,6 +53,12 @@ export type UiStrings = { fpa_col_competition: string; fpa_agenda_empty: string; fpa_source_credit: string; + /** News archive page title */ + news_archive_title: string; + /** "All" filter label for category tabs */ + news_all_filter: string; + /** News archive page meta description */ + news_archive_desc: string; }; const en: UiStrings = { @@ -104,6 +110,9 @@ const en: UiStrings = { fpa_col_competition: 'Competition', fpa_agenda_empty: 'Fixture list is temporarily unavailable.', fpa_source_credit: 'Data: Portuguese Handball Federation (ws.fpa.pt).', + news_archive_title: 'News', + news_all_filter: 'All', + news_archive_desc: 'Latest news and stories across all sections.', }; const pt: UiStrings = { @@ -155,6 +164,9 @@ const pt: UiStrings = { fpa_col_competition: 'Competição', fpa_agenda_empty: 'A agenda não está disponível de momento.', fpa_source_credit: 'Dados: Federação Portuguesa de Andebol (ws.fpa.pt).', + news_archive_title: 'Notícias', + news_all_filter: 'Todas', + news_archive_desc: 'Últimas notícias e histórias de todas as secções.', }; export function getUiStrings(locale: string): UiStrings { diff --git a/frontend/src/pages/noticias.astro b/frontend/src/pages/noticias.astro new file mode 100644 index 0000000..b2d7801 --- /dev/null +++ b/frontend/src/pages/noticias.astro @@ -0,0 +1,432 @@ +--- +import Base from '../layouts/Base.astro'; +import { getDomainConfig } from '../lib/domain.js'; +import { getThemeComponents } from '../lib/theme.js'; +import { BridgeClient, MockBridgeClient, bridgeRequestHeaders } from '../lib/bridge.js'; +import { getUiStrings } from '../lib/ui-i18n.js'; +import { relativeTime, truncate } from '../lib/format.js'; + +const host = Astro.request.headers.get('host') ?? 'localhost'; +const site_host = host.split(':')[0]; +const config = getDomainConfig(host); +const { ArticleCard } = getThemeComponents(config.theme); +const ui = getUiStrings(config.locale); + +const active_cat = Astro.url.searchParams.get('cat') ?? ''; +const page = parseInt(Astro.url.searchParams.get('page') ?? '1', 10); +const per_page = 12; + +let bridge: BridgeClient; +try { + bridge = new BridgeClient(config.bridge_endpoint, site_host); + await fetch(`${config.bridge_endpoint}/health`, { + headers: bridgeRequestHeaders(site_host), + signal: AbortSignal.timeout(2000), + }); +} catch { + bridge = new MockBridgeClient(); +} + +/* Hero: show on first page without category filter */ +let hero: Awaited>['hero'][number] | null = null; +if (page === 1 && !active_cat) { + try { + const homepage = await bridge.getHomepage(); + hero = homepage.hero[0] ?? null; + } catch { + /* fall through */ + } +} + +/* Paginated article list */ +let paginated: Awaited>; +try { + paginated = await bridge.getArticles({ + category: active_cat || undefined, + page, + per_page, + }); +} catch { + paginated = { items: [], total: 0, page: 1, per_page, total_pages: 0 }; +} + +/* Deduplicate hero from grid */ +const grid_items = hero + ? paginated.items.filter(a => a.slug !== hero!.slug) + : paginated.items; + +const total_pages = paginated.total_pages; +const has_prev = page > 1; +const has_next = page < total_pages; +const categories = config.categories; + +/* Canonical URL */ +const qs_parts: string[] = []; +if (active_cat) qs_parts.push(`cat=${active_cat}`); +if (page > 1) qs_parts.push(`page=${page}`); +const canonical = `${Astro.url.origin}/noticias${qs_parts.length ? '?' + qs_parts.join('&') : ''}`; + +/* Active category label for meta */ +const active_cat_label = active_cat + ? categories.find(c => c.slug === active_cat)?.label ?? active_cat + : ''; +const page_title = active_cat_label + ? `${active_cat_label} — ${ui.news_archive_title} — ${config.site_name}` + : `${ui.news_archive_title} — ${config.site_name}`; + +/* Pagination link builder */ +function paginationHref(p: number): string { + const parts: string[] = []; + if (active_cat) parts.push(`cat=${active_cat}`); + if (p > 1) parts.push(`page=${p}`); + return `/noticias${parts.length ? '?' + parts.join('&') : ''}`; +} + +const breadcrumb_schema = { + '@context': 'https://schema.org', + '@type': 'BreadcrumbList', + itemListElement: [ + { '@type': 'ListItem', position: 1, name: ui.breadcrumb_home, item: Astro.url.origin }, + { '@type': 'ListItem', position: 2, name: ui.news_archive_title, item: `${Astro.url.origin}/noticias` }, + ], +}; +--- + + {/* ── Hero banner (page 1, no filter) ── */} + {hero && ( +
+ +
+
+ {hero.category.name} +

+ {hero.title} +

+

{truncate(hero.excerpt, 160)}

+
+ {hero.author.name} + + + + {hero.reading_time} {ui.min_read} +
+
+
+
+ )} + +
+ {/* ── Page heading (shown when no hero, or when filtering) ── */} + {!hero && ( +
+

{active_cat_label || ui.news_archive_title}

+ {total_pages > 1 && ( +

+ {paginated.total} {paginated.total === 1 ? ui.article_one : ui.articles_many} + {` — ${ui.pageOf(page, total_pages)}`} +

+ )} +
+ )} + + {/* ── Category filter tabs ── */} + + + {/* ── Article grid ── */} +
+ {grid_items.map(article => ( + + ))} +
+ + {grid_items.length === 0 && ( +

{ui.no_articles_category}

+ )} + + {/* ── Pagination ── */} + {total_pages > 1 && ( + + )} +
+ + +