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

main
ts 155 lines 5.04 KB
Raw
1 import { scopeProjectRef } from "@t3tools/client-runtime/environment";
2 import {
3 getNewProjectRepository,
4 type NewProjectPublishTarget,
5 } from "@t3tools/client-runtime/operations/projects";
6 import {
7 isAtomCommandInterrupted,
8 squashAtomCommandFailure,
9 } from "@t3tools/client-runtime/state/runtime";
10 import type { EnvironmentId } from "@t3tools/contracts";
11 import { msg } from "gt-react";
12
13 import { stackedThreadToast, toastManager } from "~/components/ui/toast";
14 import { m } from "~/i18n/messages";
15 import { waitForProject } from "~/state/entities";
16 import { projectEnvironment } from "~/state/projects";
17 import { sourceControlEnvironment } from "~/state/sourceControl";
18 import { useAtomCommand } from "~/state/use-atom-command";
19 import { useNewThreadHandler } from "./useHandleNewThread";
20 import { suggestAppAccessForRemote } from "../components/sourceControlAppAccessToast";
21
22 function errorMessage(error: unknown): string {
23 return error instanceof Error && error.message.trim().length > 0
24 ? error.message
25 : m(msg("An error occurred."));
26 }
27
28 export function useNewProject() {
29 const createNew = useAtomCommand(projectEnvironment.createNew, { reportFailure: false });
30 const publishRepository = useAtomCommand(sourceControlEnvironment.publishRepository, {
31 reportFailure: false,
32 });
33 const handleNewThread = useNewThreadHandler();
34
35 const publishNewRepository = async (input: {
36 readonly environmentId: EnvironmentId;
37 readonly workspaceRoot: string;
38 readonly target: NewProjectPublishTarget;
39 }) => {
40 const { label } = input.target.definition;
41 const result = await publishRepository({
42 environmentId: input.environmentId,
43 input: {
44 cwd: input.workspaceRoot,
45 provider: input.target.definition.kind,
46 repository: getNewProjectRepository(input.target, input.workspaceRoot),
47 visibility: "private",
48 },
49 });
50 if (result._tag === "Failure") {
51 if (!isAtomCommandInterrupted(result)) {
52 toastManager.add(
53 stackedThreadToast({
54 type: "error",
55 title: m(msg("Could not create the {label} repository", { label })),
56 description: m(
57 msg("{error} Use Publish Repository in the Git menu to try again.", {
58 error: errorMessage(squashAtomCommandFailure(result)),
59 }),
60 ),
61 }),
62 );
63 }
64 return;
65 }
66 toastManager.add(
67 stackedThreadToast({
68 type: "success",
69 title: m(msg("Published to {label}", { label })),
70 description: result.value.repository.nameWithOwner,
71 }),
72 );
73 void suggestAppAccessForRemote(input.environmentId, result.value.remoteUrl);
74 };
75
76 return async (input: {
77 readonly environmentId: EnvironmentId;
78 readonly name: string;
79 readonly publishTo: NewProjectPublishTarget | null;
80 }): Promise<boolean> => {
81 const result = await createNew({
82 environmentId: input.environmentId,
83 input: { name: input.name },
84 });
85 if (result._tag === "Failure") {
86 if (!isAtomCommandInterrupted(result)) {
87 toastManager.add(
88 stackedThreadToast({
89 type: "error",
90 title: m(msg("Could not create the project")),
91 description: errorMessage(squashAtomCommandFailure(result)),
92 }),
93 );
94 }
95 return false;
96 }
97
98 const { projectId, workspaceRoot, commitError } = result.value;
99 toastManager.add(
100 stackedThreadToast(
101 commitError === undefined
102 ? {
103 type: "success",
104 title: m(msg("Created {name}", { name: input.name })),
105 description: workspaceRoot,
106 }
107 : {
108 type: "warning",
109 title: m(msg("Created {name} without a first commit", { name: input.name })),
110 description: m(
111 msg("{error} The project is in {path}.", {
112 error: commitError,
113 path: workspaceRoot,
114 }),
115 ),
116 },
117 ),
118 );
119 if (input.publishTo) {
120 void publishNewRepository({
121 environmentId: input.environmentId,
122 workspaceRoot,
123 target: input.publishTo,
124 });
125 }
126
127 const projectRef = scopeProjectRef(input.environmentId, projectId);
128 // Wait for the stored project path; drafts use it as their key.
129 const project = await waitForProject(projectRef).catch((error: unknown) => {
130 toastManager.add(
131 stackedThreadToast({
132 type: "error",
133 title: m(msg("Failed to open project")),
134 description: m(
135 msg("{error} It will appear in the sidebar once this client catches up.", {
136 error: errorMessage(error),
137 }),
138 ),
139 }),
140 );
141 return null;
142 });
143 if (project === null) return true;
144 await handleNewThread(projectRef).catch((error: unknown) => {
145 toastManager.add(
146 stackedThreadToast({
147 type: "error",
148 title: m(msg("Failed to open project")),
149 description: errorMessage(error),
150 }),
151 );
152 });
153 return true;
154 };
155 }