Magnet input: keydown

This commit is contained in:
Igor Katson 2023-12-14 12:42:22 +00:00
parent 5a30b3fb0c
commit 74ae3cfca2
No known key found for this signature in database
GPG key ID: B4EC22B66D61A3F5
9 changed files with 59 additions and 49 deletions

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View file

@ -4,14 +4,14 @@
"src": "assets/logo.svg"
},
"index.css": {
"file": "assets/index-bc066ae5.css",
"file": "assets/index-458d2033.css",
"src": "index.css"
},
"index.html": {
"css": [
"assets/index-bc066ae5.css"
"assets/index-458d2033.css"
],
"file": "assets/index-9469b905.js",
"file": "assets/index-1daa8daf.js",
"isEntry": true,
"src": "index.html"
}

View file

@ -1,19 +1,18 @@
import { ReactNode } from "react";
export const Button: React.FC<{
onClick: React.MouseEventHandler<HTMLButtonElement>;
onClick: () => void;
variant?: "cancel" | "primary" | "secondary" | "danger" | "none";
className?: string;
disabled?: boolean;
children: ReactNode;
}> = ({ onClick, children, className, disabled, variant }) => {
let variantClassNames = {
secondary:
"hover:bg-blue-600 transition-colors duration-100 hover:text-white",
secondary: "hover:bg-blue-500 transition-colors hover:text-white",
danger:
"bg-red-500 text-white border-green-50 hover:border-red-700 hover:bg-red-600",
primary: "bg-blue-400 text-white hover:bg-blue-600",
cancel: "bg-slate-50 hover:bg-slate-200",
"bg-red-400 text-white border-green-50 hover:border-red-700 hover:bg-red-600",
primary: "bg-blue-600 text-white hover:bg-blue-800 disabled:bg-blue-200",
cancel: "hover:bg-slate-200",
none: "",
}[variant ?? "secondary"];
return (
@ -21,7 +20,7 @@ export const Button: React.FC<{
disabled={disabled}
onClick={(e) => {
e.preventDefault();
onClick(e);
onClick();
}}
className={`flex inline-flex items-center gap-1 border rounded-lg border disabled:cursor-not-allowed px-2 py-1 transition-colors duration-300 ${variantClassNames} ${className}`}
>

View file

@ -12,6 +12,12 @@ export const MagnetInput = ({ className }: { className?: string }) => {
const [inputValue, setInputValue] = useState("");
const [modalIsOpen, setModalIsOpen] = useState(false);
const submit = () => {
setMagnet(inputValue);
setInputValue("");
setModalIsOpen(false);
};
const clear = () => {
setModalIsOpen(false);
setMagnet(null);
@ -38,6 +44,11 @@ export const MagnetInput = ({ className }: { className?: string }) => {
value={inputValue}
name="magnet"
onChange={(e) => setInputValue(e.target.value)}
onKeyDown={(e) => {
if (e.key === "Enter" && !!inputValue) {
submit();
}
}}
placeholder="magnet:?xt=urn:btih:..."
help="Enter magnet or HTTP(S) URL to the .torrent"
/>
@ -47,15 +58,7 @@ export const MagnetInput = ({ className }: { className?: string }) => {
<Button variant="cancel" onClick={clear}>
Cancel
</Button>
<Button
disabled={!inputValue}
variant="primary"
onClick={() => {
setMagnet(inputValue);
setInputValue("");
setModalIsOpen(false);
}}
>
<Button disabled={!inputValue} variant="primary" onClick={submit}>
Add
</Button>
</ModalFooter>

View file

@ -7,13 +7,17 @@ export const Fieldset = ({
className,
}: {
children: ReactNode;
label: string;
label?: string;
help?: string;
className?: string;
}) => {
return (
<fieldset className={`mb-4 ${className}`}>
<label className="text-md font-md mb-3 block">{label}</label>
{label && (
<label className="text-md font-md mb-3 block pb-1 border-b">
{label}
</label>
)}
{children}
</fieldset>
);

View file

@ -6,6 +6,7 @@ export const FormInput: React.FC<{
autoFocus?: boolean;
name: string;
inputType?: string;
onKeyDown?: React.KeyboardEventHandler<HTMLInputElement>;
placeholder?: string;
help?: string;
disabled?: boolean;
@ -16,13 +17,14 @@ export const FormInput: React.FC<{
name,
disabled,
onChange,
onKeyDown,
label,
help,
inputType,
placeholder,
}) => {
return (
<div className="flex flex-col gap-2 text-sm mb-6">
<div className="flex flex-col gap-2 text-sm mb-2">
<label htmlFor={name}>{label}</label>
<input
autoFocus={autoFocus}
@ -33,9 +35,10 @@ export const FormInput: React.FC<{
disabled={disabled}
placeholder={placeholder}
value={value}
onKeyDown={onKeyDown}
onChange={onChange}
/>
{help && <div className="text-xs text-slate-500 mb-3">{help}</div>}
{help && <div className="text-xs text-slate-500">{help}</div>}
</div>
);
};

View file

@ -36,7 +36,6 @@ export const FileSelectionModal = (props: {
const [outputFolder, setOutputFolder] = useState<string>("");
const ctx = useContext(AppContext);
const API = useContext(APIContext);
// const [Modal, , , closeModal] = useModal({ fullScreen: true });
const selectAll = () => {
setSelectedFiles(
@ -108,7 +107,16 @@ export const FileSelectionModal = (props: {
} else if (listTorrentResponse) {
return (
<Form>
<Fieldset className="mb-4" label="Pick the files to download">
<FormInput
label="Output folder"
name="output_folder"
inputType="text"
value={outputFolder}
onChange={(e) => setOutputFolder(e.target.value)}
/>
<Fieldset>
<label className="text-sm mb-2 block">Select files</label>
<div className="mb-3 flex gap-2">
<Button onClick={selectAll} className="text-sm">
Select all
@ -127,15 +135,8 @@ export const FileSelectionModal = (props: {
/>
))}
</Fieldset>
<Fieldset label="Options">
<FormInput
label="Output folder"
name="output_folder"
inputType="text"
value={outputFolder}
onChange={(e) => setOutputFolder(e.target.value)}
/>
{/* <Fieldset label="Options">
<FormCheckbox
label="Increase timeouts"
checked={unpopularTorrent}
@ -143,7 +144,7 @@ export const FileSelectionModal = (props: {
help="This might be useful for unpopular torrents with few peers. It will slow down fast torrents though."
name="increase_timeouts"
/>
</Fieldset>
</Fieldset> */}
</Form>
);
}

View file

@ -210,7 +210,7 @@ export const ConfigModal: React.FC<{
</Tab>
<Tab name="DHT" currentTab={tab}>
<Fieldset label="DHT config">
<Fieldset>
<FormCheck
label="Enable DHT"
name="dht.disable"
@ -241,7 +241,7 @@ export const ConfigModal: React.FC<{
</Tab>
<Tab name="TCP Listen" currentTab={tab}>
<Fieldset label="TCP Listener config">
<Fieldset>
<FormCheck
label="Listen on TCP"
name="tcp_listen.disable"
@ -281,7 +281,7 @@ export const ConfigModal: React.FC<{
</Tab>
<Tab name="Session" currentTab={tab}>
<Fieldset label="Session persistence">
<Fieldset>
<FormCheck
label="Enable persistence"
name="persistence.disable"
@ -302,7 +302,7 @@ export const ConfigModal: React.FC<{
</Tab>
<Tab name="Peer options" currentTab={tab}>
<Fieldset label="Peer connection options">
<Fieldset>
<FormInput
label="Connect timeout (seconds)"
inputType="number"
@ -324,7 +324,7 @@ export const ConfigModal: React.FC<{
</Tab>
<Tab name="HTTP API" currentTab={tab}>
<Fieldset label="HTTP API config">
<Fieldset>
<FormCheck
label="Enable HTTP API"
name="http_api.disable"
@ -365,7 +365,7 @@ export const ConfigModal: React.FC<{
</ModalBody>
<ModalFooter>
{!!handleCancel && (
<Button variant="secondary" onClick={handleCancel}>
<Button variant="cancel" onClick={handleCancel}>
Cancel
</Button>
)}