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

56 lines
1.5 KiB
TypeScript
Raw Normal View History

2024-06-26 09:02:05 +01:00
import { Box, TextField } from '@mui/material';
2024-07-09 21:58:08 +01:00
import React, { useContext, useEffect } 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';
2024-07-09 21:58:08 +01:00
import { replaceSpecialCharacters } from '../../utils/string';
2024-06-21 20:06:07 +01:00
2024-06-21 22:35:56 +01:00
export default function ToolTextResult({
title = 'Result',
value
}: {
title?: string;
value: string;
}) {
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 = () => {
const filename = 'output-omni-tools.txt';
const blob = new Blob([value], { type: 'text/plain' });
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} />
2024-06-26 08:59:18 +01:00
<TextField
2024-07-09 21:58:08 +01:00
value={replaceSpecialCharacters(value)}
2024-06-26 08:59:18 +01:00
fullWidth
multiline
2025-02-25 06:17:10 +00:00
sx={{
'&.MuiTextField-root': {
backgroundColor: 'white'
}
}}
2024-06-26 08:59:18 +01:00
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
}