aboutsummaryrefslogtreecommitdiff
path: root/components/manga/panels/thirdPanel.js
blob: f13b49db59faa71e4ab209a09a0147356fe888b1 (plain) (blame)
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
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 ThirdPanel({
  aniId,
  data,
  chapterData,
  hasRun,
  currentId,
  currentChapter,
  seekPage,
  setSeekPage,
  visible,
  setVisible,
  session,
  scaleImg,
  setMobileVisible,
  mobileVisible,
  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) {
      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 - 1);
          setSeekPage(index - 1);
        }
      } else if (event.key === "ArrowLeft") {
        if (index < image.length - 1) {
          setIndex(index + 1);
          setSeekPage(index + 1);
        }
        if (index + 1 >= image.length - 2 && !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 - 1) {
      setIndex(index + 1);
      setSeekPage(index + 1);
    }
    if (index + 1 >= image.length - 2 && !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;
    }
  };

  const handlePrev = () => {
    if (index > 0) {
      setIndex(index - 1);
      setSeekPage(index - 1);
    }
  };

  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 justify-center items-center lg:scrollbar-thin scrollbar-thumb-txt scrollbar-thumb-rounded-sm overflow-x-hidden`}
            >
              <Image
                key={image[image.length - index - 1]?.url}
                width={500}
                height={500}
                className="w-full h-screen object-contain"
                onClick={() => setMobileVisible(!mobileVisible)}
                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"
                style={{
                  transform: `scale(${scaleImg})`,
                  transformOrigin: "top",
                }}
              />
            </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}/{data.length}
        </span>
      </div>
    </div>
  );
}