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