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

ChildThreadApprovals.tsx

92 lines3.4 KB

1import { useAtomValue } from "@effect/atom-react";
2import { AuthOrchestrationOperateScope, type ScopedThreadRef } from "@t3tools/contracts";
3import type { ChildThreadApproval } from "@t3tools/client-runtime/state/thread-requests";
4import {
5 isAtomCommandInterrupted,
6 squashAtomCommandFailure,
7} from "@t3tools/client-runtime/state/runtime";
8import { T, useGT, Var } from "gt-react-native";
9import { useState } from "react";
10import { useEnvironmentScope } from "../../state/session";
11import { ScrollView, View } from "react-native";
12import Animated, { useAnimatedStyle, type DerivedValue } from "react-native-reanimated";
13import { AppText } from "../../components/ui/AppText";
14import { childThreadApprovalsAtom, threadEnvironment } from "../../state/threads";
15import { useAtomCommand } from "../../state/use-atom-command";
16import { PendingApprovalCard } from "./PendingApprovalCard";
17​
18function ChildApproval({ entry }: { entry: ChildThreadApproval }) {
19 const gt = useGT();
20 const canRespond = useEnvironmentScope(entry.thread.environmentId, AuthOrchestrationOperateScope);
21 const respond = useAtomCommand(threadEnvironment.respondToApproval, { reportFailure: false });
22 const [responding, setResponding] = useState(false);
23 const [error, setError] = useState<string | null>(null);
24 return (
25 <View className="mx-3.5 my-2 gap-2">
26 <AppText className="font-t3-bold text-sm text-foreground">
27 <T>
28 Approval from <Var>{entry.title}</Var>
29 </T>
30 </AppText>
31 <PendingApprovalCard
32 canOperateThread={canRespond}
33 approval={entry.approval}
34 respondingApprovalId={responding ? entry.approval.requestId : null}
35 onRespond={async (requestId, decision) => {
36 if (!canRespond || responding) return;
37 setResponding(true);
38 setError(null);
39 const result = await respond({
40 environmentId: entry.thread.environmentId,
41 input: { threadId: entry.thread.threadId, requestId, decision },
42 });
43 if (result._tag === "Failure" && !isAtomCommandInterrupted(result)) {
44 const failure = squashAtomCommandFailure(result);
45 setError(
46 failure instanceof Error ? failure.message : gt`Failed to submit approval decision.`,
47 );
48 }
49 setResponding(false);
50 return result;
51 }}
52 />
53 {error !== null ? (
54 <AppText
55 accessibilityRole="alert"
56 accessibilityLiveRegion="polite"
57 className="font-sans text-sm text-danger-foreground"
58 >
59 {error}
60 </AppText>
61 ) : null}
62 </View>
63 );
65​
66export function ChildThreadApprovals({
67 threadRef,
68 maxHeight,
69}: {
70 threadRef: ScopedThreadRef | null;
71 maxHeight: DerivedValue<number>;
72}) {
73 const approvals = useAtomValue(childThreadApprovalsAtom(threadRef));
74 const heightStyle = useAnimatedStyle(() => ({ maxHeight: Math.min(320, maxHeight.value / 2) }));
75 if (approvals.length === 0) return null;
76 return (
77 <Animated.View style={heightStyle} className="overflow-hidden">
78 <ScrollView keyboardShouldPersistTaps="handled" nestedScrollEnabled style={{ flexShrink: 1 }}>
79 {approvals.map((entry) => (
80 <ChildApproval
81 key={JSON.stringify([
82 entry.thread.environmentId,
83 entry.thread.threadId,
84 entry.approval.requestId,
85 ])}
86 entry={entry}
87 />
88 ))}
89 </ScrollView>
90 </Animated.View>
91 );