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

olderSettledShelf.ts

120 lines4.8 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 useEffect(() => {
40 const releases = (JSON.parse(shelfKey) as typeof shelfScopes).flatMap(({ scope, pages }) =>
41 pages === 0
42 ? []
43 : [
44 threadFamilies.mountRows(
45 appAtomRegistry,
46 scope.environmentId,
47 olderSettledThreads.rowsKey(scope, pages),
48 olderSettledThreads.pageRowsAtom(scope, pages),
49 ),
50 ],
51 );
52 return () => {
53 for (const release of releases) release();
54 };
55 }, [shelfKey]);
56​
57 const olderOf = (environmentId: EnvironmentId) => shelf.get(environmentId)?.older ?? null;
58 const pagesOf = (environmentId: EnvironmentId) => shelf.get(environmentId)?.pages;
59 const frontier = settledShelfFrontier([...shelf.values()]);
60 const loaded = shelfScopes.filter(({ pages }) => pages > 0);
61 return {
62 environments: shelf,
63 visibleCount: input.expanded ? input.visibleCount : 0,
64 pages,
65 grow: (next: ReadonlyMap<EnvironmentId, number>) => setRequested({ scopeKey, pages: next }),
66 isListed: (thread: EnvironmentThreadShell) => isListedSettledThread(thread.source, frontier),
67 /** Older settled threads in scope that are not local; add it to the local Settled rows. */
68 unloadedCount: (threads: ReadonlyArray<EnvironmentThreadShell>) =>
69 unloadedOlderSettledCount({ olderSettled: olderOf, scopes: input.scopes, threads }),
70 loading: loaded.some(({ scope }) => pagesOf(scope.environmentId)?.status === "pending"),
71 failed: loaded.some(({ scope }) => pagesOf(scope.environmentId)?.status === "failed"),
72 retry: () => {
73 for (const { scope, pages } of loaded) {
74 olderSettledThreads.retryPages(appAtomRegistry, scope, pages);
75 }
76 },
77 };
79​
80/** Reads the next pages while the shelf lists fewer rows than it shows. */
81export function useOlderSettledShelfGrowth(
82 shelf: ReturnType<typeof useOlderSettledShelf>,
83 listedSettledThreads: ReadonlyArray<EnvironmentThreadShell>,
84) {
85 const wanted = wantedOlderSettledPages({
86 requested: shelf.pages,
87 environments: shelf.environments,
88 listedCount: listedSettledThreads.length,
89 visibleCount: shelf.visibleCount,
90 });
91 const { grow, pages } = shelf;
92 useEffect(() => {
93 if (wanted !== pages) grow(wanted);
94 }, [wanted, pages, grow]);
96​
97/**
98 * Threads of these projects that this client does not hold (older settled threads), for counts
99 * shown outside the shelf, such as a removal confirm.
100 */
101export function readUnloadedThreadCount(
102 members: ReadonlyArray<{ readonly environmentId: EnvironmentId; readonly id: ProjectId }>,
103 threads: ReadonlyArray<EnvironmentThreadShell>,
104): number {
105 return unloadedOlderSettledCount({
106 olderSettled: (environmentId) =>
107 appAtomRegistry.get(olderSettledThreads.olderSettledAtom(environmentId)),
108 scopes: [...new Set(members.map((member) => member.environmentId))].map((environmentId) => ({
109 environmentId,
110 projectIds: members.flatMap((member) =>
111 member.environmentId === environmentId ? [member.id] : [],
112 ),
113 })),
114 threads: threads.filter((thread) =>
115 members.some(
116 (member) => member.environmentId === thread.environmentId && member.id === thread.projectId,
117 ),
118 ),
119 });