"use client"; import { Fragment, useCallback, useEffect, useRef, useState } from "react"; import NewArticleForm from "@/components/articles/NewArticleForm"; import NewProjectMenu from "@/components/NewProjectMenu"; import { Button } from "@/components/ui/button"; import { LitList } from "@/components/ui/lit-list"; import { LitActions, LitGroup, LitLine } from "@/components/ui/lit-line"; import { createHref, type Make } from "@/lib/hrefs"; import type { CreateData } from "@/lib/create"; import { ArticleVideoForm, BoardForm, ResearchForm } from "./forms"; import RecurringForm from "./RecurringForm"; // --------------------------------------------------------------------------- // CREATE (plans/umtool-redesign.md, P5; canvas "Create"): "What are you // making?" as ONE lit list of lines in four groups -- Research · Article in the // left column, Video · Song in the right -- each line a thing to make and the // button that opens its form, inline under it. Every form is a thin shell over // a writer that already exists (forms.tsx, NewArticleForm, NewProjectMenu); the // result navigates to what was made. // // Keys: j/k (or the arrows) glide the lit line, Enter opens its form (the first // field takes the focus), Escape closes it and puts the focus back on the line. // The open form is in the address (`?make=`), so a reload or a link lands on // it; `?from=/` (the editor's hand-off) opens "Video from a // recording" prefilled. // --------------------------------------------------------------------------- const LIST_KEYS = new Set(["j", "k", "ArrowUp", "ArrowDown", "Home", "End"]); type Line = { make: Make; title: string; sub: string; action: string }; const plural = (n: number, one: string, many = `${one}s`) => `${n} ${n === 1 ? one : many}`; function lines(d: CreateData): { group: string; lines: Line[] }[][] { const board = d.cases.find((c) => c.quotes > 0) ?? d.cases[0]; const boardSub = board ? `${board.title} · ${plural(board.quotes, "quote")}${d.cases.length > 1 ? ` · ${plural(d.cases.length, "board")}` : ""}` : "No boards yet"; return [ [ { group: "Research", lines: [{ make: "research", title: "Research a question", sub: "Opens a case: search, read, pin quotes to a board", action: "Open a case" }] }, { group: "Article", lines: [ { make: "article", title: "From a question", sub: "A sweep writes iteration 1 · estimate shown before it runs", action: "Start sweep" }, { make: "article-board", title: "From a board", sub: boardSub, action: "Draft article" }, // R5: a living report or a dated series, on a schedule the editor keeps. { make: "recurring", title: "A recurring report", sub: d.schedules ? `Living or a dated series · ${plural(d.schedules, "schedule")}` : "Living, or a dated series · the editor keeps the schedule", action: "Set a schedule" }, ], }, ], [ { group: "Video", lines: [ { make: "video-article", title: "From an article", sub: "Chains a cut from the current report", action: "Choose article" }, { make: "video-board", title: "From a board", sub: "A timeline scaffolded from the pinned cites", action: "Choose board" }, { make: "video-recording", title: "From a recording", sub: "A video URL, or / from the archive", action: "Choose recording" }, { make: "video-highlights", title: "Highlights from a recording", sub: "A long stream cut to its laughs and comments · needs its speakers", action: "Choose recording" }, { make: "video-report", title: "From a sweep report", sub: d.reports.length ? `A cited markdown report on disk · ${plural(d.reports.length, "found", "found")}` : "A cited markdown report on disk", action: "Choose report", }, ], }, { group: "Song", lines: [{ make: "song", title: "A song cut", sub: "From the song piles in Tools", action: "New song" }] }, ], ]; } export default function CreateView({ data, initial, from, initialCase, initialArticle, }: { data: CreateData; initial: Make | null; from: string | null; initialCase: string | null; initialArticle: string | null; }) { const [open, setOpen] = useState(initial); const root = useRef(null); const columns = lines(data); // The open form is the address, so a reload lands on it (no navigation: the // page has everything it needs). const show = useCallback((m: Make | null) => { setOpen(m); try { window.history.replaceState(window.history.state, "", m ? createHref({ make: m }) : createHref()); } catch { // a browser that refuses is only a lost bookmark } }, []); // Closing puts the focus back on the line's button, so the keys go on. const close = useCallback( (m: Make) => { show(null); requestAnimationFrame(() => root.current?.querySelector(`[data-make="${m}"]`)?.focus()); }, [show], ); // An open form takes the focus: its first field (the one marked // data-autofocus, else the first control), once the list's controls are // live -- a form opened from the address was server-rendered inside the // list's disabled fieldset, where React's autoFocus never runs. useEffect(() => { if (!open) return; let frames = 0; let raf = 0; const go = () => { const region = root.current?.querySelector(`[data-make-form="${open}"]`); const el = region?.querySelector("[data-autofocus]") ?? region?.querySelector("input:not([type=hidden]):not([type=radio]):not([type=checkbox]), textarea, select"); if (el && !el.matches(":disabled")) { if (!region?.contains(document.activeElement)) el.focus(); region?.scrollIntoView({ block: "nearest" }); return; } if (++frames < 30) raf = requestAnimationFrame(go); }; raf = requestAnimationFrame(go); return () => cancelAnimationFrame(raf); }, [open]); const form = (m: Make) => { const cancel = () => close(m); switch (m) { case "research": return ; case "article": return ( ); case "recurring": return ; case "article-board": return ; case "video-board": return ; case "video-article": return ; case "video-recording": return data.kinds.recording ? ( ) : null; case "video-highlights": return data.kinds.recording ? ( ) : null; case "video-report": return data.kinds.report ? ( ) : null; case "song": return data.kinds.song ? : null; } }; return (
{columns.map((col, i) => (
{col.map((g) => ( {g.lines.map((l) => ( { if (e.key === "Enter" && e.target === e.currentTarget) { e.preventDefault(); show(open === l.make ? null : l.make); } }} >
{l.title} {l.sub}
{open === l.make && (
{ if (!e.metaKey && !e.ctrlKey && !e.altKey && LIST_KEYS.has(e.key)) e.stopPropagation(); }} > {form(l.make)}
)}
))}
))}
))}
); }