Open History to see the last commit
1import { useAtomValue } from "@effect/atom-react";
2import { useEffect } from "react";
3import { Atom } from "effect/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";
13import { queuedThreadKeysAtom } from "./use-thread-outbox";
14
15export const threadArrangementOpenAtom = Atom.make(false).pipe(Atom.keepAlive);
16
17export const threadDropBusyAtom = Atom.make(false).pipe(Atom.keepAlive);
18
19export const pendingThreadOrderAtom = Atom.make<PendingThreadOrder | null>(null).pipe(
20 Atom.keepAlive,
21);
22
23export function usePendingThreadOrder(nowMinute: string, snoozeWakeTick: number) {
24 const pending = useAtomValue(pendingThreadOrderAtom);
25 // A timed wake can change section membership without a shell event. Use the
26 // lists' existing clocks to retire that hold and re-enable their move menus.
27 useEffect(() => {
28 getPendingThreadOrder();
29 }, [nowMinute, snoozeWakeTick]);
30 return pending;
31}
32
33let refreshPendingOrder: (() => void) | undefined;
34
35export function getPendingThreadOrder(): PendingThreadOrder | null {
36 refreshPendingOrder?.();
37 return appAtomRegistry.get(pendingThreadOrderAtom);
38}
39
40export function beginPendingThreadOrder(pending: PendingThreadOrder) {
41 const unsubscribers: (() => void)[] = [];
42 const cancel = () => {
43 if (refreshPendingOrder !== refresh) return;
44 refreshPendingOrder = undefined;
45 for (const unsubscribe of unsubscribers) unsubscribe();
46 appAtomRegistry.set(pendingThreadOrderAtom, null);
47 };
48 const refresh = () => {
49 if (refreshPendingOrder !== refresh) return;
50 const current = appAtomRegistry.get(pendingThreadOrderAtom);
51 if (current === null) return;
52 const configs = appAtomRegistry.get(environmentServerConfigsAtom);
53 const ordered = getThreadListV2OrderedSection({
54 threads: appAtomRegistry.get(environmentThreadShells.threadShellsAtom),
55 section: current.section,
56 now: new Date().toISOString(),
57 queuedThreadKeys: appAtomRegistry.get(queuedThreadKeysAtom),
58 settlementEnvironmentIds: new Set(
59 [...configs].flatMap(([id, config]) =>
60 config.environment.capabilities.threadSettlement === true ? [id] : [],
61 ),
62 ),
63 snoozeEnvironmentIds: new Set(
64 [...configs].flatMap(([id, config]) =>
65 config.environment.capabilities.threadSnooze === true ? [id] : [],
66 ),
67 ),
68 });
69 const next = reconcilePendingThreadOrder(current, ordered);
70 if (next === null) cancel();
71 else if (next !== current) appAtomRegistry.set(pendingThreadOrderAtom, next);
72 };
73 refreshPendingOrder = refresh;
74 appAtomRegistry.set(pendingThreadOrderAtom, pending);
75 unsubscribers.push(
76 appAtomRegistry.subscribe(environmentThreadShells.threadShellsAtom, refresh),
77 appAtomRegistry.subscribe(environmentServerConfigsAtom, refresh),
78 appAtomRegistry.subscribe(queuedThreadKeysAtom, refresh),
79 );
80 return {
81 isPending: () => {
82 refresh();
83 return refreshPendingOrder === refresh;
84 },
85 complete: () => {
86 if (refreshPendingOrder !== refresh) return;
87 const current = appAtomRegistry.get(pendingThreadOrderAtom);
88 if (current !== null) {
89 appAtomRegistry.set(pendingThreadOrderAtom, { ...current, commandsComplete: true });
90 refresh();
91 }
92 },
93 cancel,
94 };
95}