Open History to see the last commit
1import { uuidv4 } from "../../lib/uuid";
2import * as Dialog from "../../components/ui/Dialog";
3import { useNavigation } from "@react-navigation/native";
4import { vcsActionLabelText } from "@t3tools/client-runtime/state/vcs";
5import { useGT, useMessages } from "gt-react-native";
6import { AsyncResult } from "effect/reactivity";
7import * as Cause from "effect/Cause";
8import { AuthFilesystemReadScope, AuthSourceControlWriteScope } from "@t3tools/contracts";
9import type {
10 EnvironmentProject,
11 EnvironmentThreadShell,
12} from "@t3tools/client-runtime/state/shell";
13import { useEnvironmentQuery } from "../../state/query";
14import { useEnvironmentScope } from "../../state/session";
15import { useAtomCommand } from "../../state/use-atom-command";
16import { vcsActionManager, vcsEnvironment } from "../../state/vcs";
17import { useVcsActionState } from "../../state/use-vcs-action-state";
18import { ThreadHeader } from "./ThreadHeader";
19
20export 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 tracksRepositories: (props.project.trackedFolders?.length ?? 0) > 0,
63 currentBranch: props.thread.branch,
64 gitStatus: status.data,
65 gitOperationLabel: vcsActionLabelText(progress, messages),
66 canOpenTerminal: false,
67 canOperateTerminal: false,
68 canOpenFiles: canReadFiles && props.cwd !== null,
69 projectScripts: [],
70 terminalSessions: [],
71 onOpenFilesInspector: openFiles,
72 onOpenGitInspector: requiresThread,
73 onOpenTerminal: requiresThread,
74 onOpenNewTerminal: requiresThread,
75 onRunProjectScript: async () => {
76 requiresThread();
77 },
78 onPull: async () => {
79 if (!canWriteGit || !props.cwd) return;
80 const result = await pull({ environmentId, input: { cwd: props.cwd } });
81 if (AsyncResult.isFailure(result))
82 Dialog.alert(gt("Could not pull"), String(Cause.squash(result.cause)));
83 },
84 onRunAction: async (input) => {
85 if (!canWriteGit || !props.cwd) return null;
86 const result = await runAction({
87 ...input,
88 projectId: props.project.id,
89 actionId: uuidv4(),
90 });
91 if (AsyncResult.isFailure(result)) {
92 Dialog.alert(gt("Git action failed"), String(Cause.squash(result.cause)));
93 return null;
94 }
95 return result.value;
96 },
97 }}
98 />
99 );
100}