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

useThreadWorkspace.ts

56 lines2.1 KB

1import { useAtomValue } from "@effect/atom-react";
2import { scopeProjectRef } from "@t3tools/client-runtime/environment";
3import type { EnvironmentThreadShell } from "@t3tools/client-runtime/state/shell";
4import { missingWorkspacePath, type EnvironmentId } from "@t3tools/contracts";
5​
6import { useProject } from "../../state/entities";
7import { filesystemEnvironment } from "../../state/filesystem";
8import { projectEnvironment } from "../../state/projects";
9import { useEnvironmentQuery } from "../../state/query";
10import { environmentServerConfigsAtom } from "../../state/server";
11​
12export function useCanMoveThreadWorkspace(environmentId: EnvironmentId | null) {
13 const permitted = useAtomValue(
14 projectEnvironment.moveThreadWorkspace.permissionAtom(environmentId),
15 );
16 return useAtomValue(
17 environmentServerConfigsAtom,
18 (configs) =>
19 permitted &&
20 environmentId !== null &&
21 configs.get(environmentId)?.environment.capabilities.threadWorkspaceMoves === true,
22 );
24​
25export function useMissingWorkspace(thread: EnvironmentThreadShell | null | undefined) {
26 const project = useProject(
27 thread ? scopeProjectRef(thread.environmentId, thread.projectId) : null,
28 );
29 const cwd = thread?.worktreePath ?? project?.workspaceRoot;
30 const recovery = useEnvironmentQuery(
31 thread && cwd && missingWorkspacePath(thread.runtime?.lastError) === cwd
32 ? filesystemEnvironment.workspaceRecovery({
33 environmentId: thread.environmentId,
34 input: {
35 path: cwd,
36 failureKey: thread.runtime?.lastErrorSource ?? thread.latestRun?.runId ?? thread.id,
37 },
38 })
39 : null,
40 );
41 if (
42 !thread ||
43 !cwd ||
44 missingWorkspacePath(thread.runtime?.lastError) !== cwd ||
45 // A failed check, such as on a connection without filesystem read, must not block sending.
46 recovery.error !== null ||
47 recovery.data?.entries[0]?.kind === "directory"
48 )
49 return null;
50 return {
51 path: cwd,
52 changeProject: thread.worktreePath === null && project?.isScratch !== true,
53 checking: recovery.isPending,
54 check: recovery.refresh,
55 };