2025-07-18 03:40:01 +01:00
|
|
|
import { Box, useTheme } from '@mui/material';
|
2025-07-18 03:26:42 +01:00
|
|
|
import React, { useContext, useRef } from 'react';
|
|
|
|
|
import { CustomSnackBarContext } from '../../contexts/CustomSnackBarContext';
|
|
|
|
|
import InputHeader from '../InputHeader';
|
|
|
|
|
import InputFooter from './InputFooter';
|
|
|
|
|
import { useTranslation } from 'react-i18next';
|
|
|
|
|
import Editor from '@monaco-editor/react';
|
|
|
|
|
import { globalInputHeight } from '../../config/uiConfig';
|
|
|
|
|
|
|
|
|
|
export default function ToolCodeInput({
|
|
|
|
|
value,
|
|
|
|
|
onChange,
|
|
|
|
|
title = 'Input text',
|
|
|
|
|
language
|
|
|
|
|
}: {
|
|
|
|
|
title?: string;
|
|
|
|
|
value: string;
|
|
|
|
|
language: string;
|
|
|
|
|
onChange: (value: string) => void;
|
|
|
|
|
}) {
|
|
|
|
|
const { t } = useTranslation();
|
|
|
|
|
const { showSnackBar } = useContext(CustomSnackBarContext);
|
|
|
|
|
const fileInputRef = useRef<HTMLInputElement>(null);
|
2025-07-18 03:40:01 +01:00
|
|
|
const theme = useTheme();
|
2025-07-18 03:26:42 +01:00
|
|
|
|
|
|
|
|
const handleCopy = () => {
|
|
|
|
|
navigator.clipboard
|
|
|
|
|
.writeText(value)
|
|
|
|
|
.then(() => showSnackBar(t('toolTextInput.copied'), 'success'))
|
|
|
|
|
.catch((err) => {
|
|
|
|
|
showSnackBar(t('toolTextInput.copyFailed', { error: err }), 'error');
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const handleFileChange = (event: React.ChangeEvent<HTMLInputElement>) => {
|
|
|
|
|
const file = event.target.files?.[0];
|
|
|
|
|
if (file) {
|
|
|
|
|
const reader = new FileReader();
|
|
|
|
|
reader.onload = (e) => {
|
|
|
|
|
const text = e.target?.result;
|
|
|
|
|
if (typeof text === 'string') {
|
|
|
|
|
onChange(text);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
reader.readAsText(file);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const handleImportClick = () => {
|
|
|
|
|
fileInputRef.current?.click();
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<Box>
|
|
|
|
|
<InputHeader title={title || t('toolTextInput.input')} />
|
|
|
|
|
<Box height={globalInputHeight}>
|
|
|
|
|
<Editor
|
|
|
|
|
height={'87%'}
|
|
|
|
|
language={language}
|
2025-07-18 03:40:01 +01:00
|
|
|
theme={theme.palette.mode === 'dark' ? 'vs-dark' : 'light'}
|
2025-07-18 03:26:42 +01:00
|
|
|
value={value}
|
|
|
|
|
onChange={(value) => onChange(value ?? '')}
|
|
|
|
|
/>
|
|
|
|
|
<InputFooter handleCopy={handleCopy} handleImport={handleImportClick} />
|
|
|
|
|
<input
|
|
|
|
|
type="file"
|
|
|
|
|
accept="*"
|
|
|
|
|
ref={fileInputRef}
|
|
|
|
|
style={{ display: 'none' }}
|
|
|
|
|
onChange={handleFileChange}
|
|
|
|
|
/>
|
|
|
|
|
</Box>
|
|
|
|
|
</Box>
|
|
|
|
|
);
|
|
|
|
|
}
|