import React, { ReactNode, useState } from "react"; import { RqbitDesktopConfig } from "./configuration"; import { ErrorComponent } from "rqbit-webui/src/components/ErrorComponent"; import { invokeAPI } from "./api"; import { ErrorDetails } from "rqbit-webui/src/api-types"; import { FormCheckbox } from "rqbit-webui/src/components/forms/FormCheckbox"; import { FormInput as FI } from "rqbit-webui/src/components/forms/FormInput"; import { ModalBody } from "rqbit-webui/src/components/modal/ModalBody"; import { Modal } from "rqbit-webui/src/components/modal/Modal"; import { Fieldset } from "rqbit-webui/src/components/forms/Fieldset"; import { ModalFooter } from "rqbit-webui/src/components/modal/ModalFooter"; import { Button } from "rqbit-webui/src/components/buttons/Button"; import { formatBytes } from "rqbit-webui/src/helper/formatBytes"; const FormCheck: React.FC<{ label: string; name: string; checked: boolean; onChange: React.ChangeEventHandler; disabled?: boolean; help?: string; }> = ({ label, name, checked, onChange, disabled, help }) => { return ( ); }; 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 ( ); }; type TAB = | "Home" | "DHT" | "Session" | "Peer options" | "HTTP API" | "TCP Listen" | "UPnP Server"; const TABS: readonly TAB[] = [ "Home", "DHT", "Session", "TCP Listen", "Peer options", "HTTP API", "UPnP Server", ] as const; const Tab: React.FC<{ name: TAB; currentTab: TAB; children: ReactNode; }> = ({ name, currentTab, children }) => { const show = name === currentTab; if (!show) { return; } return
{children}
; }; 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); let [tab, setTab] = useState("Home"); 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 (
{TABS.map((t, i) => { const isActive = t === tab; let classNames = "text-slate-300"; if (isActive) { classNames = "text-slate-800 border-b-2 border-blue-800 dark:border-blue-200 dark:text-white"; } return ( ); })}
{defaultConfig.disable_upload !== undefined && config.disable_upload !== undefined && ( )}
0 ? "current " + formatBytes(config.ratelimits.download_bps ?? 0) + " per second" : "currently disabled" })`} /> 0 ? "current " + formatBytes(config.ratelimits.upload_bps ?? 0) + " per second" : "currently disabled" })`} />
{!!handleCancel && ( )}
); };