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

useScratchProject.ts

98 lines3.9 KB

1import { scopeProjectRef } from "@t3tools/client-runtime/environment";
2import {
3 isAtomCommandInterrupted,
4 squashAtomCommandFailure,
5} from "@t3tools/client-runtime/state/runtime";
6import type { EnvironmentProject } from "@t3tools/client-runtime/state/shell";
7import type { EnvironmentId } from "@t3tools/contracts";
8import { useCallback } from "react";
9​
10import { stackedThreadToast, toastManager } from "~/components/ui/toast";
11import { waitForProject } from "~/state/entities";
12import { useEnvironments } from "~/state/environments";
13import { projectEnvironment } from "~/state/projects";
14import { useAtomCommand } from "~/state/use-atom-command";
15import { useNewThreadHandler } from "./useHandleNewThread";
16​
17function reportScratchFailure(error: unknown) {
18 toastManager.add(
19 stackedThreadToast({
20 type: "error",
21 title: "Could not start without a project",
22 description: error instanceof Error ? error.message : "An error occurred.",
23 }),
24 );
26​
27/**
28 * Threads without a project live in the environment's scratch project, a
29 * plain folder the server owns (users see it as "No project"). The server
30 * creates it on first use; after that it is an ordinary project on the
31 * non-git path, and each thread gets its own subfolder.
32 */
33export function useScratchProject() {
34 const { environments } = useEnvironments();
35 const ensureScratch = useAtomCommand(projectEnvironment.ensureScratch, { reportFailure: false });
36 const handleNewThread = useNewThreadHandler();
37​
38 /** The scratch folder of a connected environment, or null when it offers none. */
39 const scratchWorkspaceRootFor = useCallback(
40 (environmentId: EnvironmentId | null): string | null => {
41 const environment = environments.find((entry) => entry.environmentId === environmentId);
42 return environment?.connection.phase === "connected"
43 ? (environment.serverConfig?.scratchWorkspaceRoot ?? null)
44 : null;
45 },
46 [environments],
47 );
48​
49 // A thread without a project starts on the machine the user is working on,
50 // and only there. With no current machine (the hosted app with nothing
51 // open), it starts on the one machine that offers it, never a silent pick.
52 const scratchEnvironmentId = useCallback(
53 (current: EnvironmentId | null): EnvironmentId | null => {
54 if (current !== null) return scratchWorkspaceRootFor(current) !== null ? current : null;
55 const offering = environments.filter(
56 (entry) => scratchWorkspaceRootFor(entry.environmentId) !== null,
57 );
58 return offering.length === 1 ? (offering[0]?.environmentId ?? null) : null;
59 },
60 [environments, scratchWorkspaceRootFor],
61 );
62​
63 /** Resolves to the scratch project once it is in this client's store. */
64 const openScratchProject = useCallback(
65 async (environmentId: EnvironmentId): Promise<EnvironmentProject | null> => {
66 const result = await ensureScratch({ environmentId, input: {} });
67 if (result._tag === "Failure") {
68 if (!isAtomCommandInterrupted(result)) {
69 reportScratchFailure(squashAtomCommandFailure(result));
70 }
71 return null;
72 }
73 // Drafts key off the project's stored path and settings, so wait for
74 // the create event to reach the store before targeting one.
75 return waitForProject(scopeProjectRef(environmentId, result.value.projectId)).catch(
76 (error: unknown) => {
77 reportScratchFailure(error);
78 return null;
79 },
80 );
81 },
82 [ensureScratch],
83 );
84​
85 const startScratchThread = useCallback(
86 async (environmentId: EnvironmentId) => {
87 const project = await openScratchProject(environmentId);
88 if (project) {
89 await handleNewThread(scopeProjectRef(project.environmentId, project.id)).catch(
90 reportScratchFailure,
91 );
92 }
93 },
94 [handleNewThread, openScratchProject],
95 );
96​
97 return { scratchWorkspaceRootFor, scratchEnvironmentId, openScratchProject, startScratchThread };