The best way to control Claude Code, Codex and more from any computer or phone

main
ts 123 lines 4.01 KB
Raw
1 import { errorDisplayMessage } from "@t3tools/client-runtime/errors";
2 import { pipe } from "effect/Function";
3 import {
4 isAtomCommandInterrupted,
5 squashAtomCommandFailure,
6 } from "@t3tools/client-runtime/state/runtime";
7 import type { EnvironmentId } from "@t3tools/contracts";
8 import { useNavigation } from "@react-navigation/native";
9 import * as Clipboard from "expo-clipboard";
10 import { useGT } from "gt-react-native";
11 import { m } from "../../i18n/messages";
12
13 import * as Dialog from "../../components/ui/Dialog";
14 import { projectEnvironment } from "../../state/projects";
15 import { useAtomCommand } from "../../state/use-atom-command";
16 import {
17 createNewTaskDraft,
18 setComposerDraftText,
19 waitForComposerDraftsLoaded,
20 } from "../../state/use-composer-drafts";
21 import { useSelectWorkspaceDetail } from "../layout/workspace/useSelectWorkspaceDetail";
22 import { usePrepareTriage } from "./usePrepareTriage";
23
24 const CLI_COMMAND = "npx t3 triage";
25
26 /**
27 * In-app `t3 triage`, as on web: the server writes the session files and
28 * clones the matching source, then a No project draft opens with the prompt.
29 */
30 export function useStartTriage() {
31 const navigation = useNavigation();
32 const gt = useGT();
33 const selectDetail = useSelectWorkspaceDetail();
34 const prepare = usePrepareTriage();
35 const openScratch = useAtomCommand(projectEnvironment.openScratch, { reportFailure: false });
36
37 const fail = (message: string) =>
38 Dialog.alert(
39 gt("Could not start triage"),
40 gt("{message} Run {command} in a terminal on that machine instead.", {
41 message,
42 command: CLI_COMMAND,
43 }),
44 [
45 {
46 text: gt("Copy command"),
47 onPress: () =>
48 void Clipboard.setStringAsync(CLI_COMMAND).catch(() =>
49 Dialog.alert(
50 gt("Could not copy"),
51 gt("Type {command} instead.", { command: CLI_COMMAND }),
52 ),
53 ),
54 },
55 { text: gt("OK"), style: "cancel" },
56 ],
57 );
58 const errorMessage = (error: unknown, fallback: string) =>
59 error instanceof Error ? (errorDisplayMessage(error, m) ?? error.message) : fallback;
60
61 const activeRoute = () => {
62 const state = navigation.getState();
63 return state?.routes[state.index];
64 };
65
66 return async (environmentId: EnvironmentId) => {
67 // If the user goes elsewhere while this waits, the draft stays closed
68 // instead of pulling them away. Split view changes params, not the route.
69 const startRoute = activeRoute();
70 const routeChanged = () => {
71 const route = activeRoute();
72 if (route === startRoute) return false;
73 if (route === undefined || startRoute === undefined) return true;
74 return route.key !== startRoute.key || route.params !== startRoute.params;
75 };
76 const scratch = await openScratch({ environmentId, input: {} });
77 if (scratch._tag === "Failure") {
78 if (!isAtomCommandInterrupted(scratch)) {
79 pipe(
80 squashAtomCommandFailure(scratch),
81 (failure) =>
82 errorMessage(failure, gt("This machine cannot start threads without a project.")),
83 fail,
84 );
85 }
86 return;
87 }
88 const prepared = await prepare(environmentId);
89 if (prepared._tag === "Failure") {
90 if (!isAtomCommandInterrupted(prepared)) {
91 pipe(
92 squashAtomCommandFailure(prepared),
93 (failure) => errorMessage(failure, gt("The server could not prepare it.")),
94 fail,
95 );
96 }
97 return;
98 }
99 try {
100 await waitForComposerDraftsLoaded();
101 } catch {
102 fail(gt("Drafts could not be loaded."));
103 return;
104 }
105 if (routeChanged()) return;
106 const draftId = createNewTaskDraft({
107 environmentId,
108 projectId: scratch.value.id,
109 });
110 setComposerDraftText(draftId, prepared.value.prompt);
111 selectDetail({
112 name: "Thread",
113 params: {
114 draft: {
115 environmentId,
116 projectId: scratch.value.id,
117 title: gt("Triage", { $context: "draft thread title" }),
118 draftId,
119 },
120 },
121 });
122 };
123 }