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