import React, { useEffect, useContext, ReactElement, useState } from "react"; import { RootState } from "threetwo-ui-typings"; import { isEmpty, map } from "lodash"; import prettyBytes from "pretty-bytes"; import dayjs from "dayjs"; import ellipsize from "ellipsize"; import { useQuery } from "@tanstack/react-query"; import axios from "axios"; import { LIBRARY_SERVICE_BASE_URI } from "../../constants/endpoints"; import { useStore } from "../../store"; import { useShallow } from "zustand/react/shallow"; import { useParams } from "react-router-dom"; interface IDownloadsPanelProps { key: number; } export const DownloadsPanel = ( props: IDownloadsPanelProps, ): ReactElement | null => { const { comicObjectId } = useParams<{ comicObjectId: string }>(); const [bundles, setBundles] = useState([]); const { airDCPPSocketInstance } = useStore( useShallow((state) => ({ airDCPPSocketInstance: state.airDCPPSocketInstance, })), ); // Fetch the downloaded files and currently-downloading file(s) from AirDC++ const { data: comicObject, isSuccess } = useQuery({ queryKey: ["bundles"], queryFn: async () => await axios({ url: `${LIBRARY_SERVICE_BASE_URI}/getComicBookById`, method: "POST", headers: { "Content-Type": "application/json; charset=utf-8", }, data: { id: `${comicObjectId}`, }, }), }); const getBundles = async (comicObject) => { if (comicObject?.data.acquisition.directconnect) { const filteredBundles = comicObject.data.acquisition.directconnect.downloads.map( async ({ bundleId }) => { return await airDCPPSocketInstance.get(`queue/bundles/${bundleId}`); }, ); return await Promise.all(filteredBundles); } }; useEffect(() => { getBundles(comicObject).then((result) => { setBundles(result); }); }, [comicObject]); const Bundles = (props) => { return (
| Filename | Size | Download Time | Bundle ID |
|---|---|---|---|
{ellipsize(bundle.name, 58)}{ellipsize(bundle.target, 68)} |
{prettyBytes(bundle.size)} | {dayjs .unix(bundle.time_finished) .format("h:mm on ddd, D MMM, YYYY")} | {bundle.id} |