1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
|
import { useEffect, useRef, useState } from "react";
import Image from "next/image";
import {
ArrowsPointingOutIcon,
ArrowsPointingInIcon,
} from "@heroicons/react/24/outline";
import { useAniList } from "../../../lib/anilist/useAnilist";
import { getHeaders } from "@/utils/imageUtils";
export default function SecondPanel({
aniId,
chapterData,
data,
hasRun,
currentChapter,
currentId,
seekPage,
setSeekPage,
visible,
setVisible,
session,
providerId,
}) {
const [index, setIndex] = useState(0);
const [image, setImage] = useState(null);
const { markProgress } = useAniList(session);
useEffect(() => {
setIndex(0);
setSeekPage(0);
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [data, currentId]);
const seekToIndex = (newIndex) => {
if (newIndex >= 0 && newIndex < data.length) {
// if newIndex is odd, decrease it by 1 to show the previous page
if (newIndex % 2 !== 0) {
newIndex = newIndex - 1;
}
setIndex(newIndex);
setSeekPage(newIndex);
}
};
useEffect(() => {
seekToIndex(seekPage);
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [seekPage]);
useEffect(() => {
if (data && Array.isArray(data) && data?.length > 0) {
setImage([...data].reverse()); // Create a copy of data before reversing
}
}, [data]);
useEffect(() => {
const handleKeyDown = (event) => {
if (event.key === "ArrowRight") {
if (index > 0) {
setIndex(index - 2);
setSeekPage(index - 2);
}
} else if (event.key === "ArrowLeft") {
if (index < image.length - 2) {
setIndex(index + 2);
setSeekPage(index + 2);
}
if (index + 1 >= image.length - 4 && !hasRun.current) {
const current = chapterData.chapters?.find(
(x) => x.id === currentChapter.id
);
const chapterNumber = chapterData.chapters.indexOf(current) + 1;
if (chapterNumber) {
markProgress(aniId, chapterNumber);
}
hasRun.current = true;
}
}
};
window.addEventListener("keydown", handleKeyDown);
return () => {
window.removeEventListener("keydown", handleKeyDown);
};
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [index, image]);
const handleNext = () => {
if (index < image.length - 2) {
setIndex(index + 2);
setSeekPage(index + 2);
}
if (index + 1 >= image.length - 4 && !hasRun.current) {
console.log("marking progress");
const current = chapterData.chapters?.find(
(x) => x.id === currentChapter.id
);
const chapterNumber = chapterData.chapters.indexOf(current) + 1;
if (chapterNumber) {
markProgress(aniId, chapterNumber);
}
markProgress(aniId, chapterNumber);
hasRun.current = true;
}
};
const handlePrev = () => {
if (index > 0) {
setIndex(index - 2);
setSeekPage(index - 2);
}
};
return (
<div className="flex-grow h-screen">
<div className="flex items-center w-full relative group">
{image && Array.isArray(image) && image?.length > 0 ? (
<>
<div
className={`flex w-full ${
image[image.length - index - 2]?.url
? "justify-between"
: "justify-center"
}`}
>
{image[image.length - index - 2]?.url && (
<Image
key={image[image.length - index - 2]?.url}
width={500}
height={500}
className="w-1/2 h-screen object-contain"
src={`https://api.consumet.org/utils/image-proxy?url=${encodeURIComponent(
image[image.length - index - 2]?.url
)}${
image[image.length - index - 2]?.headers?.Referer
? `&headers=${encodeURIComponent(
JSON.stringify(
image[image.length - index - 2]?.headers
)
)}`
: `&headers=${encodeURIComponent(
JSON.stringify(getHeaders(providerId))
)}`
}`}
alt="Manga Page"
/>
)}
<Image
key={image[image.length - index - 1]?.url}
width={500}
height={500}
className="w-1/2 h-screen object-contain"
src={`https://api.consumet.org/utils/image-proxy?url=${encodeURIComponent(
image[image.length - index - 1]?.url
)}${
image[image.length - index - 1]?.headers?.Referer
? `&headers=${encodeURIComponent(
JSON.stringify(image[image.length - index - 1]?.headers)
)}`
: `&headers=${encodeURIComponent(
JSON.stringify(getHeaders(providerId))
)}`
}`}
alt="Manga Page"
/>
</div>
<div className="absolute w-full hidden group-hover:flex justify-between mt-4">
<button
className="px-4 py-2 bg-secondary text-white rounded-r"
onClick={handleNext}
>
Next
</button>
<button
className="px-4 py-2 bg-secondary text-white rounded-l"
onClick={handlePrev}
>
Previous
</button>
</div>
</>
) : (
<div className="w-full flex-center h-full">
{data.error || "Not found"} :(
</div>
)}
<span className="absolute hidden group-hover:flex bottom-5 left-5 bg-secondary p-2">
{visible ? (
<button type="button" onClick={() => setVisible(!visible)}>
<ArrowsPointingOutIcon className="w-5 h-5" />
</button>
) : (
<button type="button" onClick={() => setVisible(!visible)}>
<ArrowsPointingInIcon className="w-5 h-5" />
</button>
)}
</span>
<span className="absolute hidden group-hover:flex bottom-5 right-5 bg-secondary p-2">
Page {index + 1}
{index + 2 > data.length ? "" : `-${index + 2}`}/{data.length}
</span>
</div>
</div>
);
}
|