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

DraftThreadHeader.tsx

101 lines3.8 KB

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 cwd={props.cwd}
57 inspectorMode={null}
58 onOpenFilesInspector={openFiles}
59 gitControls={{
60 environmentId,
61 threadId: props.thread.id,
62 hasThread: false,
63 tracksRepositories: (props.project.trackedFolders?.length ?? 0) > 0,
64 currentBranch: props.thread.branch,
65 gitStatus: status.data,
66 gitOperationLabel: vcsActionLabelText(progress, messages),
67 canOpenTerminal: false,
68 canOperateTerminal: false,
69 canOpenFiles: canReadFiles && props.cwd !== null,
70 projectScripts: [],
71 terminalSessions: [],
72 onOpenFilesInspector: openFiles,
73 onOpenGitInspector: requiresThread,
74 onOpenTerminal: requiresThread,
75 onOpenNewTerminal: requiresThread,
76 onRunProjectScript: async () => {
77 requiresThread();
78 },
79 onPull: async () => {
80 if (!canWriteGit || !props.cwd) return;
81 const result = await pull({ environmentId, input: { cwd: props.cwd } });
82 if (AsyncResult.isFailure(result))
83 Dialog.alert(gt`Could not pull`, String(Cause.squash(result.cause)));
84 },
85 onRunAction: async (input) => {
86 if (!canWriteGit || !props.cwd) return null;
87 const result = await runAction({
88 ...input,
89 projectId: props.project.id,
90 actionId: uuidv4(),
91 });
92 if (AsyncResult.isFailure(result)) {
93 Dialog.alert(gt`Git action failed`, String(Cause.squash(result.cause)));
94 return null;
95 }
96 return result.value;
97 },
98 }}
99 />
100 );