SPB Git

Repository Public

SessionEditor.tsx 3.6 KB 52 lines tsx

1"use client";2import Link from "next/link";3import { useEffect, useState } from "react";4import { Icon } from "@/components/ui/Icon";5import { Tabs } from "@/components/ui/Tabs";6import { fmtDate, useLoad } from "./client";7import { CapsulesPanel } from "./editor/Capsules";8import { MetaForm } from "./editor/MetaForm";9import { PublicationPanel } from "./editor/Publication";10import { ResourcesPanel } from "./editor/Resources";11import { SectionsPanel } from "./editor/Sections";12import type { SessionDetail, SessionPayload } from "./types";13import { Loading, PageTitle, SessionBadges } from "./ui";1415const TABS = ["fiche", "contenus", "capsules", "ressources", "publication"] as const;16export interface PanelProps { s: SessionDetail; payload: SessionPayload; apply: (s: SessionDetail) => void; reload: () => Promise<void> }1718export function SessionEditor({ id, canPublish }: { id: string; canPublish: boolean }) {19  const [tab, setTab] = useState<string>("fiche"), [busyCapsule, setBusyCapsule] = useState(false);20  const { data, error, reload, set } = useLoad<SessionPayload>(`sessions/${id}`, { pollMs: busyCapsule ? 2500 : undefined });21  // The tab follows the address (#capsules…), so links from the dashboard and the browser's back button land on the right one.22  useEffect(() => {23    const sync = () => { const h = window.location.hash.slice(1); if ((TABS as readonly string[]).includes(h)) setTab(h); };24    sync();25    window.addEventListener("hashchange", sync);26    return () => window.removeEventListener("hashchange", sync);27  }, []);28  useEffect(() => { setBusyCapsule(Boolean(data?.session.capsuleRows.some((c) => c.status === "queued" || c.status === "processing"))); }, [data]);29  if (!data) return <><PageTitle eyebrow="Séance" title={id} /><Loading error={error} what="la séance" /></>;30  const s = data.session, props: PanelProps = { s, payload: data, apply: (next) => set({ ...data, session: next }), reload };31  const change = (t: string) => { setTab(t); window.history.replaceState(null, "", `#${t}`); };32  return (33    <div data-testid="session-editor" data-session={s.id} data-state={s.state} data-public={s.public}>34      <p className="mb-3 text-sm"><Link href="/admin/seances" className="link">← Toutes les séances</Link></p>35      <PageTitle eyebrow={`Séance ${String(s.number).padStart(2, "0")} · ${s.id} · partie ${s.part}`} title={s.title}36        actions={<>37          <a href={`/admin/apercu/seances/${s.slug}`} target="_blank" rel="noopener" className="btn btn-ghost" data-testid="open-preview"><Icon name="external" size={17} />Aperçu privé</a>38          {s.public === "published" && <a href={`/seances/${s.slug}`} target="_blank" rel="noopener" className="btn btn-ghost"><Icon name="external" size={17} />Page publique</a>}39        </>}>40        <div className="flex flex-wrap items-center gap-x-3 gap-y-1.5"><SessionBadges s={s} /><span className="text-sm text-faint">Modifiée le {fmtDate(s.updatedAt)}</span></div>41      </PageTitle>42      <Tabs label="Parties de l’éditeur de séance" active={tab} onChange={change} tabs={[43        { id: "fiche", label: "Fiche", panel: <MetaForm {...props} /> },44        { id: "contenus", label: `Contenus (${s.sectionRows.length})`, panel: <SectionsPanel {...props} /> },45        { id: "capsules", label: `Capsules vidéo (${s.capsuleRows.length})`, panel: <CapsulesPanel {...props} /> },46        { id: "ressources", label: `Ressources (${s.resourceRows.length})`, panel: <ResourcesPanel {...props} /> },47        { id: "publication", label: "Publication", panel: <PublicationPanel {...props} canPublish={canPublish} /> },48      ]} />49    </div>50  );51}52
Files main
Menu

Theme