import {
ButtonGroup,
Center,
Icon,
IconButton,
Image,
List,
ListItem,
Modal,
ModalBody,
ModalCloseButton,
ModalContent,
ModalHeader,
ModalOverlay,
Spacer,
Stack,
Text,
Tooltip,
useDisclosure,
} from '@chakra-ui/react';
import { __ } from '@wordpress/i18n';
import saveAs from 'file-saver';
import React, { useState } from 'react';
import { DocumentViewer } from 'react-documents';
import { BiShow } from 'react-icons/bi';
import {
BsFileArrowDown,
BsFileExcel,
BsFilePdf,
BsFilePpt,
BsFileWord,
BsFileZip,
} from 'react-icons/bs';
import { CustomIcon } from '../../../assets/js/back-end/components/common/CustomIcon';
import MasteriyoPlayer from '../../../assets/js/back-end/components/common/masteriyoPlayer/MasteriyoPlayer';
import { Trash } from '../../../assets/js/back-end/constants/images';
import {
getFileNameFromURL,
isArray,
isEmpty,
} from '../../../assets/js/back-end/utils/utils';
interface Props {
files: DownloadMaterials;
isDownloadable?: boolean;
isPreviewable?: boolean;
onRemove?: (file: DownloadMaterial) => void;
docPreviewNotice: string;
hidePreviewNotice?: boolean;
}
export const getIcon = (file: DownloadMaterial) => {
switch (file.mime_type) {
case 'application/msword':
return ;
case 'application/pdf':
return ;
case 'application/vnd.openxmlformats-officedocument.presentationml.presentation':
return ;
case 'application/vnd.openxmlformats-officedocument.wordprocessingml.document':
return ;
case 'application/zip':
return ;
case 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet':
return ;
case 'application/vnd.ms-excel':
return ;
default:
return ;
}
};
const DocPreview: React.FC = (props) => {
const { isOpen, onClose, onOpen } = useDisclosure();
const {
files,
onRemove,
isDownloadable,
isPreviewable = true,
docPreviewNotice,
hidePreviewNotice = false,
} = props;
const [currentFile, setCurrentFile] = useState();
const onPreviewPress = (file: DownloadMaterial) => {
setCurrentFile(file);
onOpen();
};
const onDownloadPress = (file: DownloadMaterial) => {
saveAs(file?.url, getFileNameFromURL(file?.url));
};
const videoMimeType = [
'video/mp4',
'video/mpeg',
'video/webm',
'video/ogg',
'video/mkv',
'video/avi',
'video/webm',
'video/x-flv',
'video/quicktime',
'video/x-ms-wmv',
'video/flv',
];
const audioMimeType = [
'audio/mp3',
'audio/m4a',
'audio/oga',
'audio/ogg',
'audio/wav',
'audio/opus',
'audio/flac',
'audio/x-m4a',
'audio/x-ms-wma',
'audio/mpeg',
'audio/wma',
];
const imageMimeType = [
'image/jpeg',
'image/png',
'image/jpg',
'image/gif',
'image/webp',
];
return (
<>
{isArray(files) &&
files?.map((file, index) => (
{getIcon(file)}
onDownloadPress(file) : undefined
}
color={'saint-blue'}
lineHeight={'24px'}
>
{getFileNameFromURL(file?.url)}
{file?.formatted_file_size}
{file?.mime_type !== 'application/zip' && isPreviewable ? (
}
onClick={() => onPreviewPress(file)}
/>
) : null}
{onRemove ? (
}
onClick={() => onRemove(file)}
/>
) : null}
))}
{!isEmpty(files) && !hidePreviewNotice ? (
<>
{docPreviewNotice}
>
) : null}
{currentFile?.title}
{audioMimeType.includes(currentFile?.mime_type as string) ? (
) : videoMimeType.includes(currentFile?.mime_type as string) ? (
) : imageMimeType.includes(currentFile?.mime_type as string) ? (
) : (
)}
>
);
};
export default DocPreview;