import { Empty, Flex, Popup } from '@fruits-chain/react-native-xiaoshu' import type { PropsWithChildren } from 'react' import React, { forwardRef, useImperativeHandle, useMemo, useState, } from 'react' import { Text, StyleSheet } from 'react-native' import { URL } from 'react-native-url-polyfill' import type { UploadItem } from '../../interface' import ImagePreview from './ImagePreview' import type { BasicPreviewProps } from './interface' import VideoPreview from './VideoPreview' type PreviewComponent = React.ComponentType export interface CustomPreview { [key: string]: PreviewComponent } interface PreviewProps { customPreview?: CustomPreview list: UploadItem[] } export interface PreviewInstance { preview: (file: UploadItem) => void } const _Preview: React.ForwardRefRenderFunction< PreviewInstance, PreviewProps > = ({ customPreview = {}, list }, ref) => { const [visible, setVisible] = useState(false) const [currentFile, setCurrentFile] = useState() useImperativeHandle(ref, () => ({ preview(file) { setCurrentFile(file) setVisible(true) }, })) function closeModal() { setVisible(false) } function handleRequestPopupClose() { closeModal() return true } const Compnent = useMemo(() => { if (!currentFile) { return } const path = currentFile.previewPath const mapper: { [key: string]: PreviewComponent } = { mp4: VideoPreview, png: ImagePreview, jpg: ImagePreview, jpeg: ImagePreview, PNG: ImagePreview, JPG: ImagePreview, JPEG: ImagePreview, ...customPreview, } const pathName = new URL(path).pathname for (const key of Object.keys(mapper)) { if (pathName.endsWith(`.${key}`)) { return mapper[key] } } }, [currentFile, customPreview]) return ( {currentFile?.name} } onClose={closeModal} /> {Compnent ? ( setCurrentFile(curr)} target={currentFile} onClose={() => setVisible(false)} list={list} /> ) : ( )} ) } const Preview = forwardRef(_Preview) as ( props: PropsWithChildren & { ref?: React.Ref }, ) => React.ReactElement const styles = StyleSheet.create({ titleText: { fontSize: 16, lineHeight: 22, color: '#11151A', textAlign: 'center', marginRight: 28, fontWeight: 'bold', }, flex1: { flex: 1, }, }) export default Preview