Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

olderSettledShelf.ts

122 lines5.1 KB

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