@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 04:45 UTC

main
ts 124 lines 5.17 KB
Raw
1 import { useAtomValue } from "@effect/atom-react";
2 import type { EnvironmentThreadShell } from "@t3tools/client-runtime/state/models";
3 import {
4 isListedSettledThread,
5 settledShelfFrontier,
6 sortOlderSettledScopes,
7 unloadedOlderSettledCount,
8 wantedOlderSettledPages,
9 type OlderSettledScope,
10 } from "@t3tools/client-runtime/state/threads";
11 import type { EnvironmentId, ProjectId } from "@t3tools/contracts";
12 import { useEffect, useState } from "react";
13
14 import { appAtomRegistry } from "../rpc/atomRegistry";
15 import { olderSettledThreads, threadFamilies } from "./threads";
16
17 const NO_PAGES: ReadonlyMap<EnvironmentId, number> = new Map();
18
19 /**
20 * The Settled shelf for one scope when shells leave older settled threads out: which rows it may
21 * list, how many threads it holds in all, and the pages read below each environment's cut, whose
22 * rows merge into the thread list. `useOlderSettledShelfGrowth` reads the next pages.
23 */
24 export function useOlderSettledShelf(input: {
25 readonly scopes: ReadonlyArray<OlderSettledScope>;
26 /** A collapsed shelf mounts no pages, so a reconnect reads none; expanding restores them. */
27 readonly expanded: boolean;
28 /** Rows the shelf shows; pages are read until it lists that many. */
29 readonly visibleCount: number;
30 }) {
31 const scopes = sortOlderSettledScopes(input.scopes);
32 const scopeKey = JSON.stringify(scopes);
33 const [requested, setRequested] = useState({ scopeKey, pages: NO_PAGES });
34 const pages = requested.scopeKey === scopeKey && input.expanded ? requested.pages : NO_PAGES;
35 const shelfScopes = scopes.map((scope) => ({
36 scope,
37 pages: pages.get(scope.environmentId) ?? 0,
38 }));
39 const shelfKey = JSON.stringify(shelfScopes);
40 const shelf = useAtomValue(olderSettledThreads.shelfAtom(shelfScopes));
41 // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- mounts the requested older settled pages into the thread family registry while shown
42 useEffect(() => {
43 const releases = (JSON.parse(shelfKey) as typeof shelfScopes).flatMap(({ scope, pages }) =>
44 pages === 0
45 ? []
46 : [
47 threadFamilies.mountRows(
48 appAtomRegistry,
49 scope.environmentId,
50 olderSettledThreads.rowsKey(scope, pages),
51 olderSettledThreads.pageRowsAtom(scope, pages),
52 ),
53 ],
54 );
55 return () => {
56 for (const release of releases) release();
57 };
58 }, [shelfKey]);
59
60 const olderOf = (environmentId: EnvironmentId) => shelf.get(environmentId)?.older ?? null;
61 const pagesOf = (environmentId: EnvironmentId) => shelf.get(environmentId)?.pages;
62 const frontier = settledShelfFrontier([...shelf.values()]);
63 const loaded = shelfScopes.filter(({ pages }) => pages > 0);
64 return {
65 environments: shelf,
66 visibleCount: input.expanded ? input.visibleCount : 0,
67 pages,
68 grow: (next: ReadonlyMap<EnvironmentId, number>) => setRequested({ scopeKey, pages: next }),
69 isListed: (thread: EnvironmentThreadShell) => isListedSettledThread(thread.source, frontier),
70 /** Older settled threads in scope that are not local; add it to the local Settled rows. */
71 unloadedCount: (threads: ReadonlyArray<EnvironmentThreadShell>) =>
72 unloadedOlderSettledCount({ olderSettled: olderOf, scopes, threads }),
73 loading: loaded.some(({ scope }) => pagesOf(scope.environmentId)?.status === "pending"),
74 failed: loaded.some(({ scope }) => pagesOf(scope.environmentId)?.status === "failed"),
75 retry: () => {
76 for (const { scope, pages } of loaded) {
77 olderSettledThreads.retryPages(appAtomRegistry, scope, pages);
78 }
79 },
80 };
81 }
82
83 /** Reads the next pages while the shelf lists fewer rows than it shows. */
84 export function useOlderSettledShelfGrowth(
85 shelf: ReturnType<typeof useOlderSettledShelf>,
86 listedSettledThreads: ReadonlyArray<EnvironmentThreadShell>,
87 ) {
88 const wanted = wantedOlderSettledPages({
89 requested: shelf.pages,
90 environments: shelf.environments,
91 listedCount: listedSettledThreads.length,
92 visibleCount: shelf.visibleCount,
93 });
94 const { grow, pages } = shelf;
95 // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- requests more older settled pages when the listed threads cannot fill the visible count
96 useEffect(() => {
97 if (wanted !== pages) grow(wanted);
98 }, [wanted, pages, grow]);
99 }
100
101 /**
102 * Threads of these projects that this client does not hold (older settled threads), for counts
103 * shown outside the shelf, such as a removal confirm.
104 */
105 export function readUnloadedThreadCount(
106 members: ReadonlyArray<{ readonly environmentId: EnvironmentId; readonly id: ProjectId }>,
107 threads: ReadonlyArray<EnvironmentThreadShell>,
108 ): number {
109 return unloadedOlderSettledCount({
110 olderSettled: (environmentId) =>
111 appAtomRegistry.get(olderSettledThreads.olderSettledAtom(environmentId)),
112 scopes: [...new Set(members.map((member) => member.environmentId))].map((environmentId) => ({
113 environmentId,
114 projectIds: members.flatMap((member) =>
115 member.environmentId === environmentId ? [member.id] : [],
116 ),
117 })),
118 threads: threads.filter((thread) =>
119 members.some(
120 (member) => member.environmentId === thread.environmentId && member.id === thread.projectId,
121 ),
122 ),
123 });
124 }