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