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

use-thread-selection.ts

140 lines4.5 KB

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