2023-11-19 20:03:35 +01:00
|
|
|
import { useEffect } from "react";
|
|
|
|
|
|
|
|
import { addBookmark, removeBookmark } from "@/backend/accounts/bookmarks";
|
|
|
|
import { useBackendUrl } from "@/hooks/auth/useBackendUrl";
|
|
|
|
import { AccountWithToken, useAuthStore } from "@/stores/auth";
|
|
|
|
import { BookmarkUpdateItem, useBookmarkStore } from "@/stores/bookmarks";
|
|
|
|
|
|
|
|
const syncIntervalMs = 5 * 1000;
|
|
|
|
|
|
|
|
async function syncBookmarks(
|
|
|
|
items: BookmarkUpdateItem[],
|
|
|
|
finish: (id: string) => void,
|
|
|
|
url: string,
|
2023-12-23 06:24:43 +01:00
|
|
|
account: AccountWithToken | null,
|
2023-11-19 20:03:35 +01:00
|
|
|
) {
|
|
|
|
for (const item of items) {
|
|
|
|
// complete it beforehand so it doesn't get handled while in progress
|
|
|
|
finish(item.id);
|
|
|
|
|
2023-11-24 17:11:00 +01:00
|
|
|
if (!account) continue; // not logged in, dont sync to server
|
2023-11-19 20:03:35 +01:00
|
|
|
|
|
|
|
try {
|
|
|
|
if (item.action === "delete") {
|
|
|
|
await removeBookmark(url, account, item.tmdbId);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (item.action === "add") {
|
|
|
|
await addBookmark(url, account, {
|
2023-11-21 21:26:26 +01:00
|
|
|
meta: {
|
|
|
|
poster: item.poster,
|
|
|
|
title: item.title ?? "",
|
|
|
|
type: item.type ?? "",
|
|
|
|
year: item.year ?? NaN,
|
|
|
|
},
|
2023-11-19 20:03:35 +01:00
|
|
|
tmdbId: item.tmdbId,
|
|
|
|
});
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
console.error(
|
|
|
|
`Failed to sync bookmark: ${item.tmdbId} - ${item.action}`,
|
2023-12-23 06:24:43 +01:00
|
|
|
err,
|
2023-11-19 20:03:35 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export function BookmarkSyncer() {
|
|
|
|
const clearUpdateQueue = useBookmarkStore((s) => s.clearUpdateQueue);
|
|
|
|
const removeUpdateItem = useBookmarkStore((s) => s.removeUpdateItem);
|
|
|
|
const url = useBackendUrl();
|
|
|
|
|
|
|
|
// when booting for the first time, clear update queue.
|
|
|
|
// we dont want to process persisted update items
|
|
|
|
useEffect(() => {
|
|
|
|
clearUpdateQueue();
|
|
|
|
}, [clearUpdateQueue]);
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
const interval = setInterval(() => {
|
|
|
|
(async () => {
|
|
|
|
const state = useBookmarkStore.getState();
|
|
|
|
const user = useAuthStore.getState();
|
|
|
|
await syncBookmarks(
|
|
|
|
state.updateQueue,
|
|
|
|
removeUpdateItem,
|
|
|
|
url,
|
2023-12-23 06:24:43 +01:00
|
|
|
user.account,
|
2023-11-19 20:03:35 +01:00
|
|
|
);
|
|
|
|
})();
|
|
|
|
}, syncIntervalMs);
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
clearInterval(interval);
|
|
|
|
};
|
|
|
|
}, [removeUpdateItem, url]);
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|