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 } 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.";
24}
25
26export function useNewProject() {
27 const createNew = useAtomCommand(projectEnvironment.createNew, { reportFailure: false });
28 const publishRepository = useAtomCommand(sourceControlEnvironment.publishRepository, {
29 reportFailure: false,
30 });
31 const handleNewThread = useNewThreadHandler();
32
33 const publishNewRepository = useCallback(
34 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: `Could not create the ${label} repository`,
55 description: `${errorMessage(squashAtomCommandFailure(result))} Use Publish Repository in the Git menu to try again.`,
56 }),
57 );
58 }
59 return;
60 }
61 toastManager.add(
62 stackedThreadToast({
63 type: "success",
64 title: `Published to ${label}`,
65 description: result.value.repository.nameWithOwner,
66 }),
67 );
68 },
69 [publishRepository],
70 );
71
72 return useCallback(
73 async (input: {
74 readonly environmentId: EnvironmentId;
75 readonly name: string;
76 readonly publishTo: NewProjectPublishTarget | null;
77 }): Promise<boolean> => {
78 const result = await createNew({
79 environmentId: input.environmentId,
80 input: { name: input.name },
81 });
82 if (result._tag === "Failure") {
83 if (!isAtomCommandInterrupted(result)) {
84 toastManager.add(
85 stackedThreadToast({
86 type: "error",
87 title: "Could not create the project",
88 description: errorMessage(squashAtomCommandFailure(result)),
89 }),
90 );
91 }
92 return false;
93 }
94
95 const { projectId, workspaceRoot, commitError } = result.value;
96 toastManager.add(
97 stackedThreadToast(
98 commitError === undefined
99 ? { type: "success", title: `Created ${input.name}`, description: workspaceRoot }
100 : {
101 type: "warning",
102 title: `Created ${input.name} without a first commit`,
103 description: `${commitError} The project is in ${workspaceRoot}.`,
104 },
105 ),
106 );
107 if (input.publishTo) {
108 void publishNewRepository({
109 environmentId: input.environmentId,
110 workspaceRoot,
111 target: input.publishTo,
112 });
113 }
114
115 const projectRef = scopeProjectRef(input.environmentId, projectId);
116 // Drafts key off the project's stored path, so wait for the create event
117 // to reach the store before opening one.
118 const project = await waitForProject(projectRef).catch((error: unknown) => {
119 toastManager.add(
120 stackedThreadToast({
121 type: "error",
122 title: "Failed to open project",
123 description: `${errorMessage(error)} It will appear in the sidebar once this client catches up.`,
124 }),
125 );
126 return null;
127 });
128 if (project === null) return true;
129 await handleNewThread(projectRef).catch((error: unknown) => {
130 toastManager.add(
131 stackedThreadToast({
132 type: "error",
133 title: "Failed to open project",
134 description: errorMessage(error),
135 }),
136 );
137 });
138 return true;
139 },
140 [createNew, handleNewThread, publishNewRepository],
141 );
142}