|
1 | 1 | import React, { useCallback, useEffect, useMemo, useRef, useState } from "react"; |
| 2 | +import { Alert } from "react-native"; |
2 | 3 |
|
3 | 4 | import type { |
4 | 5 | EnvironmentId, |
@@ -44,6 +45,7 @@ import { scopedProjectKey } from "../../lib/scopedEntities"; |
44 | 45 | import { appAtomRegistry } from "../../state/atom-registry"; |
45 | 46 | import { projectEnvironment } from "../../state/projects"; |
46 | 47 | import { useEnvironmentQuery } from "../../state/query"; |
| 48 | +import { useAtomCommand } from "../../state/use-atom-command"; |
47 | 49 | import { |
48 | 50 | appendComposerDraftAttachments, |
49 | 51 | type ComposerDraftInsertion, |
@@ -90,6 +92,10 @@ import { |
90 | 92 | useSavedRemoteConnections, |
91 | 93 | } from "../../state/use-remote-environment-registry"; |
92 | 94 | import { availableScratchWorkspaceRoot } from "@t3tools/client-runtime/operations/projects"; |
| 95 | +import { |
| 96 | + isAtomCommandInterrupted, |
| 97 | + squashAtomCommandFailure, |
| 98 | +} from "@t3tools/client-runtime/state/runtime"; |
93 | 99 | import { isScratchProject } from "@t3tools/client-runtime/state/projects"; |
94 | 100 | import { EnvironmentProject } from "@t3tools/client-runtime/state/shell"; |
95 | 101 | import { type VcsRef } from "@t3tools/client-runtime/state/vcs"; |
@@ -201,6 +207,13 @@ type NewTaskFlowContextValue = { |
201 | 207 | */ |
202 | 208 | readonly openDraft: (draftKey: string) => boolean; |
203 | 209 | readonly selectEnvironment: (environmentId: EnvironmentId) => void; |
| 210 | + /** |
| 211 | + * Moves the draft to another machine. A draft without a project resolves to |
| 212 | + * that machine's "No project" folder first, creating it when needed. |
| 213 | + */ |
| 214 | + readonly switchEnvironment: (environmentId: EnvironmentId) => Promise<boolean>; |
| 215 | + /** The machine a switch in progress is heading to. */ |
| 216 | + readonly switchingToEnvironmentId: EnvironmentId | null; |
204 | 217 | readonly setSelectedModelKey: ( |
205 | 218 | key: string | null, |
206 | 219 | options?: ReadonlyArray<ProviderOptionSelection>, |
@@ -798,6 +811,53 @@ export function NewTaskFlowProvider(props: React.PropsWithChildren) { |
798 | 811 | [projects, selectedProject, carryDraftContentTo], |
799 | 812 | ); |
800 | 813 |
|
| 814 | + const openScratch = useAtomCommand(projectEnvironment.openScratch, { reportFailure: false }); |
| 815 | + const [switchingToEnvironmentId, setSwitchingToEnvironmentId] = useState<EnvironmentId | null>( |
| 816 | + null, |
| 817 | + ); |
| 818 | + // The latest switch wins: a slower, earlier one must not retarget the draft. |
| 819 | + const latestSwitchRef = useRef<object | null>(null); |
| 820 | + const switchEnvironment = useCallback( |
| 821 | + async (environmentId: EnvironmentId): Promise<boolean> => { |
| 822 | + if (environmentId === selectedEnvironmentId) { |
| 823 | + latestSwitchRef.current = null; |
| 824 | + setSwitchingToEnvironmentId(null); |
| 825 | + return true; |
| 826 | + } |
| 827 | + if (!isScratchDraft) { |
| 828 | + selectEnvironment(environmentId); |
| 829 | + return true; |
| 830 | + } |
| 831 | + const request = {}; |
| 832 | + latestSwitchRef.current = request; |
| 833 | + setSwitchingToEnvironmentId(environmentId); |
| 834 | + try { |
| 835 | + const result = await openScratch({ environmentId, input: {} }); |
| 836 | + if (latestSwitchRef.current !== request) return false; |
| 837 | + if (result._tag === "Success") { |
| 838 | + setProject(result.value); |
| 839 | + return true; |
| 840 | + } |
| 841 | + if (!isAtomCommandInterrupted(result)) { |
| 842 | + const error = squashAtomCommandFailure(result); |
| 843 | + Alert.alert( |
| 844 | + "Could not switch machine", |
| 845 | + error instanceof Error |
| 846 | + ? error.message |
| 847 | + : "The folder for threads without a project could not be created.", |
| 848 | + ); |
| 849 | + } |
| 850 | + return false; |
| 851 | + } finally { |
| 852 | + if (latestSwitchRef.current === request) { |
| 853 | + latestSwitchRef.current = null; |
| 854 | + setSwitchingToEnvironmentId(null); |
| 855 | + } |
| 856 | + } |
| 857 | + }, |
| 858 | + [isScratchDraft, openScratch, selectEnvironment, selectedEnvironmentId, setProject], |
| 859 | + ); |
| 860 | + |
801 | 861 | const setWorkspaceMode = useCallback( |
802 | 862 | (mode: WorkspaceMode) => { |
803 | 863 | if (!selectedProjectDraftKey) { |
@@ -1244,6 +1304,8 @@ export function NewTaskFlowProvider(props: React.PropsWithChildren) { |
1244 | 1304 | setProject, |
1245 | 1305 | openDraft, |
1246 | 1306 | selectEnvironment, |
| 1307 | + switchEnvironment, |
| 1308 | + switchingToEnvironmentId, |
1247 | 1309 | setSelectedModelKey, |
1248 | 1310 | setWorkspaceMode, |
1249 | 1311 | selectBranch, |
@@ -1310,6 +1372,8 @@ export function NewTaskFlowProvider(props: React.PropsWithChildren) { |
1310 | 1372 | openDraft, |
1311 | 1373 | selectBranch, |
1312 | 1374 | selectEnvironment, |
| 1375 | + switchEnvironment, |
| 1376 | + switchingToEnvironmentId, |
1313 | 1377 | setInteractionMode, |
1314 | 1378 | setPrompt, |
1315 | 1379 | setRuntimeMode, |
|
0 commit comments