diff --git a/app/components/ContinueWatching/ContinueWatching.tsx b/app/components/ContinueWatching/ContinueWatching.tsx new file mode 100644 index 0000000..aabc78a --- /dev/null +++ b/app/components/ContinueWatching/ContinueWatching.tsx @@ -0,0 +1,90 @@ +"use client"; + +import { Card } from "flowbite-react"; +import { ReleaseLinkList } from "#/components/ReleaseLink/ReleaseLinkList"; +import { useUserStore } from "#/store/auth"; +import { ENDPOINTS } from "#/api/config"; +import { BookmarksList, useSWRfetcher } from "#/api/utils"; +import useSWR from "swr"; +import { useEffect, useState } from "react"; + +export const ContinueWatching = () => { + const userStore = useUserStore(); + + function useFetchReleases(listName: string) { + let url: string; + if (userStore.token) { + url = `${ENDPOINTS.user.bookmark}/all/${BookmarksList[listName]}/0?sort=1&token=${userStore.token}`; + } + const { data, isLoading, error } = useSWR(url, useSWRfetcher); + return [data, isLoading, error]; + } + + const [watchingData, watchingLoading, watchingError] = + useFetchReleases("watching"); + const [plannedData, plannedLoading, plannedError] = + useFetchReleases("planned"); + const [delayedData, delayedLoading, delayedError] = + useFetchReleases("delayed"); + + const [releaseData, setReleaseData] = useState([]); + + const firstN = (arr, n = 1) => arr.slice(0, n); + function _randomize(array: any[], limit: number) { + const toRand = array.slice(); + let currentIndex = toRand.length; + while (currentIndex != 0) { + let randomIndex = Math.floor(Math.random() * currentIndex); + currentIndex--; + [toRand[currentIndex], toRand[randomIndex]] = [ + toRand[randomIndex], + toRand[currentIndex], + ]; + } + return firstN(toRand, limit); + } + + useEffect(() => { + if (!watchingLoading && !plannedLoading && !delayedLoading) { + const data = [ + ...(watchingData.content || []), + ...(plannedData.content || []), + ...(delayedData.content || []), + ]; + console.log("loaded data:", data); + const randomizedData = _randomize(data, 3); + setReleaseData(randomizedData); + } + // eslint-disable-next-line react-hooks/exhaustive-deps + }, [watchingLoading, plannedLoading, delayedLoading]); + + if ( + !userStore.isAuth || + watchingLoading || + plannedLoading || + delayedLoading || + releaseData.length == 0 + ) + return <>; + + console.log("randomized data:", releaseData); + + return ( + +
+

Продолжить просмотр

+
+
+ {releaseData.map((release: any) => { + return ( + + ); + })} +
+
+ ); +}; diff --git a/app/components/ReleaseLink/ReleaseLinkList.tsx b/app/components/ReleaseLink/ReleaseLinkList.tsx index 4fcfa0c..7974f51 100644 --- a/app/components/ReleaseLink/ReleaseLinkList.tsx +++ b/app/components/ReleaseLink/ReleaseLinkList.tsx @@ -22,6 +22,7 @@ export const ReleaseLinkList = (props: { settings?: { showGenres?: boolean; showDescription?: boolean; + showOrigTitle?: boolean; }; chipsSettings?: { enabled: boolean; @@ -49,6 +50,7 @@ export const ReleaseLinkList = (props: { const settings = { showGenres: true, showDescription: true, + showOrigTitle: true, ...props.settings, }; const chipsSettings = props.chipsSettings || {}; @@ -86,7 +88,7 @@ export const ReleaseLinkList = (props: { return ( {index > 0 && ", "} {genre} @@ -95,12 +97,12 @@ export const ReleaseLinkList = (props: { })} {props.title_ru && ( -

+

{props.title_ru}

)} - {props.title_original && ( -

+ {settings.showOrigTitle && props.title_original && ( +

{props.title_original}

)} diff --git a/app/pages/Release.tsx b/app/pages/Release.tsx index fbc59cb..c57f6a7 100644 --- a/app/pages/Release.tsx +++ b/app/pages/Release.tsx @@ -18,6 +18,7 @@ import { CommentsMain } from "#/components/Comments/Comments.Main"; import { InfoLists } from "#/components/InfoLists/InfoLists"; import { ENDPOINTS } from "#/api/config"; import { usePreferencesStore } from "#/store/preferences"; +import { ContinueWatching } from "#/components/ContinueWatching/ContinueWatching"; export const ReleasePage = (props: any) => { const userStore = useUserStore(); @@ -167,6 +168,7 @@ export const ReleasePage = (props: any) => { related_releases={data.release.related_releases} /> )} + {userStore.token && }