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

threadHandoff.ts

43 lines1.9 KB

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