import { FC, useCallback, useState } from "react"; import { useDownloadChapterArchive, useFetchChapter, useGetMangaChapters, } from "@/api/mangamochi"; import { Check, Database, Download, Eye, Loader2 } from "lucide-react"; import { Button } from "@/components/ui/button"; import { useQueryClient } from "@tanstack/react-query"; import { useRouter } from "next/navigation"; interface MangaChapterProps { mangaId: number; mangaProviderId: number; } export const MangaChapter: FC = ({ mangaId, mangaProviderId, }) => { const router = useRouter(); const { isPending, data, queryKey } = useGetMangaChapters(mangaProviderId); const queryClient = useQueryClient(); const { mutate: mutateDownloadChapterArchive, isPending: isPendingDownloadChapter, } = useDownloadChapterArchive({ mutation: { onSuccess: (data, { chapterId }) => { const url = window.URL.createObjectURL(data); const link = document.createElement("a"); link.href = url; link.setAttribute("download", chapterId + ".cbz"); document.body.appendChild(link); link.click(); link.remove(); window.URL.revokeObjectURL(url); }, }, }); const { mutate, isPending: isPendingFetchChapter } = useFetchChapter({ mutation: { onSuccess: () => queryClient.invalidateQueries({ queryKey }), onSettled: () => setFetchingId(null), }, }); const [fetchingId, setFetchingId] = useState(null); const fetchChapter = useCallback( (mangaChapterId: number) => { setFetchingId(mangaChapterId); mutate({ chapterId: mangaChapterId }); }, [mutate], ); const handleReadChapter = (chapterNumber: number) => { router.push(`/manga/${mangaId}/chapter/${chapterNumber}`); }; return (
{isPending ? (
Loading chapters...
) : data ? (
{data.data?.map((chapter) => { return (
{chapter.isRead ? ( ) : ( {/*{chapter}*/} )}

{chapter.title}

{chapter.downloaded && (

In database

)}
{chapter.downloaded ? (
) : ( )}
); })}
) : null}
); };