import { 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(); const navigate = useNavigate(); const { hash } = useLocation(); const [messages, setMessages] = useState(null); const [error, setError] = useState(null); const [replyTo, setReplyTo] = useState(null); const scrollRef = useRef(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

error: {error}

; if (!messages) return

loading…

; 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 (
{/* Project identity (name, description, repo) lives on AboutPage now. */} {roots.length === 0 ? (

nothing here yet — start the first thread

) : (
    {roots.map((msg) => (
  • @{msg.author}{" "} {timeAgo(msg.createdAt)}

    {msg.body}

      {repliesOf(msg.id).map((reply) => (
    • @{reply.author}{" "} {timeAgo(reply.createdAt)}

      {reply.body}

    • ))}

    {msg.cutIssueNumber != null && ( ✂ issue #{msg.cutIssueNumber} )} {user && replyTo !== msg.id && ( )} {user && msg.cutIssueNumber == null && ( )}

    {user && replyTo === msg.id && ( post(body, msg.id)} /> )}
  • ))}
)}
{!user ? (

sign in to start a thread

) : spool.threadsLocked && !spool.canStartThread ? (