omni-tools/src/components/result/ToolTextResult.tsx

87 lines
2.4 KiB
TypeScript
Raw Normal View History

2025-04-03 19:55:29 +00:00
import { Box, CircularProgress, TextField, Typography } from '@mui/material';
2025-03-26 20:24:46 +00:00
import React, { useContext } from 'react';
2024-06-23 16:02:05 +01:00
import { CustomSnackBarContext } from '../../contexts/CustomSnackBarContext';
2024-06-24 02:51:22 +01:00
import InputHeader from '../InputHeader';
2024-06-25 01:10:41 +01:00
import ResultFooter from './ResultFooter';
2025-03-26 20:24:46 +00:00
import { replaceSpecialCharacters } from '@utils/string';
import mime from 'mime';
2025-04-03 19:55:29 +00:00
import { globalInputHeight } from '../../config/uiConfig';
2024-06-21 20:06:07 +01:00
2024-06-21 22:35:56 +01:00
export default function ToolTextResult({
title = 'Result',
2025-03-26 20:24:46 +00:00
value,
2025-03-29 18:01:50 +05:30
extension = 'txt',
2025-04-03 19:55:29 +00:00
keepSpecialCharacters,
loading
2024-06-21 22:35:56 +01:00
}: {
title?: string;
value: string;
2025-03-26 20:24:46 +00:00
extension?: string;
keepSpecialCharacters?: boolean;
2025-04-03 19:55:29 +00:00
loading?: boolean;
2024-06-21 22:35:56 +01:00
}) {
2024-06-23 16:02:05 +01:00
const { showSnackBar } = useContext(CustomSnackBarContext);
const handleCopy = () => {
navigator.clipboard
.writeText(value)
.then(() => showSnackBar('Text copied', 'success'))
.catch((err) => {
showSnackBar('Failed to copy: ' + err, 'error');
});
};
const handleDownload = () => {
2025-03-26 20:24:46 +00:00
const filename = `output-omni-tools.${extension}`;
2024-06-23 16:02:05 +01:00
2025-03-26 20:24:46 +00:00
const mimeType = mime.getType(extension) || 'text/plain';
const blob = new Blob([value], {
type: mimeType
});
2024-06-23 16:02:05 +01:00
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);
};
2024-06-21 20:06:07 +01:00
return (
<Box>
2024-06-24 02:51:22 +01:00
<InputHeader title={title} />
2025-04-03 19:55:29 +00:00
{loading ? (
<Box
sx={{
display: 'flex',
flexDirection: 'column',
alignItems: 'center',
justifyContent: 'center',
height: globalInputHeight
}}
>
<CircularProgress />
<Typography variant="body2" sx={{ mt: 2 }}>
Loading... This may take a moment.
</Typography>
</Box>
) : (
<TextField
value={
keepSpecialCharacters ? value : replaceSpecialCharacters(value)
2025-02-25 06:17:10 +00:00
}
2025-04-03 19:55:29 +00:00
fullWidth
multiline
sx={{
'&.MuiTextField-root': {
backgroundColor: 'background.paper'
}
}}
rows={10}
inputProps={{ 'data-testid': 'text-result' }}
/>
)}
2024-06-25 01:10:41 +01:00
<ResultFooter handleCopy={handleCopy} handleDownload={handleDownload} />
2024-06-21 20:06:07 +01:00
</Box>
2024-06-21 22:35:56 +01:00
);
2024-06-21 20:06:07 +01:00
}