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

use-thread-selection.ts

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