import PropTypes from 'prop-types'; import { useEffect, useState } from 'react'; import { CssVarsProvider } from '@mui/joy/styles'; import { Modal, Button, FormControl, FormLabel, Input, Stack, DialogTitle, DialogContent, ModalDialog, Select, Option, IconButton, Checkbox, Tabs, TabList, Tab, TabPanel } from '@mui/joy'; import theme from '/src/theme'; import Visibility from '@mui/icons-material/Visibility'; import VisibilityOff from '@mui/icons-material/VisibilityOff'; const EditHostModal = ({ isHidden, form, setForm, handleEditHost, setIsEditHostHidden, hostConfig }) => { const [showPassword, setShowPassword] = useState(false); const [activeTab, setActiveTab] = useState(0); const [isLoading, setIsLoading] = useState(false); useEffect(() => { if (!isHidden && hostConfig) { setForm({ name: hostConfig.name || '', folder: hostConfig.folder || '', ip: hostConfig.ip || '', user: hostConfig.user || '', password: hostConfig.password || '', rsaKey: hostConfig.rsaKey || '', port: hostConfig.port || 22, authMethod: hostConfig.password ? 'password' : hostConfig.rsaKey ? 'rsaKey' : 'Select Auth', rememberHost: true, storePassword: !!(hostConfig.password || hostConfig.rsaKey), }); } }, [isHidden, hostConfig]); const handleFileChange = (e) => { const file = e.target.files[0]; if (file.name.endsWith('.rsa') || file.name.endsWith('.key') || file.name.endsWith('.pem') || file.name.endsWith('.der') || file.name.endsWith('.p8') || file.name.endsWith('.ssh') || file.name.endsWith('.pub')) { const reader = new FileReader(); reader.onload = (evt) => { setForm((prev) => ({ ...prev, rsaKey: evt.target.result })); }; reader.readAsText(file); } else { alert('Please upload a valid RSA private key file.'); } }; const handleAuthChange = (newMethod) => { setForm((prev) => ({ ...prev, authMethod: newMethod })); }; const handleStorePasswordChange = (checked) => { setForm((prev) => ({ ...prev, storePassword: Boolean(checked), password: checked ? prev.password : "", rsaKey: checked ? prev.rsaKey : "", authMethod: checked ? prev.authMethod : "Select Auth" })); }; const isFormValid = () => { const { ip, user, port, authMethod, password, rsaKey, storePassword } = form; if (!ip?.trim() || !user?.trim() || !port) return false; const portNum = Number(port); if (isNaN(portNum) || portNum < 1 || portNum > 65535) return false; if (Boolean(storePassword) && authMethod === 'password' && !password?.trim()) return false; if (Boolean(storePassword) && authMethod === 'rsaKey' && !rsaKey && !hostConfig?.rsaKey) return false; if (Boolean(storePassword) && authMethod === 'Select Auth') return false; return true; }; const handleSubmit = async (event) => { event.preventDefault(); if (isLoading) return; setIsLoading(true); try { await handleEditHost(hostConfig, { name: form.name || form.ip, folder: form.folder, ip: form.ip, user: form.user, password: form.authMethod === 'password' ? form.password : undefined, rsaKey: form.authMethod === 'rsaKey' ? form.rsaKey : undefined, port: String(form.port), }); } finally { setIsLoading(false); } }; return ( !isLoading && setIsEditHostHidden(true)} sx={{ position: 'fixed', inset: 0, display: 'flex', alignItems: 'center', justifyContent: 'center', backdropFilter: 'blur(5px)', backgroundColor: 'rgba(0, 0, 0, 0.2)', }} > Edit Host
setActiveTab(val)} sx={{ backgroundColor: theme.palette.general.disabled, borderRadius: '8px', padding: '8px', marginBottom: '16px', width: '100%', }} > Basic Info Connection Authentication Host Name setForm((prev) => ({ ...prev, name: e.target.value }))} sx={{ backgroundColor: theme.palette.general.primary, color: theme.palette.text.primary }} /> Folder setForm((prev) => ({ ...prev, folder: e.target.value }))} sx={{ backgroundColor: theme.palette.general.primary, color: theme.palette.text.primary }} /> Host IP setForm((prev) => ({ ...prev, ip: e.target.value }))} sx={{ backgroundColor: theme.palette.general.primary, color: theme.palette.text.primary }} /> 65535}> Host Port setForm((prev) => ({ ...prev, port: e.target.value }))} sx={{ backgroundColor: theme.palette.general.primary, color: theme.palette.text.primary }} /> Host User setForm((prev) => ({ ...prev, user: e.target.value }))} sx={{ backgroundColor: theme.palette.general.primary, color: theme.palette.text.primary }} /> Store Password handleStorePasswordChange(e.target.checked)} sx={{ color: theme.palette.text.primary, '&.Mui-checked': { color: theme.palette.text.primary } }} /> {form.storePassword && ( Authentication Method )} {form.authMethod === 'password' && form.storePassword && ( Password
setForm((prev) => ({ ...prev, password: e.target.value }))} sx={{ backgroundColor: theme.palette.general.primary, color: theme.palette.text.primary, flex: 1 }} /> setShowPassword(!showPassword)} sx={{ color: theme.palette.text.primary, marginLeft: 1 }} > {showPassword ? : }
)} {form.authMethod === 'rsaKey' && form.storePassword && ( Public Key {hostConfig?.rsaKey && !form.rsaKey && ( Existing key detected. Upload to replace. )} )}
); }; EditHostModal.propTypes = { isHidden: PropTypes.bool.isRequired, form: PropTypes.object.isRequired, setForm: PropTypes.func.isRequired, handleEditHost: PropTypes.func.isRequired, setIsEditHostHidden: PropTypes.func.isRequired, hostConfig: PropTypes.object }; export default EditHostModal;