style: readability pass on delegate-written hooks and models

This commit is contained in:
Callum Alpass committed 2026-11-02 22:37:00 +11:00
1 parent a6cfb9a2cd
commit d5b6406cae
9 files changed
+844 -309

No files matched your search

+141 -35
View File
@@ -1,5 +1,11 @@
import type { KeyEvent } from "@opentui/core";
import type { OrchestrationV2ArchivedShellSnapshot, OrchestrationV2OlderSettledCursor, OrchestrationV2ShellSnapshot, OrchestrationV2ThreadShell, ThreadId } from "@t3tools/contracts";
import type {
OrchestrationV2ArchivedShellSnapshot,
OrchestrationV2OlderSettledCursor,
OrchestrationV2ShellSnapshot,
OrchestrationV2ThreadShell,
ThreadId,
} from "@t3tools/contracts";
import { ThreadId as ThreadIdSchema } from "@t3tools/contracts";
import * as DateTime from "effect/DateTime";
import { useEffect, useMemo, useState } from "react";
@@ -10,6 +16,9 @@ import type { PaletteCommand } from "../model/palette.ts";
type Command = PaletteCommand & { readonly run: () => void };
/** How many settled threads one "show more" step reveals, as the web's shelf pages. */
const SETTLED_PAGE_SIZE = 30;
/** The sidebar's fetched shelves live separately from its live shell. */
export function useThreadLibrary(session: T3Session, snapshot: OrchestrationV2ShellSnapshot | null, query: string) {
const [archived, setArchived] = useState(false);
@@ -17,79 +26,176 @@ export function useThreadLibrary(session: T3Session, snapshot: OrchestrationV2Sh
const [older, setOlder] = useState<readonly OrchestrationV2ThreadShell[]>([]);
const [cursor, setCursor] = useState<OrchestrationV2OlderSettledCursor | null>(null);
const [end, setEnd] = useState(false);
const [limit, setLimit] = useState(30);
const [limit, setLimit] = useState(SETTLED_PAGE_SIZE);
const [loading, setLoading] = useState(false);
const [error, setError] = useState<string | null>(null);
// A new live cut-off makes every fetched older page stale, so paging starts over.
const cut = snapshot?.olderSettled?.before;
useEffect(() => {
setOlder([]);
setCursor(null);
setEnd(false);
setLimit(30);
setLimit(SETTLED_PAGE_SIZE);
}, [cut]);
const refreshArchive = () => session.archivedThreads().then(setArchive);
useEffect(() => {
if (!archived) { setLoading(false); return; }
if (!archived) {
setLoading(false);
return;
}
let cancelled = false;
setLoading(true);
setError(null);
session.archivedThreads().then(
(value) => { if (!cancelled) { setArchive(value); setLoading(false); } },
(error) => { if (!cancelled) { setError(String(error)); setLoading(false); } },
(value) => {
if (cancelled) return;
setArchive(value);
setLoading(false);
},
(archiveError) => {
if (cancelled) return;
setError(String(archiveError));
setLoading(false);
},
);
return () => { cancelled = true; };
return () => {
cancelled = true;
};
}, [session, archived]);
const merged = useMemo(() => mergeThreadShells(snapshot, older), [snapshot, older]);
const listSnapshot = archived && snapshot ? { ...snapshot, projects: archive?.projects ?? snapshot.projects, threads: archive?.threads ?? [] } : merged;
const allRows = useMemo(() => threadRows(listSnapshot, { query, archived, settledLimit: limit }), [listSnapshot, query, archived, limit]);
const totalSettled = useMemo(() => threadRows(merged, { query, settledLimit: Infinity }).filter((row) => row.section === "settled").length, [merged, query]);
const hasOlder = !archived && (totalSettled > limit || (!end && (snapshot?.olderSettled?.counts.some((count) => count.count > 0) ?? false)));
// The archive keeps the live snapshot's shape so threadRows reads both the same way.
const listSnapshot =
archived && snapshot
? { ...snapshot, projects: archive?.projects ?? snapshot.projects, threads: archive?.threads ?? [] }
: merged;
const allRows = useMemo(
() => threadRows(listSnapshot, { query, archived, settledLimit: limit }),
[listSnapshot, query, archived, limit],
);
const totalSettled = useMemo(() => {
const settled = threadRows(merged, { query, settledLimit: Infinity });
return settled.filter((row) => row.section === "settled").length;
}, [merged, query]);
const serverHasOlder = snapshot?.olderSettled?.counts.some((count) => count.count > 0) ?? false;
const hasOlder = !archived && (totalSettled > limit || (!end && serverHasOlder));
const loadOlder = async () => {
if (loading) return;
setLimit((value) => value + 30);
setLimit((value) => value + SETTLED_PAGE_SIZE);
// Rows already fetched fill the wider limit without another request.
if (!cut || end || totalSettled > limit) return;
setLoading(true);
setError(null);
try {
const page = await session.olderSettledThreads({ before: cursor ?? { settledAt: cut }, limit: 30 });
const page = await session.olderSettledThreads({ before: cursor ?? { settledAt: cut }, limit: SETTLED_PAGE_SIZE });
setOlder((previous) => [...previous, ...page.threads]);
const last = page.threads.at(-1);
if (last?.settledAt) setCursor({ settledAt: DateTime.formatIso(last.settledAt), threadId: last.id });
setEnd(page.threads.length < 30);
} catch (error) {
setError(String(error));
if (last?.settledAt) {
setCursor({ settledAt: DateTime.formatIso(last.settledAt), threadId: last.id });
}
setEnd(page.threads.length < SETTLED_PAGE_SIZE);
} catch (loadError) {
setError(String(loadError));
} finally {
setLoading(false);
}
};
const toggle = () => { setError(null); setArchived((value) => !value); };
const toggle = () => {
setError(null);
setArchived((value) => !value);
};
const unarchive = async (id: ThreadId) => {
await session.unarchiveThread(id);
await refreshArchive();
};
const reorder = async (id: ThreadId, delta: 1 | -1) => {
const row = allRows.find((row) => row.thread.id === id);
const row = allRows.find((candidate) => candidate.thread.id === id);
// The web's Working shelf makes Active chronological, not manually reorderable.
if (row?.section === "active") { setError("Active threads are ordered by return time, as in T3 Code's Working shelf."); return; }
const plan = threadReorder(allRows, id, delta, threadRows(listSnapshot, { archived, settledLimit: limit }));
if (row?.section === "active") {
setError("Active threads are ordered by return time, as in T3 Code's Working shelf.");
return;
}
const unfilteredRows = threadRows(listSnapshot, { archived, settledLimit: limit });
const plan = threadReorder(allRows, id, delta, unfilteredRows);
if (!plan || archived) return;
try {
for (const assignment of plan.assignments) await session.reorderThread(ThreadIdSchema.make(assignment.id), plan.section, assignment.orderKey);
} catch (error) { setError(String(error)); }
for (const assignment of plan.assignments) {
await session.reorderThread(ThreadIdSchema.make(assignment.id), plan.section, assignment.orderKey);
}
} catch (reorderError) {
setError(String(reorderError));
}
};
const commands = (id: ThreadId | undefined): Command[] => [
{ id: "archived", title: archived ? "Show active threads" : "Show archived threads", shortcut: "A", keywords: ["archive", "history", "restore"], run: toggle },
...(hasOlder ? [{ id: "older-settled", title: "Show more settled threads", keywords: ["older", "history", "load"], run: () => void loadOlder() }] : []),
...(id && archive?.threads.some((thread) => thread.id === id) ? [{ id: "unarchive", title: "Unarchive thread", keywords: ["restore", "archived"], run: () => void unarchive(id).catch((error) => setError(String(error))) }] : []),
...(id && allRows.some((row) => row.thread.id === id && row.section === "pinned") ? [
{ id: "reorder-up", title: "Move pinned thread up", shortcut: "K", keywords: ["reorder", "sort"], run: () => void reorder(id, -1) },
{ id: "reorder-down", title: "Move pinned thread down", shortcut: "J", keywords: ["reorder", "sort"], run: () => void reorder(id, 1) },
] : []),
];
const key = (key: KeyEvent, id: ThreadId | undefined): boolean => {
if (key.name === "a" && key.shift) return toggle(), true;
if (id && key.shift && (key.name === "j" || key.name === "k")) return void reorder(id, key.name === "j" ? 1 : -1), true;
const commands = (id: ThreadId | undefined): Command[] => {
const list: Command[] = [
{
id: "archived",
title: archived ? "Show active threads" : "Show archived threads",
shortcut: "A",
keywords: ["archive", "history", "restore"],
run: toggle,
},
];
if (hasOlder) {
list.push({
id: "older-settled",
title: "Show more settled threads",
keywords: ["older", "history", "load"],
run: () => void loadOlder(),
});
}
if (id && archive?.threads.some((thread) => thread.id === id)) {
list.push({
id: "unarchive",
title: "Unarchive thread",
keywords: ["restore", "archived"],
run: () => void unarchive(id).catch((unarchiveError) => setError(String(unarchiveError))),
});
}
if (id && allRows.some((row) => row.thread.id === id && row.section === "pinned")) {
list.push(
{
id: "reorder-up",
title: "Move pinned thread up",
shortcut: "K",
keywords: ["reorder", "sort"],
run: () => void reorder(id, -1),
},
{
id: "reorder-down",
title: "Move pinned thread down",
shortcut: "J",
keywords: ["reorder", "sort"],
run: () => void reorder(id, 1),
},
);
}
return list;
};
const key = (keyEvent: KeyEvent, id: ThreadId | undefined): boolean => {
if (keyEvent.name === "a" && keyEvent.shift) {
toggle();
return true;
}
if (id && keyEvent.shift && (keyEvent.name === "j" || keyEvent.name === "k")) {
void reorder(id, keyEvent.name === "j" ? 1 : -1);
return true;
}
return false;
};
return { archived, allRows, merged, archive, commands, key, hasOlder, loadOlder, loading, error };
}