import {Fragment, useEffect, useState} from "react";
import axios, {AxiosResponse} from 'axios'
interface DownloadRequestResponse {
id: string;
status: string;
link: string;
}
interface DownloadRequest {
link: string
}
const calculateProgressAndAppend = (di: DownloadRequestResponse): { di: DownloadRequestResponse, progress?: number } => {
if (!di.status?.match) {
return {di}
}
const matches = di.status.match(/in-progress:\s([\d\.]+)/)
if (!matches || matches.length < 2) {
return {di}
}
const progress = matches[1]
const progressNum = parseInt(progress)
if (typeof (progressNum) !== "number") {
console.log(progress)
return {di}
}
return {di, progress: progressNum}
}
const displayDownloads = (displayItems: DownloadRequestResponse[]) => {
if (displayItems.length === 0) {
return null
}
if (!displayItems.map) {
console.log(displayItems)
}
const items = displayItems.map(calculateProgressAndAppend)
const renderListItem = (di: DownloadRequestResponse, progress?: number) => {
if (typeof (progress) !== 'number') {
return
}
return (
)
}
return
{
items
.sort((a, b) => a.progress - b.progress)
.map(({di, progress}, i) => (
{i !== 0 && (
)}
-
{renderListItem(di, progress)}
))}
}
export default function Home() {
const [downloadUrl, setDownloadUrl] = useState("")
const [downloadingItems, setDownloadingItems] = useState([])
const [downloadedItems, setDownloadedItems] = useState([])
useEffect(() => {
getInitialDownloads()
const interval = setInterval(() => {
getInitialDownloads()
}, 3000)
return () => {
clearInterval(interval)
}
}, [])
function setMockedDownloadingItems() {
setDownloadingItems([
{id: "some-id-1", status: "in-progress: 10.3", link: "https://youtube.com"},
{id: "some-id-2", status: "in-progress: 0", link: "https://youtube.com"},
{id: "some-id-3", status: "in-progress: 100.0", link: "https://youtube.com"},
{id: "some-id-4", status: "in-progress: 0", link: "https://youtube.com"},
{id: "some-id-5", status: "in-progress: 100.0", link: "https://youtube.com"},
{id: "some-id-6", status: "in-progress: 0", link: "https://youtube.com"},
{id: "some-id-7", status: "in-progress: 100.0", link: "https://youtube.com"},
{id: "some-id-8", status: "in-progress: 0", link: "https://youtube.com"},
{id: "some-id-9", status: "in-progress: 100.0", link: "https://youtube.com"},
{id: "some-id-10", status: "in-progress: 10.3", link: "https://youtube.com"},
{id: "some-id-11", status: "in-progress: 10.3", link: "https://youtube.com"},
{id: "some-id-12", status: "in-progress: 10.3", link: "https://youtube.com"},
{id: "some-id-13", status: "in-progress: 10.3", link: "https://youtube.com"},
{id: "some-id-14", status: "in-progress: 10.3", link: "https://youtube.com"},
{id: "some-id-15", status: "in-progress: 10.3", link: "https://youtube.com"},
{id: "some-id-16", status: "in-progress: 10.3", link: "https://youtube.com"},
{id: "some-id-17", status: "in-progress: 10.3", link: "https://youtube.com"},
{id: "some-id-18", status: "in-progress: 10.3", link: "https://youtube.com"},
{id: "some-id-19", status: "in-progress: 10.3", link: "https://youtube.com"},
{id: "some-id-20", status: "in-progress: 10.3", link: "https://youtube.com"},
{id: "some-id-21", status: "in-progress: 10.3", link: "https://youtube.com"},
{id: "some-id-22", status: "in-progress: 10.3", link: "https://youtube.com"},
{id: "some-id-23", status: "in-progress: 10.3", link: "https://youtube.com"},
{id: "some-id-24", status: "scheduled", link: "https://youtube.com"},
{id: "some-id-25", status: "done", link: "https://youtube.com"},
])
}
const getInitialDownloads = () => {
axios.get("http://localhost:3333/downloads?active=true"
).then(res => {
setDownloadingItems(res.data)
//setMockedDownloadingItems();
})
axios.get("http://localhost:3333/downloads?done=true"
).then(res => {
setDownloadedItems(res.data)
})
}
const download = (url: string) => {
axios.post>("http://localhost:3333/downloads", {
link: url
}).then(res => {
setDownloadingItems([{
id: res.data.id,
status: res.data.status,
link: res.data.link
}, ...downloadingItems.filter(di => di.id !== res.data.id)])
})
}
return (
)
}