aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/anify/getMangaId.ts (renamed from lib/anify/getMangaId.js)31
-rw-r--r--lib/anify/info.js24
-rw-r--r--lib/anilist/aniAdvanceSearch.ts (renamed from lib/anilist/aniAdvanceSearch.js)34
-rw-r--r--lib/anilist/getUpcomingAnime.js2
-rw-r--r--lib/anilist/useAnilist.js3
-rw-r--r--lib/context/watchPageProvider.js12
-rw-r--r--lib/hooks/useCountdownSeconds.ts54
-rw-r--r--lib/hooks/useWatchStorage.tsx28
-rw-r--r--lib/prisma.ts (renamed from lib/prisma.js)4
-rw-r--r--lib/redis.ts (renamed from lib/redis.js)12
10 files changed, 168 insertions, 36 deletions
diff --git a/lib/anify/getMangaId.js b/lib/anify/getMangaId.ts
index 6b1445f..bf7bb71 100644
--- a/lib/anify/getMangaId.js
+++ b/lib/anify/getMangaId.ts
@@ -1,8 +1,25 @@
-import axios from "axios";
+import axios, { AxiosResponse } from "axios";
-export async function fetchInfo(romaji, english, native) {
+interface Manga {
+ id: string;
+ title: {
+ romaji: string;
+ english: string;
+ native: string;
+ };
+}
+
+interface SearchResult {
+ results: Manga[];
+}
+
+export async function fetchInfo(
+ romaji: string,
+ english: string,
+ native: string
+): Promise<{ id: string } | null> {
try {
- const { data: getManga } = await axios.get(
+ const { data: getManga }: AxiosResponse<SearchResult> = await axios.get(
`https://api.anify.tv/search-advanced?query=${
english || romaji
}&type=manga`
@@ -26,10 +43,14 @@ export async function fetchInfo(romaji, english, native) {
}
}
-export default async function getMangaId(romaji, english, native) {
+export default async function getMangaId(
+ romaji: string,
+ english: string,
+ native: string
+): Promise<{ id: string } | { message: string } | { error: any }> {
try {
const data = await fetchInfo(romaji, english, native);
- if (data) {
+ if (data && "id" in data) {
return data;
} else {
return { message: "Schedule not found" };
diff --git a/lib/anify/info.js b/lib/anify/info.js
index 05159ce..8284e94 100644
--- a/lib/anify/info.js
+++ b/lib/anify/info.js
@@ -1,7 +1,6 @@
import axios from "axios";
-import { redis } from "../redis";
-export async function fetchInfo(id, key) {
+export async function fetchInfo(id) {
try {
const { data } = await axios.get(`https://api.anify.tv/info/${id}`);
return data;
@@ -11,24 +10,13 @@ export async function fetchInfo(id, key) {
}
}
-export default async function getAnifyInfo(id, key) {
+export default async function getAnifyInfo(id) {
try {
- let cached;
- if (redis) {
- cached = await redis.get(id);
- }
- if (cached) {
- return JSON.parse(cached);
+ const data = await fetchInfo(id);
+ if (data) {
+ return data;
} else {
- const data = await fetchInfo(id, key);
- if (data) {
- if (redis) {
- await redis.set(id, JSON.stringify(data), "EX", 60 * 10);
- }
- return data;
- } else {
- return { message: "Schedule not found" };
- }
+ return { message: "Anify Info Not Found!" };
}
} catch (error) {
return { error };
diff --git a/lib/anilist/aniAdvanceSearch.js b/lib/anilist/aniAdvanceSearch.ts
index ccfbd27..5251815 100644
--- a/lib/anilist/aniAdvanceSearch.js
+++ b/lib/anilist/aniAdvanceSearch.ts
@@ -1,8 +1,32 @@
+import { AnifySearchAdvanceTypes } from "types/info/AnifySearchAdvanceTypes";
import { advanceSearchQuery } from "../graphql/query";
+export type AniAdvanceSearch = {
+ search?: string;
+ type?: string;
+ genres?: any[];
+ page?: number;
+ sort?: string;
+ format?:
+ | "TV"
+ | "TV_SHORT"
+ | "MOVIE"
+ | "SPECIAL"
+ | "OVA"
+ | "ONA"
+ | "MUSIC"
+ | "MANGA"
+ | "NOVEL"
+ | "ONE_SHOT"
+ | undefined;
+ season?: string;
+ seasonYear?: number;
+ perPage?: number;
+};
+
export async function aniAdvanceSearch({
search,
- type,
+ type = "ANIME",
genres,
page,
sort,
@@ -10,7 +34,7 @@ export async function aniAdvanceSearch({
season,
seasonYear,
perPage,
-}) {
+}: AniAdvanceSearch) {
const categorizedGenres = genres?.reduce((result, item) => {
const existingEntry = result[item.type];
@@ -43,10 +67,10 @@ export async function aniAdvanceSearch({
}),
});
- const data = await response.json();
+ const data: AnifySearchAdvanceTypes = await response.json();
return {
pageInfo: {
- hasNextPage: page < data.total,
+ hasNextPage: page ?? 0 < data.total,
currentPage: page,
lastPage: Math.ceil(data.lastPage),
perPage: perPage ?? 20,
@@ -62,7 +86,7 @@ export async function aniAdvanceSearch({
large: item.coverImage,
},
description: item.description,
- duration: item.duration ?? null,
+ duration: item?.duration ?? null,
endDate: {
day: null,
month: null,
diff --git a/lib/anilist/getUpcomingAnime.js b/lib/anilist/getUpcomingAnime.js
index 2ab9315..d5249f1 100644
--- a/lib/anilist/getUpcomingAnime.js
+++ b/lib/anilist/getUpcomingAnime.js
@@ -59,7 +59,7 @@ const getUpcomingAnime = async () => {
`;
const variables = {
- season: "FALL",
+ season: currentSeason,
year: currentYear,
format: "TV",
};
diff --git a/lib/anilist/useAnilist.js b/lib/anilist/useAnilist.js
index 20c1964..323dd29 100644
--- a/lib/anilist/useAnilist.js
+++ b/lib/anilist/useAnilist.js
@@ -225,6 +225,9 @@ export const useAniList = (session) => {
// if (lists.length > 0) {
await fetchGraphQL(progressWatched, variables);
console.log(`Progress Updated: ${progress}`, status);
+ toast.success(`Progress Updated: ${progress}`, {
+ position: "bottom-right",
+ });
// }
} else if (media && media.type === "MANGA") {
let variables = {
diff --git a/lib/context/watchPageProvider.js b/lib/context/watchPageProvider.js
index a9d707b..c305710 100644
--- a/lib/context/watchPageProvider.js
+++ b/lib/context/watchPageProvider.js
@@ -9,10 +9,14 @@ export const WatchPageProvider = ({ children }) => {
currentTime: 0,
isPlaying: false,
});
- const [autoplay, setAutoPlay] = useState(false);
+ const [autoplay, setAutoPlay] = useState(null);
+ const [autoNext, setAutoNext] = useState(null);
const [marked, setMarked] = useState(0);
const [userData, setUserData] = useState(null);
+ const [dataMedia, setDataMedia] = useState(null);
+
+ const [track, setTrack] = useState(null);
return (
<WatchPageContext.Provider
@@ -29,6 +33,12 @@ export const WatchPageProvider = ({ children }) => {
setAutoPlay,
marked,
setMarked,
+ track,
+ setTrack,
+ dataMedia,
+ setDataMedia,
+ autoNext,
+ setAutoNext,
}}
>
{children}
diff --git a/lib/hooks/useCountdownSeconds.ts b/lib/hooks/useCountdownSeconds.ts
new file mode 100644
index 0000000..3d17ede
--- /dev/null
+++ b/lib/hooks/useCountdownSeconds.ts
@@ -0,0 +1,54 @@
+import { useEffect, useState } from "react";
+
+interface CountdownValues {
+ days: number;
+ hours: number;
+ minutes: number;
+ seconds: number;
+}
+
+interface Props {
+ targetDate: number;
+ update: Function;
+ countdown: CountdownValues;
+}
+
+const useCountdown = (targetDate: number, update: Function): Props => {
+ const countDownDate = new Date(targetDate).getTime();
+
+ const [countDown, setCountDown] = useState(
+ countDownDate - new Date().getTime()
+ );
+
+ useEffect(() => {
+ const interval = setInterval(() => {
+ const newCountDown = countDownDate - new Date().getTime();
+ setCountDown(newCountDown);
+ if (newCountDown <= 0 && newCountDown > -1000) {
+ update();
+ }
+ }, 1000);
+
+ return () => clearInterval(interval);
+ }, [countDownDate, update]);
+
+ return {
+ targetDate,
+ update,
+ countdown: getReturnValues(countDown),
+ };
+};
+
+const getReturnValues = (countDown: number): CountdownValues => {
+ // calculate time left
+ const days = Math.floor(countDown / (1000 * 60 * 60 * 24));
+ const hours = Math.floor(
+ (countDown % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60)
+ );
+ const minutes = Math.floor((countDown % (1000 * 60 * 60)) / (1000 * 60));
+ const seconds = Math.floor((countDown % (1000 * 60)) / 1000);
+
+ return { days, hours, minutes, seconds };
+};
+
+export { useCountdown };
diff --git a/lib/hooks/useWatchStorage.tsx b/lib/hooks/useWatchStorage.tsx
new file mode 100644
index 0000000..ee24a39
--- /dev/null
+++ b/lib/hooks/useWatchStorage.tsx
@@ -0,0 +1,28 @@
+import { UserData } from "@/components/watch/new-player/player";
+import { useState } from "react";
+
+function useWatchStorage() {
+ // Get initial value from local storage or empty object
+ const [settings, setSettings] = useState(() => {
+ const storedSettings = localStorage?.getItem("artplayer_settings");
+ return storedSettings ? JSON.parse(storedSettings) : {};
+ });
+
+ const getSettings = (id: string): UserData | undefined => {
+ return settings[id];
+ };
+
+ // Function to update settings
+ const updateSettings = (id: string, data?: any) => {
+ // Update state
+ const updatedSettings = { ...settings, [id]: data };
+ setSettings(updatedSettings);
+
+ // Update local storage
+ localStorage.setItem("artplayer_settings", JSON.stringify(updatedSettings));
+ };
+
+ return [getSettings, updateSettings];
+}
+
+export default useWatchStorage;
diff --git a/lib/prisma.js b/lib/prisma.ts
index ed8c421..55acf8d 100644
--- a/lib/prisma.js
+++ b/lib/prisma.ts
@@ -1,5 +1,9 @@
import { PrismaClient } from "@prisma/client";
+declare global {
+ var prisma: PrismaClient | undefined;
+}
+
export const prisma = global.prisma || new PrismaClient();
if (process.env.NODE_ENV !== "production") global.prisma = prisma;
diff --git a/lib/redis.js b/lib/redis.ts
index 9522e4c..1778933 100644
--- a/lib/redis.js
+++ b/lib/redis.ts
@@ -1,16 +1,16 @@
import { Redis } from "ioredis";
import { RateLimiterRedis } from "rate-limiter-flexible";
-const REDIS_URL = process.env.REDIS_URL;
+const REDIS_URL: string | undefined = process.env.REDIS_URL;
-let redis;
-let rateLimiterRedis;
-let rateLimitStrict;
-let rateSuperStrict;
+let redis: Redis;
+let rateLimiterRedis: RateLimiterRedis;
+let rateLimitStrict: RateLimiterRedis;
+let rateSuperStrict: RateLimiterRedis;
if (REDIS_URL) {
redis = new Redis(REDIS_URL);
- redis.on("error", (err) => {
+ redis.on("error", (err: Error) => {
console.error("Redis error: ", err);
});