feat: watch history store

This commit is contained in:
Adrian Castro
2024-03-27 11:26:41 +01:00
parent e691425248
commit fa2425c183
4 changed files with 705 additions and 68 deletions

View File

@@ -7,6 +7,7 @@ import { createJSONStorage, persist } from "zustand/middleware";
import type { ItemData } from "~/components/item/item";
import type { DownloadItem } from "~/hooks/DownloadManagerContext";
import type { ThemeStoreOption } from "~/stores/theme";
import type { ScrapeMedia } from "@movie-web/provider-utils";
const storage = new MMKV();
@@ -97,6 +98,7 @@ export const useDownloadHistoryStore = create<
interface BookmarkStoreState {
bookmarks: ItemData[];
setBookmarks: (bookmarks: ItemData[]) => void;
addBookmark: (item: ItemData) => void;
removeBookmark: (item: ItemData) => void;
isBookmarked: (item: ItemData) => boolean;
@@ -109,6 +111,7 @@ export const useBookmarkStore = create<
persist(
(set, get) => ({
bookmarks: [],
setBookmarks: (bookmarks: ItemData[]) => set({ bookmarks }),
addBookmark: (item: ItemData) =>
set((state) => ({
bookmarks: [...state.bookmarks, item],
@@ -128,3 +131,52 @@ export const useBookmarkStore = create<
},
),
);
interface WatchHistoryItem {
item: ItemData;
media: ScrapeMedia;
positionMillis: number;
}
interface WatchHistoryStoreState {
watchHistory: WatchHistoryItem[];
setWatchHistory: (watchHistory: WatchHistoryItem[]) => void;
addToWatchHistory: (item: ItemData, media: ScrapeMedia) => void;
removeFromWatchHistory: (item: ItemData) => void;
}
export const useWatchHistoryStore = create<
WatchHistoryStoreState,
[["zustand/persist", WatchHistoryStoreState]]
>(
persist(
(set) => ({
watchHistory: [],
setWatchHistory: (watchHistory: WatchHistoryItem[]) =>
set({ watchHistory }),
addToWatchHistory: (item: ItemData, media: ScrapeMedia) =>
set((state) => ({
watchHistory: [
...state.watchHistory.filter(
(historyItem) => historyItem.item.id !== item.id,
),
{
item,
media,
positionMillis: 0,
},
],
})),
removeFromWatchHistory: (item: ItemData) =>
set((state) => ({
watchHistory: state.watchHistory.filter(
(historyItem) => historyItem.item.id !== item.id,
),
}),
)}),
{
name: "watch-history",
storage: createJSONStorage(() => zustandStorage),
},
),
);