mirror of
https://github.com/idanoo/autobrr
synced 2025-07-27 02:39:13 +00:00

* feat(releases): support magnet links * feat(feeds): support magnet links * feat(actions): log messages * fix: component warning * fix: check hasprefix instead of hassuffix for magnet * feat(release): resolve magnet uri from link * fix(actions): deluge use magnet uri * fix(macros): add `MagnetURI` var * fix(actions): run magnet resolving before macros * feat(feeds): set download type on creation
252 lines
7.8 KiB
TypeScript
252 lines
7.8 KiB
TypeScript
import type { FieldProps } from "formik";
|
|
import { Field } from "formik";
|
|
import Select, { components, ControlProps, InputProps, MenuProps, OptionProps } from "react-select";
|
|
import { OptionBasicTyped } from "../../domain/constants";
|
|
import CreatableSelect from "react-select/creatable";
|
|
import { CustomTooltip } from "../tooltips/CustomTooltip";
|
|
|
|
interface SelectFieldProps<T> {
|
|
name: string;
|
|
label: string;
|
|
help?: string;
|
|
placeholder?: string;
|
|
defaultValue?: OptionBasicTyped<T>;
|
|
tooltip?: JSX.Element;
|
|
options: OptionBasicTyped<T>[];
|
|
}
|
|
|
|
export function SelectFieldCreatable<T>({ name, label, help, placeholder, tooltip, options }: SelectFieldProps<T>) {
|
|
return (
|
|
<div className="space-y-1 p-4 sm:space-y-0 sm:grid sm:grid-cols-3 sm:gap-4">
|
|
<div>
|
|
<label
|
|
htmlFor={name}
|
|
className="block text-sm font-medium text-gray-900 dark:text-white sm:pt-2"
|
|
>
|
|
<div className="flex">
|
|
{label} {tooltip && (<CustomTooltip anchorId={name}>{tooltip}</CustomTooltip>)}
|
|
</div>
|
|
</label>
|
|
</div>
|
|
<div className="sm:col-span-2">
|
|
<Field name={name} type="select">
|
|
{({
|
|
field,
|
|
form: { setFieldValue }
|
|
}: FieldProps) => (
|
|
<CreatableSelect
|
|
{...field}
|
|
id={name}
|
|
isClearable={true}
|
|
isSearchable={true}
|
|
components={{
|
|
Input,
|
|
Control,
|
|
Menu,
|
|
Option
|
|
}}
|
|
placeholder={placeholder ?? "Choose an option"}
|
|
styles={{
|
|
singleValue: (base) => ({
|
|
...base,
|
|
color: "unset"
|
|
})
|
|
}}
|
|
theme={(theme) => ({
|
|
...theme,
|
|
spacing: {
|
|
...theme.spacing,
|
|
controlHeight: 30,
|
|
baseUnit: 2
|
|
}
|
|
})}
|
|
// value={field?.value ? field.value : options.find(o => o.value == field?.value)}
|
|
value={field?.value ? { value: field.value, label: field.value } : field.value}
|
|
onChange={(option) => {
|
|
if (option === null) {
|
|
setFieldValue(field.name, "");
|
|
return;
|
|
} else {
|
|
setFieldValue(field.name, option.value ?? "");
|
|
}
|
|
}}
|
|
options={[...[...options, { value: field.value, label: field.value }].reduce((map, obj) => map.set(obj.value, obj), new Map()).values()]}
|
|
/>
|
|
)}
|
|
</Field>
|
|
{help && (
|
|
<p className="mt-2 text-sm text-gray-500" id={`${name}-description`}>{help}</p>
|
|
)}
|
|
</div>
|
|
</div>
|
|
);
|
|
}
|
|
|
|
const Input = (props: InputProps) => {
|
|
return (
|
|
<components.Input
|
|
{...props}
|
|
inputClassName="outline-none border-none shadow-none focus:ring-transparent"
|
|
className="text-gray-400 dark:text-gray-100"
|
|
children={props.children}
|
|
/>
|
|
);
|
|
};
|
|
|
|
const Control = (props: ControlProps) => {
|
|
return (
|
|
<components.Control
|
|
{...props}
|
|
className="p-1 block w-full dark:bg-gray-800 border border-gray-300 dark:border-gray-700 rounded-md shadow-sm focus:outline-none focus:ring-blue-500 focus:border-blue-500 dark:text-gray-100 sm:text-sm"
|
|
children={props.children}
|
|
/>
|
|
);
|
|
};
|
|
|
|
const Menu = (props: MenuProps) => {
|
|
return (
|
|
<components.Menu
|
|
{...props}
|
|
className="dark:bg-gray-800 border border-gray-300 dark:border-gray-700 dark:text-gray-400 rounded-md shadow-sm"
|
|
children={props.children}
|
|
/>
|
|
);
|
|
};
|
|
|
|
const Option = (props: OptionProps) => {
|
|
return (
|
|
<components.Option
|
|
{...props}
|
|
className="dark:text-gray-400 dark:bg-gray-800 dark:hover:bg-gray-900 dark:focus:bg-gray-900"
|
|
children={props.children}
|
|
/>
|
|
);
|
|
};
|
|
|
|
export function SelectField<T>({ name, label, help, placeholder, options }: SelectFieldProps<T>) {
|
|
return (
|
|
<div className="space-y-1 p-4 sm:space-y-0 sm:grid sm:grid-cols-3 sm:gap-4">
|
|
<div>
|
|
<label
|
|
htmlFor={name}
|
|
className="block text-sm font-medium text-gray-900 dark:text-white sm:pt-2"
|
|
>
|
|
{label}
|
|
</label>
|
|
</div>
|
|
<div className="sm:col-span-2">
|
|
<Field name={name} type="select">
|
|
{({
|
|
field,
|
|
form: { setFieldValue }
|
|
}: FieldProps) => (
|
|
<Select
|
|
{...field}
|
|
id={name}
|
|
components={{
|
|
Input,
|
|
Control,
|
|
Menu,
|
|
Option
|
|
}}
|
|
placeholder={placeholder ?? "Choose an option"}
|
|
styles={{
|
|
singleValue: (base) => ({
|
|
...base,
|
|
color: "unset"
|
|
})
|
|
}}
|
|
theme={(theme) => ({
|
|
...theme,
|
|
spacing: {
|
|
...theme.spacing,
|
|
controlHeight: 30,
|
|
baseUnit: 2
|
|
}
|
|
})}
|
|
// value={field?.value ? field.value : options.find(o => o.value == field?.value)}
|
|
value={field?.value ? { value: field.value, label: field.value } : field.value}
|
|
onChange={(option) => {
|
|
if (option === null) {
|
|
setFieldValue(field.name, "");
|
|
return;
|
|
} else {
|
|
setFieldValue(field.name, option.value ?? "");
|
|
}
|
|
}}
|
|
options={[...[...options, { value: field.value, label: field.value }].reduce((map, obj) => map.set(obj.value, obj), new Map()).values()]}
|
|
/>
|
|
)}
|
|
</Field>
|
|
{help && (
|
|
<p className="mt-2 text-sm text-gray-500" id={`${name}-description`}>{help}</p>
|
|
)}
|
|
</div>
|
|
</div>
|
|
);
|
|
}
|
|
|
|
export function SelectFieldBasic<T>({ name, label, help, placeholder, tooltip, defaultValue, options }: SelectFieldProps<T>) {
|
|
return (
|
|
<div className="space-y-1 p-4 sm:space-y-0 sm:grid sm:grid-cols-3 sm:gap-4">
|
|
<div>
|
|
<label
|
|
htmlFor={name}
|
|
className="block text-sm font-medium text-gray-900 dark:text-white sm:pt-2"
|
|
>
|
|
<div className="flex">
|
|
{label} {tooltip && (<CustomTooltip anchorId={name}>{tooltip}</CustomTooltip>)}
|
|
</div>
|
|
</label>
|
|
</div>
|
|
<div className="sm:col-span-2">
|
|
<Field name={name} type="select">
|
|
{({
|
|
field,
|
|
form: { setFieldValue }
|
|
}: FieldProps) => (
|
|
<Select
|
|
{...field}
|
|
id={name}
|
|
components={{
|
|
Input,
|
|
Control,
|
|
Menu,
|
|
Option
|
|
}}
|
|
placeholder={placeholder ?? "Choose an option"}
|
|
styles={{
|
|
singleValue: (base) => ({
|
|
...base,
|
|
color: "unset"
|
|
})
|
|
}}
|
|
theme={(theme) => ({
|
|
...theme,
|
|
spacing: {
|
|
...theme.spacing,
|
|
controlHeight: 30,
|
|
baseUnit: 2
|
|
}
|
|
})}
|
|
defaultValue={defaultValue}
|
|
value={field?.value && options.find(o => o.value == field?.value)}
|
|
onChange={(option) => {
|
|
if (option === null) {
|
|
setFieldValue(field.name, "");
|
|
return;
|
|
} else {
|
|
setFieldValue(field.name, option.value ?? "");
|
|
}
|
|
}}
|
|
options={options}
|
|
/>
|
|
)}
|
|
</Field>
|
|
{help && (
|
|
<p className="mt-2 text-sm text-gray-500" id={`${name}-description`}>{help}</p>
|
|
)}
|
|
</div>
|
|
</div>
|
|
);
|
|
}
|