SPB Git

Repository Public

page.tsx 9.5 KB 188 lines tsx

1import Link from "next/link";2import { nowMs } from "@/server/clock";3import { requireAdmin } from "@/server/session";4import { getDb } from "@/server/db";5import { dirtyDocs, getPublished } from "@/content/store";6import type { SiteContent } from "@/content/schema";7import { listAppointments, bookingSettings, ensureBookingSeed } from "@/server/booking/service";8import { fmtDateTime as fmtAppt } from "@/server/booking/tz";9import { statsSummary } from "@/server/stats";10import { mailTransport } from "@/server/mail";11import { notificationSettings } from "@/server/notify";12import { STATUS_BADGE, STATUS_LABEL } from "@/components/admin/status";13import { PageTitle } from "../doc";1415export const metadata = { title: "Tableau de bord" };1617const DOC_LABEL: Record<string, { label: string; href: string }> = {18  site: { label: "Paramètres / navigation / SEO", href: "/admin/settings" },19  theme: { label: "Apparence", href: "/admin/apparence" },20  services: { label: "Services", href: "/admin/services" },21  testimonials: { label: "Témoignages", href: "/admin/temoignages" },22  faq: { label: "FAQ", href: "/admin/faq" },23};2425export default async function Dashboard() {26  await requireAdmin();27  ensureBookingSeed();28  const db = getDb();29  const now = nowMs();30  const upcoming = listAppointments({ fromUtc: now }).filter((a) => a.status === "requested" || a.status === "confirmed");31  const pending = upcoming.filter((a) => a.status === "requested");32  const newMessages = db.prepare("SELECT id, name, subject, created_at FROM contact_messages WHERE status = 'new' ORDER BY created_at DESC LIMIT 5").all() as { id: number; name: string; subject: string; created_at: number }[];33  const clients = (db.prepare("SELECT count(*) AS n FROM users WHERE role = 'client'").get() as { n: number }).n;34  const dirty = dirtyDocs();35  const s7 = statsSummary(7);36  const s30 = statsSummary(30);37  const max = Math.max(1, ...s30.series.map((d) => d.views));38  const site = getPublished<SiteContent>("site");39  const notif = notificationSettings();40  const transport = mailTransport();41  const todo: { text: string; href: string }[] = [];42  if (!notif.email) todo.push({ text: "Indiquez le courriel où recevoir les notifications (rendez-vous, messages, sommaire).", href: "/admin/settings/notifications" });43  if (site && !site.contact.email && !site.contact.phone) todo.push({ text: "Ajoutez les coordonnées publiques (courriel, téléphone).", href: "/admin/settings" });44  todo.push({ text: "Complétez la biographie et les qualifications réelles (page À propos).", href: "/admin/pages/a-propos" });45  todo.push({ text: "Faites valider la politique de confidentialité, puis retirez l’encadré d’avis.", href: "/admin/pages/confidentialite" });46  if (transport === "log") todo.push({ text: "Aucun service de courriel n’est configuré sur cet environnement : les courriels sont seulement journalisés.", href: "/admin/courriels" });47  if (!bookingSettings().autoConfirm && pending.length) todo.unshift({ text: `${pending.length} demande(s) de rendez-vous à valider.`, href: "/admin/appointments?view=liste&status=requested" });4849  const stat = (label: string, value: string | number, href: string, hint?: string) => (50    <Link href={href} className="card hover:border-slate-500 block p-4">51      <p className="text-ink-500 text-xs font-semibold tracking-wide uppercase">{label}</p>52      <p className="text-navy-900 mt-1 font-serif text-4xl">{value}</p>53      {hint ? <p className="text-ink-500 mt-1 text-xs">{hint}</p> : null}54    </Link>55  );5657  return (58    <>59      <PageTitle title="Tableau de bord" intro="Vue d’ensemble du site, des rendez-vous et des messages." />60      <div className="grid grid-cols-2 gap-3 lg:grid-cols-4">61        {stat("À valider", pending.length, "/admin/appointments?view=liste&status=requested", "demandes de rendez-vous")}62        {stat("À venir", upcoming.length, "/admin/appointments", "rendez-vous")}63        {stat("Nouveaux messages", newMessages.length, "/admin/messages")}64        {stat("Pages vues (7 j)", s7.totals.views, "/admin", `${s7.totals.visitors} visiteurs · ${clients} compte(s) client`)}65      </div>6667      <div className="mt-6 grid gap-6 lg:grid-cols-2">68        <section className="card p-4 sm:p-5" aria-labelledby="d-rdv">69          <div className="flex items-center justify-between gap-3">70            <h2 id="d-rdv" className="text-navy-900 font-serif text-xl">71              Prochains rendez-vous72            </h2>73            <Link href="/admin/appointments" className="text-sm font-semibold underline underline-offset-4">74              Agenda75            </Link>76          </div>77          {upcoming.length === 0 ? (78            <p className="text-ink-500 mt-3 text-sm">Aucun rendez-vous à venir.</p>79          ) : (80            <ul className="divide-pale-300/70 mt-2 divide-y">81              {upcoming.slice(0, 6).map((a) => (82                <li key={a.id}>83                  <Link href={`/admin/appointments/${a.id}`} className="hover:bg-ivory-50 flex min-h-14 items-center justify-between gap-3 px-1 py-2">84                    <span className="min-w-0">85                      <span className="text-navy-900 block truncate text-sm font-semibold">{a.name}</span>86                      <span className="text-ink-500 block truncate text-xs first-letter:uppercase">87                        {fmtAppt(a.start_utc)} · {a.type_title}88                      </span>89                    </span>90                    <span className={`badge ${STATUS_BADGE[a.status]}`}>{STATUS_LABEL[a.status]}</span>91                  </Link>92                </li>93              ))}94            </ul>95          )}96        </section>9798        <section className="card p-4 sm:p-5" aria-labelledby="d-todo">99          <h2 id="d-todo" className="text-navy-900 font-serif text-xl">100            À faire101          </h2>102          <ul className="mt-2 flex flex-col gap-1">103            {todo.map((t) => (104              <li key={t.text}>105                <Link href={t.href} className="hover:bg-ivory-50 flex min-h-11 items-start gap-2 rounded px-1 py-2 text-sm">106                  <span aria-hidden="true" className="text-gold-700">107                    →108                  </span>109                  <span>{t.text}</span>110                </Link>111              </li>112            ))}113          </ul>114          {dirty.length ? (115            <>116              <h3 className="text-navy-900 mt-4 text-sm font-semibold">Brouillons non publiés</h3>117              <ul className="mt-1 flex flex-wrap gap-2">118                {dirty.map((d) => {119                  const page = d.key.startsWith("page:");120                  const info = page ? { label: `Page « ${d.key.slice(5)} »`, href: `/admin/pages/${d.key.slice(5)}` } : DOC_LABEL[d.key];121                  return (122                    <li key={d.key}>123                      <Link href={info?.href ?? "/admin/pages"} className="badge badge-gold min-h-8">124                        {info?.label ?? d.key}125                      </Link>126                    </li>127                  );128                })}129              </ul>130            </>131          ) : null}132        </section>133134        <section className="card p-4 sm:p-5" aria-labelledby="d-stats">135          <h2 id="d-stats" className="text-navy-900 font-serif text-xl">136            Fréquentation — 30 jours137          </h2>138          <p className="text-ink-500 text-xs">139            {s30.totals.views} pages vues · {s30.totals.visitors} visiteurs (cumul quotidien). Mesure interne, agrégée, sans témoin.140          </p>141          <div className="mt-4 flex h-28 items-end gap-[2px]" role="img" aria-label={`Pages vues par jour sur 30 jours, maximum ${max}`}>142            {s30.series.map((d) => (143              <span key={d.day} title={`${d.day} : ${d.views} pages vues, ${d.visitors} visiteurs`} className="bg-slate-500/80 min-h-[2px] flex-1 rounded-t-sm" style={{ height: `${Math.max(2, (d.views / max) * 100)}%` }} />144            ))}145          </div>146          {s30.top.length ? (147            <ol className="mt-4 flex flex-col gap-1 text-sm">148              {s30.top.slice(0, 6).map((t) => (149                <li key={t.path} className="flex items-center justify-between gap-3">150                  <span className="text-ink-900 truncate">{t.path}</span>151                  <span className="text-ink-500 tabular-nums">{t.views}</span>152                </li>153              ))}154            </ol>155          ) : (156            <p className="text-ink-500 mt-3 text-sm">Aucune visite enregistrée pour le moment.</p>157          )}158        </section>159160        <section className="card p-4 sm:p-5" aria-labelledby="d-msg">161          <div className="flex items-center justify-between gap-3">162            <h2 id="d-msg" className="text-navy-900 font-serif text-xl">163              Nouveaux messages164            </h2>165            <Link href="/admin/messages" className="text-sm font-semibold underline underline-offset-4">166              Tous167            </Link>168          </div>169          {newMessages.length === 0 ? (170            <p className="text-ink-500 mt-3 text-sm">Aucun nouveau message.</p>171          ) : (172            <ul className="divide-pale-300/70 mt-2 divide-y">173              {newMessages.map((m) => (174                <li key={m.id}>175                  <Link href={`/admin/messages/${m.id}`} className="hover:bg-ivory-50 block min-h-12 px-1 py-2">176                    <span className="text-navy-900 block truncate text-sm font-semibold">{m.subject}</span>177                    <span className="text-ink-500 block truncate text-xs">{m.name}</span>178                  </Link>179                </li>180              ))}181            </ul>182          )}183        </section>184      </div>185    </>186  );187}188
Files main
Menu

Theme