Repository Public
1import Link from "next/link";2import type { Locale, Sheet } from "@/schema";3import { allBlocks, tableOfContents } from "@/schema";4import { dict, fill, formatDate, KIND_LABEL, LEVEL_LABEL } from "@/i18n/dict";5import { FitMath } from "@/renderer/FitMath";6import { Inline, inlineToPlain } from "@/renderer/inline";7import { MathView } from "@/renderer/math";8import { latexToUnicode } from "@/renderer/mathtext";9import { SheetView } from "@/renderer/SheetView";1011const T = dict({12 by: ["Par", "By"], published: ["Publiée le", "Published"], updated: ["Mise à jour le", "Updated"], min: ["{n} min de lecture", "{n} min read"],13 toc: ["Sommaire", "Contents"], formulas: ["Formulaire", "Formula sheet"], formulasHint: ["Les équations de la fiche, d’un coup d’œil", "Every equation of the sheet at a glance"], eq: ["Équation", "Equation"], objectives: ["Après cette fiche, vous saurez", "After this sheet you will be able to"],14});1516/** Reading layout shared by the public page and the private preview: meta line, contents, objectives, the sheet. */17export function SheetReader({ doc, locale, publishedAt, updatedAt, readingMinutes, topics, children, assetBase = "/media" }: {18 doc: Sheet; locale: Locale; publishedAt?: string; updatedAt?: string; readingMinutes: number; topics?: { slug: string; name: string }[]; children?: React.ReactNode;19 /** Where the images of this document are served from (public media by default; a preview has its own route). */20 assetBase?: string;21}) {22 const t = T(locale);23 const toc = tableOfContents(doc).filter((e) => e.level === 2);24 const m = doc.metadata;25 const article = doc.layout.format === "article";26 const formulas = allBlocks(doc).filter((b) => b.type === "equation").slice(0, 9);27 return (28 <div className="mx-auto max-w-[1240px] safe-x">29 <div className="pb-5 pt-6 sm:pt-8">30 <p className="eyebrow flex flex-wrap gap-x-2 gap-y-1">31 {(topics ?? []).map((tp) => <Link key={tp.slug} href={`/topics/${tp.slug}`} className="hover:text-ink">{tp.name}</Link>)}32 <span>{KIND_LABEL(locale)[m.kind]}</span><span>{LEVEL_LABEL(locale)[m.level]}</span>33 </p>34 <p className="mt-2 text-sm text-muted">35 {t.by} <Link href="/about" rel="author" className="font-semibold text-ink underline-offset-4 hover:underline">{m.author}</Link>36 {publishedAt && <> · {t.published} <time dateTime={publishedAt}>{formatDate(publishedAt, locale)}</time></>}37 {updatedAt && publishedAt && updatedAt.slice(0, 10) !== publishedAt.slice(0, 10) && <> · {t.updated} <time dateTime={updatedAt}>{formatDate(updatedAt, locale)}</time></>}38 {" · "}{fill(t.min, { n: readingMinutes })}39 </p>40 </div>41 {(toc.length > 2 || m.objectives.length > 0) && article && (42 <div className="no-print mb-6 grid gap-4 md:grid-cols-2">43 {toc.length > 2 && (44 <details className="card px-4 py-1 md:py-3" open>45 <summary className="flex min-h-11 cursor-pointer items-center font-semibold">{t.toc}</summary>46 <ol className="pb-3 text-[0.9375rem] leading-snug text-muted">47 {toc.map((e) => <li key={e.id}><a href={`#${e.id}`} className="block py-1.5 hover:text-ink" lang={m.locale}><Inline text={e.text} /></a></li>)}48 </ol>49 </details>50 )}51 {m.objectives.length > 0 && (52 <div className="card px-4 py-3">53 <p className="font-semibold">{t.objectives}</p>54 <ul className="mt-2 list-disc space-y-1.5 pl-5 text-[0.9375rem] leading-snug text-muted" lang={m.locale}>{m.objectives.map((o, i) => <li key={i}><Inline text={o} /></li>)}</ul>55 </div>56 )}57 </div>58 )}59 {article && formulas.length >= 2 && (60 <section className="fx no-print" data-fitroot aria-labelledby="h-fx">61 <FitMath />62 <header className="fx-head">63 <span className="fx-emblem" aria-hidden="true">ƒ</span>64 <h2 id="h-fx" className="fx-title">{t.formulas}</h2>65 <p className="fx-hint">{t.formulasHint}</p>66 </header>67 <ol className="fx-list" lang={m.locale}>68 {formulas.map((b, i) => b.type !== "equation" ? null : (69 <li key={b.id} className={[...latexToUnicode(b.latex)].length > 44 ? "fx-wide" : undefined}>70 <a href={`#${b.id}`} className="fx-card">71 <span className="fx-label"><span className="fx-n">{String(i + 1).padStart(2, "0")}</span>{b.label ? inlineToPlain(b.label) : `${t.eq} ${i + 1}`}</span>72 <span className="fx-math"><MathView latex={b.latex} display /></span>73 </a>74 </li>75 ))}76 </ol>77 </section>78 )}79 <div className={article ? "overflow-hidden rounded-[18px] border border-line" : "overflow-x-auto rounded-[18px] border border-line"}>80 <SheetView doc={doc} locale={m.locale} mode="public" assetUrl={(id) => `${assetBase}/${id}`} />81 </div>82 {children}83 </div>84 );85}86