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

main
ts 108 lines 4.14 KB
Raw
1 import { thrownErrorMessage } from "@t3tools/client-runtime/errors";
2 import { RegistryContext } from "@effect/atom-react";
3 import { scopeProjectRef } from "@t3tools/client-runtime/environment";
4 import { availableScratchWorkspaceRoot } from "@t3tools/client-runtime/operations/projects";
5 import {
6 isAtomCommandInterrupted,
7 runAtomCommand,
8 squashAtomCommandFailure,
9 } from "@t3tools/client-runtime/state/runtime";
10 import type { EnvironmentProject } from "@t3tools/client-runtime/state/shell";
11 import type { EnvironmentId } from "@t3tools/contracts";
12 import type { AtomRegistry } from "effect/reactivity";
13 import { msg } from "gt-react";
14 import { useContext } from "react";
15
16 import { stackedThreadToast, toastManager } from "~/components/ui/toast";
17 import { m } from "~/i18n/messages";
18 import {
19 type EnvironmentPresentation,
20 useEnvironments,
21 usePrimaryEnvironmentId,
22 } from "~/state/environments";
23 import { projectEnvironment } from "~/state/projects";
24 import { type NewThreadHandler, useNewThreadHandler } from "./useHandleNewThread";
25
26 function reportScratchFailure(title: string, error: unknown) {
27 toastManager.add(
28 stackedThreadToast({
29 type: "error",
30 title,
31 description: thrownErrorMessage(error, m) ?? m(msg("An error occurred.")),
32 }),
33 );
34 }
35
36 type ScratchEnvironment = Pick<EnvironmentPresentation, "connection" | "serverConfig"> & {
37 readonly environmentId: EnvironmentId;
38 };
39
40 export function createScratchProjectActions(input: {
41 readonly environments: () => Iterable<ScratchEnvironment>;
42 readonly primaryEnvironmentId: () => EnvironmentId | null;
43 readonly registry: AtomRegistry.AtomRegistry;
44 readonly handleNewThread: NewThreadHandler;
45 }) {
46 const scratchWorkspaceRootFor = (environmentId: EnvironmentId | null): string | null => {
47 let environment: ScratchEnvironment | undefined;
48 for (const entry of input.environments()) {
49 if (entry.environmentId === environmentId) environment = entry;
50 }
51 return availableScratchWorkspaceRoot(environment?.connection.phase, environment?.serverConfig);
52 };
53
54 // The primary (local) environment wins; `fallback` only applies when it offers no scratch root.
55 const scratchEnvironmentId = (fallback: EnvironmentId | null): EnvironmentId | null => {
56 const primary = input.primaryEnvironmentId();
57 if (primary !== null && scratchWorkspaceRootFor(primary) !== null) return primary;
58 if (fallback !== null && scratchWorkspaceRootFor(fallback) !== null) return fallback;
59 const offering = [...input.environments()].filter(
60 (entry) => scratchWorkspaceRootFor(entry.environmentId) !== null,
61 );
62 return offering.length === 1 ? (offering[0]?.environmentId ?? null) : null;
63 };
64
65 const openScratchProject = async (
66 environmentId: EnvironmentId,
67 failureTitle = m(msg("Could not start without a project")),
68 ): Promise<EnvironmentProject | null> => {
69 const command = projectEnvironment.openScratch;
70 const result = await runAtomCommand(
71 input.registry,
72 command,
73 { environmentId, input: {} },
74 { label: command.label, reportFailure: false, reportDefect: true },
75 );
76 if (result._tag === "Success") return result.value;
77 if (!isAtomCommandInterrupted(result)) {
78 reportScratchFailure(failureTitle, squashAtomCommandFailure(result));
79 }
80 return null;
81 };
82
83 const startScratchThread = async (environmentId: EnvironmentId) => {
84 const project = await openScratchProject(environmentId);
85 if (project) {
86 await input
87 .handleNewThread(scopeProjectRef(project.environmentId, project.id))
88 .catch((error: unknown) =>
89 reportScratchFailure(m(msg("Could not start without a project")), error),
90 );
91 }
92 };
93
94 return { scratchWorkspaceRootFor, scratchEnvironmentId, openScratchProject, startScratchThread };
95 }
96
97 export function useScratchProject() {
98 const { environments } = useEnvironments();
99 const primaryEnvironmentId = usePrimaryEnvironmentId();
100 const registry = useContext(RegistryContext);
101 const handleNewThread = useNewThreadHandler();
102 return createScratchProjectActions({
103 environments: () => environments,
104 primaryEnvironmentId: () => primaryEnvironmentId,
105 registry,
106 handleNewThread,
107 });
108 }