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

turnThreadIntoProject.ts

47 lines2.0 KB

1import {
2 isAtomCommandInterrupted,
3 runAtomCommand,
4 squashAtomCommandFailure,
5} from "@t3tools/client-runtime/state/runtime";
6import type { EnvironmentThreadShell } from "@t3tools/client-runtime/state/shell";
7import { AuthOrchestrationOperateScope } from "@t3tools/contracts";
8import * as Haptics from "expo-haptics";
9​
10import * as Dialog from "../../components/ui/Dialog";
11import { appAtomRegistry } from "../../state/atom-registry";
12import { environmentProjects, projectEnvironment } from "../../state/projects";
13import { environmentServerConfigsAtom } from "../../state/server";
14import { readEnvironmentScope } from "../../state/session";
15​
16/** A No project thread that already has its own folder, on a server that can promote it. */
17export function canTurnThreadIntoProject(thread: EnvironmentThreadShell): boolean {
18 if (thread.worktreePath === null) return false;
19 const config = appAtomRegistry.get(environmentServerConfigsAtom).get(thread.environmentId);
20 if (config?.environment.capabilities.threadProjectFromScratch !== true) return false;
21 const project = appAtomRegistry.get(
22 environmentProjects.projectAtom({
23 environmentId: thread.environmentId,
24 projectId: thread.projectId,
25 }),
26 );
27 return project?.isScratch === true;
29​
30export async function turnThreadIntoProject(thread: EnvironmentThreadShell): Promise<void> {
31 const title = "Could not turn thread into a project";
32 if (!readEnvironmentScope(thread.environmentId, AuthOrchestrationOperateScope)) {
33 Dialog.alert(title, "This connection cannot change threads.");
34 return;
35 }
36 void Haptics.impactAsync(Haptics.ImpactFeedbackStyle.Light);
37 const result = await runAtomCommand(
38 appAtomRegistry,
39 projectEnvironment.createFromThread,
40 { environmentId: thread.environmentId, input: { threadId: thread.id } },
41 { reportFailure: false },
42 );
43 if (result._tag === "Failure" && !isAtomCommandInterrupted(result)) {
44 const error = squashAtomCommandFailure(result);
45 Dialog.alert(title, error instanceof Error ? error.message : "Try again.");
46 }