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