import { RefObject } from "react"; import { BackHandler, Keyboard } from "react-native"; import WebView, { WebViewMessageEvent } from "react-native-webview"; import Applications, { IConfig } from "../../applications/Applications"; import FunctionApplication from "../../applications/FunctionApplication"; import { getSdkJsEndLoad, sdk_js } from "../../sdk/sdk_js"; export interface DataEvent { object?: object; string?: string; number?: number; boolean?: boolean; } export interface Resolve { type: string; payload: DataEvent; } const useAppPlayWithoutState = ( refWebview: RefObject, onListenEvent: (event: string, data: string, callback: (data: any) => void, err: (err: any) => void) => void ) => { const onLoadEnd = () => { let funct = ''; Object.keys(FunctionApplication.funct).map((item, index) => { funct += `${index === 0 ? '' : ','}${item}` }) refWebview.current?.injectJavaScript(sdk_js()); refWebview.current?.injectJavaScript(`typeof window.asyncMiniAppSdk === 'function' && window.asyncMiniAppSdk(),true;`) if (FunctionApplication.context && FunctionApplication.context.extras) { refWebview.current?.injectJavaScript(getSdkJsEndLoad()); } handleResolveEvent('initSdk', { string: funct }); }; const application = new Applications(); const getConfig = async (): Promise => { return await application.requetConfig(); }; const handleNativeKeyboard = () => { const keyboardDidShow = Keyboard.addListener( "keyboardDidShow", onHandleKeyboard("keyboardDidShow") ); const keyboardDidHide = Keyboard.addListener( "keyboardDidHide", onHandleKeyboard("keyboardDidHide") ); const keyboardWillHide = Keyboard.addListener( "keyboardWillHide", onHandleKeyboard("keyboardWillHide") ); const keyboardWillShow = Keyboard.addListener( "keyboardWillShow", onHandleKeyboard("keyboardWillShow") ); return () => { keyboardDidShow.remove(); keyboardDidHide.remove(); keyboardWillHide.remove(); keyboardWillShow.remove(); }; }; const onHandleKeyboard = (event: string) => () => { handleResolveEvent(`native.${event}`, { string: event }); }; const handleHardwareBackPress = () => { const hardwareBackPress = BackHandler.addEventListener( "hardwareBackPress", onHandleHardwareBackPress ); return () => { hardwareBackPress.remove(); }; }; const onHandleHardwareBackPress = () => { handleResolveEvent(`native.hardwareBackPress`, { boolean: true }); return true; }; const handleMessage = async (e: WebViewMessageEvent) => { const message = e.nativeEvent.data; const regex_valid_message = /([\w.]+)-([\w]+)\.?(.*)/g; const match = regex_valid_message.exec(message); if (match) { const event = match[1]; const random = match[2]; const data = match[3]; const event_response = `${event}-${random}`; if (event === 'context') { return handleResolveEvent(event_response, { object: FunctionApplication.context }); }; if (!checkFunction(event, random)) { return; } const funct = FunctionApplication.funct[event].name; const params = typeof data === 'undefined' ? `()` : `(${data})`; onListenEvent && onListenEvent(event, `${funct}${params}`, data => { let dataRes = {}; if (typeof data === 'object') { dataRes = { object: data }; } if (typeof data === 'string') { dataRes = { string: data }; } if (typeof data === 'boolean') { dataRes = { boolean: data }; } if (typeof data === 'number') { dataRes = { number: data }; } handleResolveEvent(event_response, dataRes); }, err => { handleRejectEvent(event_response, err); }); } if (message) { console.log('message', message); const event = message.split(" "); onListenEvent && onListenEvent(event[0], event[1], data => { }, err => { }); } }; const checkFunction = (event: string, random: string) => { const event_response = `${event}-${random}`; const arrFunct = Object.keys(FunctionApplication.funct); if (!arrFunct.includes(event)) { handleRejectEvent(event_response, `Chức năng này chưa được hỗ trợ`); return false; } return true; } const handleResolveEvent = (event: string, data: DataEvent) => { const resolve: Resolve = { type: "reolve", payload: data, }; refWebview.current?.injectJavaScript(` typeof window.Miniapp !== 'undefined' && typeof window.Miniapp.callbackEvent === 'function' && window.Miniapp.callbackEvent('${event}', ${JSON.stringify( resolve )}); `); }; const handleRejectEvent = (event: string, data: string) => { const reject = { type: "reject", message: data, }; refWebview.current && refWebview.current.injectJavaScript( `typeof window.Miniapp !== 'undefined' && typeof window.Miniapp.callbackEvent === 'function' && window.Miniapp.callbackEvent('${event}', ${JSON.stringify( reject )})` ); }; return { handleMessage, onLoadEnd, handleNativeKeyboard, handleHardwareBackPress, getConfig, handleResolveEvent, handleRejectEvent }; }; export default useAppPlayWithoutState;