Something went wrong. Try again.
spool is a community-first, lightweight discussion forum, issue tracker, and announcement hub for your project.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202import { useCallback, useEffect, useLayoutEffect, useRef, useState, type FormEvent, type KeyboardEvent,} from "react";import { Link, useLocation, useNavigate, useOutletContext, useParams } from "react-router-dom";import { api, timeAgo, type ThreadMessage } from "../api";import { useAuth } from "../auth";import type { SpoolLayoutContext } from "../components/SpoolLayout";
export function ThreadsPage() { const { slug } = useParams<{ slug: string }>(); const { user } = useAuth(); const { spool } = useOutletContext<SpoolLayoutContext>(); const navigate = useNavigate(); const { hash } = useLocation(); const [messages, setMessages] = useState<ThreadMessage[] | null>(null); const [error, setError] = useState<string | null>(null); const [replyTo, setReplyTo] = useState<number | null>(null); const scrollRef = useRef<HTMLDivElement>(null); const didInitialScroll = useRef(false); const stickToBottom = useRef(false);
const load = useCallback( () => api.threads(slug!).then(setMessages).catch((e) => setError(e.message)), [slug] );
useEffect(() => { // Navigating between spools reuses this component — reset and refetch. didInitialScroll.current = false; setMessages(null); void load(); }, [load]);
function scrollToBottom() { const el = scrollRef.current; if (el) el.scrollTop = el.scrollHeight; }
// Scrolling must happen *after* React renders the new messages into the // DOM (before that, scrollHeight is still the old height) — hence a layout // effect keyed on `messages`, not a call right after load(). Runs before // paint so there's no visible jump. useLayoutEffect(() => { if (!messages) return; if (!didInitialScroll.current) { didInitialScroll.current = true; if (hash) { document.getElementById(hash.slice(1))?.scrollIntoView({ block: "center" }); } else { scrollToBottom(); } } else if (stickToBottom.current) { stickToBottom.current = false; scrollToBottom(); } }, [messages, hash]);
if (error) return <p className="state-line">error: {error}</p>; if (!messages) return <p className="state-line">loading…</p>;
const roots = messages.filter((m) => m.parentId === null); const repliesOf = (id: number) => messages.filter((m) => m.parentId === id);
async function post(body: string, parentId?: number) { await api.postMessage(slug!, body, parentId); setReplyTo(null); if (parentId == null) stickToBottom.current = true; await load(); }
// Create a new issue (referred to as 'cutting') async function cut(id: number) { const issue = await api.cutMessage(id); navigate(`/s/${slug}/issues/${issue.number}`); }
return ( <div className="pinned-page"> <div className="page-scroll" ref={scrollRef}> {/* Project identity (name, description, repo) lives on AboutPage now. */} {roots.length === 0 ? ( <p className="state-line">nothing here yet — start the first thread</p> ) : ( <ul className="threads"> {roots.map((msg) => ( <li key={msg.id} id={`msg-${msg.id}`} className="msg"> <p className="meta"> <strong>@{msg.author}</strong>{" "} <span className="meta-dim">{timeAgo(msg.createdAt)}</span> </p> <p className="msg-body">{msg.body}</p> <ul className="msg-replies"> {repliesOf(msg.id).map((reply) => ( <li key={reply.id} className="msg"> <p className="meta"> <strong>@{reply.author}</strong>{" "} <span className="meta-dim">{timeAgo(reply.createdAt)}</span> </p> <p className="msg-body">{reply.body}</p> </li> ))} </ul> <p className="msg-actions"> {msg.cutIssueNumber != null && ( <Link className="msg-cut" to={`/s/${slug}/issues/${msg.cutIssueNumber}`}> ✂ issue #{msg.cutIssueNumber} </Link> )} {user && replyTo !== msg.id && ( <button className="msg-act" onClick={() => setReplyTo(msg.id)}> reply </button> )} {user && msg.cutIssueNumber == null && ( <button className="msg-act" onClick={() => cut(msg.id)}> cut an issue </button> )} </p> {user && replyTo === msg.id && ( <MessageForm placeholder="Reply to this thread…" autoFocus onPost={(body) => post(body, msg.id)} /> )} </li> ))} </ul> )}
</div>
<div className="threads-dock"> {!user ? ( <p className="state-line"> <a href="/auth/github">sign in</a> to start a thread </p> ) : spool.threadsLocked && !spool.canStartThread ? ( <div className="composer"> <textarea rows={2} placeholder="The owner of this spool has disabled outside conversation." disabled /> <button className="btn" type="button" disabled> post </button> </div> ) : ( <MessageForm placeholder="Start a thread… (enter to send)" onPost={(body) => post(body)} /> )} </div> </div> );}
function MessageForm({ placeholder, autoFocus, onPost,}: { placeholder: string; autoFocus?: boolean; onPost: (body: string) => Promise<void>;}) { const [busy, setBusy] = useState(false);
async function submit(e: FormEvent<HTMLFormElement>) { e.preventDefault(); const form = e.currentTarget; const body = String(new FormData(form).get("body")).trim(); if (!body) return; setBusy(true); try { await onPost(body); form.reset(); } finally { setBusy(false); } }
// Enter sends, shift+enter makes a newline — chat-fast, per the brief. function onKeyDown(e: KeyboardEvent<HTMLTextAreaElement>) { if (e.key === "Enter" && !e.shiftKey) { e.preventDefault(); e.currentTarget.form?.requestSubmit(); } }
return ( <form className="composer" onSubmit={submit}> <textarea name="body" rows={2} placeholder={placeholder} autoFocus={autoFocus} onKeyDown={onKeyDown} required /> <button className="btn" type="submit" disabled={busy}> post </button> </form> );}