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