Open History to see the last commit
1import { scopedThreadKey } from "@t3tools/client-runtime/environment";
2import { offersLinkedEnvironments } from "@t3tools/client-runtime/link-request";
3import { createThreadHandoffEnvironmentAtoms } from "@t3tools/client-runtime/state/peerLinks";
4import type { EnvironmentId, ScopedThreadRef } from "@t3tools/contracts";
5import { Atom } from "effect/reactivity";
6
7import { registerThreadOverlay } from "../components/chat/threadOverlayRetention";
8import { connectionAtomRuntime } from "../connection/runtime";
9import { appAtomRegistry } from "../rpc/atomRegistry";
10import { environmentPresentations } from "./presentation";
11
12export const threadHandoffEnvironment = createThreadHandoffEnvironmentAtoms(connectionAtomRuntime);
13
14/** Whether `environmentId` offers links and this client may move its threads over one. */
15export const canMoveThreadsAtom = Atom.family((environmentId: EnvironmentId) =>
16 Atom.make(
17 (get) =>
18 offersLinkedEnvironments(
19 get(environmentPresentations.serverConfigsAtom).get(environmentId),
20 ) && get(threadHandoffEnvironment.start.permissionAtom(environmentId)),
21 ).pipe(Atom.withLabel(`thread-handoff:available:${environmentId}`)),
22);
23
24export function readCanMoveThreads(environmentId: EnvironmentId): boolean {
25 return appAtomRegistry.get(canMoveThreadsAtom(environmentId));
26}
27
28/**
29 * Which thread has the "Continue on another machine" dialog open. The thread menu and the
30 * command palette both close as their command runs, so the root renders the dialog once.
31 */
32export const threadHandoffDialogThreadAtom = Atom.make<ScopedThreadRef | null>(null).pipe(
33 Atom.keepAlive,
34 Atom.withLabel("thread-handoff:dialog-thread"),
35);
36
37registerThreadOverlay((threadKey) => {
38 const current = appAtomRegistry.get(threadHandoffDialogThreadAtom);
39 if (current !== null && scopedThreadKey(current) !== threadKey) {
40 appAtomRegistry.set(threadHandoffDialogThreadAtom, null);
41 }
42});
43
44export function openThreadHandoffDialog(threadRef: ScopedThreadRef): void {
45 appAtomRegistry.set(threadHandoffDialogThreadAtom, threadRef);
46}
47
48/** Failed moves whose notice the user closed. The thread keeps the failure until it moves again. */
49export const dismissedThreadHandoffsAtom = Atom.make<ReadonlySet<string>>(new Set<string>()).pipe(
50 Atom.keepAlive,
51 Atom.withLabel("thread-handoff:dismissed"),
52);