Open History to see the last commit
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 fullAccessOverrideExplanation,
6 PUSH_HOOKS_APPROVAL_QUESTION,
7} from "@t3tools/client-runtime/state/thread-requests";
8import {
9 isAtomCommandInterrupted,
10 squashAtomCommandFailure,
11} from "@t3tools/client-runtime/state/runtime";
12import { T, useGT, useMessages, Var } from "gt-react";
13import { useState } from "react";
14import { useEnvironmentScope } from "../../state/session";
15import { childThreadApprovalsAtom, threadEnvironment } from "../../state/threads";
16import { useAtomCommand } from "../../state/use-atom-command";
17import { ApprovalText } from "./ApprovalText";
18import { ComposerPendingApprovalActions } from "./ComposerPendingApprovalActions";
19import { PushHooksApprovalDetails } from "./PushHooksApprovalDetails";
20
21function ChildApproval({ entry }: { entry: ChildThreadApproval }) {
22 const canRespond = useEnvironmentScope(entry.thread.environmentId, AuthOrchestrationOperateScope);
23 const respond = useAtomCommand(threadEnvironment.respondToApproval, { reportFailure: false });
24 const gt = useGT();
25 const messages = useMessages();
26 const [responding, setResponding] = useState(false);
27 const [error, setError] = useState<string | null>(null);
28 const { approval } = entry;
29 return (
30 <section
31 aria-label={gt("Approval from {title}", { title: entry.title })}
32 className="rounded-lg border border-border bg-background p-3 text-xs"
33 >
34 <p className="mb-2 font-medium">
35 <T>
36 Approval needed from <Var>{entry.title}</Var>
37 </T>
38 </p>
39 {approval.pushHooks ? (
40 <>
41 <p className="mb-1">{messages(PUSH_HOOKS_APPROVAL_QUESTION)}</p>
42 <PushHooksApprovalDetails pushHooks={approval.pushHooks} />
43 </>
44 ) : (
45 <ApprovalText text={approval.detail || approval.requestKind} />
46 )}
47 {approval.appName ? <p className="mt-2 text-muted-foreground">{approval.appName}</p> : null}
48 {approval.responseCapability !== "live" ? (
49 <p className="mt-2 text-warning">
50 <T>Provider process is gone. Interrupt or restart the child thread to respond.</T>
51 </p>
52 ) : null}
53 {approval.reason ? <p className="mt-2 text-muted-foreground">{approval.reason}</p> : null}
54 {approval.fullAccessOverride ? (
55 <p className="mt-2 text-muted-foreground">
56 {fullAccessOverrideExplanation(approval.fullAccessOverride, messages)}
57 </p>
58 ) : null}
59 <div className="mt-2 flex flex-wrap gap-2">
60 <ComposerPendingApprovalActions
61 presentation="inline"
62 requestId={approval.requestId}
63 isResponding={responding}
64 canRespond={approval.responseCapability === "live"}
65 disabled={!canRespond}
66 options={approval.options}
67 onRespondToApproval={async (requestId, decision) => {
68 if (!canRespond || responding) return;
69 setResponding(true);
70 setError(null);
71 const result = await respond({
72 environmentId: entry.thread.environmentId,
73 input: { threadId: entry.thread.threadId, requestId, decision },
74 });
75 if (result._tag === "Failure" && !isAtomCommandInterrupted(result)) {
76 const failure = squashAtomCommandFailure(result);
77 setError(
78 failure instanceof Error
79 ? failure.message
80 : gt("Failed to submit approval decision."),
81 );
82 }
83 setResponding(false);
84 return result;
85 }}
86 />
87 </div>
88 {error !== null ? (
89 <p role="alert" className="mt-2 text-destructive">
90 {error}
91 </p>
92 ) : null}
93 </section>
94 );
95}
96
97export function ChildThreadApprovals({ threadRef }: { threadRef: ScopedThreadRef | null }) {
98 const approvals = useAtomValue(childThreadApprovalsAtom(threadRef));
99 if (approvals.length === 0) return null;
100 return (
101 <div className="mb-2 flex max-h-80 flex-col gap-2 overflow-auto">
102 {approvals.map((entry) => (
103 <ChildApproval
104 key={JSON.stringify([
105 entry.thread.environmentId,
106 entry.thread.threadId,
107 entry.approval.requestId,
108 ])}
109 entry={entry}
110 />
111 ))}
112 </div>
113 );
114}