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