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