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

useNewProject.ts

150 lines5.0 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`),
55 description: m(
56 msg`${{ error: errorMessage(squashAtomCommandFailure(result)) }} Use Publish Repository in the Git menu to try again.`,
57 ),
58 }),
59 );
60 }
61 return;
62 }
63 toastManager.add(
64 stackedThreadToast({
65 type: "success",
66 title: m(msg`Published to ${{ label }}`),
67 description: result.value.repository.nameWithOwner,
68 }),
69 );
70 void suggestAppAccessForRemote(input.environmentId, result.value.remoteUrl);
71 };
72​
73 return async (
74 input: {
75 readonly environmentId: EnvironmentId;
76 readonly name: string;
77 readonly publishTo: NewProjectPublishTarget | null;
78 },
79 openProject: (projectRef: ScopedProjectRef, project: EnvironmentProject) => Promise<void>,
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: input.name }}`),
105 description: workspaceRoot,
106 }
107 : {
108 type: "warning",
109 title: m(msg`Created ${{ name: input.name }} without a first commit`),
110 description: m(
111 msg`${{ error: commitError }} The project is in ${{ path: workspaceRoot }}.`,
112 ),
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 // Wait for the stored project path; drafts use it as their key.
126 const project = await waitForProject(projectRef).catch((error: unknown) => {
127 toastManager.add(
128 stackedThreadToast({
129 type: "error",
130 title: m(msg("Failed to open project")),
131 description: m(
132 msg`${{ error: errorMessage(error) }} It will appear in the sidebar once this client catches up.`,
133 ),
134 }),
135 );
136 return null;
137 });
138 if (project === null) return true;
139 await openProject(projectRef, project).catch((error: unknown) => {
140 toastManager.add(
141 stackedThreadToast({
142 type: "error",
143 title: m(msg("Failed to open project")),
144 description: errorMessage(error),
145 }),
146 );
147 });
148 return true;
149 };