/* * 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/. */ import Stack from '@mui/material/Stack'; import { useTranslation } from 'react-i18next'; import IconButton from '@mui/material/IconButton'; import BookmarkIcon from '@mui/icons-material/Bookmark'; import BookmarkBorderIcon from '@mui/icons-material/BookmarkBorder'; import OpenInNewIcon from '@mui/icons-material/OpenInNew'; import DownloadIcon from '@mui/icons-material/Download'; import ReplayIcon from '@mui/icons-material/Replay'; import { memo, useMemo, useRef } from 'react'; import DeleteIcon from '@mui/icons-material/Delete'; import FavoriteIcon from '@mui/icons-material/Favorite'; import FavoriteBorderIcon from '@mui/icons-material/FavoriteBorder'; import { CustomTooltip } from '@/modules/core/components/CustomTooltip.tsx'; import { actionToTranslationKey, ChapterAction, Chapters } from '@/modules/chapter/services/Chapters.ts'; import { ReaderStateChapters, TReaderStateMangaContext } from '@/modules/reader/types/Reader.types.ts'; import { DownloadStateIndicator } from '@/modules/core/components/DownloadStateIndicator.tsx'; import { ReaderStatePages } from '@/modules/reader/types/ReaderProgressBar.types.ts'; import { withPropsFrom } from '@/modules/core/hoc/withPropsFrom.tsx'; import { useReaderStateChaptersContext } from '@/modules/reader/contexts/state/ReaderStateChaptersContext.tsx'; import { userReaderStatePagesContext } from '@/modules/reader/contexts/state/ReaderStatePagesContext.tsx'; import { useManageMangaLibraryState } from '@/modules/manga/hooks/useManageMangaLibraryState.tsx'; import { FALLBACK_MANGA } from '@/modules/manga/Manga.constants.ts'; import { useReaderStateMangaContext } from '@/modules/reader/contexts/state/ReaderStateMangaContext.tsx'; const DownloadButton = ({ currentChapter }: Required>) => { const { t } = useTranslation(); const downloadStatus = Chapters.useDownloadStatusFromCache(currentChapter?.id ?? -1); if (currentChapter && Chapters.isDownloaded(currentChapter)) { return ( Chapters.performAction('delete', [currentChapter.id], {})} color="inherit"> ); } if (downloadStatus) { return ; } return ( Chapters.performAction('download', [currentChapter?.id ?? -1], {})} color="inherit" > ); }; const ACTION_FALLBACK_MANGA = { ...FALLBACK_MANGA, title: 'Fallback', inLibrary: false, }; const BaseReaderNavBarDesktopActions = memo( ({ manga, currentChapter, pageLoadStates, setPageLoadStates, setRetryFailedPagesKeyPrefix, }: Required> & Pick & Pick) => { const { id, isBookmarked, realUrl } = currentChapter ?? { id: -1, isBookmarked: false, realUrl: '' }; const { inLibrary } = manga ?? ACTION_FALLBACK_MANGA; const { t } = useTranslation(); const { CategorySelectComponent, updateLibraryState } = useManageMangaLibraryState( manga ?? ACTION_FALLBACK_MANGA, ); const pageRetryKeyPrefix = useRef(0); const haveSomePagesFailedToLoad = useMemo( () => pageLoadStates.some((pageLoadState) => pageLoadState.error), [pageLoadStates], ); const bookmarkAction: Extract = isBookmarked ? 'unbookmark' : 'bookmark'; return ( <> {inLibrary ? : } Chapters.performAction(bookmarkAction, [id], {})} color="inherit"> {isBookmarked ? : } { setPageLoadStates((statePageLoadStates) => statePageLoadStates.map((pageLoadState) => ({ url: pageLoadState.url, loaded: pageLoadState.loaded, })), ); setRetryFailedPagesKeyPrefix(`${pageRetryKeyPrefix.current}`); pageRetryKeyPrefix.current = (pageRetryKeyPrefix.current + 1) % 1000; }} disabled={!haveSomePagesFailedToLoad} color="inherit" > {CategorySelectComponent} ); }, ); export const ReaderNavBarDesktopActions = withPropsFrom( BaseReaderNavBarDesktopActions, [useReaderStateChaptersContext, userReaderStatePagesContext, useReaderStateMangaContext], ['currentChapter', 'pageLoadStates', 'setPageLoadStates', 'setRetryFailedPagesKeyPrefix', 'manga'], );