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

useNewProject.ts

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