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

main
tsx 99 lines 3.75 KB
Raw
1 import { uuidv4 } from "../../lib/uuid";
2 import * as Dialog from "../../components/ui/Dialog";
3 import { useNavigation } from "@react-navigation/native";
4 import { vcsActionLabelText } from "@t3tools/client-runtime/state/vcs";
5 import { useGT, useMessages } from "gt-react-native";
6 import { AsyncResult } from "effect/reactivity";
7 import * as Cause from "effect/Cause";
8 import { AuthFilesystemReadScope, AuthSourceControlWriteScope } from "@t3tools/contracts";
9 import type {
10 EnvironmentProject,
11 EnvironmentThreadShell,
12 } from "@t3tools/client-runtime/state/shell";
13 import { useEnvironmentQuery } from "../../state/query";
14 import { useEnvironmentScope } from "../../state/session";
15 import { useAtomCommand } from "../../state/use-atom-command";
16 import { vcsActionManager, vcsEnvironment } from "../../state/vcs";
17 import { useVcsActionState } from "../../state/use-vcs-action-state";
18 import { ThreadHeader } from "./ThreadHeader";
19
20 export function DraftThreadHeader(props: {
21 readonly thread: EnvironmentThreadShell;
22 readonly project: EnvironmentProject;
23 readonly cwd: string | null;
24 }) {
25 const gt = useGT();
26 const messages = useMessages();
27 const navigation = useNavigation();
28 const environmentId = props.thread.environmentId;
29 const canReadFiles = useEnvironmentScope(environmentId, AuthFilesystemReadScope);
30 const canWriteGit = useEnvironmentScope(environmentId, AuthSourceControlWriteScope);
31 const status = useEnvironmentQuery(
32 props.cwd ? vcsEnvironment.status({ environmentId, input: { cwd: props.cwd } }) : null,
33 );
34 const target = { environmentId, cwd: props.cwd };
35 const progress = useVcsActionState(target);
36 const runAction = useAtomCommand(vcsActionManager.runStackedAction(target), {
37 reportFailure: false,
38 });
39 const pull = useAtomCommand(vcsEnvironment.pull, { reportFailure: false });
40 const openFiles = () => {
41 if (!props.cwd || !canReadFiles) return;
42 navigation.navigate("NewTaskFiles", {
43 environmentId,
44 cwd: props.cwd,
45 projectName: props.project.title,
46 });
47 };
48 const requiresThread = () =>
49 Dialog.alert(gt("Available after the first message"), gt("This action needs a saved thread."));
50 return (
51 <ThreadHeader
52 draft
53 title={gt("New thread")}
54 subtitle=""
55 hasThreadCwd={props.cwd !== null}
56 inspectorMode={null}
57 onOpenFilesInspector={openFiles}
58 gitControls={{
59 environmentId,
60 threadId: props.thread.id,
61 hasThread: false,
62 currentBranch: props.thread.branch,
63 gitStatus: status.data,
64 gitOperationLabel: vcsActionLabelText(progress, messages),
65 canOpenTerminal: false,
66 canOperateTerminal: false,
67 canOpenFiles: canReadFiles && props.cwd !== null,
68 projectScripts: [],
69 terminalSessions: [],
70 onOpenFilesInspector: openFiles,
71 onOpenGitInspector: requiresThread,
72 onOpenTerminal: requiresThread,
73 onOpenNewTerminal: requiresThread,
74 onRunProjectScript: async () => {
75 requiresThread();
76 },
77 onPull: async () => {
78 if (!canWriteGit || !props.cwd) return;
79 const result = await pull({ environmentId, input: { cwd: props.cwd } });
80 if (AsyncResult.isFailure(result))
81 Dialog.alert(gt("Could not pull"), String(Cause.squash(result.cause)));
82 },
83 onRunAction: async (input) => {
84 if (!canWriteGit || !props.cwd) return null;
85 const result = await runAction({
86 ...input,
87 projectId: props.project.id,
88 actionId: uuidv4(),
89 });
90 if (AsyncResult.isFailure(result)) {
91 Dialog.alert(gt("Git action failed"), String(Cause.squash(result.cause)));
92 return null;
93 }
94 return result.value;
95 },
96 }}
97 />
98 );
99 }