Files
com.blitzwallet/context-store/webViewContext.js
T
Blake KaufmanandGitHub 43856f261f NWC (#133)
* removing fetch requests when app is in background

* adding transaction capabilities to nwc

* style fixes + slider size bug fixes

* fixed spelling bug

* switching nwc to main wallet to lnurl so we can block navigation

* fixed "noster" spelling mistake

* fixed adding custom amount bug

* adding action notifications

* fixed eslint

* adding nostr settings homepage

* fixing safearea view since its a standalone page now

* adding nip5 verification

* remove brackets

* converted npub to hex and added preview to address

* chaning nostr connect to nostr wallet connect

* moving background register outside of use effect

* bump build version

* bump version

* fixing dropdown placement differnces

* using firebase messaging for android and expo for ios

* fixed dark mode style bug

* publish 13194 event to relay

* fixed error message page on long erros

* adding lookup invoice nwc event

* adding alrady paid invoice flag

* removing sparkID from invoice return

* added customizable error screen height prop

* improve sendpayment by blocking incoming events
2025-07-30 07:25:23 -04:00

112 lines
3.6 KiB
JavaScript

import React, {createContext, use, useEffect, useRef, useState} from 'react';
import WebView from 'react-native-webview';
import {Platform} from 'react-native';
import {getLocalStorageItem, setLocalStorageItem} from '../app/functions';
import handleWebviewClaimMessage from '../app/functions/boltz/handle-webview-claim-message';
import {useAppStatus} from './appStatus';
// Create a context for the WebView ref
const WebViewContext = createContext(null);
export const WebViewProvider = ({children}) => {
const {didGetToHomepage} = useAppStatus();
const webViewRef = useRef(null);
const [isWEbViewReady, setIsWebViewReady] = useState(false);
const handleClaimRetryRef = useRef(null);
const startClaimRetryRef = useRef(null);
// const [webViewArgs, setWebViewArgs] = useState({
// navigate: null,
// page: null,
// function: null,
// });
useEffect(() => {
if (!didGetToHomepage) return;
if (!isWEbViewReady) return;
if (startClaimRetryRef.current) return;
startClaimRetryRef.current = true;
async function handleUnclaimedReverseSwaps() {
console.log('Checking for unclaimed reverse swaps');
let savedClaimInfo =
JSON.parse(await getLocalStorageItem('savedReverseSwapInfo')) || [];
console.log('Saved claim info length:', savedClaimInfo.length);
console.log('Saved claim info:', savedClaimInfo);
if (savedClaimInfo.length === 0) return;
let newClaims = [];
for (const claim in savedClaimInfo) {
const createdOn = new Date(claim.createdOn);
const now = new Date();
if (!claim?.swapInfo?.id) continue;
if (claim.numberOfTries > 15) continue;
if (createdOn - now < 1000 * 60 * 3) {
// If the claim is less than 3 minutes old, skip it to prevent race condition with the websocket
newClaims.push(claim);
continue;
}
newClaims.push({...claim, numberOfTries: claim.numberOfTries + 1});
console.log('Processing claim:', claim);
const claimInfo = JSON.stringify(claim);
webViewRef.current.injectJavaScript(
`window.claimReverseSubmarineSwap(${claimInfo}); void(0);`,
);
await new Promise(resolve => setTimeout(resolve, 3000));
}
setLocalStorageItem('savedReverseSwapInfo', JSON.stringify(newClaims));
}
if (handleClaimRetryRef.current) {
console.log(
'Clearing previous claim retry interval',
handleClaimRetryRef.current,
);
clearInterval(handleClaimRetryRef.current);
}
console.log('Setting up claim retry interval');
// handleClaimRetryRef.current = setInterval(
// handleUnclaimedReverseSwaps,
// 30000,
// );
}, [isWEbViewReady, didGetToHomepage]);
return (
<WebViewContext.Provider
value={{
webViewRef,
// webViewArgs,
// setWebViewArgs,
}}>
{children}
<WebView
domStorageEnabled
javaScriptEnabled
ref={webViewRef}
containerStyle={{position: 'absolute', top: 1000, left: 1000}}
source={
Platform.OS === 'ios'
? require('boltz-swap-web-context')
: {uri: 'file:///android_asset/boltzSwap.html'}
}
originWhitelist={['*']}
onMessage={event =>
handleWebviewClaimMessage(
// webViewArgs.navigate,
event,
// webViewArgs.page,
// webViewArgs.function,
)
}
onLoadEnd={() => setIsWebViewReady(true)}
/>
</WebViewContext.Provider>
);
};
export const useWebView = () => {
return React.useContext(WebViewContext);
};