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 { AsyncResult } from "effect/reactivity";
5import * as Cause from "effect/Cause";
6import { AuthFilesystemReadScope, AuthSourceControlWriteScope } from "@t3tools/contracts";
7import type {
8 EnvironmentProject,
9 EnvironmentThreadShell,
10} from "@t3tools/client-runtime/state/shell";
11import { NATIVE_LIQUID_GLASS_SUPPORTED } from "../../native/native-glass";
12import { useAppearancePreferences } from "../settings/appearance/AppearancePreferencesProvider";
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 navigation = useNavigation();
26 const { themeVariables } = useAppearancePreferences();
27 const environmentId = props.thread.environmentId;
28 const canReadFiles = useEnvironmentScope(environmentId, AuthFilesystemReadScope);
29 const canWriteGit = useEnvironmentScope(environmentId, AuthSourceControlWriteScope);
30 const status = useEnvironmentQuery(
31 props.cwd ? vcsEnvironment.status({ environmentId, input: { cwd: props.cwd } }) : null,
32 );
33 const target = { environmentId, cwd: props.cwd };
34 const progress = useVcsActionState(target);
35 const runAction = useAtomCommand(vcsActionManager.runStackedAction(target), {
36 reportFailure: false,
37 });
38 const pull = useAtomCommand(vcsEnvironment.pull, { reportFailure: false });
39 const openFiles = () => {
40 if (!props.cwd || !canReadFiles) return;
41 navigation.navigate("NewTaskFiles", {
42 environmentId,
43 cwd: props.cwd,
44 projectName: props.project.title,
45 });
46 };
47 const requiresThread = () =>
48 Dialog.alert("Available after the first message", "This action needs a saved thread.");
49 return (
50 <ThreadHeader
51 draft
52 title="New thread"
53 subtitle=""
54 headerColor={themeVariables["--color-header"]}
55 usesNativeHeaderGlass={NATIVE_LIQUID_GLASS_SUPPORTED}
56 hasThreadCwd={props.cwd !== null}
57 hasWorkspaceRoot={Boolean(props.project.workspaceRoot)}
58 fileInspectorSupported={false}
59 inspectorMode={null}
60 onToggleInspector={openFiles}
61 onOpenFilesInspector={openFiles}
62 onOpenGitInspector={requiresThread}
63 gitControls={{
64 environmentId,
65 threadId: props.thread.id,
66 hasThread: false,
67 currentBranch: props.thread.branch,
68 gitStatus: status.data,
69 gitOperationLabel: progress.currentLabel,
70 canOpenTerminal: false,
71 canOperateTerminal: false,
72 canOpenFiles: canReadFiles && props.cwd !== null,
73 projectScripts: [],
74 terminalSessions: [],
75 onOpenFilesInspector: openFiles,
76 onOpenGitInspector: requiresThread,
77 onOpenTerminal: requiresThread,
78 onOpenNewTerminal: requiresThread,
79 onRunProjectScript: async () => {
80 requiresThread();
81 },
82 onPull: async () => {
83 if (!canWriteGit || !props.cwd) return;
84 const result = await pull({ environmentId, input: { cwd: props.cwd } });
85 if (AsyncResult.isFailure(result))
86 Dialog.alert("Could not pull", String(Cause.squash(result.cause)));
87 },
88 onRunAction: async (input) => {
89 if (!canWriteGit || !props.cwd) return null;
90 const result = await runAction({ ...input, actionId: uuidv4() });
91 if (AsyncResult.isFailure(result)) {
92 Dialog.alert("Git action failed", String(Cause.squash(result.cause)));
93 return null;
94 }
95 return result.value;
96 },
97 }}
98 />
99 );
100}