import { Component, ChangeDetectionStrategy, ChangeDetectorRef, Input, OnInit, PLATFORM_ID, TransferState, afterNextRender, inject } from '@angular/core'; import { RECENT_LINES, RECENT_SNAPSHOT, recentKey } from './recent-snapshot'; import { isPlatformBrowser } from '@angular/common'; import { RouterLink } from '@angular/router'; import { chromeLang, chromeText, dateLocale } from '../shared/chrome-lang'; export interface ChangeLine { day: string; tool: string | null; text: string; user_suggested: boolean; } /** Published change lines, newest first: one tool's, or every tool's. */ export async function fetchChanges(tool: string | null, limit: number): Promise { return (await fetchChangesPage(tool, limit, 0)).items; } /** One page of published lines, newest first, and whether older ones follow. */ export async function fetchChangesPage(tool: string | null, limit: number, offset: number): Promise<{ items: ChangeLine[]; more: boolean }> { const q = new URLSearchParams({ limit: String(limit), offset: String(offset) }); if (tool) q.set('tool', tool); const r = await fetch(`/api/changes?${q}`); if (!r.ok) throw new Error(`The list could not be loaded (${r.status}).`); const body = await r.json(); return { items: (body.items ?? []) as ChangeLine[], more: !!body.more }; } /** "13 Oct 2026" for an ISO day (or "13 ott 2026", "13. Okt. 2026" in another locale). */ export function dayText(day: string, locale = 'en-GB'): string { const d = new Date(`${day}T12:00:00Z`); return d.toLocaleDateString(locale, { day: 'numeric', month: 'short', year: 'numeric', timeZone: 'UTC' }); } /** * "Recent changes" under a tool's notes: its last few published What's new * lines (backend routers/changes.py), and nothing at all when there are none. * Lines that came from a visitor's suggestion carry the "Suggested by a * visitor" tag. * * The prerendered page already holds the lines as they were at the last * deploy (recent-snapshot.ts), so crawlers see them; the browser starts from * the same lines (TransferState) and then asks the API. Fresh lines replace * the shown ones only when there are as many of them, so nothing is inserted * or removed once the page is on screen (no layout shift); a tool with no * lines at the last deploy waits for the next one. */ @Component({ selector: 'app-recent-changes', imports: [RouterLink], changeDetection: ChangeDetectionStrategy.OnPush, template: ` @if (items.length) {

{{ t('Recent changes') }}

{{ t('Everything new on the site') }}
} `, styles: ` :host { display: block; } .recent { margin: 1.5rem 0 0; color: var(--text-muted); font-size: .92rem; } h2 { font-size: 1.05rem; font-weight: 600; color: var(--text); margin: 0 0 .5rem; } ul { margin: 0 0 .5rem; padding-left: 1.2rem; max-width: 80ch; } li { margin-bottom: .3rem; } time { font-variant-numeric: tabular-nums; color: var(--text); font-weight: 600; margin-right: .35rem; } .tag { display: inline-block; margin-left: .4rem; padding: 0 .45rem; border-radius: 999px; font-size: .74rem; font-weight: 700; background: var(--brand-light); color: var(--brand-dark); } a { font-size: .86rem; } `, }) export class RecentChangesComponent implements OnInit { @Input() tool = ''; items: ChangeLine[] = []; private readonly state = inject(TransferState); private readonly snapshot = inject(RECENT_SNAPSHOT, { optional: true }); private readonly browser = isPlatformBrowser(inject(PLATFORM_ID)); /** The lines as at the last deploy: from the snapshot when prerendering, from the page's state in the browser. */ ngOnInit(): void { if (!this.tool) return; const key = recentKey(this.tool); if (!this.browser) { const lines = (this.snapshot?.tools[this.tool] ?? []).slice(0, RECENT_LINES).map((c) => ({ ...c, tool: this.tool })); this.items = lines; // Set even when empty: it tells the browser this page came prerendered with these lines. this.state.set(key, lines); return; } if (this.state.hasKey(key)) { this.items = this.state.get(key, []); this.state.remove(key); } else { // Reached by a move inside the app: nothing on screen yet, so the lines are simply fetched. this.prerendered = false; } } /** False when the page was not prerendered with the lines (a move inside the app): the fetched lines are then shown as they come. */ private prerendered = true; readonly day = dayText; protected readonly lang = chromeLang(); protected readonly dateLocale = dateLocale; protected t(english: string): string { return chromeText(this.lang(), english); } private readonly cdr = inject(ChangeDetectorRef); constructor() { if (!this.browser) return; afterNextRender(() => void this.load()); } private async load(): Promise { if (!this.tool || (this.prerendered && !this.items.length)) return; try { const fresh = await fetchChanges(this.tool, RECENT_LINES); // On a prerendered page, only line for line: a list that grew or shrank would move what is below it. if (this.prerendered && fresh.length !== this.items.length) return; this.items = fresh; this.cdr.markForCheck(); } catch { // Offline or no API: the lines from the last deploy stay. } } }