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, useGT, useMessages } from "gt-react-native"; |
| 2 | import { Button } from "../../components/ui/Button"; |
| 3 | import { AppActivityIndicator } from "../../components/ui/AppActivityIndicator"; |
| 4 | import { ConnectionTraceId } from "./ConnectionTraceId"; |
| 5 | import { |
| 6 | connectionErrorText, |
| 7 | type EnvironmentConnectionPhase, |
| 8 | type EnvironmentConnectionPresentation, |
| 9 | } from "@t3tools/client-runtime/connection"; |
| 10 | import { SymbolView } from "../../components/ui/AppSymbol"; |
| 11 | import { View } from "react-native"; |
| 12 | |
| 13 | import { AppText as Text } from "../../components/ui/AppText"; |
| 14 | import type { MessageResolver } from "../../i18n/messages"; |
| 15 | |
| 16 | function noticeTitle( |
| 17 | phase: EnvironmentConnectionPhase, |
| 18 | environmentLabel: string, |
| 19 | awaitingApproval: boolean, |
| 20 | resolve: MessageResolver, |
| 21 | ): string { |
| 22 | const environment = environmentLabel; |
| 23 | if (awaitingApproval) |
| 24 | return resolve(msg("Waiting for {environment} to approve this device", { environment })); |
| 25 | switch (phase) { |
| 26 | case "offline": |
| 27 | return resolve(msg("You are offline")); |
| 28 | case "connecting": |
| 29 | return resolve(msg("Connecting to {environment}...", { environment })); |
| 30 | case "reconnecting": |
| 31 | return resolve(msg("Reconnecting to {environment}...", { environment })); |
| 32 | case "unsupported": |
| 33 | return resolve(msg("Client not supported")); |
| 34 | case "error": |
| 35 | return resolve(msg("{environment} is unavailable", { environment })); |
| 36 | case "available": |
| 37 | return resolve(msg("{environment} is disconnected", { environment })); |
| 38 | case "connected": |
| 39 | return ""; |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | type ConnectionNoticeResource = "preview" | "review" | "terminal"; |
| 44 | |
| 45 | const OFFLINE_DETAIL = { |
| 46 | preview: msg( |
| 47 | "Cached data remains available. The preview will load when your connection returns.", |
| 48 | ), |
| 49 | review: msg("Cached data remains available. The review will load when your connection returns."), |
| 50 | terminal: msg( |
| 51 | "Cached data remains available. The terminal will load when your connection returns.", |
| 52 | ), |
| 53 | } satisfies Record<ConnectionNoticeResource, string>; |
| 54 | |
| 55 | const CONNECTING_DETAIL = { |
| 56 | preview: msg("The preview will load as soon as the environment is ready."), |
| 57 | review: msg("The review will load as soon as the environment is ready."), |
| 58 | terminal: msg("The terminal will load as soon as the environment is ready."), |
| 59 | } satisfies Record<ConnectionNoticeResource, string>; |
| 60 | |
| 61 | const RECONNECT_DETAIL = { |
| 62 | preview: msg("Reconnect the environment to load the preview."), |
| 63 | review: msg("Reconnect the environment to load the review."), |
| 64 | terminal: msg("Reconnect the environment to load the terminal."), |
| 65 | } satisfies Record<ConnectionNoticeResource, string>; |
| 66 | |
| 67 | function noticeDetail( |
| 68 | phase: EnvironmentConnectionPhase, |
| 69 | resource: ConnectionNoticeResource, |
| 70 | error: string | null, |
| 71 | awaitingApproval: boolean, |
| 72 | resolve: MessageResolver, |
| 73 | ): string { |
| 74 | if (error && awaitingApproval) return error; |
| 75 | if (error) { |
| 76 | return phase === "reconnecting" |
| 77 | ? resolve(msg("The app will keep retrying automatically. {error}", { error })) |
| 78 | : error; |
| 79 | } |
| 80 | |
| 81 | switch (phase) { |
| 82 | case "offline": |
| 83 | return resolve(OFFLINE_DETAIL[resource]); |
| 84 | case "connecting": |
| 85 | case "reconnecting": |
| 86 | return resolve(CONNECTING_DETAIL[resource]); |
| 87 | case "unsupported": |
| 88 | return resolve(msg("Use compatible versions of the app and server to connect.")); |
| 89 | case "available": |
| 90 | case "error": |
| 91 | return resolve(RECONNECT_DETAIL[resource]); |
| 92 | case "connected": |
| 93 | return ""; |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | export function EnvironmentConnectionNotice(props: { |
| 98 | readonly environmentLabel: string; |
| 99 | readonly connection: EnvironmentConnectionPresentation; |
| 100 | readonly resource: ConnectionNoticeResource; |
| 101 | readonly onRetry: () => void; |
| 102 | }) { |
| 103 | const gt = useGT(); |
| 104 | const m = useMessages(); |
| 105 | const isRetrying = |
| 106 | props.connection.phase === "connecting" || props.connection.phase === "reconnecting"; |
| 107 | |
| 108 | return ( |
| 109 | <View className="flex-1 items-center justify-center px-8"> |
| 110 | <View className="max-w-[320px] items-center gap-3"> |
| 111 | {isRetrying ? ( |
| 112 | <AppActivityIndicator size="small" colorClassName={"accent-icon-muted"} /> |
| 113 | ) : ( |
| 114 | <SymbolView |
| 115 | name={props.connection.phase === "offline" ? "wifi.slash" : "bolt.horizontal.circle"} |
| 116 | size={24} |
| 117 | tintColorClassName={"accent-icon-muted"} |
| 118 | type="monochrome" |
| 119 | /> |
| 120 | )} |
| 121 | |
| 122 | <Text className="text-center text-lg font-t3-bold text-foreground"> |
| 123 | {noticeTitle( |
| 124 | props.connection.phase, |
| 125 | props.environmentLabel, |
| 126 | props.connection.awaitingApproval === true, |
| 127 | m, |
| 128 | )} |
| 129 | </Text> |
| 130 | <Text className="text-center text-sm leading-normal text-foreground-muted"> |
| 131 | {noticeDetail( |
| 132 | props.connection.phase, |
| 133 | props.resource, |
| 134 | connectionErrorText(props.connection, m), |
| 135 | props.connection.awaitingApproval === true, |
| 136 | m, |
| 137 | )} |
| 138 | {props.connection.traceId ? ( |
| 139 | <ConnectionTraceId traceId={props.connection.traceId} /> |
| 140 | ) : null} |
| 141 | </Text> |
| 142 | |
| 143 | {props.connection.phase !== "offline" && props.connection.phase !== "unsupported" ? ( |
| 144 | <View className="mt-1"> |
| 145 | <Button label={gt("Retry now")} tone="secondary" onPress={props.onRetry} /> |
| 146 | </View> |
| 147 | ) : null} |
| 148 | </View> |
| 149 | </View> |
| 150 | ); |
| 151 | } |