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

main
ts 102 lines 4.11 KB
Raw
1 import { squashAtomCommandFailure } from "@t3tools/client-runtime/state/runtime";
2 import {
3 findProjectByPath,
4 inferProjectTitleFromPath,
5 } from "@t3tools/client-runtime/state/projects";
6 import { useAtomValue } from "@effect/atom-react";
7 import type { DesktopAppActivationRequest } from "@t3tools/contracts";
8 import { msg } from "gt-react";
9
10 import { handleDesktopAppActivationRequest } from "../desktopAppActivation";
11 import { m } from "../i18n/messages";
12 import { newProjectId } from "../lib/utils";
13 import { readProjects, waitForProject } from "../state/entities";
14 import { useEnvironment } from "../state/environments";
15 import { localEnvironmentIdAtom } from "../state/primaryEnvironment";
16 import { projectEnvironment } from "../state/projects";
17 import { useEnvironmentShellHasSnapshot } from "../state/shell";
18 import { useAtomCommand } from "../state/use-atom-command";
19 import { useOrchestrationCommand } from "../state/use-orchestration-command";
20 import { reportProjectFailure } from "./useProjectGroupUpdate";
21 import { useNewThreadHandler } from "./useHandleNewThread";
22
23 /**
24 * Opens a folder on this computer as a project in this computer's environment: an
25 * existing project opens (and unarchives), a new one is added. Used by `t3 <dir>`
26 * activations and folders dropped on the sidebar. `ready` is false until that
27 * environment's projects have loaded, so a known folder is not added twice.
28 */
29 export function useOpenLocalWorkspace() {
30 const localEnvironment = useEnvironment(useAtomValue(localEnvironmentIdAtom));
31 const createProject = useAtomCommand(projectEnvironment.create, { reportFailure: false });
32 const updateProject = useOrchestrationCommand(projectEnvironment.update, {
33 label: "project unarchive",
34 });
35 const openThread = useNewThreadHandler();
36 const shellHasSnapshot = useEnvironmentShellHasSnapshot(localEnvironment?.environmentId ?? null);
37 const ready =
38 localEnvironment?.connection.phase === "connected" &&
39 localEnvironment.serverConfig !== null &&
40 shellHasSnapshot;
41
42 const open = (request: DesktopAppActivationRequest) =>
43 handleDesktopAppActivationRequest(request, {
44 getTarget: () => {
45 if (
46 localEnvironment?.connection.phase !== "connected" ||
47 localEnvironment.serverConfig === null
48 ) {
49 return null;
50 }
51 return {
52 environmentId: localEnvironment.environmentId,
53 platform: localEnvironment.serverConfig.environment.platform.os,
54 };
55 },
56 findProject: (environmentId, workspaceRoot) =>
57 findProjectByPath(
58 readProjects().filter((project) => project.environmentId === environmentId),
59 workspaceRoot,
60 ) ?? null,
61 createProject: async (environmentId, workspaceRoot) => {
62 const projectId = newProjectId();
63 const result = await createProject({
64 environmentId,
65 input: {
66 projectId,
67 title: inferProjectTitleFromPath(workspaceRoot),
68 workspaceRoot,
69 createWorkspaceRootIfMissing: false,
70 defaultModelSelection: null,
71 },
72 });
73 if (result._tag === "Failure") {
74 const error = squashAtomCommandFailure(result);
75 throw error instanceof Error
76 ? error
77 : new Error(m(msg("T3 Code could not add the project.")));
78 }
79 return projectId;
80 },
81 waitForProject: async (projectRef) => {
82 await waitForProject(projectRef);
83 },
84 openThread: (projectRef) => {
85 const project = readProjects().find(
86 (candidate) =>
87 candidate.environmentId === projectRef.environmentId &&
88 candidate.id === projectRef.projectId,
89 );
90 // Opening an archived project's folder again brings the project back.
91 if (project?.archivedAt != null) {
92 void updateProject({
93 environmentId: projectRef.environmentId,
94 input: { projectId: projectRef.projectId, archived: false },
95 }).then((result) => reportProjectFailure(m(msg("Failed to unarchive project")), result));
96 }
97 return openThread(projectRef);
98 },
99 });
100
101 return { ready, open };
102 }