|
| 1 | +import { createBottomTabNavigator } from "@react-navigation/bottom-tabs"; |
| 2 | +import { NavigationContainer } from "@react-navigation/native"; |
| 3 | +import Constants from "expo-constants"; |
| 4 | +import { GlassView } from "expo-glass-effect"; |
| 5 | +import { StatusBar } from "expo-status-bar"; |
| 6 | +import { useEffect, useMemo, useState } from "react"; |
| 7 | +import { Platform, StyleSheet, Text, View } from "react-native"; |
| 8 | +import { SafeAreaProvider, SafeAreaView } from "react-native-safe-area-context"; |
| 9 | +import { Exceptionless } from "@exceptionless/react-native"; |
| 10 | + |
| 11 | +import { callbackLog, getLogEntries, subscribeToLogs } from "./logging"; |
| 12 | +import ErrorsScreen from "./screens/ErrorsScreen"; |
| 13 | +import EventsScreen from "./screens/EventsScreen"; |
| 14 | +import LogsScreen from "./screens/LogsScreen"; |
| 15 | + |
| 16 | +type TabParamList = { |
| 17 | + Errors: undefined; |
| 18 | + Events: undefined; |
| 19 | + Logs: undefined; |
| 20 | +}; |
| 21 | + |
| 22 | +const Tab = createBottomTabNavigator<TabParamList>(); |
| 23 | + |
| 24 | +const serverUrl = getServerUrl(); |
| 25 | + |
| 26 | +function TabIcon({ label, focused }: { label: string; focused: boolean }) { |
| 27 | + return <Text style={[styles.tabIcon, focused && styles.tabIconFocused]}>{label}</Text>; |
| 28 | +} |
| 29 | + |
| 30 | +/** |
| 31 | + * Resolves the dev server URL based on the current platform. |
| 32 | + * - Web: localhost works directly. |
| 33 | + * - iOS Simulator: shares the host Mac's network. |
| 34 | + * - Real iOS device: needs the dev machine's IP, extracted from Expo's hostUri. |
| 35 | + */ |
| 36 | +function getServerUrl(): string { |
| 37 | + if (__DEV__ && Platform.OS !== "web") { |
| 38 | + const hostUri = Constants.expoConfig?.hostUri; |
| 39 | + if (hostUri) { |
| 40 | + try { |
| 41 | + const hostname = new URL(`http://${hostUri}`).hostname; |
| 42 | + return `http://${hostname}:7110`; |
| 43 | + } catch { |
| 44 | + // Fall through to default |
| 45 | + } |
| 46 | + } |
| 47 | + } |
| 48 | + return "http://localhost:7110"; |
| 49 | +} |
| 50 | + |
| 51 | +function TopDiagnostics() { |
| 52 | + const [logs, setLogs] = useState(() => getLogEntries()); |
| 53 | + const latestLog = logs.at(-1); |
| 54 | + const errorCount = useMemo(() => logs.filter((entry) => entry.level === "error").length, [logs]); |
| 55 | + |
| 56 | + useEffect(() => subscribeToLogs(() => setLogs(getLogEntries())), []); |
| 57 | + |
| 58 | + return ( |
| 59 | + <SafeAreaView edges={["top"]} style={styles.diagnosticsSafeArea}> |
| 60 | + <View style={styles.diagnostics}> |
| 61 | + <View style={styles.diagnosticsTitleRow}> |
| 62 | + <Text style={styles.diagnosticsTitle}>Exceptionless Expo</Text> |
| 63 | + <Text style={styles.diagnosticsPill}>SDK 56</Text> |
| 64 | + </View> |
| 65 | + <Text style={styles.diagnosticsServer} numberOfLines={1}> |
| 66 | + {serverUrl} |
| 67 | + </Text> |
| 68 | + <View style={styles.diagnosticsMetaRow}> |
| 69 | + <Text style={styles.diagnosticsMeta}>{logs.length} logs</Text> |
| 70 | + <Text style={styles.diagnosticsMeta}>{errorCount} errors</Text> |
| 71 | + <Text style={styles.diagnosticsMeta}>sessions on</Text> |
| 72 | + </View> |
| 73 | + <Text style={styles.latestLog} numberOfLines={2}> |
| 74 | + {latestLog ? `[${latestLog.level.toUpperCase()}] ${latestLog.message}` : "Waiting for Exceptionless startup logs..."} |
| 75 | + </Text> |
| 76 | + </View> |
| 77 | + </SafeAreaView> |
| 78 | + ); |
| 79 | +} |
| 80 | + |
| 81 | +export default function App() { |
| 82 | + useEffect(() => { |
| 83 | + void Exceptionless.startup((config) => { |
| 84 | + config.apiKey = "LhhP1C9gijpSKCslHHCvwdSIz298twx271nTest"; |
| 85 | + config.serverUrl = serverUrl; |
| 86 | + config.services.log = callbackLog; |
| 87 | + config.defaultTags.push("Example", "Expo"); |
| 88 | + config.useSessions(true, 60000, true); |
| 89 | + }); |
| 90 | + }, []); |
| 91 | + |
| 92 | + return ( |
| 93 | + <SafeAreaProvider> |
| 94 | + <NavigationContainer> |
| 95 | + <View style={styles.appShell}> |
| 96 | + <TopDiagnostics /> |
| 97 | + <Tab.Navigator |
| 98 | + screenOptions={{ |
| 99 | + headerShown: false, |
| 100 | + tabBarActiveTintColor: "#0f172a", |
| 101 | + tabBarInactiveTintColor: "#64748b", |
| 102 | + tabBarLabelStyle: styles.tabLabel, |
| 103 | + tabBarStyle: styles.tabBar, |
| 104 | + tabBarBackground: () => <GlassView glassEffectStyle="regular" isInteractive style={StyleSheet.absoluteFill} tintColor="rgba(255,255,255,0.58)" /> |
| 105 | + }} |
| 106 | + > |
| 107 | + <Tab.Screen |
| 108 | + name="Errors" |
| 109 | + component={ErrorsScreen} |
| 110 | + options={{ |
| 111 | + title: "Errors", |
| 112 | + tabBarIcon: ({ focused }) => <TabIcon label="!" focused={focused} /> |
| 113 | + }} |
| 114 | + /> |
| 115 | + <Tab.Screen |
| 116 | + name="Events" |
| 117 | + component={EventsScreen} |
| 118 | + options={{ |
| 119 | + title: "Events", |
| 120 | + tabBarIcon: ({ focused }) => <TabIcon label="|" focused={focused} /> |
| 121 | + }} |
| 122 | + /> |
| 123 | + <Tab.Screen |
| 124 | + name="Logs" |
| 125 | + component={LogsScreen} |
| 126 | + options={{ |
| 127 | + title: "Logs", |
| 128 | + tabBarIcon: ({ focused }) => <TabIcon label="#" focused={focused} /> |
| 129 | + }} |
| 130 | + /> |
| 131 | + </Tab.Navigator> |
| 132 | + </View> |
| 133 | + <StatusBar style="auto" /> |
| 134 | + </NavigationContainer> |
| 135 | + </SafeAreaProvider> |
| 136 | + ); |
| 137 | +} |
| 138 | + |
| 139 | +const styles = StyleSheet.create({ |
| 140 | + appShell: { |
| 141 | + flex: 1, |
| 142 | + backgroundColor: "#fff" |
| 143 | + }, |
| 144 | + diagnosticsSafeArea: { |
| 145 | + backgroundColor: "#fff" |
| 146 | + }, |
| 147 | + diagnostics: { |
| 148 | + borderBottomColor: "#e5e7eb", |
| 149 | + borderBottomWidth: StyleSheet.hairlineWidth, |
| 150 | + paddingBottom: 10, |
| 151 | + paddingHorizontal: 16, |
| 152 | + paddingTop: 8 |
| 153 | + }, |
| 154 | + diagnosticsTitleRow: { |
| 155 | + alignItems: "center", |
| 156 | + flexDirection: "row", |
| 157 | + justifyContent: "space-between" |
| 158 | + }, |
| 159 | + diagnosticsTitle: { |
| 160 | + color: "#111827", |
| 161 | + fontSize: 17, |
| 162 | + fontWeight: "700" |
| 163 | + }, |
| 164 | + diagnosticsPill: { |
| 165 | + backgroundColor: "#eef2ff", |
| 166 | + borderRadius: 999, |
| 167 | + color: "#3730a3", |
| 168 | + fontSize: 12, |
| 169 | + fontWeight: "700", |
| 170 | + overflow: "hidden", |
| 171 | + paddingHorizontal: 10, |
| 172 | + paddingVertical: 4 |
| 173 | + }, |
| 174 | + diagnosticsServer: { |
| 175 | + color: "#475569", |
| 176 | + fontSize: 12, |
| 177 | + marginTop: 4 |
| 178 | + }, |
| 179 | + diagnosticsMetaRow: { |
| 180 | + flexDirection: "row", |
| 181 | + gap: 8, |
| 182 | + marginTop: 8 |
| 183 | + }, |
| 184 | + diagnosticsMeta: { |
| 185 | + backgroundColor: "#f8fafc", |
| 186 | + borderColor: "#e2e8f0", |
| 187 | + borderRadius: 999, |
| 188 | + borderWidth: StyleSheet.hairlineWidth, |
| 189 | + color: "#334155", |
| 190 | + fontSize: 11, |
| 191 | + fontWeight: "600", |
| 192 | + overflow: "hidden", |
| 193 | + paddingHorizontal: 8, |
| 194 | + paddingVertical: 3 |
| 195 | + }, |
| 196 | + latestLog: { |
| 197 | + color: "#111827", |
| 198 | + fontFamily: Platform.select({ ios: "Menlo", default: "monospace" }), |
| 199 | + fontSize: 11, |
| 200 | + lineHeight: 15, |
| 201 | + marginTop: 8 |
| 202 | + }, |
| 203 | + tabBar: { |
| 204 | + backgroundColor: "rgba(255,255,255,0.5)", |
| 205 | + borderTopColor: "rgba(148,163,184,0.22)", |
| 206 | + borderTopWidth: StyleSheet.hairlineWidth, |
| 207 | + elevation: 0, |
| 208 | + height: 78, |
| 209 | + paddingBottom: 14, |
| 210 | + paddingTop: 8, |
| 211 | + position: "absolute", |
| 212 | + shadowColor: "#0f172a", |
| 213 | + shadowOffset: { height: -4, width: 0 }, |
| 214 | + shadowOpacity: 0.08, |
| 215 | + shadowRadius: 18 |
| 216 | + }, |
| 217 | + tabIcon: { |
| 218 | + color: "#64748b", |
| 219 | + fontSize: 18, |
| 220 | + fontWeight: "800", |
| 221 | + lineHeight: 20 |
| 222 | + }, |
| 223 | + tabIconFocused: { |
| 224 | + color: "#0f172a" |
| 225 | + }, |
| 226 | + tabLabel: { |
| 227 | + fontSize: 12, |
| 228 | + fontWeight: "700" |
| 229 | + } |
| 230 | +}); |
0 commit comments