Open History to see the last commit
1import { useAtomValue } from "@effect/atom-react";
2import { useCallback, useMemo, useState } from "react";
3
4import { type ProviderApprovalDecision, type RuntimeRequestId } from "@t3tools/contracts";
5import { derivePendingThreadRequests } from "@t3tools/client-runtime/state/thread-requests";
6import { Atom } from "effect/unstable/reactivity";
7
8import { threadEnvironment } from "../state/threads";
9import { scopedRequestKey } from "../lib/scopedEntities";
10import {
11 buildPendingUserInputAnswers,
12 setPendingUserInputCustomAnswer,
13 type PendingUserInputDraftAnswer,
14} from "../lib/threadActivity";
15import { appAtomRegistry } from "./atom-registry";
16import { useSelectedThreadProjection } from "./use-thread-detail";
17import { useThreadSelection } from "./use-thread-selection";
18import { useAtomCommand } from "./use-atom-command";
19
20const userInputDraftsByRequestKeyAtom = Atom.make<
21 Record<string, Record<string, PendingUserInputDraftAnswer>>
22>({}).pipe(Atom.keepAlive, Atom.withLabel("mobile:user-input-drafts"));
23
24function setUserInputDraftOption(requestKey: string, questionId: string, label: string): void {
25 const current = appAtomRegistry.get(userInputDraftsByRequestKeyAtom);
26 appAtomRegistry.set(userInputDraftsByRequestKeyAtom, {
27 ...current,
28 [requestKey]: {
29 ...current[requestKey],
30 [questionId]: {
31 selectedOptionLabel: label,
32 },
33 },
34 });
35}
36
37function setUserInputDraftCustomAnswer(
38 requestKey: string,
39 questionId: string,
40 customAnswer: string,
41): void {
42 const current = appAtomRegistry.get(userInputDraftsByRequestKeyAtom);
43 appAtomRegistry.set(userInputDraftsByRequestKeyAtom, {
44 ...current,
45 [requestKey]: {
46 ...current[requestKey],
47 [questionId]: setPendingUserInputCustomAnswer(
48 current[requestKey]?.[questionId],
49 customAnswer,
50 ),
51 },
52 });
53}
54
55export function useSelectedThreadRequests() {
56 const respondToApproval = useAtomCommand(
57 threadEnvironment.respondToApproval,
58 "thread approval response",
59 );
60 const respondToUserInput = useAtomCommand(
61 threadEnvironment.respondToUserInput,
62 "thread user input response",
63 );
64 const { selectedThread: selectedThreadShell } = useThreadSelection();
65 const selectedThread = useSelectedThreadProjection();
66 const userInputDraftsByRequestKey = useAtomValue(userInputDraftsByRequestKeyAtom);
67 const [respondingApprovalId, setRespondingApprovalId] = useState<RuntimeRequestId | null>(null);
68 const [respondingUserInputId, setRespondingUserInputId] = useState<RuntimeRequestId | null>(null);
69
70 const pendingRequests = useMemo(
71 () =>
72 selectedThread === null
73 ? { approvals: [], userInputs: [] }
74 : derivePendingThreadRequests(selectedThread.projection),
75 [selectedThread],
76 );
77 const activePendingApprovals = pendingRequests.approvals;
78 const activePendingApproval = activePendingApprovals[0] ?? null;
79 const activePendingUserInputs = pendingRequests.userInputs;
80 const activePendingUserInput = activePendingUserInputs[0] ?? null;
81 const activePendingUserInputDrafts =
82 activePendingUserInput && selectedThreadShell
83 ? (userInputDraftsByRequestKey[
84 scopedRequestKey(selectedThreadShell.environmentId, activePendingUserInput.requestId)
85 ] ?? {})
86 : {};
87 const activePendingUserInputAnswers = activePendingUserInput
88 ? buildPendingUserInputAnswers(activePendingUserInput.questions, activePendingUserInputDrafts)
89 : null;
90
91 const onSelectUserInputOption = useCallback(
92 (requestId: RuntimeRequestId, questionId: string, label: string) => {
93 if (!selectedThreadShell) {
94 return;
95 }
96
97 const requestKey = scopedRequestKey(selectedThreadShell.environmentId, requestId);
98 setUserInputDraftOption(requestKey, questionId, label);
99 },
100 [selectedThreadShell],
101 );
102
103 const onChangeUserInputCustomAnswer = useCallback(
104 (requestId: RuntimeRequestId, questionId: string, customAnswer: string) => {
105 if (!selectedThreadShell) {
106 return;
107 }
108
109 const requestKey = scopedRequestKey(selectedThreadShell.environmentId, requestId);
110 setUserInputDraftCustomAnswer(requestKey, questionId, customAnswer);
111 },
112 [selectedThreadShell],
113 );
114
115 const onRespondToApproval = useCallback(
116 async (requestId: RuntimeRequestId, decision: ProviderApprovalDecision) => {
117 if (!selectedThreadShell) {
118 return;
119 }
120 if (
121 activePendingApprovals.find((approval) => approval.requestId === requestId)
122 ?.responseCapability !== "live"
123 ) {
124 return;
125 }
126
127 setRespondingApprovalId(requestId);
128 const result = await respondToApproval({
129 environmentId: selectedThreadShell.environmentId,
130 input: {
131 threadId: selectedThreadShell.id,
132 requestId,
133 decision,
134 },
135 });
136 setRespondingApprovalId((current) => (current === requestId ? null : current));
137 return result;
138 },
139 [activePendingApprovals, respondToApproval, selectedThreadShell],
140 );
141
142 const onSubmitUserInput = useCallback(async () => {
143 if (
144 !selectedThreadShell ||
145 !activePendingUserInput ||
146 activePendingUserInput.responseCapability !== "live" ||
147 !activePendingUserInputAnswers
148 ) {
149 return;
150 }
151
152 setRespondingUserInputId(activePendingUserInput.requestId);
153 const result = await respondToUserInput({
154 environmentId: selectedThreadShell.environmentId,
155 input: {
156 threadId: selectedThreadShell.id,
157 requestId: activePendingUserInput.requestId,
158 answers: activePendingUserInputAnswers,
159 },
160 });
161 setRespondingUserInputId((current) =>
162 current === activePendingUserInput.requestId ? null : current,
163 );
164 return result;
165 }, [
166 activePendingUserInput,
167 activePendingUserInputAnswers,
168 respondToUserInput,
169 selectedThreadShell,
170 ]);
171
172 return {
173 activePendingApproval,
174 activePendingUserInput,
175 activePendingUserInputDrafts,
176 activePendingUserInputAnswers,
177 respondingApprovalId,
178 respondingUserInputId,
179 onRespondToApproval,
180 onSelectUserInputOption,
181 onChangeUserInputCustomAnswer,
182 onSubmitUserInput,
183 };
184}