From 0607bb4495d9abdeda635c748687fb9f4476a395 Mon Sep 17 00:00:00 2001 From: marcobaobao Date: Wed, 2 Aug 2023 18:52:48 +0200 Subject: [PATCH] code refacoring --- frontend/src/components/DownloadCard.tsx | 110 +++++++++++++++++ frontend/src/components/DownloadsCardView.tsx | 22 ++-- frontend/src/components/StackableResult.tsx | 111 ------------------ 3 files changed, 117 insertions(+), 126 deletions(-) create mode 100644 frontend/src/components/DownloadCard.tsx delete mode 100644 frontend/src/components/StackableResult.tsx diff --git a/frontend/src/components/DownloadCard.tsx b/frontend/src/components/DownloadCard.tsx new file mode 100644 index 0000000..c080a90 --- /dev/null +++ b/frontend/src/components/DownloadCard.tsx @@ -0,0 +1,110 @@ +import EightK from '@mui/icons-material/EightK' +import FourK from '@mui/icons-material/FourK' +import Hd from '@mui/icons-material/Hd' +import Sd from '@mui/icons-material/Sd' +import { + Button, + Card, + CardActionArea, + CardActions, + CardContent, + CardMedia, + Chip, + LinearProgress, + Skeleton, + Stack, + Typography +} from '@mui/material' +import { RPCResult } from '../types' +import { ellipsis, formatSpeedMiB, mapProcessStatus, roundMiB } from '../utils' + +type Props = { + download: RPCResult + onStop: () => void + onCopy: () => void +} + +const Resolution: React.FC<{ resolution?: string }> = ({ resolution }) => { + if (!resolution) return null + if (resolution.includes('4320')) return + if (resolution.includes('2160')) return + if (resolution.includes('1080')) return + if (resolution.includes('720')) return + return null +} + +const DownloadCard: React.FC = ({ download, onStop, onCopy }) => { + const isCompleted = () => download.progress.percentage === '-1' + + const percentageToNumber = () => isCompleted() + ? 100 + : Number(download.progress.percentage.replace('%', '')) + + return ( + + { + navigator.clipboard.writeText(download.info.url) + onCopy() + }}> + {download.info.thumbnail !== '' ? + : + + } + + {download.info.title !== '' ? + + {ellipsis(download.info.title, 54)} + : + + } + + + + {!isCompleted() ? download.progress.percentage : ''} + + +   + {!isCompleted() ? formatSpeedMiB(download.progress.speed) : ''} + + + {roundMiB(download.info.filesize_approx ?? 0)} + + + + {download.progress.percentage ? + : + null + } + + + + + + + ) +} + +export default DownloadCard \ No newline at end of file diff --git a/frontend/src/components/DownloadsCardView.tsx b/frontend/src/components/DownloadsCardView.tsx index f843711..d2fc404 100644 --- a/frontend/src/components/DownloadsCardView.tsx +++ b/frontend/src/components/DownloadsCardView.tsx @@ -1,11 +1,10 @@ -import { Grid } from "@mui/material" -import { Fragment } from "react" +import { Grid } from '@mui/material' import { useRecoilValue } from 'recoil' import { activeDownloadsState } from '../atoms/downloads' -import { useToast } from "../hooks/toast" +import { useToast } from '../hooks/toast' import { useI18n } from '../hooks/useI18n' import { useRPC } from '../hooks/useRPC' -import { StackableResult } from "./StackableResult" +import DownloadCard from './DownloadCard' const DownloadsCardView: React.FC = () => { const downloads = useRecoilValue(activeDownloadsState) ?? [] @@ -21,20 +20,13 @@ const DownloadsCardView: React.FC = () => { { downloads.map(download => ( - - + abort(download.id)} onCopy={() => pushMessage(i18n.t('clipboardAction'), 'info')} - resolution={download.info.resolution ?? ''} - speed={download.progress.speed} - size={download.info.filesize_approx ?? 0} - status={download.progress.process_status} /> - + )) } diff --git a/frontend/src/components/StackableResult.tsx b/frontend/src/components/StackableResult.tsx deleted file mode 100644 index 90a2128..0000000 --- a/frontend/src/components/StackableResult.tsx +++ /dev/null @@ -1,111 +0,0 @@ -import { EightK, FourK, Hd, Sd } from '@mui/icons-material' -import { - Button, - Card, - CardActionArea, - CardActions, - CardContent, - CardMedia, - Chip, - LinearProgress, - Skeleton, - Stack, - Typography -} from '@mui/material' -import { ellipsis, formatSpeedMiB, mapProcessStatus, roundMiB } from '../utils' - -type Props = { - title: string - url: string - thumbnail: string - resolution: string - percentage: string - size: number - speed: number - status: number - onStop: () => void - onCopy: () => void -} - -export function StackableResult({ - title, - url, - thumbnail, - resolution, - percentage, - speed, - size, - status, - onStop, - onCopy, -}: Props) { - const isCompleted = () => percentage === '-1' - - const percentageToNumber = () => isCompleted() - ? 100 - : Number(percentage.replace('%', '')) - - const guessResolution = (xByY: string): any => { - if (!xByY) return null - if (xByY.includes('4320')) return () - if (xByY.includes('2160')) return () - if (xByY.includes('1080')) return () - if (xByY.includes('720')) return () - return null - } - - return ( - - { - navigator.clipboard.writeText(url) - onCopy() - }}> - {thumbnail !== '' ? - : - - } - - {title !== '' ? - - {ellipsis(title, 54)} - : - - } - - - {!isCompleted() ? percentage : ''} - {!isCompleted() ? formatSpeedMiB(speed) : ''} - {roundMiB(size ?? 0)} - {guessResolution(resolution)} - - {percentage ? - : - null - } - - - - - - - ) -} \ No newline at end of file