diff --git a/bridge/src/routes/homepage.ts b/bridge/src/routes/homepage.ts index f660d74..b077dc6 100644 --- a/bridge/src/routes/homepage.ts +++ b/bridge/src/routes/homepage.ts @@ -12,6 +12,57 @@ const TRENDING_COUNT = 5; const FEATURED_COUNT = 4; const LATEST_COUNT = 12; const BY_CATEGORY_COUNT = 4; +/** Extra posts so trending / featured / latest stay full after de-duplicating from hero. */ +const MAIN_FEED_PER_PAGE = HERO_COUNT + TRENDING_COUNT + FEATURED_COUNT + LATEST_COUNT + 24; +/** Fetch more per category than we show so we can skip posts already used above the fold. */ +const BY_CATEGORY_FETCH = Math.max(BY_CATEGORY_COUNT * 10, 32); + +function add_to_used(articles: ArticleListItem[], used: Set): void { + for (const a of articles) { + used.add(a.id); + } +} + +/** + * Walks `pool` in order and takes up to `limit` items whose ids are not in `used`, + * then registers those ids in `used`. Applies `patch` to each picked item. + */ +function take_unique( + pool: ArticleListItem[], + used: Set, + limit: number, + patch: Partial>, +): ArticleListItem[] { + const out: ArticleListItem[] = []; + for (const item of pool) { + if (used.has(item.id)) { + continue; + } + used.add(item.id); + out.push({ ...item, ...patch }); + if (out.length >= limit) { + break; + } + } + return out; +} + +/** First `limit` items with distinct ids (sticky + latest often overlap). */ +function take_unique_ids(pool: ArticleListItem[], limit: number): ArticleListItem[] { + const seen = new Set(); + const out: ArticleListItem[] = []; + for (const item of pool) { + if (seen.has(item.id)) { + continue; + } + seen.add(item.id); + out.push(item); + if (out.length >= limit) { + break; + } + } + return out; +} const empty_homepage = (): Omit => ({ hero: [], @@ -34,27 +85,26 @@ export async function homepage_routes(app: FastifyInstance): Promise { // Fetch in parallel where possible const [sticky_posts, latest_result, categories_list] = await Promise.all([ client.get_sticky_posts(HERO_COUNT).catch((): ArticleListItem[] => []), - client.get_posts({ per_page: LATEST_COUNT + TRENDING_COUNT }), + client.get_posts({ per_page: MAIN_FEED_PER_PAGE }), client.get_categories().catch(() => []), ]); const all_latest = latest_result.items; - // Hero = sticky posts (or first N from latest if none) - const hero = sticky_posts.length >= HERO_COUNT - ? sticky_posts.slice(0, HERO_COUNT) - : [...sticky_posts, ...all_latest.slice(0, HERO_COUNT - sticky_posts.length)]; + // Hero = sticky first, then backfill from latest; de-dupe ids (stickies usually also in the feed) + const hero_pool = + sticky_posts.length >= HERO_COUNT + ? sticky_posts + : [...sticky_posts, ...all_latest]; + const hero = take_unique_ids(hero_pool, HERO_COUNT); - // Trending = most recent posts marked is_trending (first N for now; can swap with view-count logic later) - const trending: ArticleListItem[] = all_latest - .slice(0, TRENDING_COUNT) - .map(p => ({ ...p, is_trending: true })); + const used = new Set(); + add_to_used(hero, used); - // Featured = first FEATURED_COUNT from latest - const featured: ArticleListItem[] = all_latest.slice(0, FEATURED_COUNT).map(p => ({ ...p, is_featured: true })); - - // Latest = all fetched - const latest = all_latest.slice(0, LATEST_COUNT); + // Each band below uses only posts not already shown above (avoids repeated images) + const trending = take_unique(all_latest, used, TRENDING_COUNT, { is_trending: true }); + const featured = take_unique(all_latest, used, FEATURED_COUNT, { is_featured: true }); + const latest = take_unique(all_latest, used, LATEST_COUNT, {}); // Per-category sections using domain config categories const category_slugs = config.categories.map(c => c.slug); @@ -64,7 +114,8 @@ export async function homepage_routes(app: FastifyInstance): Promise { category_slugs.map(async (slug) => { const cat = categories_list.find(c => c.slug === slug); if (!cat) return { slug, items: [] as ArticleListItem[] }; - const items = await client.get_posts_by_category_id(cat.id, BY_CATEGORY_COUNT); + const raw = await client.get_posts_by_category_id(cat.id, BY_CATEGORY_FETCH); + const items = take_unique(raw, used, BY_CATEGORY_COUNT, {}); return { slug, items }; }), ); diff --git a/frontend/src/components/home/LatestGrid.astro b/frontend/src/components/home/LatestGrid.astro index 202eab4..9659b84 100644 --- a/frontend/src/components/home/LatestGrid.astro +++ b/frontend/src/components/home/LatestGrid.astro @@ -9,17 +9,32 @@ interface Props { title?: string; show_more_href?: string; locale?: string; + /** `band` adds a subtle full-width background between major homepage sections */ + variant?: 'default' | 'band'; + /** Stable id for heading + `aria-labelledby` when multiple grids appear on one page */ + titleId?: string; } -const { theme, articles, title, show_more_href, locale = 'en-US' } = Astro.props; +const { + theme, + articles, + title, + show_more_href, + locale = 'en-US', + variant = 'default', + titleId = 'latest-title', +} = Astro.props; const { ArticleCard } = getThemeComponents(theme); const ui = getUiStrings(locale); const heading = title ?? ui.latest_news; --- -
+
-

{heading}

+

{heading}

{show_more_href && ( {ui.view_all} )} @@ -38,6 +53,11 @@ const heading = title ?? ui.latest_news; padding: var(--space-8) 0; } + .latest-section--band { + background: var(--color-bg-secondary); + border-block: 1px solid var(--color-border); + } + .latest-header { display: flex; align-items: flex-end; diff --git a/frontend/src/lib/ui-i18n.ts b/frontend/src/lib/ui-i18n.ts index 58695eb..8ca078e 100644 --- a/frontend/src/lib/ui-i18n.ts +++ b/frontend/src/lib/ui-i18n.ts @@ -20,6 +20,8 @@ export type UiStrings = { next_page: string; min_read: string; featured: string; + /** Label for the homepage trending strip */ + trending_strip: string; privacy: string; terms: string; sitemap: string; @@ -71,6 +73,7 @@ const en: UiStrings = { next_page: 'Next page', min_read: 'min read', featured: 'Featured', + trending_strip: 'Trending', privacy: 'Privacy policy', terms: 'Terms', sitemap: 'Sitemap', @@ -121,6 +124,7 @@ const pt: UiStrings = { next_page: 'Página seguinte', min_read: 'min de leitura', featured: 'Destaque', + trending_strip: 'Tendências', privacy: 'Política de privacidade', terms: 'Termos', sitemap: 'Mapa do site', diff --git a/frontend/src/pages/index.astro b/frontend/src/pages/index.astro index a699f64..55df207 100644 --- a/frontend/src/pages/index.astro +++ b/frontend/src/pages/index.astro @@ -5,11 +5,13 @@ import DiscoverSection from '../themes/institutional/DiscoverSection.astro'; import FpaAgendaSection from '../themes/institutional/FpaAgendaSection.astro'; import { getDomainConfig } from '../lib/domain.js'; import { getThemeComponents } from '../lib/theme.js'; +import { getUiStrings } from '../lib/ui-i18n.js'; import { BridgeClient, MockBridgeClient, bridgeRequestHeaders } from '../lib/bridge.js'; const host = Astro.request.headers.get('host') ?? 'localhost'; const site_host = host.split(':')[0]; const config = getDomainConfig(host); +const ui = getUiStrings(config.locale); const { TrendingBar, HeroSection, CategorySection } = getThemeComponents(config.theme); let bridge: BridgeClient; @@ -52,7 +54,7 @@ const website_schema = { description={config.tagline} schema={website_schema} > - + {config.fpa_agenda_enabled && homepage.fpa_agenda != null && ( @@ -65,9 +67,22 @@ const website_schema = {
)} + {homepage.featured.length > 0 && ( + + )} + diff --git a/frontend/src/themes/sports/CategorySection.astro b/frontend/src/themes/sports/CategorySection.astro index 65dfc1a..d287678 100644 --- a/frontend/src/themes/sports/CategorySection.astro +++ b/frontend/src/themes/sports/CategorySection.astro @@ -1,5 +1,6 @@ --- import type { ArticleListItem, CategoryConfig } from '../../types/index.js'; +import { getUiStrings } from '../../lib/ui-i18n.js'; import ArticleCard from './ArticleCard.astro'; interface Props { @@ -9,13 +10,14 @@ interface Props { } const { category, articles, locale = 'en-US' } = Astro.props; +const ui = getUiStrings(locale); --- {articles.length > 0 && (

{category.label}

- More {category.label} + {ui.view_all}
diff --git a/frontend/src/themes/sports/TrendingBar.astro b/frontend/src/themes/sports/TrendingBar.astro index 365ed2a..1abb2d3 100644 --- a/frontend/src/themes/sports/TrendingBar.astro +++ b/frontend/src/themes/sports/TrendingBar.astro @@ -1,10 +1,13 @@ --- import type { ArticleListItem } from '../../types/index.js'; +import { getUiStrings } from '../../lib/ui-i18n.js'; interface Props { articles: ArticleListItem[]; + locale?: string; } -const { articles } = Astro.props; +const { articles, locale = 'en-US' } = Astro.props; +const ui = getUiStrings(locale); --- {articles.length > 0 && (