The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { AppActivityIndicator } from "../../components/ui/AppActivityIndicator"; |
| 2 | import { ScreenScrollView as ScrollView } from "../../components/ui/ScreenScrollView"; |
| 3 | import * as Application from "expo-application"; |
| 4 | import Constants from "expo-constants"; |
| 5 | import * as Updates from "expo-updates"; |
| 6 | import { useAtomValue } from "@effect/atom-react"; |
| 7 | import * as Effect from "effect/Effect"; |
| 8 | import * as Schema from "effect/Schema"; |
| 9 | import { AsyncResult, Atom } from "effect/reactivity"; |
| 10 | import { T, useGT } from "gt-react-native"; |
| 11 | import { useState } from "react"; |
| 12 | import { View } from "react-native"; |
| 13 | import { useSafeAreaInsets } from "react-native-safe-area-context"; |
| 14 | |
| 15 | import { SymbolView } from "../../components/ui/AppSymbol"; |
| 16 | import { AppText as Text } from "../../components/ui/AppText"; |
| 17 | import { tryCopyTextWithHaptic } from "../../lib/copyTextWithHaptic"; |
| 18 | import { mobilePreferencesAtom, saveMobilePreferences } from "../../state/preferences"; |
| 19 | import { useAtomCommand } from "../../state/use-atom-command"; |
| 20 | import { crashReportingAvailable, setCrashReportsEnabled } from "../observability/observe"; |
| 21 | import { ListActionRow as SettingsActionRow } from "../../components/ui/ListActionRow"; |
| 22 | import { SettingsScreen } from "../settings/components/SettingsScreen"; |
| 23 | import { SettingsSection } from "../settings/components/SettingsSection"; |
| 24 | import { SettingsSwitchRow } from "../settings/components/SettingsSwitchRow"; |
| 25 | import { |
| 26 | formatStartupCrashReport, |
| 27 | parseStartupCrashRecords, |
| 28 | type StartupCrashRecord, |
| 29 | } from "./crash-log-model"; |
| 30 | |
| 31 | // expo-updates retains only seven days of logs. |
| 32 | const LOG_WINDOW_MS = 7 * 24 * 60 * 60 * 1000; |
| 33 | |
| 34 | type CrashLogState = |
| 35 | | { readonly status: "loading" } |
| 36 | | { readonly status: "unavailable" } |
| 37 | | { readonly status: "ready"; readonly records: ReadonlyArray<StartupCrashRecord> }; |
| 38 | |
| 39 | class CrashLogReadError extends Schema.TaggedError<CrashLogReadError>()("CrashLogReadError", { |
| 40 | cause: Schema.Defect(), |
| 41 | }) { |
| 42 | override get message(): string { |
| 43 | return "Could not read the expo-updates log."; |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | // Not kept alive: each visit to the screen re-reads the log. |
| 48 | const crashLogAtom = Atom.make( |
| 49 | Effect.tryPromise({ |
| 50 | try: () => Updates.readLogEntriesAsync(LOG_WINDOW_MS), |
| 51 | catch: (cause) => new CrashLogReadError({ cause }), |
| 52 | }).pipe( |
| 53 | Effect.map((entries): CrashLogState => ({ |
| 54 | status: "ready", |
| 55 | records: parseStartupCrashRecords(entries), |
| 56 | })), |
| 57 | Effect.catch((error) => |
| 58 | Effect.logWarning("[diagnostics] could not read the expo-updates log", error).pipe( |
| 59 | Effect.as<CrashLogState>({ status: "unavailable" }), |
| 60 | ), |
| 61 | ), |
| 62 | ), |
| 63 | ).pipe(Atom.withLabel("mobile:diagnostics-crash-log")); |
| 64 | |
| 65 | const UNAVAILABLE_CRASH_LOG_ATOM = Atom.make( |
| 66 | AsyncResult.success<CrashLogState>({ status: "unavailable" }), |
| 67 | ); |
| 68 | |
| 69 | function useCrashLogState(): CrashLogState { |
| 70 | const result = useAtomValue(Updates.isEnabled ? crashLogAtom : UNAVAILABLE_CRASH_LOG_ATOM); |
| 71 | return AsyncResult.isSuccess(result) ? result.value : { status: "loading" }; |
| 72 | } |
| 73 | |
| 74 | function appIdentity() { |
| 75 | return { |
| 76 | version: Constants.expoConfig?.version ?? "0.0.0", |
| 77 | // EAS assigns build numbers remotely, so expo-constants never sees them; |
| 78 | // only the installed binary knows its own. |
| 79 | build: Application.nativeBuildVersion ?? "dev", |
| 80 | }; |
| 81 | } |
| 82 | |
| 83 | export function SettingsDiagnosticsRouteScreen() { |
| 84 | const insets = useSafeAreaInsets(); |
| 85 | const state = useCrashLogState(); |
| 86 | const gt = useGT(); |
| 87 | const [copied, setCopied] = useState(false); |
| 88 | |
| 89 | const records = state.status === "ready" ? state.records : []; |
| 90 | const copyReport = async () => { |
| 91 | const ok = await tryCopyTextWithHaptic(formatStartupCrashReport(records, appIdentity()), { |
| 92 | target: "crash report", |
| 93 | }); |
| 94 | if (ok) setCopied(true); |
| 95 | }; |
| 96 | |
| 97 | return ( |
| 98 | <SettingsScreen title={gt("Diagnostics")}> |
| 99 | <ScrollView |
| 100 | contentInsetAdjustmentBehavior="automatic" |
| 101 | showsVerticalScrollIndicator={false} |
| 102 | className="flex-1" |
| 103 | contentContainerClassName="gap-6 px-5 pt-4" |
| 104 | contentContainerStyle={{ paddingBottom: Math.max(insets.bottom, 18) + 18 }} |
| 105 | > |
| 106 | {crashReportingAvailable ? <CrashReportsSetting /> : null} |
| 107 | |
| 108 | <SettingsSection title={gt("Startup crashes")}> |
| 109 | {state.status === "loading" ? ( |
| 110 | <View className="items-center gap-3 px-6 py-8"> |
| 111 | <AppActivityIndicator /> |
| 112 | <Text className="text-center text-sm text-foreground-muted"> |
| 113 | <T>Reading crash log…</T> |
| 114 | </Text> |
| 115 | </View> |
| 116 | ) : state.status === "unavailable" ? ( |
| 117 | <EmptyState |
| 118 | icon="exclamationmark.triangle" |
| 119 | title={gt("Crash log unavailable")} |
| 120 | detail={gt("Startup crash records are only kept in store and TestFlight builds.")} |
| 121 | /> |
| 122 | ) : records.length === 0 ? ( |
| 123 | <EmptyState |
| 124 | icon="checkmark.circle" |
| 125 | title={gt("No startup crashes")} |
| 126 | detail={gt("Nothing has taken the app down during launch in the last 7 days.")} |
| 127 | /> |
| 128 | ) : ( |
| 129 | records.map((record, index) => ( |
| 130 | <CrashRow key={record.timestamp} record={record} first={index === 0} /> |
| 131 | )) |
| 132 | )} |
| 133 | </SettingsSection> |
| 134 | |
| 135 | <View className="gap-3"> |
| 136 | <SettingsSection title={gt("Actions")}> |
| 137 | <SettingsActionRow |
| 138 | icon={copied ? "checkmark" : "doc.on.doc"} |
| 139 | label={copied ? gt("Copied") : gt("Copy crash report")} |
| 140 | disabled={state.status !== "ready"} |
| 141 | onPress={() => void copyReport()} |
| 142 | /> |
| 143 | </SettingsSection> |
| 144 | <Text className="px-2 text-sm leading-normal text-foreground-muted"> |
| 145 | <T> |
| 146 | Paste the report into a GitHub issue. It contains the app version, the JavaScript |
| 147 | error message, and the component stack. Error messages can quote values from the app, |
| 148 | so read it over before sharing. |
| 149 | </T> |
| 150 | </Text> |
| 151 | </View> |
| 152 | </ScrollView> |
| 153 | </SettingsScreen> |
| 154 | ); |
| 155 | } |
| 156 | |
| 157 | function CrashReportsSetting() { |
| 158 | const preferences = useAtomValue(mobilePreferencesAtom); |
| 159 | const savePreferences = useAtomCommand(saveMobilePreferences, { reportFailure: false }); |
| 160 | // One change at a time, so a slow save never turns sending back on after a later opt-out. |
| 161 | const [saving, setSaving] = useState(false); |
| 162 | const gt = useGT(); |
| 163 | const ready = AsyncResult.isSuccess(preferences) && !preferences.waiting; |
| 164 | const enabled = AsyncResult.isSuccess(preferences) |
| 165 | ? preferences.value.crashReportsEnabled !== false |
| 166 | : true; |
| 167 | |
| 168 | const change = (value: boolean) => { |
| 169 | setSaving(true); |
| 170 | // Sending stops before the save and starts only after it. |
| 171 | if (!value) void setCrashReportsEnabled(false); |
| 172 | savePreferences({ crashReportsEnabled: value }) |
| 173 | .then((result) => { |
| 174 | if (result._tag === "Failure") { |
| 175 | console.warn("[diagnostics] could not save the crash report setting", result.cause); |
| 176 | return value ? undefined : setCrashReportsEnabled(true); |
| 177 | } |
| 178 | return value ? setCrashReportsEnabled(true) : undefined; |
| 179 | }) |
| 180 | .catch((error: unknown) => { |
| 181 | console.warn("[diagnostics] could not apply the crash report setting", error); |
| 182 | }) |
| 183 | .finally(() => setSaving(false)); |
| 184 | }; |
| 185 | |
| 186 | return ( |
| 187 | <View className="gap-3"> |
| 188 | <SettingsSection title={gt("Crash reports")}> |
| 189 | <SettingsSwitchRow |
| 190 | icon="tray.and.arrow.up" |
| 191 | label={gt("Send crash reports")} |
| 192 | disabled={!ready || saving} |
| 193 | value={enabled} |
| 194 | onValueChange={change} |
| 195 | /> |
| 196 | </SettingsSection> |
| 197 | <Text className="px-2 text-sm leading-normal text-foreground-muted"> |
| 198 | <T> |
| 199 | When the app crashes or hits an error, it sends the error message, the stack trace, the |
| 200 | app version, and the device model and OS version to the T3 Code team. Each launch also |
| 201 | sends startup timings. Reports carry a random ID for this install. |
| 202 | </T> |
| 203 | </Text> |
| 204 | </View> |
| 205 | ); |
| 206 | } |
| 207 | |
| 208 | function EmptyState(props: { |
| 209 | readonly icon: "exclamationmark.triangle" | "checkmark.circle"; |
| 210 | readonly title: string; |
| 211 | readonly detail: string; |
| 212 | }) { |
| 213 | return ( |
| 214 | <View className="items-center gap-2 px-6 py-8"> |
| 215 | <SymbolView |
| 216 | name={props.icon} |
| 217 | size={28} |
| 218 | tintColorClassName="accent-icon" |
| 219 | type="monochrome" |
| 220 | weight="regular" |
| 221 | /> |
| 222 | <Text className="text-center text-base text-foreground">{props.title}</Text> |
| 223 | <Text className="text-center text-sm text-foreground-muted">{props.detail}</Text> |
| 224 | </View> |
| 225 | ); |
| 226 | } |
| 227 | |
| 228 | function CrashRow(props: { readonly record: StartupCrashRecord; readonly first: boolean }) { |
| 229 | const { record } = props; |
| 230 | return ( |
| 231 | <View className={props.first ? "gap-1.5 p-4" : "gap-1.5 border-t border-border-subtle p-4"}> |
| 232 | <Text className="text-xs text-foreground-muted"> |
| 233 | {new Date(record.timestamp).toLocaleString()} |
| 234 | </Text> |
| 235 | <Text selectable className="text-base leading-snug text-danger-foreground"> |
| 236 | {record.description} |
| 237 | </Text> |
| 238 | {record.frames.length > 0 ? ( |
| 239 | <Text selectable className="font-mono text-xs leading-snug text-foreground-muted"> |
| 240 | {record.frames.slice(0, 4).join("\n")} |
| 241 | </Text> |
| 242 | ) : null} |
| 243 | </View> |
| 244 | ); |
| 245 | } |