Open History to see the last commit
1import { scopeProjectRef } from "@t3tools/client-runtime/environment";
2import { availableScratchWorkspaceRoot } from "@t3tools/client-runtime/operations/projects";
3import {
4 isAtomCommandInterrupted,
5 squashAtomCommandFailure,
6} from "@t3tools/client-runtime/state/runtime";
7import type { EnvironmentProject } from "@t3tools/client-runtime/state/shell";
8import type { EnvironmentId } from "@t3tools/contracts";
9import { useCallback } from "react";
10
11import { stackedThreadToast, toastManager } from "~/components/ui/toast";
12import { useEnvironments } from "~/state/environments";
13import { projectEnvironment } from "~/state/projects";
14import { useAtomCommand } from "~/state/use-atom-command";
15import { useNewThreadHandler } from "./useHandleNewThread";
16
17function reportScratchFailure(title: string, error: unknown) {
18 toastManager.add(
19 stackedThreadToast({
20 type: "error",
21 title,
22 description: error instanceof Error ? error.message : "An error occurred.",
23 }),
24 );
25}
26
27export function useScratchProject() {
28 const { environments } = useEnvironments();
29 const openScratch = useAtomCommand(projectEnvironment.openScratch, { reportFailure: false });
30 const handleNewThread = useNewThreadHandler();
31
32 const scratchWorkspaceRootFor = useCallback(
33 (environmentId: EnvironmentId | null): string | null => {
34 const environment = environments.find((entry) => entry.environmentId === environmentId);
35 return availableScratchWorkspaceRoot(
36 environment?.connection.phase,
37 environment?.serverConfig,
38 );
39 },
40 [environments],
41 );
42
43 const scratchEnvironmentId = useCallback(
44 (current: EnvironmentId | null): EnvironmentId | null => {
45 if (current !== null) return scratchWorkspaceRootFor(current) !== null ? current : null;
46 const offering = environments.filter(
47 (entry) => scratchWorkspaceRootFor(entry.environmentId) !== null,
48 );
49 return offering.length === 1 ? (offering[0]?.environmentId ?? null) : null;
50 },
51 [environments, scratchWorkspaceRootFor],
52 );
53
54 const openScratchProject = useCallback(
55 async (
56 environmentId: EnvironmentId,
57 failureTitle = "Could not start without a project",
58 ): Promise<EnvironmentProject | null> => {
59 const result = await openScratch({ environmentId, input: {} });
60 if (result._tag === "Success") return result.value;
61 if (!isAtomCommandInterrupted(result)) {
62 reportScratchFailure(failureTitle, squashAtomCommandFailure(result));
63 }
64 return null;
65 },
66 [openScratch],
67 );
68
69 const startScratchThread = useCallback(
70 async (environmentId: EnvironmentId) => {
71 const project = await openScratchProject(environmentId);
72 if (project) {
73 await handleNewThread(scopeProjectRef(project.environmentId, project.id)).catch(
74 (error: unknown) => reportScratchFailure("Could not start without a project", error),
75 );
76 }
77 },
78 [handleNewThread, openScratchProject],
79 );
80
81 return { scratchWorkspaceRootFor, scratchEnvironmentId, openScratchProject, startScratchThread };
82}