Continuous Integration / build_web (push) Failing after 0s
Continuous Integration / build (macos-latest) (push) Skipped
Continuous Integration / build (ubuntu-22.04) (push) Skipped
Continuous Integration / build (windows-11-arm) (push) Skipped
Continuous Integration / build (windows-latest) (push) Skipped
Continuous Integration / nightly_release (push) Skipped
- Rust client for the WebUI API: password/API-key auth, categories, magnet and .torrent add, sync polling, pause/resume/delete - downloads page with progress, speed, ETA, filters, notifications and per-torrent actions - settings page with OS keyring storage, self-signed TLS, Jellyfin refresh and polling options - cross-platform keyring setup for Windows, macOS and Linux
289 lines
7.5 KiB
TypeScript
289 lines
7.5 KiB
TypeScript
import {
|
|
Button,
|
|
Dialog,
|
|
DialogActions,
|
|
DialogContent,
|
|
DialogTitle,
|
|
FormControlLabel,
|
|
MenuItem,
|
|
Stack,
|
|
Switch,
|
|
Tab,
|
|
Tabs,
|
|
TextField,
|
|
Typography,
|
|
} from "@mui/material";
|
|
import { useMutation, useQuery } from "@tanstack/react-query";
|
|
import { open } from "@tauri-apps/plugin-dialog";
|
|
import { enqueueSnackbar } from "notistack";
|
|
import React, { useEffect, useState } from "react";
|
|
import {
|
|
qbAddTorrent,
|
|
qbCategoriesQueryOptions,
|
|
toQbError,
|
|
} from "@/utils/qbittorrent";
|
|
import type {
|
|
QbAddResult,
|
|
QbSettings,
|
|
QbTorrent,
|
|
} from "@/utils/qbittorrent/types";
|
|
|
|
type Props = {
|
|
open: boolean;
|
|
onClose: () => void;
|
|
settings: QbSettings;
|
|
onAdded: (result: QbAddResult) => void;
|
|
/** Used to preselect the category of a torrent being retried. */
|
|
torrent?: QbTorrent | null;
|
|
};
|
|
|
|
type TabValue = "magnet" | "file";
|
|
|
|
export default function AddTorrentDialog({
|
|
open: isOpen,
|
|
onClose,
|
|
settings,
|
|
onAdded,
|
|
torrent,
|
|
}: Props) {
|
|
const [tab, setTab] = useState<TabValue>("magnet");
|
|
const [magnet, setMagnet] = useState("");
|
|
const [filePath, setFilePath] = useState<string | null>(null);
|
|
const [fileName, setFileName] = useState<string | null>(null);
|
|
const [category, setCategory] = useState(settings.defaultCategory ?? "");
|
|
const [paused, setPaused] = useState(settings.addPaused);
|
|
const [autoTmm, setAutoTmm] = useState(settings.autoTmm);
|
|
|
|
const categories = useQuery(qbCategoriesQueryOptions(isOpen));
|
|
|
|
useEffect(() => {
|
|
if (!isOpen) return;
|
|
setTab("magnet");
|
|
setMagnet("");
|
|
setFilePath(null);
|
|
setFileName(null);
|
|
setCategory(torrent?.category || settings.defaultCategory || "");
|
|
setPaused(settings.addPaused);
|
|
setAutoTmm(settings.autoTmm);
|
|
}, [isOpen, torrent, settings]);
|
|
|
|
const pickFile = async () => {
|
|
try {
|
|
const selected = await open({
|
|
multiple: false,
|
|
directory: false,
|
|
filters: [{ name: "Torrent", extensions: ["torrent"] }],
|
|
});
|
|
if (typeof selected === "string") {
|
|
setFilePath(selected);
|
|
setFileName(selected.split(/[\\/]/).pop() ?? selected);
|
|
}
|
|
} catch (error) {
|
|
enqueueSnackbar(`Не удалось открыть диалог файла: ${error}`, {
|
|
variant: "error",
|
|
});
|
|
}
|
|
};
|
|
|
|
const add = useMutation({
|
|
mutationFn: () =>
|
|
qbAddTorrent({
|
|
magnet: tab === "magnet" ? magnet.trim() : null,
|
|
filePath: tab === "file" ? filePath : null,
|
|
category: category || null,
|
|
paused,
|
|
autoTmm,
|
|
}),
|
|
onSuccess: (result) => {
|
|
if (result.status === "duplicate") {
|
|
enqueueSnackbar(
|
|
result.message ?? "Такая раздача уже добавлена в qBittorrent",
|
|
{ variant: "warning" },
|
|
);
|
|
return;
|
|
}
|
|
if (result.status === "invalid") {
|
|
enqueueSnackbar(
|
|
result.message ?? "qBittorrent не принял этот торрент",
|
|
{ variant: "error" },
|
|
);
|
|
return;
|
|
}
|
|
enqueueSnackbar(
|
|
result.status === "pending"
|
|
? "Раздача добавлена, qBittorrent получает метаданные"
|
|
: "Раздача добавлена",
|
|
{ variant: "success" },
|
|
);
|
|
onAdded(result);
|
|
onClose();
|
|
},
|
|
onError: (error) => {
|
|
enqueueSnackbar(toQbError(error).message, { variant: "error" });
|
|
},
|
|
});
|
|
|
|
const magnetValid = magnet.trim().startsWith("magnet:?");
|
|
const canSubmit =
|
|
!add.isPending &&
|
|
((tab === "magnet" && magnetValid) || (tab === "file" && !!filePath));
|
|
|
|
const selectedCategory = categories.data?.find(
|
|
(item) => item.name === category,
|
|
);
|
|
|
|
return (
|
|
<Dialog
|
|
open={isOpen}
|
|
onClose={add.isPending ? undefined : onClose}
|
|
fullWidth
|
|
maxWidth="sm"
|
|
disableScrollLock
|
|
slotProps={{
|
|
paper: {
|
|
sx: {
|
|
backgroundColor: "rgba(20, 20, 30, 0.82)",
|
|
backdropFilter: "blur(24px) saturate(180%)",
|
|
backgroundImage:
|
|
"linear-gradient(rgba(255, 255, 255, 0.05), rgba(255, 255, 255, 0))",
|
|
border: "1px solid rgba(255, 255, 255, 0.08)",
|
|
boxShadow: "0 20px 50px rgba(0,0,0,0.5)",
|
|
borderRadius: 4,
|
|
overflow: "hidden",
|
|
},
|
|
},
|
|
}}
|
|
>
|
|
<DialogTitle sx={{ pb: 0 }}>
|
|
<Stack direction="row" spacing={1.5} sx={{ alignItems: "center" }}>
|
|
<span
|
|
className="material-symbols-rounded"
|
|
style={{ color: "var(--mui-palette-primary-main)" }}
|
|
>
|
|
download
|
|
</span>
|
|
<Typography variant="h6" sx={{ fontWeight: 700 }}>
|
|
Добавить раздачу
|
|
</Typography>
|
|
</Stack>
|
|
</DialogTitle>
|
|
<DialogContent sx={{ pt: 1 }}>
|
|
<Tabs
|
|
value={tab}
|
|
onChange={(_, value: TabValue) => setTab(value)}
|
|
variant="fullWidth"
|
|
sx={{ mb: 2, minHeight: 40 }}
|
|
>
|
|
<Tab value="magnet" label="Magnet-ссылка" />
|
|
<Tab value="file" label=".torrent файл" />
|
|
</Tabs>
|
|
|
|
{tab === "magnet" ? (
|
|
<TextField
|
|
autoFocus
|
|
fullWidth
|
|
multiline
|
|
minRows={3}
|
|
maxRows={6}
|
|
label="magnet:?xt=urn:btih:…"
|
|
placeholder="Вставьте magnet-ссылку"
|
|
value={magnet}
|
|
onChange={(event) => setMagnet(event.target.value)}
|
|
error={magnet.length > 0 && !magnetValid}
|
|
helperText={
|
|
magnet.length > 0 && !magnetValid
|
|
? "Ссылка должна начинаться с magnet:?"
|
|
: "Например, magnet:?xt=urn:btih:…"
|
|
}
|
|
/>
|
|
) : (
|
|
<Stack spacing={1.5}>
|
|
<Button
|
|
variant="outlined"
|
|
size="large"
|
|
onClick={pickFile}
|
|
startIcon={
|
|
<span className="material-symbols-rounded">folder_open</span>
|
|
}
|
|
sx={{
|
|
borderStyle: "dashed",
|
|
py: 3,
|
|
justifyContent: "flex-start",
|
|
textTransform: "none",
|
|
}}
|
|
>
|
|
{fileName ?? "Выбрать .torrent файл"}
|
|
</Button>
|
|
<Typography variant="caption" sx={{ opacity: 0.6 }}>
|
|
Файл отправится в qBittorrent как есть; категория и папка
|
|
применятся на стороне сервера.
|
|
</Typography>
|
|
</Stack>
|
|
)}
|
|
|
|
<TextField
|
|
select
|
|
fullWidth
|
|
label="Категория"
|
|
value={category}
|
|
onChange={(event) => setCategory(event.target.value)}
|
|
sx={{ mt: 2 }}
|
|
disabled={categories.isLoading}
|
|
helperText={
|
|
categories.isError
|
|
? "Не удалось загрузить категории qBittorrent"
|
|
: selectedCategory
|
|
? `Папка: ${selectedCategory.savePath || "по умолчанию"}`
|
|
: "Без категории — папка qBittorrent по умолчанию"
|
|
}
|
|
>
|
|
<MenuItem value="">Без категории</MenuItem>
|
|
{(categories.data ?? []).map((item) => (
|
|
<MenuItem key={item.name} value={item.name}>
|
|
{item.name}
|
|
</MenuItem>
|
|
))}
|
|
</TextField>
|
|
|
|
<Stack
|
|
direction={{ xs: "column", sm: "row" }}
|
|
spacing={{ xs: 0, sm: 2 }}
|
|
sx={{ mt: 1 }}
|
|
>
|
|
<FormControlLabel
|
|
control={
|
|
<Switch
|
|
checked={paused}
|
|
onChange={(_, checked) => setPaused(checked)}
|
|
/>
|
|
}
|
|
label="Добавить на паузе"
|
|
/>
|
|
<FormControlLabel
|
|
control={
|
|
<Switch
|
|
checked={autoTmm}
|
|
onChange={(_, checked) => setAutoTmm(checked)}
|
|
/>
|
|
}
|
|
label="Автоуправление категорией"
|
|
/>
|
|
</Stack>
|
|
</DialogContent>
|
|
<DialogActions sx={{ px: 3, pb: 2.5 }}>
|
|
<Button onClick={onClose} disabled={add.isPending}>
|
|
Отмена
|
|
</Button>
|
|
<Button
|
|
variant="contained"
|
|
loading={add.isPending}
|
|
disabled={!canSubmit}
|
|
onClick={() => add.mutate()}
|
|
>
|
|
Добавить
|
|
</Button>
|
|
</DialogActions>
|
|
</Dialog>
|
|
);
|
|
}
|