import React, { useEffect, useRef, useState } from 'react'; import { Platform, View, StyleSheet, Alert, BackHandler } from 'react-native'; import WebView, { type WebViewMessageEvent } from 'react-native-webview'; import { addEventListener } from '@react-native-community/netinfo'; import GestureRecognizer from 'react-native-swipe-gestures'; import loaderPage from './defaultPage'; import NoInternet from './noInternet'; import ToolBarView from './toolBar'; import type { BotDataModel, LocationDataModel, ToolBarDataModel, UiConfigModel, } from './dataModel/BotDataModel'; import { compareUrls } from './utils'; const WebViewPage = ({ pageUrl, botData, isInternalRedirection, toolBarData, location, isNativePopUpForExit, onExit, openExternalPage, onMessage, customLoader, uiConfig, }: { pageUrl: string; botData: BotDataModel; isInternalRedirection: boolean; toolBarData: ToolBarDataModel; location: LocationDataModel; isNativePopUpForExit: boolean; openExternalPage: (url: string) => void; onExit: () => void; onMessage: (event: WebViewMessageEvent) => void; customLoader?: { enabled: boolean; html?: string; }; uiConfig?: UiConfigModel; }) => { const config = { velocityThreshold: 0.3, directionalOffsetThreshold: 80, }; const webviewRef = useRef(null); const [isLoading, setLoading] = useState(true); const [isPageFailed, setPageFailed] = useState(false); const [isInternetAvailable, setInternetAvailable] = useState( true ); const onTryAgain = () => { if (isInternetAvailable) { webviewRef?.current?.reload(); } else { setLoading(true); setPageFailed(false); setTimeout(() => { setLoading(false); if (isInternetAvailable) { webviewRef?.current?.reload(); } else { setPageFailed(true); } }, 2000); } }; const onBackPress = () => { if (isInternetAvailable && !isNativePopUpForExit && !isPageFailed) { webviewRef?.current?.injectJavaScript( `window.updateFromAndroid('endchat','{}')` ); } else { Alert.alert('', 'Do you want to exit?', [ { text: 'No', onPress: () => console.log('Cancel Pressed'), style: 'cancel', }, { text: 'Yes', onPress: () => onExit() }, ]); } return true; }; const injectJavaScriptFunction = () => { webviewRef?.current?.injectJavaScript( `window.updateFromAndroid('${Platform.OS}','')` ); // webviewRef?.current?.injectJavaScript( // `window.updateFromAndroid('android','')` // ); webviewRef?.current?.injectJavaScript( `window.updateFromAndroid('psid','${JSON.stringify(botData)}')` ); console.log('botData=====>', JSON.stringify(botData)); }; useEffect(() => { const backHandler = BackHandler.addEventListener( 'hardwareBackPress', onBackPress ); return () => backHandler.remove(); // eslint-disable-next-line react-hooks/exhaustive-deps }, []); useEffect(() => { const unsubscribe = addEventListener((state) => { setInternetAvailable(state.isConnected); console.log('Is connected?', state.isConnected); }); return () => unsubscribe(); }, []); useEffect(() => { if (location && location.latitude !== 0 && location.longitude !== 0) { webviewRef?.current?.injectJavaScript( `window.getDataFromApp('${JSON.stringify(location)}')` ); } }, [location]); useEffect(() => { setLoading(pageUrl !== ''); }, [pageUrl]); const getWebViewSource = () => { if (pageUrl !== '') { return { uri: pageUrl }; } if (customLoader?.enabled && customLoader.html) { return { html: customLoader.html }; } return { html: loaderPage }; }; return ( onBackPress()} config={config} style={styles.rootView} > {isLoading ? null : ( )} } nestedScrollEnabled={true} setSupportMultipleWindows={!isInternalRedirection} onMessage={(event: WebViewMessageEvent) => { onMessage(event); }} onShouldStartLoadWithRequest={(nativeEvent) => { console.log( 'onShouldStartLoadWithRequest===============>', nativeEvent.url ); if ( isInternalRedirection && !compareUrls(nativeEvent.url, pageUrl) ) { // if (isInternalRedirection && nativeEvent.url !== pageUrl) { openExternalPage(nativeEvent.url); } const returnValue = // !isInternalRedirection || nativeEvent.url === pageUrl; !isInternalRedirection || compareUrls(nativeEvent.url, pageUrl); console.log('returnValue', returnValue); return returnValue; }} injectedJavaScriptBeforeContentLoaded={` window.ori_platform = '${Platform.OS}'; `} onLoadStart={() => { if (isInternetAvailable) { setLoading(true); setPageFailed(false); } }} onLoadEnd={(syntheticEvent) => { setLoading(false); const { nativeEvent } = syntheticEvent; if (pageUrl === nativeEvent.url) { injectJavaScriptFunction(); } }} onError={() => { setLoading(false); setPageFailed(true); }} onHttpError={() => { setLoading(false); setPageFailed(true); }} /> ); }; const styles = StyleSheet.create({ rootView: { flex: 1, }, viewBackground: {}, }); export default WebViewPage;