mirror of
https://github.com/movie-web/native-app.git
synced 2025-09-13 12:43:25 +00:00
feat: watch history store
This commit is contained in:
@@ -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),
|
||||
},
|
||||
),
|
||||
);
|
Reference in New Issue
Block a user