feat: implement reading tracker for chapter pages
This commit is contained in:
parent
73485fadda
commit
325811ce48
@ -40,12 +40,10 @@ export const customInstance = <T>(
|
|||||||
options?: AxiosRequestConfig,
|
options?: AxiosRequestConfig,
|
||||||
): Promise<T> => {
|
): Promise<T> => {
|
||||||
const source = axios.CancelToken.source();
|
const source = axios.CancelToken.source();
|
||||||
const promise = Api({
|
return Api({
|
||||||
...config,
|
...config,
|
||||||
...options,
|
...options,
|
||||||
cancelToken: source.token,
|
cancelToken: source.token,
|
||||||
paramsSerializer: { indexes: null },
|
paramsSerializer: { indexes: null },
|
||||||
}).then(({ data }) => data);
|
}).then(({ data }) => data);
|
||||||
|
|
||||||
return promise;
|
|
||||||
};
|
};
|
||||||
|
|||||||
2618
api/mangamochi.ts
2618
api/mangamochi.ts
File diff suppressed because it is too large
Load Diff
@ -7,21 +7,26 @@ import { ArrowLeft, ChevronLeft, ChevronRight, Home } from "lucide-react";
|
|||||||
import { Button } from "@/components/ui/button";
|
import { Button } from "@/components/ui/button";
|
||||||
import { ThemeToggle } from "@/components/theme-toggle";
|
import { ThemeToggle } from "@/components/theme-toggle";
|
||||||
import { useGetMangaChapterImages, useMarkAsRead } from "@/api/mangamochi";
|
import { useGetMangaChapterImages, useMarkAsRead } from "@/api/mangamochi";
|
||||||
|
import { useReadingTracker } from "@/hooks/use-reading-tracker";
|
||||||
|
|
||||||
export default function ChapterReaderPage() {
|
export default function ChapterReaderPage() {
|
||||||
|
const { setCurrentChapterPage, getCurrentChapterPage } = useReadingTracker();
|
||||||
|
|
||||||
const params = useParams();
|
const params = useParams();
|
||||||
const router = useRouter();
|
const router = useRouter();
|
||||||
const mangaId = Number(params.id);
|
const mangaId = Number(params.id);
|
||||||
const chapterNumber = Number(params.chapterNumber);
|
const chapterNumber = Number(params.chapterNumber);
|
||||||
|
|
||||||
|
const [currentPage, setCurrentPage] = useState(
|
||||||
|
getCurrentChapterPage(chapterNumber) ?? 1,
|
||||||
|
);
|
||||||
|
|
||||||
const { data, isLoading } = useGetMangaChapterImages(chapterNumber);
|
const { data, isLoading } = useGetMangaChapterImages(chapterNumber);
|
||||||
|
|
||||||
const { mutate } = useMarkAsRead();
|
const { mutate } = useMarkAsRead();
|
||||||
|
|
||||||
const [currentPage, setCurrentPage] = useState(1);
|
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!data) {
|
if (!data || isLoading) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -30,6 +35,21 @@ export default function ChapterReaderPage() {
|
|||||||
}
|
}
|
||||||
}, [data, mutate, currentPage]);
|
}, [data, mutate, currentPage]);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
setCurrentChapterPage(chapterNumber, currentPage);
|
||||||
|
}, [chapterNumber, currentPage]);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (!isLoading && !data?.data) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const storedChapterPage = getCurrentChapterPage(chapterNumber);
|
||||||
|
if (storedChapterPage) {
|
||||||
|
setCurrentPage(storedChapterPage);
|
||||||
|
}
|
||||||
|
}, [getCurrentChapterPage, isLoading, data?.data]);
|
||||||
|
|
||||||
if (!data?.data) {
|
if (!data?.data) {
|
||||||
return (
|
return (
|
||||||
<div className="flex min-h-screen items-center justify-center bg-background">
|
<div className="flex min-h-screen items-center justify-center bg-background">
|
||||||
|
|||||||
@ -257,6 +257,7 @@ export default function MangaDetailPage() {
|
|||||||
</p>
|
</p>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
{provider.supportsChapterFetch && (
|
||||||
<div className={"pr-4"}>
|
<div className={"pr-4"}>
|
||||||
<Button
|
<Button
|
||||||
size="sm"
|
size="sm"
|
||||||
@ -270,7 +271,7 @@ export default function MangaDetailPage() {
|
|||||||
<Database className="h-4 w-4" />
|
<Database className="h-4 w-4" />
|
||||||
Fetch from Provider
|
Fetch from Provider
|
||||||
</Button>
|
</Button>
|
||||||
</div>
|
</div>)}
|
||||||
</div>
|
</div>
|
||||||
<ChevronDown
|
<ChevronDown
|
||||||
className={`h-5 w-5 text-muted-foreground transition-transform ${
|
className={`h-5 w-5 text-muted-foreground transition-transform ${
|
||||||
|
|||||||
@ -8,7 +8,6 @@ import { BookOpen, Search } from "lucide-react";
|
|||||||
import { Input } from "@/components/ui/input";
|
import { Input } from "@/components/ui/input";
|
||||||
import { ThemeToggle } from "@/components/theme-toggle";
|
import { ThemeToggle } from "@/components/theme-toggle";
|
||||||
import { useGetMangas } from "@/api/mangamochi";
|
import { useGetMangas } from "@/api/mangamochi";
|
||||||
import { useAuth } from "@/contexts/auth-context";
|
|
||||||
import { AuthHeader } from "@/components/auth-header";
|
import { AuthHeader } from "@/components/auth-header";
|
||||||
import { ImportDropdown } from "@/components/import-dropdown";
|
import { ImportDropdown } from "@/components/import-dropdown";
|
||||||
import { SortOption, SortOptions } from "@/components/sort-options";
|
import { SortOption, SortOptions } from "@/components/sort-options";
|
||||||
|
|||||||
52
hooks/use-reading-tracker.ts
Normal file
52
hooks/use-reading-tracker.ts
Normal file
@ -0,0 +1,52 @@
|
|||||||
|
import { useCallback } from "react";
|
||||||
|
|
||||||
|
interface ReadingTrackerData {
|
||||||
|
chapterPage: { [chapterId: number]: number };
|
||||||
|
}
|
||||||
|
|
||||||
|
export const useReadingTracker = () => {
|
||||||
|
const setCurrentChapterPage = useCallback(
|
||||||
|
(id: number, pageNumber: number) => {
|
||||||
|
const jsonString = localStorage.getItem("readingTrackerData");
|
||||||
|
|
||||||
|
let readingTrackerData: ReadingTrackerData;
|
||||||
|
try {
|
||||||
|
readingTrackerData = jsonString
|
||||||
|
? JSON.parse(jsonString)
|
||||||
|
: { chapterPage: {} };
|
||||||
|
} catch (error) {
|
||||||
|
console.error("Error parsing reading tracker data:", error);
|
||||||
|
readingTrackerData = { chapterPage: {} };
|
||||||
|
}
|
||||||
|
|
||||||
|
const updatedData = {
|
||||||
|
...readingTrackerData,
|
||||||
|
chapterPage: {
|
||||||
|
...readingTrackerData.chapterPage,
|
||||||
|
[id]: pageNumber,
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
localStorage.setItem("readingTrackerData", JSON.stringify(updatedData));
|
||||||
|
},
|
||||||
|
[],
|
||||||
|
);
|
||||||
|
|
||||||
|
const getCurrentChapterPage = useCallback(
|
||||||
|
(id: number): number | undefined => {
|
||||||
|
const jsonString = localStorage.getItem("readingTrackerData");
|
||||||
|
if (!jsonString) return undefined;
|
||||||
|
|
||||||
|
try {
|
||||||
|
const readingTrackerData: ReadingTrackerData = JSON.parse(jsonString);
|
||||||
|
return readingTrackerData.chapterPage[id];
|
||||||
|
} catch (error) {
|
||||||
|
console.error("Error parsing reading tracker data:", error);
|
||||||
|
return undefined;
|
||||||
|
}
|
||||||
|
},
|
||||||
|
[],
|
||||||
|
);
|
||||||
|
|
||||||
|
return { setCurrentChapterPage, getCurrentChapterPage };
|
||||||
|
};
|
||||||
Loading…
x
Reference in New Issue
Block a user