Open History to see the last commit
1import { useRoute, type RouteProp } from "@react-navigation/native";
2import { useMemo, useRef } from "react";
3import {
4 EnvironmentId,
5 type OrchestrationThread,
6 ThreadId,
7 type ScopedProjectRef,
8 type ScopedThreadRef,
9} from "@t3tools/contracts";
10import type { EnvironmentThreadShell } from "@t3tools/client-runtime/state/shell";
11import * as Option from "effect/Option";
12
13import { useProject, useThreadShell } from "../state/entities";
14import { useEnvironmentThread } from "../state/threads";
15import {
16 useRemoteEnvironmentRuntime,
17 useSavedRemoteConnection,
18} from "./use-remote-environment-registry";
19type ThreadSelectionRouteParams = {
20 readonly environmentId?: string | string[];
21 readonly threadId?: string | string[];
22};
23
24function firstRouteParam(value: string | string[] | undefined): string | null {
25 if (Array.isArray(value)) {
26 return value[0] ?? null;
27 }
28
29 return value ?? null;
30}
31
32function latestUserMessageAt(thread: OrchestrationThread): OrchestrationThread["updatedAt"] | null {
33 for (let index = thread.messages.length - 1; index >= 0; index -= 1) {
34 const message = thread.messages[index];
35 if (message?.role === "user") {
36 return message.createdAt;
37 }
38 }
39
40 return null;
41}
42
43function threadDetailToShell(
44 environmentId: EnvironmentId,
45 thread: OrchestrationThread,
46): EnvironmentThreadShell {
47 return {
48 environmentId,
49 id: thread.id,
50 projectId: thread.projectId,
51 title: thread.title,
52 modelSelection: thread.modelSelection,
53 runtimeMode: thread.runtimeMode,
54 interactionMode: thread.interactionMode,
55 branch: thread.branch,
56 worktreePath: thread.worktreePath,
57 linkedPullRequest: thread.linkedPullRequest ?? null,
58 branchPullRequest: thread.branchPullRequest ?? null,
59 latestTurn: thread.latestTurn,
60 createdAt: thread.createdAt,
61 updatedAt: thread.updatedAt,
62 archivedAt: thread.archivedAt,
63 settledOverride: thread.settledOverride,
64 settledAt: thread.settledAt,
65 unsettledAt: thread.unsettledAt,
66 activeOrderKey: thread.activeOrderKey,
67 pinnedAt: thread.pinnedAt,
68 pinOrderKey: thread.pinOrderKey,
69 snoozedUntil: thread.snoozedUntil ?? null,
70 snoozedAt: thread.snoozedAt ?? null,
71 session: thread.session,
72 latestUserMessageAt: latestUserMessageAt(thread),
73 hasPendingApprovals: false,
74 hasPendingUserInput: false,
75 hasActionableProposedPlan: false,
76 };
77}
78
79function useResolvedThreadSelection(params: ThreadSelectionRouteParams | undefined) {
80 const routeParams = params ?? {};
81 const routeThreadRef = useMemo<ScopedThreadRef | null>(() => {
82 const environmentId = firstRouteParam(routeParams.environmentId);
83 const threadId = firstRouteParam(routeParams.threadId);
84 if (!environmentId || !threadId) {
85 return null;
86 }
87
88 return {
89 environmentId: EnvironmentId.make(environmentId),
90 threadId: ThreadId.make(threadId),
91 };
92 }, [routeParams.environmentId, routeParams.threadId]);
93 const lastRouteThreadRef = useRef<ScopedThreadRef | null>(null);
94 if (routeThreadRef !== null) {
95 lastRouteThreadRef.current = routeThreadRef;
96 }
97 const selectedThreadRef = routeThreadRef ?? lastRouteThreadRef.current;
98 const selectedThreadShell = useThreadShell(selectedThreadRef);
99 const selectedThreadDetailState = useEnvironmentThread(
100 selectedThreadRef?.environmentId ?? null,
101 selectedThreadRef?.threadId ?? null,
102 );
103 const selectedThreadDetail = Option.getOrNull(selectedThreadDetailState.data);
104 const selectedThread = useMemo(
105 () =>
106 selectedThreadShell ??
107 (selectedThreadRef !== null && selectedThreadDetail !== null
108 ? threadDetailToShell(selectedThreadRef.environmentId, selectedThreadDetail)
109 : null),
110 [selectedThreadDetail, selectedThreadRef, selectedThreadShell],
111 );
112 const selectedProjectRef = useMemo<ScopedProjectRef | null>(
113 () =>
114 selectedThread === null
115 ? null
116 : {
117 environmentId: selectedThread.environmentId,
118 projectId: selectedThread.projectId,
119 },
120 [selectedThread],
121 );
122 const selectedThreadProject = useProject(selectedProjectRef);
123 const selectedEnvironmentId = selectedThread?.environmentId ?? null;
124 const selectedEnvironmentConnection = useSavedRemoteConnection(selectedEnvironmentId);
125 const selectedEnvironmentRuntime = useRemoteEnvironmentRuntime(selectedEnvironmentId);
126
127 return useMemo(
128 () => ({
129 selectedThreadRef,
130 selectedThread,
131 selectedThreadProject,
132 selectedEnvironmentConnection,
133 selectedEnvironmentRuntime,
134 }),
135 [
136 selectedEnvironmentConnection,
137 selectedEnvironmentRuntime,
138 selectedThread,
139 selectedThreadProject,
140 selectedThreadRef,
141 ],
142 );
143}
144
145type ThreadSelectionState = ReturnType<typeof useResolvedThreadSelection>;
146
147export function useThreadSelection(): ThreadSelectionState {
148 const route = useRoute<RouteProp<Record<string, ThreadSelectionRouteParams | undefined>>>();
149 return useResolvedThreadSelection(route.params);
150}