rqbit/crates/librqbit/webui/src/components/buttons/TorrentActions.tsx

152 lines
3.9 KiB
TypeScript
Raw Normal View History

import { useContext, useState } from "react";
import { TorrentStats } from "../../api-types";
2023-12-17 19:27:22 +00:00
import { APIContext, RefreshTorrentStatsContext } from "../../context";
import { IconButton } from "./IconButton";
import { DeleteTorrentModal } from "../modal/DeleteTorrentModal";
import {
FaCog,
FaPause,
FaPlay,
FaTrash,
FaClipboardList,
} from "react-icons/fa";
2023-12-17 19:27:22 +00:00
import { useErrorStore } from "../../stores/errorStore";
2024-08-10 13:30:02 +01:00
import { ErrorComponent } from "../ErrorComponent";
export const TorrentActions: React.FC<{
id: number;
statsResponse: TorrentStats;
extendedView: boolean;
setExtendedView: (extendedView: boolean) => void;
}> = ({ id, statsResponse, extendedView, setExtendedView }) => {
let state = statsResponse.state;
let [disabled, setDisabled] = useState<boolean>(false);
let [deleting, setDeleting] = useState<boolean>(false);
let refreshCtx = useContext(RefreshTorrentStatsContext);
const canPause = state == "live";
const canUnpause = state == "paused" || state == "error";
const canConfigure = state == "paused" || state == "live";
2023-12-17 19:27:22 +00:00
const setCloseableError = useErrorStore((state) => state.setCloseableError);
const API = useContext(APIContext);
const unpause = () => {
setDisabled(true);
API.start(id)
.then(
() => {
refreshCtx.refresh();
},
(e) => {
2023-12-17 19:27:22 +00:00
setCloseableError({
text: `Error starting torrent id=${id}`,
details: e,
});
2024-08-10 13:30:02 +01:00
},
)
.finally(() => setDisabled(false));
};
const pause = () => {
setDisabled(true);
API.pause(id)
.then(
() => {
refreshCtx.refresh();
},
(e) => {
2023-12-17 19:27:22 +00:00
setCloseableError({
text: `Error pausing torrent id=${id}`,
details: e,
});
2024-08-10 13:30:02 +01:00
},
)
.finally(() => setDisabled(false));
};
const startDeleting = () => {
setDisabled(true);
setDeleting(true);
};
const cancelDeleting = () => {
setDisabled(false);
setDeleting(false);
};
const playlistUrl = API.getPlaylistUrl(id);
const setAlert = useErrorStore((state) => state.setAlert);
const copyPlaylistUrlToClipboard = async () => {
if (!playlistUrl) {
return;
}
try {
await navigator.clipboard.writeText(playlistUrl);
} catch (e) {
2024-08-10 13:30:02 +01:00
setAlert({
text: "Copy playlist URL",
details: {
text: (
<>
<p>
Copy{" "}
<a href={playlistUrl} className="text-blue-500">
playlist URL
</a>{" "}
to clipboard and paste into e.g. VLC to play.
</p>
</>
),
},
});
return;
}
setAlert({
text: "Copied",
details: {
2024-08-10 13:30:02 +01:00
text: "Playlist URL copied to clipboard. Paste into e.g. VLC to play.",
},
});
};
return (
2023-12-15 14:44:36 +00:00
<div className="flex w-full justify-center gap-2 dark:text-slate-300">
{canUnpause && (
<IconButton onClick={unpause} disabled={disabled}>
2023-12-17 10:34:35 +00:00
<FaPlay className="hover:text-green-600" />
</IconButton>
)}
{canPause && (
<IconButton onClick={pause} disabled={disabled}>
2023-12-17 10:34:35 +00:00
<FaPause className="hover:text-amber-500" />
</IconButton>
)}
{canConfigure && (
<IconButton
onClick={() => setExtendedView(!extendedView)}
disabled={disabled}
>
<FaCog className="hover:text-green-600" />
</IconButton>
)}
<IconButton onClick={startDeleting} disabled={disabled}>
2023-12-17 10:34:35 +00:00
<FaTrash className="hover:text-red-500" />
</IconButton>
<IconButton
href={playlistUrl ?? "#"}
onClick={copyPlaylistUrlToClipboard}
>
<FaClipboardList className="hover:text-green-500" />
2024-08-04 13:36:39 +02:00
</IconButton>
<DeleteTorrentModal id={id} show={deleting} onHide={cancelDeleting} />
</div>
);
};