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 { msg, T, Var } from "gt-react"; |
| 2 | |
| 3 | import { m } from "../i18n/messages"; |
| 4 | import { type SlowRpcAckRequest, subscribeSlowRpcAckRequests } from "../rpc/requestLatencyState"; |
| 5 | import { toastManager } from "./ui/toast"; |
| 6 | |
| 7 | function describeSlowRequests(requests: ReadonlyArray<SlowRpcAckRequest>): string { |
| 8 | const count = requests.length; |
| 9 | const thresholdSeconds = Math.round( |
| 10 | Math.min(...requests.map((request) => request.thresholdMs)) / 1000, |
| 11 | ); |
| 12 | |
| 13 | return m( |
| 14 | msg( |
| 15 | "{count, plural, one {{count} request} other {{count} requests}} waiting longer than {thresholdSeconds}s.", |
| 16 | { count, thresholdSeconds }, |
| 17 | ), |
| 18 | ); |
| 19 | } |
| 20 | |
| 21 | function SlowRequestDetails({ requests }: { requests: ReadonlyArray<SlowRpcAckRequest> }) { |
| 22 | return ( |
| 23 | <ul className="space-y-2.5 text-xs text-muted-foreground"> |
| 24 | {requests.map((request) => ( |
| 25 | <li |
| 26 | className="min-w-0 border-border/50 border-b pb-2 last:border-b-0 last:pb-0" |
| 27 | key={request.requestId} |
| 28 | > |
| 29 | <div className="wrap-break-word font-medium text-foreground">{request.tag}</div> |
| 30 | <div className="mt-0.5 text-3xs opacity-75"> |
| 31 | <T> |
| 32 | Started <Var>{new Date(request.startedAt).toLocaleTimeString()}</Var> |
| 33 | </T> |
| 34 | </div> |
| 35 | </li> |
| 36 | ))} |
| 37 | </ul> |
| 38 | ); |
| 39 | } |
| 40 | |
| 41 | /** One warning toast that tracks the set of slow requests while any are outstanding. */ |
| 42 | export function startSlowRpcRequestToasts(): () => void { |
| 43 | let toastId: ReturnType<typeof toastManager.add> | null = null; |
| 44 | |
| 45 | const unsubscribe = subscribeSlowRpcAckRequests((slowRequests) => { |
| 46 | if (slowRequests.length === 0) { |
| 47 | if (toastId !== null) { |
| 48 | toastManager.close(toastId); |
| 49 | toastId = null; |
| 50 | } |
| 51 | return; |
| 52 | } |
| 53 | |
| 54 | const nextToast = { |
| 55 | data: { |
| 56 | expandableContent: <SlowRequestDetails requests={slowRequests} />, |
| 57 | expandableDescriptionTrigger: true, |
| 58 | expandableLabels: { collapse: m(msg("Hide requests")), expand: m(msg("Show requests")) }, |
| 59 | }, |
| 60 | description: describeSlowRequests(slowRequests), |
| 61 | timeout: 0, |
| 62 | title: m(msg("Some requests are slow")), |
| 63 | type: "warning" as const, |
| 64 | }; |
| 65 | |
| 66 | if (toastId === null) { |
| 67 | toastId = toastManager.add(nextToast); |
| 68 | } else { |
| 69 | toastManager.update(toastId, nextToast); |
| 70 | } |
| 71 | }); |
| 72 | |
| 73 | return () => { |
| 74 | unsubscribe(); |
| 75 | if (toastId !== null) toastManager.close(toastId); |
| 76 | }; |
| 77 | } |