2024-06-24 02:51:22 +01:00
|
|
|
import { Box } from '@mui/material';
|
2024-06-25 01:10:41 +01:00
|
|
|
import React, { useContext } from 'react';
|
2024-06-24 02:51:22 +01:00
|
|
|
import InputHeader from '../InputHeader';
|
|
|
|
|
import greyPattern from '@assets/grey-pattern.png';
|
2024-06-24 03:57:52 +01:00
|
|
|
import { globalInputHeight } from '../../config/uiConfig';
|
2024-06-25 01:10:41 +01:00
|
|
|
import ResultFooter from './ResultFooter';
|
|
|
|
|
import { CustomSnackBarContext } from '../../contexts/CustomSnackBarContext';
|
2024-06-24 02:51:22 +01:00
|
|
|
|
|
|
|
|
export default function ToolFileResult({
|
|
|
|
|
title = 'Result',
|
2024-06-25 01:10:41 +01:00
|
|
|
value,
|
|
|
|
|
extension
|
2024-06-24 02:51:22 +01:00
|
|
|
}: {
|
|
|
|
|
title?: string;
|
|
|
|
|
value: File | null;
|
2024-06-25 01:10:41 +01:00
|
|
|
extension: string;
|
2024-06-24 02:51:22 +01:00
|
|
|
}) {
|
|
|
|
|
const [preview, setPreview] = React.useState<string | null>(null);
|
2024-06-25 01:10:41 +01:00
|
|
|
const { showSnackBar } = useContext(CustomSnackBarContext);
|
2024-06-24 02:51:22 +01:00
|
|
|
|
|
|
|
|
React.useEffect(() => {
|
|
|
|
|
if (value) {
|
|
|
|
|
const objectUrl = URL.createObjectURL(value);
|
|
|
|
|
setPreview(objectUrl);
|
|
|
|
|
|
|
|
|
|
return () => URL.revokeObjectURL(objectUrl);
|
|
|
|
|
} else {
|
|
|
|
|
setPreview(null);
|
|
|
|
|
}
|
|
|
|
|
}, [value]);
|
|
|
|
|
|
2024-06-25 01:10:41 +01:00
|
|
|
const handleCopy = () => {
|
|
|
|
|
if (value) {
|
|
|
|
|
const blob = new Blob([value], { type: value.type });
|
|
|
|
|
const clipboardItem = new ClipboardItem({ [value.type]: blob });
|
|
|
|
|
|
|
|
|
|
navigator.clipboard
|
|
|
|
|
.write([clipboardItem])
|
|
|
|
|
.then(() => showSnackBar('File copied', 'success'))
|
|
|
|
|
.catch((err) => {
|
|
|
|
|
showSnackBar('Failed to copy: ' + err, 'error');
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const handleDownload = () => {
|
|
|
|
|
if (value) {
|
|
|
|
|
const filename = 'output-omni-tools.' + extension;
|
|
|
|
|
|
|
|
|
|
const blob = new Blob([value], { type: value.type });
|
|
|
|
|
const url = window.URL.createObjectURL(blob);
|
|
|
|
|
const a = document.createElement('a');
|
|
|
|
|
a.href = url;
|
|
|
|
|
a.download = filename;
|
|
|
|
|
document.body.appendChild(a);
|
|
|
|
|
a.click();
|
|
|
|
|
document.body.removeChild(a);
|
|
|
|
|
window.URL.revokeObjectURL(url);
|
|
|
|
|
}
|
|
|
|
|
};
|
2025-03-10 04:13:10 +00:00
|
|
|
|
|
|
|
|
// Determine the file type based on MIME type
|
|
|
|
|
const getFileType = () => {
|
|
|
|
|
if (!value) return 'unknown';
|
|
|
|
|
if (value.type.startsWith('image/')) return 'image';
|
|
|
|
|
if (value.type.startsWith('video/')) return 'video';
|
|
|
|
|
if (value.type.startsWith('audio/')) return 'audio';
|
|
|
|
|
return 'unknown';
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const fileType = getFileType();
|
|
|
|
|
|
2024-06-24 02:51:22 +01:00
|
|
|
return (
|
|
|
|
|
<Box>
|
|
|
|
|
<InputHeader title={title} />
|
|
|
|
|
<Box
|
|
|
|
|
sx={{
|
|
|
|
|
width: '100%',
|
2024-06-24 03:57:52 +01:00
|
|
|
height: globalInputHeight,
|
2024-06-24 02:51:22 +01:00
|
|
|
border: preview ? 0 : 1,
|
|
|
|
|
borderRadius: 2,
|
2025-02-25 06:17:10 +00:00
|
|
|
boxShadow: '5',
|
|
|
|
|
bgcolor: 'white'
|
2024-06-24 02:51:22 +01:00
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
{preview && (
|
|
|
|
|
<Box
|
|
|
|
|
width={'100%'}
|
|
|
|
|
height={'100%'}
|
|
|
|
|
sx={{
|
|
|
|
|
display: 'flex',
|
|
|
|
|
alignItems: 'center',
|
|
|
|
|
justifyContent: 'center',
|
|
|
|
|
backgroundImage: `url(${greyPattern})`
|
|
|
|
|
}}
|
|
|
|
|
>
|
2025-03-10 04:13:10 +00:00
|
|
|
{fileType === 'image' && (
|
|
|
|
|
<img
|
|
|
|
|
src={preview}
|
|
|
|
|
alt="Result"
|
|
|
|
|
style={{ maxWidth: '100%', maxHeight: globalInputHeight }}
|
|
|
|
|
/>
|
|
|
|
|
)}
|
|
|
|
|
{fileType === 'video' && (
|
|
|
|
|
<video
|
|
|
|
|
src={preview}
|
|
|
|
|
controls
|
|
|
|
|
style={{ maxWidth: '100%', maxHeight: globalInputHeight }}
|
|
|
|
|
/>
|
|
|
|
|
)}
|
|
|
|
|
{fileType === 'audio' && (
|
|
|
|
|
<audio
|
|
|
|
|
src={preview}
|
|
|
|
|
controls
|
|
|
|
|
style={{ width: '100%', maxWidth: '500px' }}
|
|
|
|
|
/>
|
|
|
|
|
)}
|
|
|
|
|
{fileType === 'unknown' && (
|
|
|
|
|
<Box sx={{ padding: 2, textAlign: 'center' }}>
|
|
|
|
|
File processed successfully. Click download to save the result.
|
|
|
|
|
</Box>
|
|
|
|
|
)}
|
2024-06-24 02:51:22 +01:00
|
|
|
</Box>
|
|
|
|
|
)}
|
|
|
|
|
</Box>
|
2024-06-25 01:10:41 +01:00
|
|
|
<ResultFooter
|
|
|
|
|
disabled={!value}
|
|
|
|
|
handleCopy={handleCopy}
|
|
|
|
|
handleDownload={handleDownload}
|
|
|
|
|
/>
|
2024-06-24 02:51:22 +01:00
|
|
|
</Box>
|
|
|
|
|
);
|
|
|
|
|
}
|