2022-11-26 13:51:56 +01:00
|
|
|
/*
|
|
|
|
|
* Copyright (C) Contributors to the Suwayomi project
|
|
|
|
|
*
|
|
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
|
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
|
|
|
|
*/
|
|
|
|
|
|
2024-04-14 15:50:12 +02:00
|
|
|
import CircularProgress from '@mui/material/CircularProgress';
|
|
|
|
|
import Box from '@mui/material/Box';
|
2022-11-26 13:51:56 +01:00
|
|
|
import Typography from '@mui/material/Typography';
|
2023-03-23 13:59:32 +01:00
|
|
|
import { useTranslation } from 'react-i18next';
|
2024-07-11 17:41:38 +02:00
|
|
|
import { DownloadState } from '@/lib/graphql/generated/graphql.ts';
|
2025-01-12 15:05:09 +01:00
|
|
|
import { ChapterIdInfo, Chapters } from '@/modules/chapter/services/Chapters.ts';
|
2024-10-05 16:09:34 +02:00
|
|
|
import { TranslationKey } from '@/Base.types.ts';
|
2022-11-26 13:51:56 +01:00
|
|
|
|
2023-10-04 22:34:32 +02:00
|
|
|
const DOWNLOAD_STATE_TO_TRANSLATION_KEY_MAP: { [state in DownloadState]: TranslationKey } = {
|
|
|
|
|
DOWNLOADING: 'download.state.label.downloading',
|
|
|
|
|
ERROR: 'download.state.label.error',
|
|
|
|
|
FINISHED: 'download.state.label.finished',
|
|
|
|
|
QUEUED: 'download.state.label.queued',
|
2023-03-23 13:59:32 +01:00
|
|
|
} as const;
|
|
|
|
|
|
2025-01-12 15:05:09 +01:00
|
|
|
export const DownloadStateIndicator = ({ chapterId }: { chapterId: ChapterIdInfo['id'] }) => {
|
2023-03-23 13:59:32 +01:00
|
|
|
const { t } = useTranslation();
|
|
|
|
|
|
2025-01-12 15:05:09 +01:00
|
|
|
const download = Chapters.useDownloadStatusFromCache(chapterId);
|
|
|
|
|
|
|
|
|
|
if (!download) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2024-10-17 01:30:52 +02:00
|
|
|
const isDownloading = download.state === DownloadState.Downloading;
|
|
|
|
|
const isPartiallyDownloaded = download.progress !== 0;
|
|
|
|
|
|
2025-01-12 16:39:13 +01:00
|
|
|
const progress = `${Math.round(download.progress * 100)}%`;
|
|
|
|
|
|
2023-03-23 13:59:32 +01:00
|
|
|
return (
|
2022-11-26 13:51:56 +01:00
|
|
|
<Box
|
|
|
|
|
sx={{
|
2023-03-23 13:59:32 +01:00
|
|
|
position: 'relative',
|
|
|
|
|
display: 'inline-flex',
|
|
|
|
|
width: '50px',
|
2022-11-26 13:51:56 +01:00
|
|
|
justifyContent: 'center',
|
|
|
|
|
}}
|
|
|
|
|
>
|
2024-08-21 21:29:59 +02:00
|
|
|
{isDownloading && <CircularProgress variant="determinate" value={download.progress * 100} />}
|
2023-03-23 13:59:32 +01:00
|
|
|
<Box
|
|
|
|
|
sx={{
|
|
|
|
|
top: 0,
|
|
|
|
|
left: 0,
|
|
|
|
|
bottom: 0,
|
|
|
|
|
right: 0,
|
|
|
|
|
position: 'absolute',
|
|
|
|
|
display: 'flex',
|
|
|
|
|
alignItems: 'center',
|
|
|
|
|
justifyContent: 'center',
|
|
|
|
|
}}
|
|
|
|
|
>
|
2024-09-03 17:15:47 +02:00
|
|
|
<Typography
|
|
|
|
|
variant="caption"
|
|
|
|
|
component="div"
|
|
|
|
|
sx={{
|
|
|
|
|
color: 'text.secondary',
|
|
|
|
|
}}
|
|
|
|
|
>
|
2023-06-04 21:40:04 +02:00
|
|
|
<>
|
2025-01-12 16:39:13 +01:00
|
|
|
{isDownloading && progress}
|
2024-10-17 01:30:52 +02:00
|
|
|
{!isDownloading &&
|
2025-01-12 16:39:13 +01:00
|
|
|
t('global.value', {
|
|
|
|
|
value: t(DOWNLOAD_STATE_TO_TRANSLATION_KEY_MAP[download.state]),
|
|
|
|
|
unit: isPartiallyDownloaded ? ` (${progress})` : '',
|
|
|
|
|
})}
|
2023-06-04 21:40:04 +02:00
|
|
|
</>
|
2023-03-23 13:59:32 +01:00
|
|
|
</Typography>
|
|
|
|
|
</Box>
|
2022-11-26 13:51:56 +01:00
|
|
|
</Box>
|
2023-03-23 13:59:32 +01:00
|
|
|
);
|
|
|
|
|
};
|