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

thread-order.ts

89 lines3.3 KB

1import { useAtomValue } from "@effect/atom-react";
2import { useEffect } from "react";
3import { Atom } from "effect/unstable/reactivity";
4​
5import {
6 reconcilePendingThreadOrder,
7 type PendingThreadOrder,
8} from "../features/threads/threadOrder";
9import { getThreadListV2OrderedSection } from "../features/threads/threadListV2";
10import { appAtomRegistry } from "./atom-registry";
11import { environmentServerConfigsAtom } from "./server";
12import { environmentThreadShells } from "./threads";
13​
14export const pendingThreadOrderAtom = Atom.make<PendingThreadOrder | null>(null).pipe(
15 Atom.keepAlive,
16);
17​
18export function usePendingThreadOrder(nowMinute: string, snoozeWakeTick: number) {
19 const pending = useAtomValue(pendingThreadOrderAtom);
20 // A timed wake can change section membership without a shell event. Use the
21 // lists' existing clocks to retire that hold and re-enable their move menus.
22 useEffect(() => {
23 getPendingThreadOrder();
24 }, [nowMinute, snoozeWakeTick]);
25 return pending;
27​
28let refreshPendingOrder: (() => void) | undefined;
29​
30/** Shared by Home and the navigation sidebar, including their action guards. */
31export function getPendingThreadOrder(): PendingThreadOrder | null {
32 refreshPendingOrder?.();
33 return appAtomRegistry.get(pendingThreadOrderAtom);
35​
36export function beginPendingThreadOrder(pending: PendingThreadOrder) {
37 const unsubscribers: (() => void)[] = [];
38 const cancel = () => {
39 if (refreshPendingOrder !== refresh) return;
40 refreshPendingOrder = undefined;
41 for (const unsubscribe of unsubscribers) unsubscribe();
42 appAtomRegistry.set(pendingThreadOrderAtom, null);
43 };
44 const refresh = () => {
45 if (refreshPendingOrder !== refresh) return;
46 const current = appAtomRegistry.get(pendingThreadOrderAtom);
47 if (current === null) return;
48 const configs = appAtomRegistry.get(environmentServerConfigsAtom);
49 const ordered = getThreadListV2OrderedSection({
50 threads: appAtomRegistry.get(environmentThreadShells.threadShellsAtom),
51 section: current.section,
52 now: new Date().toISOString(),
53 settlementEnvironmentIds: new Set(
54 [...configs].flatMap(([id, config]) =>
55 config.environment.capabilities.threadSettlement === true ? [id] : [],
56 ),
57 ),
58 snoozeEnvironmentIds: new Set(
59 [...configs].flatMap(([id, config]) =>
60 config.environment.capabilities.threadSnooze === true ? [id] : [],
61 ),
62 ),
63 });
64 const next = reconcilePendingThreadOrder(current, ordered);
65 if (next === null) cancel();
66 else if (next !== current) appAtomRegistry.set(pendingThreadOrderAtom, next);
67 };
68 refreshPendingOrder = refresh;
69 appAtomRegistry.set(pendingThreadOrderAtom, pending);
70 unsubscribers.push(
71 appAtomRegistry.subscribe(environmentThreadShells.threadShellsAtom, refresh),
72 appAtomRegistry.subscribe(environmentServerConfigsAtom, refresh),
73 );
74 return {
75 isPending: () => {
76 refresh();
77 return refreshPendingOrder === refresh;
78 },
79 complete: () => {
80 if (refreshPendingOrder !== refresh) return;
81 const current = appAtomRegistry.get(pendingThreadOrderAtom);
82 if (current !== null) {
83 appAtomRegistry.set(pendingThreadOrderAtom, { ...current, commandsComplete: true });
84 refresh();
85 }
86 },
87 cancel,
88 };