The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 04:45 UTC
| 1 | import { useAtomValue } from "@effect/atom-react"; |
| 2 | import { AuthOrchestrationOperateScope, type ScopedThreadRef } from "@t3tools/contracts"; |
| 3 | import type { ChildThreadApproval } from "@t3tools/client-runtime/state/thread-requests"; |
| 4 | import { |
| 5 | isAtomCommandInterrupted, |
| 6 | squashAtomCommandFailure, |
| 7 | } from "@t3tools/client-runtime/state/runtime"; |
| 8 | import { T, useGT, Var } from "gt-react-native"; |
| 9 | import { useState } from "react"; |
| 10 | import { useEnvironmentScope } from "../../state/session"; |
| 11 | import { ScrollView, View } from "react-native"; |
| 12 | import Animated, { useAnimatedStyle, type DerivedValue } from "react-native-reanimated"; |
| 13 | import { AppText } from "../../components/ui/AppText"; |
| 14 | import { childThreadApprovalsAtom, threadEnvironment } from "../../state/threads"; |
| 15 | import { useAtomCommand } from "../../state/use-atom-command"; |
| 16 | import { PendingApprovalCard } from "./PendingApprovalCard"; |
| 17 | |
| 18 | function 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 |
| 47 | ? failure.message |
| 48 | : gt("Failed to submit approval decision."), |
| 49 | ); |
| 50 | } |
| 51 | setResponding(false); |
| 52 | return result; |
| 53 | }} |
| 54 | /> |
| 55 | {error !== null ? ( |
| 56 | <AppText |
| 57 | accessibilityRole="alert" |
| 58 | accessibilityLiveRegion="polite" |
| 59 | className="font-sans text-sm text-danger-foreground" |
| 60 | > |
| 61 | {error} |
| 62 | </AppText> |
| 63 | ) : null} |
| 64 | </View> |
| 65 | ); |
| 66 | } |
| 67 | |
| 68 | export function ChildThreadApprovals({ |
| 69 | threadRef, |
| 70 | maxHeight, |
| 71 | }: { |
| 72 | threadRef: ScopedThreadRef | null; |
| 73 | maxHeight: DerivedValue<number>; |
| 74 | }) { |
| 75 | const approvals = useAtomValue(childThreadApprovalsAtom(threadRef)); |
| 76 | const heightStyle = useAnimatedStyle(() => ({ maxHeight: Math.min(320, maxHeight.value / 2) })); |
| 77 | if (approvals.length === 0) return null; |
| 78 | return ( |
| 79 | <Animated.View style={heightStyle} className="overflow-hidden"> |
| 80 | <ScrollView keyboardShouldPersistTaps="handled" nestedScrollEnabled style={{ flexShrink: 1 }}> |
| 81 | {approvals.map((entry) => ( |
| 82 | <ChildApproval |
| 83 | key={JSON.stringify([ |
| 84 | entry.thread.environmentId, |
| 85 | entry.thread.threadId, |
| 86 | entry.approval.requestId, |
| 87 | ])} |
| 88 | entry={entry} |
| 89 | /> |
| 90 | ))} |
| 91 | </ScrollView> |
| 92 | </Animated.View> |
| 93 | ); |
| 94 | } |