|
| 1 | +import Constants from "expo-constants"; |
| 2 | +import * as Updates from "expo-updates"; |
| 3 | +import { useEffect, useState } from "react"; |
| 4 | +import { ActivityIndicator, Platform, Pressable, ScrollView, View } from "react-native"; |
| 5 | +import { useSafeAreaInsets } from "react-native-safe-area-context"; |
| 6 | + |
| 7 | +import { SymbolView } from "../../components/AppSymbol"; |
| 8 | +import { AppText as Text } from "../../components/AppText"; |
| 9 | +import { tryCopyTextWithHaptic } from "../../lib/copyTextWithHaptic"; |
| 10 | +import { SettingsSection } from "../settings/components/SettingsSection"; |
| 11 | +import { |
| 12 | + formatStartupCrashReport, |
| 13 | + parseStartupCrashRecords, |
| 14 | + type StartupCrashRecord, |
| 15 | +} from "./crash-log-model"; |
| 16 | + |
| 17 | +// expo-updates keeps its persistent log this long. Reading any further back |
| 18 | +// returns nothing, so this is the whole available window. |
| 19 | +const LOG_WINDOW_MS = 7 * 24 * 60 * 60 * 1000; |
| 20 | + |
| 21 | +type CrashLogState = |
| 22 | + | { readonly status: "loading" } |
| 23 | + | { readonly status: "unavailable" } |
| 24 | + | { readonly status: "ready"; readonly records: ReadonlyArray<StartupCrashRecord> }; |
| 25 | + |
| 26 | +function appIdentity() { |
| 27 | + return { |
| 28 | + version: Constants.expoConfig?.version ?? "0.0.0", |
| 29 | + build: |
| 30 | + (Platform.OS === "ios" |
| 31 | + ? Constants.platform?.ios?.buildNumber |
| 32 | + : Constants.platform?.android?.versionCode?.toString()) ?? "dev", |
| 33 | + }; |
| 34 | +} |
| 35 | + |
| 36 | +/** |
| 37 | + * Startup crashes that TestFlight and the stores strip from their reports. |
| 38 | + * expo-updates' ErrorRecovery writes the JS error and component stack to its |
| 39 | + * own log before aborting the process, so the next launch can show it here. |
| 40 | + */ |
| 41 | +export function SettingsDiagnosticsRouteScreen() { |
| 42 | + const insets = useSafeAreaInsets(); |
| 43 | + const [state, setState] = useState<CrashLogState>(() => |
| 44 | + Updates.isEnabled ? { status: "loading" } : { status: "unavailable" }, |
| 45 | + ); |
| 46 | + const [copied, setCopied] = useState(false); |
| 47 | + |
| 48 | + useEffect(() => { |
| 49 | + if (!Updates.isEnabled) return; |
| 50 | + let cancelled = false; |
| 51 | + Updates.readLogEntriesAsync(LOG_WINDOW_MS) |
| 52 | + .then((entries) => { |
| 53 | + if (cancelled) return; |
| 54 | + setState({ status: "ready", records: parseStartupCrashRecords(entries) }); |
| 55 | + }) |
| 56 | + .catch((error: unknown) => { |
| 57 | + console.warn("[diagnostics] could not read the expo-updates log", error); |
| 58 | + if (!cancelled) setState({ status: "unavailable" }); |
| 59 | + }); |
| 60 | + return () => { |
| 61 | + cancelled = true; |
| 62 | + }; |
| 63 | + }, []); |
| 64 | + |
| 65 | + const records = state.status === "ready" ? state.records : []; |
| 66 | + const copyReport = async () => { |
| 67 | + const ok = await tryCopyTextWithHaptic(formatStartupCrashReport(records, appIdentity()), { |
| 68 | + target: "crash report", |
| 69 | + }); |
| 70 | + if (ok) setCopied(true); |
| 71 | + }; |
| 72 | + |
| 73 | + return ( |
| 74 | + <View collapsable={false} className="flex-1 bg-sheet"> |
| 75 | + <ScrollView |
| 76 | + contentInsetAdjustmentBehavior="automatic" |
| 77 | + contentInset={{ bottom: Math.max(insets.bottom, 18) }} |
| 78 | + showsVerticalScrollIndicator={false} |
| 79 | + className="flex-1" |
| 80 | + contentContainerClassName="gap-6 px-5 pt-4 pb-[18px]" |
| 81 | + > |
| 82 | + <SettingsSection title="Startup crashes"> |
| 83 | + {state.status === "loading" ? ( |
| 84 | + <View className="items-center gap-3 px-6 py-8"> |
| 85 | + <ActivityIndicator /> |
| 86 | + <Text className="text-center text-sm text-foreground-muted">Reading crash log…</Text> |
| 87 | + </View> |
| 88 | + ) : state.status === "unavailable" ? ( |
| 89 | + <EmptyState |
| 90 | + icon="exclamationmark.triangle" |
| 91 | + title="Crash log unavailable" |
| 92 | + detail="Startup crash records are only kept in store and TestFlight builds." |
| 93 | + /> |
| 94 | + ) : records.length === 0 ? ( |
| 95 | + <EmptyState |
| 96 | + icon="checkmark.circle" |
| 97 | + title="No startup crashes" |
| 98 | + detail="Nothing has taken the app down during launch in the last 7 days." |
| 99 | + /> |
| 100 | + ) : ( |
| 101 | + records.map((record, index) => ( |
| 102 | + <CrashRow key={record.timestamp} record={record} first={index === 0} /> |
| 103 | + )) |
| 104 | + )} |
| 105 | + </SettingsSection> |
| 106 | + |
| 107 | + <View className="gap-3"> |
| 108 | + <SettingsSection title="Actions"> |
| 109 | + <Pressable |
| 110 | + accessibilityRole="button" |
| 111 | + disabled={state.status !== "ready"} |
| 112 | + onPress={() => void copyReport()} |
| 113 | + className="flex-row items-center gap-4 p-4 disabled:opacity-40" |
| 114 | + > |
| 115 | + <SymbolView |
| 116 | + name={copied ? "checkmark" : "doc.on.doc"} |
| 117 | + size={22} |
| 118 | + tintColorClassName={"accent-icon"} |
| 119 | + type="monochrome" |
| 120 | + weight="regular" |
| 121 | + /> |
| 122 | + <Text className="flex-1 text-lg text-foreground"> |
| 123 | + {copied ? "Copied" : "Copy crash report"} |
| 124 | + </Text> |
| 125 | + </Pressable> |
| 126 | + </SettingsSection> |
| 127 | + <Text className="px-2 text-sm leading-normal text-foreground-muted"> |
| 128 | + Paste the report into a GitHub issue. It contains the app version, the JavaScript error |
| 129 | + message, and the component stack. Error messages can quote values from the app, so read |
| 130 | + it over before sharing. |
| 131 | + </Text> |
| 132 | + </View> |
| 133 | + </ScrollView> |
| 134 | + </View> |
| 135 | + ); |
| 136 | +} |
| 137 | + |
| 138 | +function EmptyState(props: { |
| 139 | + readonly icon: "exclamationmark.triangle" | "checkmark.circle"; |
| 140 | + readonly title: string; |
| 141 | + readonly detail: string; |
| 142 | +}) { |
| 143 | + return ( |
| 144 | + <View className="items-center gap-2 px-6 py-8"> |
| 145 | + <SymbolView |
| 146 | + name={props.icon} |
| 147 | + size={28} |
| 148 | + tintColorClassName={"accent-icon"} |
| 149 | + type="monochrome" |
| 150 | + weight="regular" |
| 151 | + /> |
| 152 | + <Text className="text-center text-base text-foreground">{props.title}</Text> |
| 153 | + <Text className="text-center text-sm text-foreground-muted">{props.detail}</Text> |
| 154 | + </View> |
| 155 | + ); |
| 156 | +} |
| 157 | + |
| 158 | +function CrashRow(props: { readonly record: StartupCrashRecord; readonly first: boolean }) { |
| 159 | + const { record } = props; |
| 160 | + return ( |
| 161 | + <View className={props.first ? "gap-1.5 p-4" : "gap-1.5 border-t border-border-subtle p-4"}> |
| 162 | + <Text className="text-xs text-foreground-muted"> |
| 163 | + {new Date(record.timestamp).toLocaleString()} |
| 164 | + </Text> |
| 165 | + <Text selectable className="text-base leading-snug text-danger-foreground"> |
| 166 | + {record.description} |
| 167 | + </Text> |
| 168 | + {record.frames.length > 0 ? ( |
| 169 | + <Text selectable className="font-mono text-xs leading-snug text-foreground-muted"> |
| 170 | + {record.frames.slice(0, 4).join("\n")} |
| 171 | + </Text> |
| 172 | + ) : null} |
| 173 | + </View> |
| 174 | + ); |
| 175 | +} |
0 commit comments