Body.tsx
1.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import React, { useCallback } from 'react';
import RNCWebView, { WebViewNavigation } from 'react-native-webview';
import { WebviewState, WebviewActions, webActions } from './reducer';
import {
WebViewProgressEvent,
WebViewErrorEvent,
OnShouldStartLoadWithRequest,
} from 'react-native-webview/lib/WebViewTypes';
import ErrorView from './ErrorView';
import { Linking } from 'react-native';
const Body = React.forwardRef<
RNCWebView,
{
state: WebviewState;
dispatch: React.Dispatch<WebviewActions>;
initialUrl: string;
}
>(({ state, dispatch, initialUrl }, ref) => {
const onNavigationStateChange = useCallback(
(s: WebViewNavigation) => {
dispatch(webActions.changeNavigationState(s));
},
[dispatch]
);
const onLoadProgress = useCallback(
(s: WebViewProgressEvent) => {
dispatch(webActions.onLoadProgress(s.nativeEvent));
},
[dispatch]
);
const onError = useCallback(
(s: WebViewErrorEvent) => {
dispatch(webActions.onLoadError(s.nativeEvent));
},
[dispatch]
);
const shouldRequest: OnShouldStartLoadWithRequest = useCallback(
(request) => {
const { url } = request;
if (url.startsWith('http') || url === 'about:blank') {
return true;
} else {
dispatch(webActions.changeNavigationState(request));
Linking.canOpenURL(url)
.then((canOpen) => {
if (canOpen) {
return Linking.openURL(url);
}
})
.catch(() => {});
return false;
}
},
[dispatch]
);
return (
<RNCWebView
ref={ref}
source={{ uri: initialUrl }}
onNavigationStateChange={onNavigationStateChange}
onLoadProgress={onLoadProgress}
onError={onError}
onShouldStartLoadWithRequest={shouldRequest}
renderError={(_, code, description) => (
<ErrorView code={code} description={description} />
)}
/>
);
});
export default Body;