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

use-selected-thread-worktree.ts

49 lines2.0 KB

1import type { ScopedThreadRef } from "@t3tools/contracts";
2import * as Option from "effect/Option";
3​
4import { useProject, useQuietThreadShell } from "./entities";
5import { useEnvironmentThread } from "./threads";
6import { useSelectedThreadWorktreePath, useThreadWorktreePath } from "./use-thread-detail";
7import { useThreadSelection } from "./use-thread-selection";
8import { resolvePreferredThreadWorktreePath } from "../features/terminal/terminalLaunchContext";
9​
10export function useSelectedThreadWorktree() {
11 const { selectedThread, selectedThreadProject } = useThreadSelection();
12 const detailWorktreePath = useSelectedThreadWorktreePath();
13​
14 const selectedThreadWorktreePath = resolvePreferredThreadWorktreePath({
15 threadShellWorktreePath: selectedThread?.worktreePath ?? null,
16 threadDetailWorktreePath: detailWorktreePath,
17 });
18​
19 return {
20 selectedThreadWorktreePath,
21 selectedThreadCwd: selectedThreadWorktreePath ?? selectedThreadProject?.workspaceRoot ?? null,
22 };
24​
25/**
26 * Resolves a thread's cwd the way the selected thread's does, for a thread that is not the
27 * current route's: the shell, or the loaded detail when the shell is missing, picks the project.
28 */
29export function useThreadCwd(ref: ScopedThreadRef | null): string | null {
30 const shell = useQuietThreadShell(ref);
31 const detailState = useEnvironmentThread(
32 shell === null ? (ref?.environmentId ?? null) : null,
33 shell === null ? (ref?.threadId ?? null) : null,
34 );
35 const detailThread = Option.getOrNull(detailState.data)?.thread ?? null;
36 const detailWorktreePath = useThreadWorktreePath(ref);
37 const projectId = shell?.projectId ?? detailThread?.projectId ?? null;
38 const project = useProject(
39 ref === null || projectId === null ? null : { environmentId: ref.environmentId, projectId },
40 );
41 return (
42 resolvePreferredThreadWorktreePath({
43 threadShellWorktreePath: shell?.worktreePath ?? null,
44 threadDetailWorktreePath: detailWorktreePath,
45 }) ??
46 project?.workspaceRoot ??
47 null
48 );