From 6ce24c07c9f6a1ed9b651762a4c9a85a6d88461a Mon Sep 17 00:00:00 2001 From: Antonis Lilis Date: Fri, 13 Nov 2026 10:04:08 +0100 Subject: [PATCH] feat: Add Empower web store WebView tab with connected traces Loads the Empower Plant React app in a react-native-webview and injects the current trace via sentryWebViewProps, so the web pageload and its backend calls continue the React Native trace. Co-Authored-By: Claude Opus 5.5 --- App.tsx | 18 ++++++ package-lock.json | 15 +++++ package.json | 1 + src/navigation.ts | 1 + src/screens/WebStoreScreen.tsx | 115 +++++++++++++++++++++++++++++++++ 5 files changed, 150 insertions(+) create mode 100644 src/screens/WebStoreScreen.tsx diff --git a/App.tsx b/App.tsx index 2909adf..9567e1e 100644 --- a/App.tsx +++ b/App.tsx @@ -16,6 +16,7 @@ import ProductDetailScreen from './src/screens/ProductDetailScreen'; import ReduxScreen from './src/screens/ReduxScreen'; import CartScreen from './src/screens/CartScreen'; import CheckoutScreen from './src/screens/CheckoutScreen'; +import WebStoreScreen from './src/screens/WebStoreScreen'; import Toast from 'react-native-toast-message'; import {RootState, store} from './src/reduxApp'; @@ -210,6 +211,23 @@ const BottomTabNavigator = () => { ), }} /> + {/* Last, so existing tabs keep their positions for TDA. */} + ( + + + + ), + }} + /> ); }; diff --git a/package-lock.json b/package-lock.json index 72a33bb..40d61bd 100644 --- a/package-lock.json +++ b/package-lock.json @@ -22,6 +22,7 @@ "react-native-safe-area-context": "~5.7.0", "react-native-screens": "~4.26.0", "react-native-toast-message": "^2.5.0", + "react-native-webview": "~13.16.1", "react-redux": "^9.3.0", "redux": "^5.0.1" }, @@ -16226,6 +16227,20 @@ "react-native": "*" } }, + "node_modules/react-native-webview": { + "version": "13.16.1", + "resolved": "https://sfw.security.sentry.io/npm/react-native-webview/-/react-native-webview-13.16.1.tgz", + "integrity": "sha512-If0eHhoEdOYDcHsX+xBFwHMbWBGK1BvGDQDQdVkwtSIXiq1uiqjkpWVP2uQ1as94J0CzvFE9PUNDuhiX0Z6ubw==", + "license": "MIT", + "dependencies": { + "escape-string-regexp": "^4.0.0", + "invariant": "2.2.4" + }, + "peerDependencies": { + "react": "*", + "react-native": "*" + } + }, "node_modules/react-native/node_modules/commander": { "version": "12.1.0", "resolved": "https://sfw.security.sentry.io/npm/commander/-/commander-12.1.0.tgz", diff --git a/package.json b/package.json index ea17a25..1237b1f 100644 --- a/package.json +++ b/package.json @@ -17,6 +17,7 @@ "react-native-safe-area-context": "~5.7.0", "react-native-screens": "~4.26.0", "react-native-toast-message": "^2.5.0", + "react-native-webview": "~13.16.1", "react-redux": "^9.3.0", "redux": "^5.0.1" }, diff --git a/src/navigation.ts b/src/navigation.ts index 07e5ff3..00598e4 100644 --- a/src/navigation.ts +++ b/src/navigation.ts @@ -12,4 +12,5 @@ export type RootStackParamList = { Checkout: undefined; CartScreen: undefined; ProductDetail: Product; + WebStore: undefined; }; diff --git a/src/screens/WebStoreScreen.tsx b/src/screens/WebStoreScreen.tsx new file mode 100644 index 0000000..cd1762d --- /dev/null +++ b/src/screens/WebStoreScreen.tsx @@ -0,0 +1,115 @@ +import * as React from 'react'; +import {ActivityIndicator, StyleSheet, View} from 'react-native'; +import {SafeAreaView} from 'react-native-safe-area-context'; +import {WebView} from 'react-native-webview'; +import * as Sentry from '@sentry/react-native'; + +// The Empower Plant React web app. Its `@sentry/react` continues a trace from the +// `sentry-trace` / `baggage` tags that `sentryWebViewProps` injects, so the +// web pageload (and the backend calls it makes) join this app's trace. +const WEB_STORE_HOST = 'empower-plant.com'; + +// Open `/products` (not `/`) so the products page *is* the web pageload: only the +// pageload continues the injected trace, and it fetches /products from the backend, +// giving one RN -> React -> backend trace. +const WEB_STORE_PATH = '/products'; + +const buildWebStoreUrl = (): string => { + const params: Record = {backend: 'flask'}; + + const se = process.env.EXPO_PUBLIC_SE; + if (se) { + params.se = se; + } + + // Reuse the RN app's random user so web events are attributed to the same user. + const email = Sentry.getCurrentScope().getUser()?.email; + if (email) { + params.userEmail = email; + } + + const query = Object.entries(params) + .map(([key, value]) => `${key}=${encodeURIComponent(value)}`) + .join('&'); + return `https://${WEB_STORE_HOST}${WEB_STORE_PATH}?${query}`; +}; + +const WebStoreScreen = () => { + // A dedicated transaction spanning the WebView load. It's the parent the web + // pageload continues from, so the trace waterfall shows the web page nested + // under the time the app spent loading it. Created once per mount. + const [loadSpan] = React.useState(() => + Sentry.startInactiveSpan({ + name: 'webview/empower-plant', + op: 'ui.webview.load', + forceTransaction: true, + attributes: {url: `https://${WEB_STORE_HOST}${WEB_STORE_PATH}`}, + }), + ); + + // Computed once, with the load span active, so the injected `sentry-trace` + // points at it. Must not change across re-renders. + const [{uri, sentryProps}] = React.useState(() => ({ + uri: buildWebStoreUrl(), + sentryProps: Sentry.withActiveSpan(loadSpan, () => + Sentry.sentryWebViewProps({allowedHosts: [WEB_STORE_HOST]}), + ), + })); + + const endLoadSpan = React.useCallback( + (status?: 'ok' | 'internal_error') => { + // Ends once; later page loads (e.g. a full navigation inside the page) no-op. + if (!loadSpan.isRecording()) { + return; + } + // Span status codes: 1 = ok, 2 = error. + loadSpan.setStatus( + status === 'internal_error' + ? {code: 2, message: 'internal_error'} + : {code: 1}, + ); + loadSpan.end(); + }, + [loadSpan], + ); + + // Fallback: end the span if the user leaves before the page finishes loading. + React.useEffect(() => () => endLoadSpan(), [endLoadSpan]); + + return ( + // Tabs have no header here, so keep the page clear of the status bar. + + ( + + + + )} + // Spread first: it provides the trace injection and the `onMessage` that + // Android needs for injected JS to run. Never override + // `injectedJavaScriptBeforeContentLoaded` after it. + {...sentryProps} + onLoadEnd={() => endLoadSpan()} + onError={() => endLoadSpan('internal_error')} + onHttpError={() => endLoadSpan('internal_error')} + /> + + ); +}; + +const styles = StyleSheet.create({ + container: { + flex: 1, + backgroundColor: '#ffffff', + }, + loading: { + ...StyleSheet.absoluteFill, + alignItems: 'center', + justifyContent: 'center', + }, +}); + +export default WebStoreScreen;