|
| 1 | +import React from "react" |
1 | 2 | import { |
2 | 3 | dispatchConnectLocationChangeEvent, |
3 | 4 | ConnectPostMessageCallbackProps, |
4 | 5 | ConnectOAuthRequestedPayload, |
5 | 6 | } from "@mxenabled/widget-post-message-definitions" |
6 | | - |
7 | | -import { Type, SsoUrlProps, ConnectWidgetConfigurationProps } from "../sso" |
8 | 7 | import * as WebBrowser from "expo-web-browser" |
9 | | -import { makeWidgetComponentWithDefaults } from "./make_component" |
10 | | -import { StylingProps, useWidgetRenderer } from "./renderer" |
| 8 | +import { WebView, WebViewProps } from "react-native-webview" |
| 9 | +import { sdkVersion } from "../version" |
| 10 | +import { makeRequestInterceptor } from "./request_interceptor" |
11 | 11 |
|
12 | | -export type ConnectWidgetProps = SsoUrlProps & |
13 | | - StylingProps & |
14 | | - ConnectPostMessageCallbackProps<string> & |
15 | | - ConnectWidgetConfigurationProps & |
16 | | - JSX.IntrinsicAttributes |
| 12 | +interface ExtraProps { |
| 13 | + url: string |
| 14 | + webViewProps?: WebViewProps |
| 15 | +} |
17 | 16 |
|
18 | | -export const ConnectVerificationWidget = makeWidgetComponentWithDefaults(ConnectWidget, { |
19 | | - mode: "verification", |
20 | | - includeTransactions: false, |
21 | | -}) |
| 17 | +export type ConnectWidgetProps = ConnectPostMessageCallbackProps<string> & ExtraProps |
22 | 18 |
|
23 | 19 | export function ConnectWidget(props: ConnectWidgetProps) { |
| 20 | + const { url: webviewUrl, webViewProps, ...callbacks } = props |
| 21 | + |
24 | 22 | const onOAuthRequested = (payload: ConnectOAuthRequestedPayload) => { |
25 | 23 | const { url } = payload |
26 | 24 | WebBrowser.openAuthSessionAsync(url) |
27 | 25 |
|
28 | 26 | props.onOAuthRequested?.(payload) |
29 | 27 | } |
30 | 28 |
|
31 | | - props = { |
| 29 | + const modifiedCallbacks = { |
| 30 | + ...callbacks, |
32 | 31 | onOAuthRequested, |
33 | | - ...props, |
34 | 32 | } |
35 | 33 |
|
36 | | - const modifiedProps = { |
37 | | - ...props, |
38 | | - onOAuthRequested, |
39 | | - } |
40 | | - |
41 | | - const elem = useWidgetRenderer( |
42 | | - { ...modifiedProps, widgetType: Type.ConnectWidget }, |
43 | | - dispatchConnectLocationChangeEvent, |
| 34 | + const handler = makeRequestInterceptor(webviewUrl, { |
| 35 | + onIntercept: (url) => { |
| 36 | + dispatchConnectLocationChangeEvent(url, modifiedCallbacks) |
| 37 | + }, |
| 38 | + }) |
| 39 | + |
| 40 | + const setReactNativeSDKVersionOnWindow = ` |
| 41 | + window.MXReactNativeSDKVersion = "${sdkVersion}"; |
| 42 | + ` |
| 43 | + |
| 44 | + return ( |
| 45 | + <WebView |
| 46 | + cacheMode="LOAD_NO_CACHE" |
| 47 | + domStorageEnabled={true} |
| 48 | + incognito={true} |
| 49 | + injectedJavaScriptBeforeContentLoaded={setReactNativeSDKVersionOnWindow} |
| 50 | + javaScriptEnabled={true} |
| 51 | + onOpenWindow={(event) => { |
| 52 | + WebBrowser.openBrowserAsync(event.nativeEvent.targetUrl) |
| 53 | + }} |
| 54 | + onShouldStartLoadWithRequest={handler} |
| 55 | + originWhitelist={["*"]} |
| 56 | + scrollEnabled={true} |
| 57 | + source={{ uri: webviewUrl }} |
| 58 | + testID="widget_webview" |
| 59 | + {...webViewProps} |
| 60 | + style={webViewProps?.style || { height: "100%", width: "100%" }} |
| 61 | + /> |
44 | 62 | ) |
45 | | - |
46 | | - return elem |
47 | 63 | } |
0 commit comments