Open History to see the last commit
1import { useAtomValue } from "@effect/atom-react";
2import { defaultHandoffTarget } from "@t3tools/client-runtime/state/peerLinks";
3import {
4 isAtomCommandInterrupted,
5 squashAtomCommandFailure,
6} from "@t3tools/client-runtime/state/runtime";
7import type { EnvironmentId, ProjectId, ScopedThreadRef } from "@t3tools/contracts";
8import { Link } from "@tanstack/react-router";
9import { T, useGT, Var } from "gt-react";
10import { useState } from "react";
11
12import { appAtomRegistry } from "~/rpc/atomRegistry";
13import { useEnvironmentMachines } from "~/state/environments";
14import { useEnvironmentQuery } from "~/state/query";
15import { threadHandoffDialogThreadAtom, threadHandoffEnvironment } from "~/state/threadHandoff";
16import { useAtomCommand } from "~/state/use-atom-command";
17import { EnvironmentMachineIcon } from "../EnvironmentMachineIcon";
18import { Button } from "../ui/button";
19import {
20 Dialog,
21 DialogDescription,
22 DialogFooter,
23 DialogHeader,
24 DialogPanel,
25 DialogPopup,
26 DialogTitle,
27} from "../ui/dialog";
28import { Radio, RadioGroup } from "../ui/radio-group";
29import { Spinner } from "../ui/spinner";
30import { useHandoffProject } from "./useHandoffProject";
31import { stackedThreadToast, toastManager } from "../ui/toast";
32
33export function ThreadHandoffDialogHost() {
34 const threadRef = useAtomValue(threadHandoffDialogThreadAtom);
35 if (threadRef === null) return null;
36 return (
37 <ThreadHandoffDialog
38 key={`${threadRef.environmentId}:${threadRef.threadId}`}
39 threadRef={threadRef}
40 onClose={() => appAtomRegistry.set(threadHandoffDialogThreadAtom, null)}
41 />
42 );
43}
44
45function ThreadHandoffDialog({
46 threadRef,
47 onClose,
48}: {
49 readonly threadRef: ScopedThreadRef;
50 readonly onClose: () => void;
51}) {
52 const gt = useGT();
53 const machines = useEnvironmentMachines();
54 const targets = useEnvironmentQuery(
55 threadHandoffEnvironment.options({
56 environmentId: threadRef.environmentId,
57 input: { threadId: threadRef.threadId },
58 }),
59 );
60 const start = useAtomCommand(threadHandoffEnvironment.start, { reportFailure: false });
61 const [picked, setPicked] = useState<EnvironmentId | null>(null);
62 const options = targets.data?.options ?? [];
63 const chosen = defaultHandoffTarget(options, picked);
64 const needsProject = chosen?.projectSelectionRequired === true;
65 const project = useHandoffProject(
66 threadRef,
67 chosen?.environmentId,
68 needsProject,
69 false,
70 chosen?.projectChoices,
71 );
72
73 // The move outlasts the dialog: the thread's own notice shows how it goes.
74 const move = (projectId?: ProjectId) => {
75 if (chosen === undefined) return;
76 onClose();
77 void start({
78 environmentId: threadRef.environmentId,
79 input: {
80 threadId: threadRef.threadId,
81 environmentId: chosen.environmentId,
82 ...(projectId === undefined ? {} : { projectId }),
83 },
84 }).then((result) => {
85 if (result._tag !== "Failure" || isAtomCommandInterrupted(result)) return;
86 const error = squashAtomCommandFailure(result);
87 toastManager.add(
88 stackedThreadToast({
89 type: "error",
90 title: gt`Could not move the thread to ${{ machine: chosen.label }}`,
91 ...(error instanceof Error ? { description: error.message } : {}),
92 }),
93 );
94 });
95 };
96
97 return (
98 <Dialog
99 open
100 onOpenChange={(open) => {
101 if (!open) onClose();
102 }}
103 >
104 <DialogPopup className="max-w-lg">
105 <DialogHeader>
106 <DialogTitle>
107 <T>Continue on another machine</T>
108 </DialogTitle>
109 <DialogDescription>
110 <T>
111 The thread moves with its conversation and its code, including commits you have not
112 pushed and uncommitted changes. This copy becomes read-only.
113 </T>
114 </DialogDescription>
115 </DialogHeader>
116 <DialogPanel>
117 {targets.data === null && targets.error === null ? (
118 <p className="flex items-center gap-2 text-muted-foreground text-sm">
119 <Spinner size="sm" />
120 <T>Asking linked machines…</T>
121 </p>
122 ) : targets.data === null ? (
123 <p role="alert" className="text-destructive text-sm">
124 {targets.error}
125 </p>
126 ) : options.length === 0 ? (
127 <p className="text-muted-foreground text-sm">
128 {gt`This machine has no linked machines.`}{" "}
129 <Link to="/settings/connections" className="underline" onClick={onClose}>
130 {gt`Link one in Settings`}
131 </Link>
132 </p>
133 ) : (
134 <RadioGroup
135 aria-label={gt`Machine to continue on`}
136 value={chosen?.environmentId ?? ""}
137 onValueChange={(value) => setPicked(value as EnvironmentId)}
138 >
139 {options.map((option) => (
140 <label
141 key={option.environmentId}
142 className="flex items-start gap-2.5 text-sm has-data-disabled:text-muted-foreground"
143 >
144 <Radio
145 className="mt-0.5"
146 value={option.environmentId}
147 disabled={option.reason !== null && !option.projectSelectionRequired}
148 />
149 <span className="flex min-w-0 flex-col gap-0.5">
150 <span className="flex min-w-0 items-center gap-1.5 font-medium">
151 <EnvironmentMachineIcon
152 kind={machines.get(option.environmentId) ?? "server"}
153 className="size-3.5 shrink-0"
154 />
155 <span className="truncate">
156 <Var>{option.label}</Var>
157 </span>
158 </span>
159 {option.reason === null ? null : (
160 <span className="text-muted-foreground text-xs">{option.reason}</span>
161 )}
162 </span>
163 </label>
164 ))}
165 </RadioGroup>
166 )}
167 {project.picker}
168 </DialogPanel>
169 <DialogFooter>
170 <Button variant="outline" size="sm" onClick={onClose}>
171 <T>Cancel</T>
172 </Button>
173 <Button
174 size="sm"
175 onClick={() => {
176 if (project.clone) project.clone(move);
177 else move(project.projectId);
178 }}
179 disabled={
180 chosen === undefined || (needsProject && !project.projectId && !project.clone)
181 }
182 >
183 {project.clone
184 ? gt`Add project and move`
185 : chosen === undefined
186 ? gt`Move thread`
187 : gt`Move to ${{ machine: chosen.label }}`}
188 </Button>
189 </DialogFooter>
190 </DialogPopup>
191 </Dialog>
192 );
193}