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 snoozedUntil: thread.snoozedUntil ?? null,
66 snoozedAt: thread.snoozedAt ?? null,
67 session: thread.session,
68 latestUserMessageAt: latestUserMessageAt(thread),
69 hasPendingApprovals: false,
70 hasPendingUserInput: false,
71 hasActionableProposedPlan: false,
72 };
73}
74
75function useResolvedThreadSelection(params: ThreadSelectionRouteParams | undefined) {
76 const routeParams = params ?? {};
77 const routeThreadRef = useMemo<ScopedThreadRef | null>(() => {
78 const environmentId = firstRouteParam(routeParams.environmentId);
79 const threadId = firstRouteParam(routeParams.threadId);
80 if (!environmentId || !threadId) {
81 return null;
82 }
83
84 return {
85 environmentId: EnvironmentId.make(environmentId),
86 threadId: ThreadId.make(threadId),
87 };
88 }, [routeParams.environmentId, routeParams.threadId]);
89 const lastRouteThreadRef = useRef<ScopedThreadRef | null>(null);
90 if (routeThreadRef !== null) {
91 lastRouteThreadRef.current = routeThreadRef;
92 }
93 const selectedThreadRef = routeThreadRef ?? lastRouteThreadRef.current;
94 const selectedThreadShell = useThreadShell(selectedThreadRef);
95 const selectedThreadDetailState = useEnvironmentThread(
96 selectedThreadRef?.environmentId ?? null,
97 selectedThreadRef?.threadId ?? null,
98 );
99 const selectedThreadDetail = Option.getOrNull(selectedThreadDetailState.data);
100 const selectedThread = useMemo(
101 () =>
102 selectedThreadShell ??
103 (selectedThreadRef !== null && selectedThreadDetail !== null
104 ? threadDetailToShell(selectedThreadRef.environmentId, selectedThreadDetail)
105 : null),
106 [selectedThreadDetail, selectedThreadRef, selectedThreadShell],
107 );
108 const selectedProjectRef = useMemo<ScopedProjectRef | null>(
109 () =>
110 selectedThread === null
111 ? null
112 : {
113 environmentId: selectedThread.environmentId,
114 projectId: selectedThread.projectId,
115 },
116 [selectedThread],
117 );
118 const selectedThreadProject = useProject(selectedProjectRef);
119 const selectedEnvironmentId = selectedThread?.environmentId ?? null;
120 const selectedEnvironmentConnection = useSavedRemoteConnection(selectedEnvironmentId);
121 const selectedEnvironmentRuntime = useRemoteEnvironmentRuntime(selectedEnvironmentId);
122
123 return useMemo(
124 () => ({
125 selectedThreadRef,
126 selectedThread,
127 selectedThreadProject,
128 selectedEnvironmentConnection,
129 selectedEnvironmentRuntime,
130 }),
131 [
132 selectedEnvironmentConnection,
133 selectedEnvironmentRuntime,
134 selectedThread,
135 selectedThreadProject,
136 selectedThreadRef,
137 ],
138 );
139}
140
141type ThreadSelectionState = ReturnType<typeof useResolvedThreadSelection>;
142
143export function useThreadSelection(): ThreadSelectionState {
144 const route = useRoute<RouteProp<Record<string, ThreadSelectionRouteParams | undefined>>>();
145 return useResolvedThreadSelection(route.params);
146}