import React, { useState } from "react"; import { RqbitDesktopConfig } from "./configuration"; import { Button, Form, Modal, Row, Tab, Tabs } from "react-bootstrap"; import { ErrorComponent } from "rqbit-webui/src/components/ErrorComponent"; import { invokeAPI } from "./api"; import { ErrorDetails } from "rqbit-webui/src/api-types"; const FormCheck: React.FC<{ label: string; name: string; checked: boolean; onChange: React.ChangeEventHandler; disabled?: boolean; help?: string; }> = ({ label, name, checked, onChange, disabled, help }) => { return ( {label}
{help &&
{help}
}
); }; const FormInput: React.FC<{ label: string; name: string; value: string | number; inputType: string; onChange: React.ChangeEventHandler; disabled?: boolean; help?: string; }> = ({ label, name, value, inputType, onChange, disabled, help }) => { return ( {label}
{help &&
{help}
}
); }; export const ConfigModal: React.FC<{ show: boolean; handleStartReconfigure: () => void; handleConfigured: (config: RqbitDesktopConfig) => void; handleCancel?: () => void; initialConfig: RqbitDesktopConfig; defaultConfig: RqbitDesktopConfig; }> = ({ show, handleStartReconfigure, handleConfigured, handleCancel, initialConfig, defaultConfig, }) => { let [config, setConfig] = useState(initialConfig); let [loading, setLoading] = useState(false); const [error, setError] = useState(null); const handleInputChange: React.ChangeEventHandler = (e) => { const name: string = e.target.name; let value: string | number = e.target.value; if (e.target.type == "number") { value = e.target.valueAsNumber; } console.log(value, typeof value); const [mainField, subField] = name.split(".", 2); if (subField) { setConfig((prevConfig: any) => ({ ...prevConfig, [mainField]: { ...prevConfig[mainField], [subField]: value, }, })); } else { setConfig((prevConfig) => ({ ...prevConfig, [name]: value, })); } }; const handleToggleChange: React.ChangeEventHandler = ( e ) => { const name: string = e.target.name; const [mainField, subField] = name.split(".", 2); if (subField) { setConfig((prevConfig: any) => ({ ...prevConfig, [mainField]: { ...prevConfig[mainField], [subField]: !prevConfig[mainField][subField], }, })); } else { setConfig((prevConfig: any) => ({ ...prevConfig, [name]: !prevConfig[name], })); } }; const handleOkClick = () => { setError(null); handleStartReconfigure(); setLoading(true); invokeAPI<{}>("config_change", { config }).then( () => { setLoading(false); handleConfigured(config); }, (e: ErrorDetails) => { setLoading(false); setError({ text: "Error saving configuration", details: e, }); } ); }; return ( Configure Rqbit desktop DHT config TCP Listener config Session persistence Peer connection options HTTP API config {!!handleCancel && ( )} ); };