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

useNewProject.ts

135 lines4.4 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";
11​
12import { stackedThreadToast, toastManager } from "~/components/ui/toast";
13import { waitForProject } from "~/state/entities";
14import { projectEnvironment } from "~/state/projects";
15import { sourceControlEnvironment } from "~/state/sourceControl";
16import { useAtomCommand } from "~/state/use-atom-command";
17import { useNewThreadHandler } from "./useHandleNewThread";
18​
19function errorMessage(error: unknown): string {
20 return error instanceof Error && error.message.trim().length > 0
21 ? error.message
22 : "An error occurred.";
24​
25export function useNewProject() {
26 const createNew = useAtomCommand(projectEnvironment.createNew, { reportFailure: false });
27 const publishRepository = useAtomCommand(sourceControlEnvironment.publishRepository, {
28 reportFailure: false,
29 });
30 const handleNewThread = useNewThreadHandler();
31​
32 const publishNewRepository = async (input: {
33 readonly environmentId: EnvironmentId;
34 readonly workspaceRoot: string;
35 readonly target: NewProjectPublishTarget;
36 }) => {
37 const { label } = input.target.definition;
38 const result = await publishRepository({
39 environmentId: input.environmentId,
40 input: {
41 cwd: input.workspaceRoot,
42 provider: input.target.definition.kind,
43 repository: getNewProjectRepository(input.target, input.workspaceRoot),
44 visibility: "private",
45 },
46 });
47 if (result._tag === "Failure") {
48 if (!isAtomCommandInterrupted(result)) {
49 toastManager.add(
50 stackedThreadToast({
51 type: "error",
52 title: `Could not create the ${label} repository`,
53 description: `${errorMessage(squashAtomCommandFailure(result))} Use Publish Repository in the Git menu to try again.`,
54 }),
55 );
56 }
57 return;
58 }
59 toastManager.add(
60 stackedThreadToast({
61 type: "success",
62 title: `Published to ${label}`,
63 description: result.value.repository.nameWithOwner,
64 }),
65 );
66 };
67​
68 return async (input: {
69 readonly environmentId: EnvironmentId;
70 readonly name: string;
71 readonly publishTo: NewProjectPublishTarget | null;
72 }): Promise<boolean> => {
73 const result = await createNew({
74 environmentId: input.environmentId,
75 input: { name: input.name },
76 });
77 if (result._tag === "Failure") {
78 if (!isAtomCommandInterrupted(result)) {
79 toastManager.add(
80 stackedThreadToast({
81 type: "error",
82 title: "Could not create the project",
83 description: errorMessage(squashAtomCommandFailure(result)),
84 }),
85 );
86 }
87 return false;
88 }
89​
90 const { projectId, workspaceRoot, commitError } = result.value;
91 toastManager.add(
92 stackedThreadToast(
93 commitError === undefined
94 ? { type: "success", title: `Created ${input.name}`, description: workspaceRoot }
95 : {
96 type: "warning",
97 title: `Created ${input.name} without a first commit`,
98 description: `${commitError} The project is in ${workspaceRoot}.`,
99 },
100 ),
101 );
102 if (input.publishTo) {
103 void publishNewRepository({
104 environmentId: input.environmentId,
105 workspaceRoot,
106 target: input.publishTo,
107 });
108 }
109​
110 const projectRef = scopeProjectRef(input.environmentId, projectId);
111 // Drafts key off the project's stored path, so wait for the create event
112 // to reach the store before opening one.
113 const project = await waitForProject(projectRef).catch((error: unknown) => {
114 toastManager.add(
115 stackedThreadToast({
116 type: "error",
117 title: "Failed to open project",
118 description: `${errorMessage(error)} It will appear in the sidebar once this client catches up.`,
119 }),
120 );
121 return null;
122 });
123 if (project === null) return true;
124 await handleNewThread(projectRef).catch((error: unknown) => {
125 toastManager.add(
126 stackedThreadToast({
127 type: "error",
128 title: "Failed to open project",
129 description: errorMessage(error),
130 }),
131 );
132 });
133 return true;
134 };