import type { GetServerSideProps, NextPage } from "next"; import { getSession, useSession } from "next-auth/react"; import { useState } from "react"; import { dehydrate, QueryClient, useQuery } from "react-query"; import { CustomHead } from "../components/CustomHead"; import { Layout } from "../components/Layout"; import NextImage from "next/image"; import { getDownloads, sendDownloads, sendDeleteAllQueuedIgnored, sendDeleteVideoQueuedIgnored, sendMoveVideoQueuedIgnored, sendTasks } from "../lib/getDownloads"; import RescanIcon from "../images/icon-rescan.svg"; import DownloadIcon from "../images/icon-download.svg"; import AddIcon from "../images/icon-add.svg"; import GridViewIcon from "../images/icon-gridview.svg"; import ListViewIcon from "../images/icon-listview.svg"; import StopIcon from "../images/icon-stop.svg"; import CloseIcon from "../images/icon-close.svg"; import { getTAUrl } from "../lib/constants"; import { Tasks } from "../types/download"; const TA_BASE_URL = getTAUrl(); type ViewStyle = "grid" | "list"; type IgnoredStatus = boolean; type FormHidden = boolean; type ErrorMessage = string; type PageNumber = number; export const getServerSideProps: GetServerSideProps = async (context) => { const queryClient = new QueryClient(); const session = await getSession(context); if (!session) { return { redirect: { destination: "/auth/login", permanent: false, }, }; } await queryClient.prefetchQuery(["downloads", session.ta_token.token, false, 1], () => getDownloads(session.ta_token.token, false, 1) ); return { props: { dehydratedState: dehydrate(queryClient), session, }, }; }; const Download: NextPage = () => { const { data: session } = useSession(); const [ignoredStatus, setIgnoredStatus] = useState(false); const [pageNumber, setPageNumber] = useState(1); const { data: downloads, error, isLoading, refetch, } = useQuery( ["downloads", session.ta_token.token, ignoredStatus, pageNumber], () => getDownloads(session.ta_token.token, ignoredStatus, pageNumber), { enabled: !!session?.ta_token?.token, refetchInterval: 1500, refetchIntervalInBackground: false, } ); const [formHidden, setFormHidden] = useState(true); const [viewStyle, setViewStyle] = useState(downloads?.config?.default_view?.downloads); const [errorMessage, setErrorMessage] = useState(null); const handleSetViewstyle = (selectedViewStyle: ViewStyle) => { setViewStyle(selectedViewStyle); }; const handleSetIgnoredStatus = (selectedIgnoredStatus: IgnoredStatus) => { setIgnoredStatus(selectedIgnoredStatus); refetch(); handleSetPageNumber(1); }; const handleSetFormHidden = (selectedFormHidden: FormHidden) => { setFormHidden(selectedFormHidden); }; const handleSetErrorMessage = (selectedErrorMessage: ErrorMessage) => { setErrorMessage(selectedErrorMessage); }; const handleSetPageNumber = (selectedPageNumber: PageNumber) => { setPageNumber(selectedPageNumber); }; const addToDownloadQueue = event => { event.preventDefault(); sendDownloads(session.ta_token.token, event.target.vid_url.value).then(() => { handleSetErrorMessage(null); handleSetFormHidden(true); }) .catch(error => handleSetErrorMessage(error.message)); } const handleMoveVideoQueuedIgnored = (session: string, youtube_id: string, status: string) => { sendMoveVideoQueuedIgnored(session, youtube_id, status).then(() => { handleSetErrorMessage(null); }) .catch(error => handleSetErrorMessage(error.message)); } const handleDeleteVideoQueuedIgnored = (session: string, youtube_id: string) => { sendDeleteVideoQueuedIgnored(session, youtube_id).then(() => { handleSetErrorMessage(null); }) .catch(error => handleSetErrorMessage(error.message)); } const handleDeleteAllQueuedIgnored = (session: string, filter: string) => { sendDeleteAllQueuedIgnored(session, filter).then(() => { handleSetErrorMessage(null); }) .catch(error => handleSetErrorMessage(error.message)); } const handleSendTask = (session: string, task: Tasks) => { sendTasks(session, task).then((response) => { if (response.success) { handleSetErrorMessage(null); } else { handleSetErrorMessage(`Error running task: ${response.task}.`); } }) .catch(error => handleSetErrorMessage(error.message)); } return ( <>

Downloads

{(error || !downloads?.data) && !isLoading &&

API Connection Error

} {errorMessage &&

Failed to add downloads to the queue.

{errorMessage}

} { //
//

Adding new videos to download queue.

//

Extracting lists

//

Progress: 0/0

//
} { //
//

Rescanning channels and playlists.

//

Looking for new videos.

//
} { //
//

Downloading: `VIDEO_TITLE`

//

processing

//

`DOWNLOADED_PERCENTAGE`% of `VIDEO_SIVE``VIDEO_SIZE_UNIT` at `DOWNLOAD_SPEED``DOWNLOAD_SPEED_UNIT` - time left: `DOWNLOAD_TIME_LEFT`

//

processing

//

Moving

//

Completed

//
}
{/* Appears when video is downloading */} {/*
console.log("stopQueue()")} /> console.log("killQueue()")} />
*/}
handleSendTask(session.ta_token.token, "rescan_pending")} /> {/* rescan-icon */}

Rescan subscriptions

handleSendTask(session.ta_token.token, "download_pending")} /> {/* download-icon */}

Start download

formHidden ? handleSetFormHidden(false) : handleSetFormHidden(true)} />

Add to download queue

{!formHidden &&