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

use-selected-thread-requests.ts

181 lines5.9 KB

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