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

useStartTriage.ts

106 lines3.6 KB

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