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

useStartTriage.ts

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