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