The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { T, useGT, useMessages } from "gt-react-native"; |
| 2 | import { useAtomValue } from "@effect/atom-react"; |
| 3 | import { |
| 4 | AVAILABLE_CONNECTION_STATE, |
| 5 | type ConnectionDiagnosis, |
| 6 | connectionDiagnosisText, |
| 7 | connectionRoutes, |
| 8 | hasRelayRoute, |
| 9 | presentConnectionDiagnosis, |
| 10 | type EnvironmentPresentation, |
| 11 | } from "@t3tools/client-runtime/connection"; |
| 12 | import type { EnvironmentId } from "@t3tools/contracts"; |
| 13 | import Constants from "expo-constants"; |
| 14 | import { AsyncResult } from "effect/reactivity"; |
| 15 | import { View } from "react-native"; |
| 16 | import { tryOpenExternalUrl } from "../../lib/openExternalUrl"; |
| 17 | |
| 18 | import { AppText as Text } from "../../components/ui/AppText"; |
| 19 | import { Button } from "../../components/ui/Button"; |
| 20 | import { environmentCatalog } from "../../connection/catalog"; |
| 21 | import { cn } from "../../lib/cn"; |
| 22 | import { copyTextWithHaptic } from "../../lib/copyTextWithHaptic"; |
| 23 | |
| 24 | const HOP_MARK = { ok: "✓", failed: "✕", unchecked: "–" } as const; |
| 25 | |
| 26 | /** Same model as the web Settings row; null while connected, off, or not failed. */ |
| 27 | export function useConnectionDiagnosis( |
| 28 | environmentId: EnvironmentId, |
| 29 | presentation: EnvironmentPresentation | null, |
| 30 | ) { |
| 31 | const messages = useMessages(); |
| 32 | const state = AsyncResult.getOrElse( |
| 33 | useAtomValue(environmentCatalog.stateAtom(environmentId)), |
| 34 | () => AVAILABLE_CONNECTION_STATE, |
| 35 | ); |
| 36 | if (presentation === null || !presentation.entry.enabled) return { diagnosis: null, state }; |
| 37 | const phase = presentation.connection.phase; |
| 38 | if (phase === "connected" || phase === "unsupported") return { diagnosis: null, state }; |
| 39 | const relayOnly = |
| 40 | connectionRoutes(presentation.entry).length === 1 && hasRelayRoute(presentation.entry); |
| 41 | const diagnosis = presentConnectionDiagnosis( |
| 42 | { |
| 43 | hostLabel: presentation.entry.target.label, |
| 44 | state, |
| 45 | via: relayOnly ? "relay" : "direct", |
| 46 | clientVersion: Constants.expoConfig?.version, |
| 47 | serverVersion: presentation.serverConfig?.environment.serverVersion, |
| 48 | }, |
| 49 | messages, |
| 50 | ); |
| 51 | return { diagnosis, state }; |
| 52 | } |
| 53 | |
| 54 | export function ConnectionDiagnosisDetails(props: { |
| 55 | readonly hostLabel: string; |
| 56 | readonly diagnosis: ConnectionDiagnosis; |
| 57 | }) { |
| 58 | const gt = useGT(); |
| 59 | const { diagnosis } = props; |
| 60 | const { moreInfoUrl } = diagnosis; |
| 61 | return ( |
| 62 | <View className="gap-3"> |
| 63 | <View className="gap-0.5"> |
| 64 | {diagnosis.hops.map((hop) => ( |
| 65 | <Text |
| 66 | key={hop.hop} |
| 67 | className={cn( |
| 68 | "text-sm", |
| 69 | hop.result === "failed" ? "text-danger-foreground" : "text-foreground-muted", |
| 70 | )} |
| 71 | > |
| 72 | {hop.result === "failed" |
| 73 | ? gt("{mark} {hop} · Failed", { mark: HOP_MARK[hop.result], hop: hop.label }) |
| 74 | : hop.result === "unchecked" |
| 75 | ? gt("{mark} {hop} · Not checked", { mark: HOP_MARK[hop.result], hop: hop.label }) |
| 76 | : `${HOP_MARK[hop.result]} ${hop.label}`} |
| 77 | </Text> |
| 78 | ))} |
| 79 | </View> |
| 80 | <View className="gap-0.5"> |
| 81 | <Text className="text-sm font-t3-bold text-foreground"> |
| 82 | <T>What to try</T> |
| 83 | </Text> |
| 84 | {diagnosis.steps.map((step, index) => ( |
| 85 | <Text key={step} className="text-sm text-foreground-muted"> |
| 86 | {`${index + 1}. ${step}`} |
| 87 | </Text> |
| 88 | ))} |
| 89 | </View> |
| 90 | <Text selectable className="font-mono text-xs text-foreground-muted"> |
| 91 | {diagnosis.facts.map(([label, value]) => `${label}: ${value}`).join("\n")} |
| 92 | </Text> |
| 93 | <Button |
| 94 | label={gt("Copy diagnostics")} |
| 95 | icon="doc.on.doc" |
| 96 | onPress={() => { |
| 97 | copyTextWithHaptic(connectionDiagnosisText(props.hostLabel, diagnosis), { |
| 98 | target: "connection-diagnostics", |
| 99 | }); |
| 100 | }} |
| 101 | /> |
| 102 | {moreInfoUrl ? ( |
| 103 | <Button |
| 104 | label={gt("More info")} |
| 105 | icon="safari" |
| 106 | tone="secondary" |
| 107 | onPress={() => void tryOpenExternalUrl(moreInfoUrl)} |
| 108 | /> |
| 109 | ) : null} |
| 110 | </View> |
| 111 | ); |
| 112 | } |