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