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

useScratchProject.ts

95 lines3.6 KB

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