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

useActiveProjectTarget.ts

36 lines1.2 KB

1import { scopeThreadRef } from "@t3tools/client-runtime/environment";
2import type { EnvironmentId, ScopedThreadRef } from "@t3tools/contracts";
3​
4import { useProjects } from "~/state/entities";
5​
6import { useHandleNewThread } from "./useHandleNewThread";
7​
8export interface ActiveProjectTarget {
9 readonly environmentId: EnvironmentId;
10 readonly cwd: string;
11 readonly projectName: string;
12 readonly threadRef: ScopedThreadRef;
14​
15export function useActiveProjectTarget(): ActiveProjectTarget | null {
16 const { activeDraftThread, activeThread } = useHandleNewThread();
17 const projects = useProjects();
18 const thread = activeThread ?? activeDraftThread;
19 const threadId = activeThread?.id ?? activeDraftThread?.threadId;
20 const project = thread
21 ? projects.find(
22 (candidate) =>
23 candidate.environmentId === thread.environmentId && candidate.id === thread.projectId,
24 )
25 : null;
26 const cwd = thread?.worktreePath ?? project?.workspaceRoot;
27​
28 if (!thread || !threadId || !project || !cwd) return null;
29​
30 return {
31 environmentId: project.environmentId,
32 cwd,
33 projectName: project.title,
34 threadRef: scopeThreadRef(thread.environmentId, threadId),
35 };