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