UNPKG

@sendbird/uikit-react-native

Version:

Sendbird UIKit for React Native: A feature-rich and customizable chat UI kit with messaging, channel management, and user authentication.

128 lines 3.89 kB
function _extends() { return _extends = Object.assign ? Object.assign.bind() : function (n) { for (var e = 1; e < arguments.length; e++) { var t = arguments[e]; for (var r in t) ({}).hasOwnProperty.call(t, r) && (n[r] = t[r]); } return n; }, _extends.apply(null, arguments); } import { ReactNativeZoomableView } from '@openspacelabs/react-native-zoomable-view'; import React, { useLayoutEffect, useRef, useState } from 'react'; import { StyleSheet, useWindowDimensions } from 'react-native'; import { Box, Image, LoadingSpinner, createStyleSheet, useHeaderStyle, useUIKitTheme } from '@sendbird/uikit-react-native-foundation'; import { useIIFE } from '@sendbird/uikit-utils'; import { usePlatformService } from '../../hooks/useContext'; import SBUUtils from '../../libs/SBUUtils'; const FileViewerContent = ({ type, src, topInset = 0, bottomInset = 0, maxZoom = 4, minZoom = 1, onPress }) => { const [loading, setLoading] = useState(true); const { defaultHeight } = useHeaderStyle(); const { mediaService } = usePlatformService(); const { palette } = useUIKitTheme(); const source = { uri: src }; const onLoadEnd = () => setLoading(false); const mediaViewer = useIIFE(() => { switch (type) { case 'image': { return /*#__PURE__*/React.createElement(ZoomableImageView, { source: source, style: StyleSheet.absoluteFill, resizeMode: 'contain', onLoadEnd: onLoadEnd, zoomProps: { minZoom, maxZoom, onTouchEnd: onPress } }); } case 'video': case 'audio': { return /*#__PURE__*/React.createElement(mediaService.VideoComponent, { source: source, style: [StyleSheet.absoluteFill, { top: topInset, bottom: defaultHeight + bottomInset }], resizeMode: 'contain', onLoad: onLoadEnd }); } default: { return null; } } }); return /*#__PURE__*/React.createElement(Box, { style: styles.container }, mediaViewer, loading && /*#__PURE__*/React.createElement(LoadingSpinner, { style: { position: 'absolute' }, size: 40, color: palette.primary300 })); }; const ZoomableImageView = ({ zoomProps, ...props }) => { const { width, height } = useWindowDimensions(); const imageSize = useRef(); const [contentSizeProps, setContentSizeProps] = useState({ contentWidth: width, contentHeight: height }); useLayoutEffect(() => { SBUUtils.safeRun(async () => { if (props.source.uri) { const image = imageSize.current ?? (await SBUUtils.getImageSize(props.source.uri)); imageSize.current = image; const viewRatio = width / height; const imageRatio = image.width / image.height; const fitDirection = viewRatio > imageRatio ? 'height' : 'width'; const ratio = fitDirection === 'height' ? height / image.height : width / image.width; const actualSize = { width: image.width * ratio, height: image.height * ratio }; setContentSizeProps({ contentWidth: actualSize.width, contentHeight: actualSize.height }); } }); }, [props.source.uri, width, height]); return /*#__PURE__*/React.createElement(ReactNativeZoomableView, _extends({ visualTouchFeedbackEnabled: false, style: { width, height }, initialZoom: 1 }, contentSizeProps, zoomProps), /*#__PURE__*/React.createElement(Image, props)); }; const styles = createStyleSheet({ container: { zIndex: 0, flex: 1, alignItems: 'center', justifyContent: 'center' } }); export default FileViewerContent; //# sourceMappingURL=FileViewerContent.js.map