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

useNewProject.ts

151 lines5.2 KB

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