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

use-thread-selection.ts

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