Skip to content

Commit

Permalink
fix(macos): Fix source prop for Fabric (#3404)
Browse files Browse the repository at this point in the history
  • Loading branch information
shwanton committed May 7, 2024
1 parent 542a997 commit 7fb5c3f
Showing 1 changed file with 49 additions and 32 deletions.
81 changes: 49 additions & 32 deletions src/WebView.macos.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,35 +4,20 @@ import React, {
useImperativeHandle,
useRef,
} from 'react';
import { Image, View, ImageSourcePropType } from 'react-native';
import { Image, View, ImageSourcePropType, HostComponent } from 'react-native';
import invariant from 'invariant';

import codegenNativeCommands from 'react-native/Libraries/Utilities/codegenNativeCommands';
import RNCWebView from './WebViewNativeComponent.macos';
import RNCWebView, { Commands, NativeProps } from './RNCWebViewNativeComponent';
import RNCWebViewModule from './NativeRNCWebView';
import {
defaultOriginWhitelist,
defaultRenderError,
defaultRenderLoading,
useWebViewLogic,
} from './WebViewShared';
import { MacOSWebViewProps, NativeWebViewMacOS } from './WebViewTypes';
import { MacOSWebViewProps, WebViewSourceUri } from './WebViewTypes';

import styles from './WebView.styles';

const Commands = codegenNativeCommands({
supportedCommands: [
'goBack',
'goForward',
'reload',
'stopLoading',
'injectJavaScript',
'requestFocus',
'postMessage',
'loadUrl',
],
});

const { resolveAssetSource } = Image;

const useWarnIfChanges = <T extends unknown>(value: T, name: string) => {
Expand Down Expand Up @@ -78,7 +63,9 @@ const WebViewComponent = forwardRef<{}, MacOSWebViewProps>(
},
ref
) => {
const webViewRef = useRef<NativeWebViewMacOS | null>(null);
const webViewRef = useRef<React.ComponentRef<
HostComponent<NativeProps>
> | null>(null);

const onShouldStartLoadWithRequestCallback = useCallback(
(shouldStart: boolean, _url: string, lockIdentifier = 0) => {
Expand Down Expand Up @@ -120,18 +107,24 @@ const WebViewComponent = forwardRef<{}, MacOSWebViewProps>(
useImperativeHandle(
ref,
() => ({
goForward: () => Commands.goForward(webViewRef.current),
goBack: () => Commands.goBack(webViewRef.current),
goForward: () =>
webViewRef.current && Commands.goForward(webViewRef.current),
goBack: () => webViewRef.current && Commands.goBack(webViewRef.current),
reload: () => {
setViewState('LOADING');
Commands.reload(webViewRef.current);
if (webViewRef.current) {
Commands.reload(webViewRef.current);
}
},
stopLoading: () => Commands.stopLoading(webViewRef.current),
stopLoading: () =>
webViewRef.current && Commands.stopLoading(webViewRef.current),
postMessage: (data: string) =>
Commands.postMessage(webViewRef.current, data),
webViewRef.current && Commands.postMessage(webViewRef.current, data),
injectJavaScript: (data: string) =>
webViewRef.current &&
Commands.injectJavaScript(webViewRef.current, data),
requestFocus: () => Commands.requestFocus(webViewRef.current),
requestFocus: () =>
webViewRef.current && Commands.requestFocus(webViewRef.current),
}),
[setViewState, webViewRef]
);
Expand All @@ -156,9 +149,9 @@ const WebViewComponent = forwardRef<{}, MacOSWebViewProps>(
'lastErrorEvent expected to be non-null'
);
otherView = (renderError || defaultRenderError)(
lastErrorEvent.domain,
lastErrorEvent.code,
lastErrorEvent.description
lastErrorEvent?.domain,
lastErrorEvent?.code || 0,
lastErrorEvent?.description ?? ''
);
} else if (viewState !== 'IDLE') {
console.error(`RNCWebView invalid state encountered: ${viewState}`);
Expand All @@ -168,8 +161,31 @@ const WebViewComponent = forwardRef<{}, MacOSWebViewProps>(
const webViewContainerStyle = [styles.container, containerStyle];

const NativeWebView =
(nativeConfig?.component as typeof NativeWebViewMacOS | undefined) ||
RNCWebView;
(nativeConfig?.component as typeof RNCWebView | undefined) || RNCWebView;

const sourceResolved = resolveAssetSource(source as ImageSourcePropType);
const newSource =
typeof sourceResolved === 'object'
? Object.entries(sourceResolved as WebViewSourceUri).reduce(
(prev, [currKey, currValue]) => {
return {
...prev,
[currKey]:
currKey === 'headers' &&
currValue &&
typeof currValue === 'object'
? Object.entries(currValue).map(([key, value]) => {
return {
name: key,
value,
};
})
: currValue,
};
},
{}
)
: sourceResolved;

const webView = (
<NativeWebView
Expand All @@ -179,6 +195,7 @@ const WebViewComponent = forwardRef<{}, MacOSWebViewProps>(
cacheEnabled={cacheEnabled}
useSharedProcessPool={useSharedProcessPool}
messagingEnabled={typeof onMessageProp === 'function'}
newSource={newSource}
onLoadingError={onLoadingError}
onLoadingFinish={onLoadingFinish}
onLoadingProgress={onLoadingProgress}
Expand All @@ -196,8 +213,8 @@ const WebViewComponent = forwardRef<{}, MacOSWebViewProps>(
incognito={incognito}
mediaPlaybackRequiresUserAction={mediaPlaybackRequiresUserAction}
ref={webViewRef}
// TODO: find a better way to type this.
source={resolveAssetSource(source as ImageSourcePropType)}
// @ts-expect-error old arch only
source={sourceResolved}
style={webViewStyles}
{...nativeConfig?.props}
/>
Expand Down

0 comments on commit 7fb5c3f

Please sign in to comment.