diff --git a/src/ui/ThreadList.tsx b/src/ui/ThreadList.tsx index af626f9..8694031 100644 --- a/src/ui/ThreadList.tsx +++ b/src/ui/ThreadList.tsx @@ -4,13 +4,25 @@ import type { ThreadId } from "@t3tools/contracts"; import { useEffect, useState } from "react"; import { ClickRow, wheelStep } from "./Button.tsx"; +import { BAR } from "./edges.ts"; import { truncate } from "./text.ts"; import { colors, statusColor } from "./theme.ts"; +const SPINNER = ["⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"]; + +/** The 2-column gutter's mark for a row: a spinner while it works, then what it needs from you. */ +function statusGlyph(row: ThreadRow, spin: number): { readonly glyph: string; readonly color: string } { + if (row.status === "working") return { glyph: SPINNER[spin % SPINNER.length]!, color: colors.accent }; + if (row.status === "failed") return { glyph: "✕", color: colors.bad }; + if (row.status === "approval" || row.status === "input" || row.status === "request" || row.status === "limited") return { glyph: "◆", color: colors.warn }; + if (row.unread) return { glyph: "●", color: colors.good }; + return { glyph: " ", color: colors.faint }; +} + /** - * The web sidebar as a list: section headers, then two-line rows of - * project + status (or time), and the title, bold when it needs you and - * dimmed while it works or once read. + * The web sidebar as a list on a panel: section headers, then two-line rows of the title + * (with its status or time on the right) over the project, a status gutter on the left, + * the title bold when it needs you and dimmed while it works or once read. */ export function ThreadList(props: { readonly rows: readonly ThreadRow[]; @@ -35,8 +47,23 @@ export function ThreadList(props: { readonly onShowMore?: () => void; }) { const { rows, selected, focused, width, height, now } = props; - // Rows take two lines plus a header per section; keep the selection in view. - const visibleCount = Math.max(1, Math.floor((height - (props.filtering ? 9 : 6)) / 2)); + const collapsed = [...props.collapsed].filter(([, count]) => count > 0); + // Active goes unlabelled when it leads the list, as in the web sidebar. + const labelActive = rows.some((row) => row.section === "pinned"); + // Rows take two lines; a header takes one, plus a blank line above each but the first. + // Reserve those lines (and the filter, shelf and folded rows) so the selection stays in view. + let headerLines = 0; + let seenHeader = false; + for (const section of rows.map((row) => row.section).filter((section, index, all) => all.indexOf(section) === index)) { + if (section !== "active" || labelActive) { + headerLines += seenHeader ? 2 : 1; + seenHeader = true; + } else if (seenHeader) { + headerLines += 1; + } + } + const reserved = (props.filtering || props.filter ? 1 : 0) + collapsed.length + (props.shelfStatus ? 1 : 0) + (props.showMore ? 1 : 0) + headerLines; + const visibleCount = Math.max(1, Math.floor((height - reserved) / 2)); const maxStart = Math.max(0, rows.length - visibleCount); // The wheel scrolls the list without moving the selection; moving the selection brings it back. const [scrolled, setScrolled] = useState(null); @@ -44,60 +71,75 @@ export function ThreadList(props: { const centred = Math.max(0, Math.min(selected - Math.floor(visibleCount / 2), maxStart)); const start = Math.min(scrolled ?? centred, maxStart); const windowRows = rows.slice(start, start + visibleCount); - const inner = Math.max(8, width - 3); - // Active goes unlabelled when it leads the list, as in the web sidebar. - const labelActive = rows.some((row) => row.section === "pinned"); + // The spinner only runs while a visible row is working. + const [spin, setSpin] = useState(0); + const anyWorking = windowRows.some((row) => row.status === "working"); + useEffect(() => { + if (!anyWorking) return; + const timer = setInterval(() => setSpin((frame) => frame + 1), 100); + return () => clearInterval(timer); + }, [anyWorking]); + // The selection bar, the gutter and one space of right padding sit around the text. + const inner = Math.max(8, width - 4); const lines: React.ReactNode[] = []; let previousSection = start > 0 ? rows[start - 1]?.section : undefined; windowRows.forEach((row, offset) => { const index = start + offset; if (row.section !== previousSection) { - if (row.section !== "active" || labelActive) lines.push(); - else if (previousSection !== undefined) lines.push( ); + if (row.section !== "active" || labelActive) { + if (lines.length > 0) lines.push( ); + lines.push(); + } else if (previousSection !== undefined) lines.push( ); previousSection = row.section; } const isSelected = index === selected; const status = statusLabel(row, now); const right = status?.text ?? relativeTime(rowTime(row.thread), now); - const left = truncate(row.projectName || "No project", Math.max(4, inner - right.length - 1)); + const project = truncate(row.projectName || "No project", inner); const attention = needsAttention(row); const recede = !isSelected && (row.status === "working" || row.status === "waiting" || (row.status === "ready" && !row.unread)); - const title = truncate(row.thread.title || "New thread", inner); + const title = truncate(row.thread.title || "New thread", Math.max(4, inner - right.length - 1)); + const { glyph, color } = statusGlyph(row, spin); + const bar = isSelected ? "▌" : " "; lines.push( props.onPick(row.thread.id)} onMenu={() => props.onMenu(row.thread.id)} > - {left} + {bar} + {`${glyph} `} + {attention ? {title} : title} - {right} + {right} + + + {bar} + {` ${project}`} - {attention ? {title} : title} , ); }); - const collapsed = [...props.collapsed].filter(([, count]) => count > 0); return ( { const step = wheelStep(event); if (step) setScrolled(Math.max(0, Math.min(maxStart, start + step))); }} > {props.filtering ? ( - - + + ) : props.filter ? ( {` /${props.filter}`} @@ -121,7 +163,7 @@ export function ThreadList(props: { function SectionHeader({ label }: { readonly label: string }) { return ( - + {label} );