Do not try to update watchstatus if the user is not connected

This commit is contained in:
Zoe Roux 2023-12-13 15:37:32 +01:00
parent e13f2a7e42
commit fbc8e14125

View File

@ -18,7 +18,7 @@
* along with Kyoo. If not, see <https://www.gnu.org/licenses/>. * along with Kyoo. If not, see <https://www.gnu.org/licenses/>.
*/ */
import { WatchStatusV, queryFn } from "@kyoo/models"; import { WatchStatusV, queryFn, useAccount } from "@kyoo/models";
import { useMutation, useQueryClient } from "@tanstack/react-query"; import { useMutation, useQueryClient } from "@tanstack/react-query";
import { useEffect, useCallback } from "react"; import { useEffect, useCallback } from "react";
import { useAtomValue } from "jotai"; import { useAtomValue } from "jotai";
@ -32,6 +32,7 @@ export const WatchStatusObserver = ({
type: "episode" | "movie"; type: "episode" | "movie";
slug: string; slug: string;
}) => { }) => {
const account = useAccount();
const queryClient = useQueryClient(); const queryClient = useQueryClient();
const { mutate } = useMutation({ const { mutate } = useMutation({
mutationFn: (seconds: number) => mutationFn: (seconds: number) =>
@ -56,6 +57,7 @@ export const WatchStatusObserver = ({
// update watch status every 10 seconds and on unmount. // update watch status every 10 seconds and on unmount.
useEffect(() => { useEffect(() => {
if (!account) return;
const timer = setInterval(() => { const timer = setInterval(() => {
mutate(readProgress()); mutate(readProgress());
}, 10_000); }, 10_000);
@ -63,12 +65,13 @@ export const WatchStatusObserver = ({
clearInterval(timer); clearInterval(timer);
mutate(readProgress()); mutate(readProgress());
}; };
}, [type, slug, readProgress, mutate]); }, [account, type, slug, readProgress, mutate]);
// update watch status when play status change (and on mount). // update watch status when play status change (and on mount).
const isPlaying = useAtomValue(playAtom); const isPlaying = useAtomValue(playAtom);
useEffect(() => { useEffect(() => {
if (!account) return;
mutate(readProgress()); mutate(readProgress());
}, [type, slug, isPlaying, readProgress, mutate]); }, [account, type, slug, isPlaying, readProgress, mutate]);
return null; return null;
}; };