import { router } from "expo-router";
import { useEffect, useRef, useState } from "react";
import { BackHandler, Linking, SafeAreaView, Text, View } from "react-native";
import WebView, { WebViewMessageEvent } from "react-native-webview";
import { ShouldStartLoadRequest } from "react-native-webview/lib/WebViewTypes";
type HubbleENV = "debug" | "prod";
export default function HubbleWebView() {
authToken: "authToken", // your auth token
clientId: "id_given_by_hubble",
clientSecret: "secret_given_by_hubble",
// page: "transactions", // optional initial page to load, default is home
const baseUrl = "https://vouchers.dev.myhubble.money/classic";
const sourceUrl = `${baseUrl}?clientId=${params.clientId}&clientSecret=${params.clientSecret}&token=${params.authToken}&wrap-plt=${params.wrapPlt}&env=${params.env}&page=${params.page}`;
const webViewRef = useRef<WebView>(null);
//maintain a variable to keep track of state, if it can be popped or not
const [canGoBack, setCanGoBack] = useState(false);
const backAction = () => {
if (canGoBack && webViewRef.current) {
webViewRef.current.goBack();
return true; // prevent default behavior (app exit)
return false; // allow default behavior
const backHandler = BackHandler.addEventListener(
return () => backHandler.remove();
const onShouldStartLoadWithRequest = (e: ShouldStartLoadRequest) => {
if (e.url?.indexOf(baseUrl) === 0) {
const handleEvent = (e: WebViewMessageEvent) => {
console.log("event", e.nativeEvent.data);
const eventData = JSON.parse(e.nativeEvent.data);
const { properties, event, action } = eventData;
eventParams = { ...eventParams, ...properties };
if (action === "close") {
logEvent(event, eventParams);
function logEvent(eventType: string, eventParams: any) {
// your implementation of sending events to analytics
console.log("eventType", eventType);
<SafeAreaView style={{ flex: 1 }}>
showsVerticalScrollIndicator={false}
startInLoadingState={true}
webviewDebuggingEnabled={true} // Disable at your end
cacheEnabled={false} // Disable at your end
source={{ uri: `${sourceUrl}` }}
setSupportMultipleWindows={true}
javaScriptCanOpenWindowsAutomatically={true} // has to be true
onShouldStartLoadWithRequest={onShouldStartLoadWithRequest}
onNavigationStateChange={(navState) => {
setCanGoBack(navState.canGoBack);