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

useNewProject.ts

147 lines5.1 KB

1import { scopeProjectRef } from "@t3tools/client-runtime/environment";
2import { getNewProjectGitHubRepository } from "@t3tools/client-runtime/operations/projects";
3import {
4 isAtomCommandInterrupted,
5 squashAtomCommandFailure,
6} from "@t3tools/client-runtime/state/runtime";
7import type { EnvironmentId } from "@t3tools/contracts";
8import { useCallback } from "react";
9​
10import { stackedThreadToast, toastManager } from "~/components/ui/toast";
11import { waitForProject } from "~/state/entities";
12import { projectEnvironment } from "~/state/projects";
13import { sourceControlEnvironment } from "~/state/sourceControl";
14import { useAtomCommand } from "~/state/use-atom-command";
15import { useNewThreadHandler } from "./useHandleNewThread";
16​
17function errorMessage(error: unknown): string {
18 return error instanceof Error && error.message.trim().length > 0
19 ? error.message
20 : "An error occurred.";
22​
23/**
24 * Starts a project from just a name. The server makes a folder under its
25 * `newProjectsRoot` with a README, an icon, and a first commit; this then
26 * opens a new thread draft in it. With `github`, it also publishes the
27 * repository as private, without holding up the draft.
28 *
29 * Resolves to whether the project was created.
30 */
31export function useNewProject() {
32 const createNew = useAtomCommand(projectEnvironment.createNew, { reportFailure: false });
33 const publishRepository = useAtomCommand(sourceControlEnvironment.publishRepository, {
34 reportFailure: false,
35 });
36 const handleNewThread = useNewThreadHandler();
37​
38 const publishToGitHub = useCallback(
39 async (input: {
40 readonly environmentId: EnvironmentId;
41 readonly workspaceRoot: string;
42 readonly account: string | null;
43 }) => {
44 const result = await publishRepository({
45 environmentId: input.environmentId,
46 input: {
47 cwd: input.workspaceRoot,
48 provider: "github",
49 repository: getNewProjectGitHubRepository(input, input.workspaceRoot),
50 visibility: "private",
51 },
52 });
53 if (result._tag === "Failure") {
54 if (!isAtomCommandInterrupted(result)) {
55 toastManager.add(
56 stackedThreadToast({
57 type: "error",
58 title: "Could not create the GitHub repository",
59 description: `${errorMessage(squashAtomCommandFailure(result))} Use Publish Repository in the Git menu to try again.`,
60 }),
61 );
62 }
63 return;
64 }
65 toastManager.add(
66 stackedThreadToast({
67 type: "success",
68 title: "Published to GitHub",
69 description: result.value.repository.nameWithOwner,
70 }),
71 );
72 },
73 [publishRepository],
74 );
75​
76 return useCallback(
77 async (input: {
78 readonly environmentId: EnvironmentId;
79 readonly name: string;
80 readonly github: { readonly account: string | null } | null;
81 }): Promise<boolean> => {
82 const result = await createNew({
83 environmentId: input.environmentId,
84 input: { name: input.name },
85 });
86 if (result._tag === "Failure") {
87 if (!isAtomCommandInterrupted(result)) {
88 toastManager.add(
89 stackedThreadToast({
90 type: "error",
91 title: "Could not create the project",
92 description: errorMessage(squashAtomCommandFailure(result)),
93 }),
94 );
95 }
96 return false;
97 }
98​
99 const { projectId, workspaceRoot, commitError } = result.value;
100 // The folder sits in T3 Code's data directory, so always say where.
101 toastManager.add(
102 stackedThreadToast(
103 commitError === undefined
104 ? { type: "success", title: `Created ${input.name}`, description: workspaceRoot }
105 : {
106 type: "warning",
107 title: `Created ${input.name} without a first commit`,
108 description: `${commitError} The project is in ${workspaceRoot}.`,
109 },
110 ),
111 );
112 if (input.github) {
113 void publishToGitHub({
114 environmentId: input.environmentId,
115 workspaceRoot,
116 account: input.github.account,
117 });
118 }
119​
120 const projectRef = scopeProjectRef(input.environmentId, projectId);
121 // Drafts key off the project's stored path, so wait for the create event
122 // to reach the store before opening one.
123 const project = await waitForProject(projectRef).catch((error: unknown) => {
124 toastManager.add(
125 stackedThreadToast({
126 type: "error",
127 title: "Failed to open project",
128 description: `${errorMessage(error)} It will appear in the sidebar once this client catches up.`,
129 }),
130 );
131 return null;
132 });
133 if (project === null) return true;
134 await handleNewThread(projectRef).catch((error: unknown) => {
135 toastManager.add(
136 stackedThreadToast({
137 type: "error",
138 title: "Failed to open project",
139 description: errorMessage(error),
140 }),
141 );
142 });
143 return true;
144 },
145 [createNew, handleNewThread, publishToGitHub],
146 );