|
| 1 | +import { useAtomValue } from "@effect/atom-react"; |
| 2 | +import { |
| 3 | + AVAILABLE_CONNECTION_STATE, |
| 4 | + type ConnectionDiagnosis, |
| 5 | + connectionDiagnosisText, |
| 6 | + connectionRoutes, |
| 7 | + hasRelayRoute, |
| 8 | + presentConnectionDiagnosis, |
| 9 | + type EnvironmentPresentation, |
| 10 | +} from "@t3tools/client-runtime/connection"; |
| 11 | +import type { EnvironmentId } from "@t3tools/contracts"; |
| 12 | +import Constants from "expo-constants"; |
| 13 | +import * as Option from "effect/Option"; |
| 14 | +import { AsyncResult } from "effect/reactivity"; |
| 15 | +import { View } from "react-native"; |
| 16 | + |
| 17 | +import { AppText as Text } from "../../components/ui/AppText"; |
| 18 | +import { Button } from "../../components/ui/Button"; |
| 19 | +import { environmentCatalog } from "../../connection/catalog"; |
| 20 | +import { cn } from "../../lib/cn"; |
| 21 | +import { copyTextWithHaptic } from "../../lib/copyTextWithHaptic"; |
| 22 | + |
| 23 | +const HOP_MARK = { ok: "✓", failed: "✕", unchecked: "–" } as const; |
| 24 | +const HOP_NOTE = { ok: "", failed: " · Failed", unchecked: " · Not checked" } 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 state = Option.getOrElse( |
| 32 | + AsyncResult.value(useAtomValue(environmentCatalog.stateAtom(environmentId))), |
| 33 | + () => AVAILABLE_CONNECTION_STATE, |
| 34 | + ); |
| 35 | + if (presentation === null || !presentation.entry.enabled) return { diagnosis: null, state }; |
| 36 | + const phase = presentation.connection.phase; |
| 37 | + if (phase === "connected" || phase === "unsupported") return { diagnosis: null, state }; |
| 38 | + const relayOnly = |
| 39 | + connectionRoutes(presentation.entry).length === 1 && hasRelayRoute(presentation.entry); |
| 40 | + const diagnosis = presentConnectionDiagnosis({ |
| 41 | + hostLabel: presentation.entry.target.label, |
| 42 | + state, |
| 43 | + via: relayOnly ? "relay" : "direct", |
| 44 | + clientVersion: Constants.expoConfig?.version, |
| 45 | + serverVersion: presentation.serverConfig?.environment.serverVersion, |
| 46 | + }); |
| 47 | + return { diagnosis, state }; |
| 48 | +} |
| 49 | + |
| 50 | +export function ConnectionDiagnosisDetails(props: { |
| 51 | + readonly hostLabel: string; |
| 52 | + readonly diagnosis: ConnectionDiagnosis; |
| 53 | +}) { |
| 54 | + const { diagnosis } = props; |
| 55 | + return ( |
| 56 | + <View className="gap-3"> |
| 57 | + <View className="gap-0.5"> |
| 58 | + {diagnosis.hops.map((hop) => ( |
| 59 | + <Text |
| 60 | + key={hop.hop} |
| 61 | + className={cn( |
| 62 | + "text-sm", |
| 63 | + hop.result === "failed" ? "text-danger-foreground" : "text-foreground-muted", |
| 64 | + )} |
| 65 | + > |
| 66 | + {`${HOP_MARK[hop.result]} ${hop.label}${HOP_NOTE[hop.result]}`} |
| 67 | + </Text> |
| 68 | + ))} |
| 69 | + </View> |
| 70 | + <View className="gap-0.5"> |
| 71 | + <Text className="text-sm font-t3-bold text-foreground">What to try</Text> |
| 72 | + {diagnosis.steps.map((step, index) => ( |
| 73 | + <Text key={step} className="text-sm text-foreground-muted"> |
| 74 | + {`${index + 1}. ${step}`} |
| 75 | + </Text> |
| 76 | + ))} |
| 77 | + </View> |
| 78 | + <Text selectable className="font-mono text-xs text-foreground-muted"> |
| 79 | + {diagnosis.facts.map(([label, value]) => `${label}: ${value}`).join("\n")} |
| 80 | + </Text> |
| 81 | + <Button |
| 82 | + label="Copy diagnostics" |
| 83 | + icon="doc.on.doc" |
| 84 | + onPress={() => { |
| 85 | + copyTextWithHaptic(connectionDiagnosisText(props.hostLabel, diagnosis), { |
| 86 | + target: "connection-diagnostics", |
| 87 | + }); |
| 88 | + }} |
| 89 | + /> |
| 90 | + </View> |
| 91 | + ); |
| 92 | +} |
0 commit comments