|
| 1 | +import React, { useMemo, useCallback } from "react"; |
| 2 | +import { View, StyleSheet } from "react-native"; |
| 3 | +import { WebView, WebViewMessageEvent } from "react-native-webview"; |
| 4 | + |
| 5 | +import { Account } from "../../../storage/account"; |
| 6 | + |
| 7 | +export type DaimoPayWebViewProps = { |
| 8 | + account: Account; |
| 9 | + visible: boolean; |
| 10 | + onClose: () => void; |
| 11 | +}; |
| 12 | + |
| 13 | +export function DaimoPayWebView({ |
| 14 | + account, |
| 15 | + visible, |
| 16 | + onClose, |
| 17 | +}: DaimoPayWebViewProps) { |
| 18 | + const styles = useMemo(() => getStyles(), []); |
| 19 | + |
| 20 | + // The /embed webpage emits a message when the user closes the modal. |
| 21 | + // When this happens, close the webview. |
| 22 | + const handleMessage = useCallback( |
| 23 | + (event: WebViewMessageEvent) => { |
| 24 | + try { |
| 25 | + const data = JSON.parse(event.nativeEvent.data); |
| 26 | + if (data?.source !== "daimo-pay") return; |
| 27 | + if (data.type === "modalClosed") { |
| 28 | + onClose(); |
| 29 | + } |
| 30 | + } catch (err) { |
| 31 | + console.error("[DAIMO PAY] Unable to parse postMessage payload", err); |
| 32 | + } |
| 33 | + }, |
| 34 | + [onClose] |
| 35 | + ); |
| 36 | + |
| 37 | + if (!visible) return null; |
| 38 | + |
| 39 | + const url = buildDaimoPayUrl(account); |
| 40 | + |
| 41 | + return ( |
| 42 | + <View style={styles.overlay}> |
| 43 | + <WebView |
| 44 | + source={{ uri: url }} |
| 45 | + style={{ flex: 1, backgroundColor: "transparent" }} |
| 46 | + containerStyle={{ backgroundColor: "transparent" }} |
| 47 | + javaScriptEnabled |
| 48 | + onMessage={handleMessage} |
| 49 | + onError={(e) => console.error("[DAIMO PAY] WebView error", e)} |
| 50 | + /> |
| 51 | + </View> |
| 52 | + ); |
| 53 | +} |
| 54 | + |
| 55 | +function buildDaimoPayUrl(account: Account) { |
| 56 | + const baseUrl = "https://giftcard.pay.daimo.xyz/embed"; |
| 57 | + const params = new URLSearchParams({ |
| 58 | + toAddress: account.address, |
| 59 | + toChain: account.homeChainId.toString(), |
| 60 | + toToken: account.homeCoinAddress, |
| 61 | + intent: "Deposit to Daimo", |
| 62 | + paymentOptions: "Coinbase,Solana", |
| 63 | + }); |
| 64 | + |
| 65 | + return `${baseUrl}?${params.toString()}`; |
| 66 | +} |
| 67 | + |
| 68 | +const getStyles = () => |
| 69 | + StyleSheet.create({ |
| 70 | + overlay: { |
| 71 | + position: "absolute", |
| 72 | + top: 0, |
| 73 | + right: 0, |
| 74 | + bottom: 0, |
| 75 | + left: 0, |
| 76 | + backgroundColor: "transparent", |
| 77 | + zIndex: 1000, |
| 78 | + }, |
| 79 | + header: { |
| 80 | + flexDirection: "row", |
| 81 | + alignItems: "center", |
| 82 | + justifyContent: "flex-end", |
| 83 | + padding: 16, |
| 84 | + zIndex: 1001, |
| 85 | + }, |
| 86 | + closeButton: { |
| 87 | + padding: 8, |
| 88 | + borderRadius: 8, |
| 89 | + }, |
| 90 | + }); |
0 commit comments