Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -210,6 +211,23 @@ const BottomTabNavigator = () => {
),
}}
/>
{/* Last, so existing tabs keep their positions for TDA. */}
<Tab.Screen
name="WebStore"
component={WebStoreScreen}
options={{
tabBarIcon: ({focused}) => (
<Sentry.Unmask>
<FontAwesome6
testID="bottom-tab-webview"
name="globe"
size={30}
color={focused ? '#f6cfb2' : '#dae3e4'}
/>
</Sentry.Unmask>
),
}}
/>
</Tab.Navigator>
);
};
Expand Down
15 changes: 15 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
},
Expand Down
1 change: 1 addition & 0 deletions src/navigation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ export type RootStackParamList = {
Checkout: undefined;
CartScreen: undefined;
ProductDetail: Product;
WebStore: undefined;
};
115 changes: 115 additions & 0 deletions src/screens/WebStoreScreen.tsx
Original file line number Diff line number Diff line change
@@ -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` <meta> 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<string, string> = {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.
<SafeAreaView style={styles.container} edges={['top']}>
<WebView
testID="webview-store"
source={{uri}}
startInLoadingState
renderLoading={() => (
<View style={styles.loading}>
<ActivityIndicator size="large" color="#002626" />
</View>
)}
// 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')}
/>
</SafeAreaView>
);
};

const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#ffffff',
},
loading: {
...StyleSheet.absoluteFill,
alignItems: 'center',
justifyContent: 'center',
},
});

export default WebStoreScreen;
Loading