SPB Git

Repository Public

page.tsx 5.4 KB 66 lines tsx

1import type { Metadata } from "next";2import Link from "next/link";3import { getLocale } from "@/i18n/server";4import { dict, fill, KIND_LABEL, LEVEL_LABEL } from "@/i18n/dict";5import { KINDS, LEVELS, LOCALES } from "@/schema";6import { listPublished, listTopics } from "@/server/sheets";7import { SheetGrid } from "@/components/site/sheet-card";89export const dynamic = "force-dynamic";10const T = dict({11  title: ["Recherche", "Search"], label: ["Mots à chercher", "Search terms"], placeholder: ["Titre, concept, méthode…", "Title, concept, method…"], go: ["Chercher", "Search"],12  topic: ["Thème", "Topic"], level: ["Niveau", "Level"], lang: ["Langue", "Language"], kind: ["Format", "Format"], any: ["Tous", "Any"], fr: ["Français", "French"], en: ["Anglais", "English"],13  results: ["{n} résultat(s)", "{n} result(s)"], reset: ["Effacer les filtres", "Clear filters"], prev: ["← Précédent", "← Previous"], next: ["Suivant →", "Next →"], filters: ["Filtres", "Filters"],14});15export async function generateMetadata(): Promise<Metadata> { return { title: T(await getLocale()).title, robots: { index: false, follow: true } }; }1617const PAGE = 18;18export default async function Search({ searchParams }: { searchParams: Promise<Record<string, string | string[] | undefined>> }) {19  const sp = await searchParams; const locale = await getLocale(); const t = T(locale);20  const one = (k: string) => { const v = sp[k]; return (Array.isArray(v) ? v[0] : v)?.slice(0, 120) || undefined; };21  const pick = <A extends readonly string[]>(k: string, a: A) => { const v = one(k); return v && (a as readonly string[]).includes(v) ? (v as A[number]) : undefined; };22  const slug = (k: string) => { const v = one(k); return v && /^[a-z0-9-]{1,96}$/.test(v) ? v : undefined; };23  const f = { q: one("q"), topic: slug("topic"), tag: slug("tag"), level: pick("level", LEVELS), kind: pick("kind", KINDS), locale: pick("lang", LOCALES) };24  const pageNo = Math.min(200, Math.max(1, Number(one("page") ?? 1) || 1));25  const [res, topics] = await Promise.all([listPublished({ ...f, limit: PAGE, offset: (pageNo - 1) * PAGE, uiLocale: locale }), listTopics(locale)]);26  const href = (p: number) => { const u = new URLSearchParams(); if (f.q) u.set("q", f.q); if (f.topic) u.set("topic", f.topic); if (f.tag) u.set("tag", f.tag); if (f.level) u.set("level", f.level); if (f.kind) u.set("kind", f.kind); if (f.locale) u.set("lang", f.locale); if (p > 1) u.set("page", String(p)); const s = u.toString(); return s ? `/search?${s}` : "/search"; };27  const active = f.topic || f.tag || f.level || f.kind || f.locale;28  const sel = "field !min-h-11 !py-1.5";29  return (30    <div className="mx-auto max-w-[1240px] pt-10 safe-x">31      <h1 className="font-display text-4xl font-semibold tracking-tight sm:text-5xl">{t.title}</h1>32      <form action="/search" role="search" className="mt-6 grid gap-3">33        <div className="flex max-w-2xl gap-2">34          <label htmlFor="q" className="sr-only">{t.label}</label>35          <input id="q" name="q" type="search" defaultValue={f.q ?? ""} placeholder={t.placeholder} enterKeyHint="search" autoComplete="off" className="field" />36          <button className="btn btn-primary" type="submit">{t.go}</button>37        </div>38        <fieldset className="grid grid-cols-2 gap-3 md:grid-cols-4">39          <legend className="sr-only">{t.filters}</legend>40          <label className="grid gap-1 text-sm font-medium text-muted">{t.topic}41            <select name="topic" defaultValue={f.topic ?? ""} className={sel}><option value="">{t.any}</option>{topics.filter((x) => x.count > 0).map((x) => <option key={x.slug} value={x.slug}>{x.name}</option>)}</select></label>42          <label className="grid gap-1 text-sm font-medium text-muted">{t.level}43            <select name="level" defaultValue={f.level ?? ""} className={sel}><option value="">{t.any}</option>{LEVELS.map((x) => <option key={x} value={x}>{LEVEL_LABEL(locale)[x]}</option>)}</select></label>44          <label className="grid gap-1 text-sm font-medium text-muted">{t.lang}45            <select name="lang" defaultValue={f.locale ?? ""} className={sel}><option value="">{t.any}</option><option value="fr">{t.fr}</option><option value="en">{t.en}</option></select></label>46          <label className="grid gap-1 text-sm font-medium text-muted">{t.kind}47            <select name="kind" defaultValue={f.kind ?? ""} className={sel}><option value="">{t.any}</option>{KINDS.map((x) => <option key={x} value={x}>{KIND_LABEL(locale)[x]}</option>)}</select></label>48        </fieldset>49        {f.tag && <input type="hidden" name="tag" value={f.tag} />}50      </form>51      <p className="mt-6 flex flex-wrap items-center gap-x-4 gap-y-1 text-sm text-faint" role="status">52        <span>{fill(t.results, { n: res.total })}{f.tag ? ` · #${f.tag}` : ""}</span>53        {active && <Link href={f.q ? `/search?q=${encodeURIComponent(f.q)}` : "/search"} className="py-2 font-semibold text-accent">{t.reset}</Link>}54      </p>55      <h2 className="sr-only">{fill(t.results, { n: res.total })}</h2>56      <div className="mt-4"><SheetGrid sheets={res.items} locale={locale} /></div>57      {(pageNo > 1 || res.total > pageNo * PAGE) && (58        <nav className="mt-8 flex justify-between" aria-label="Pagination">59          {pageNo > 1 ? <Link className="btn" href={href(pageNo - 1)} rel="prev">{t.prev}</Link> : <span />}60          {res.total > pageNo * PAGE ? <Link className="btn" href={href(pageNo + 1)} rel="next">{t.next}</Link> : <span />}61        </nav>62      )}63    </div>64  );65}66
Files main
Menu

Theme