mirror of
https://github.com/movie-web/native-app.git
synced 2025-09-13 16:33:26 +00:00
feat: bookmark store
This commit is contained in:
@@ -4,6 +4,7 @@ import { MMKV } from "react-native-mmkv";
|
|||||||
import { create } from "zustand";
|
import { create } from "zustand";
|
||||||
import { createJSONStorage, persist } from "zustand/middleware";
|
import { createJSONStorage, persist } from "zustand/middleware";
|
||||||
|
|
||||||
|
import type { ItemData } from "~/components/item/item";
|
||||||
import type { DownloadItem } from "~/hooks/DownloadManagerContext";
|
import type { DownloadItem } from "~/hooks/DownloadManagerContext";
|
||||||
import type { ThemeStoreOption } from "~/stores/theme";
|
import type { ThemeStoreOption } from "~/stores/theme";
|
||||||
|
|
||||||
@@ -93,3 +94,37 @@ export const useDownloadHistoryStore = create<
|
|||||||
},
|
},
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
|
|
||||||
|
interface BookmarkStoreState {
|
||||||
|
bookmarks: ItemData[];
|
||||||
|
addBookmark: (item: ItemData) => void;
|
||||||
|
removeBookmark: (item: ItemData) => void;
|
||||||
|
isBookmarked: (item: ItemData) => boolean;
|
||||||
|
}
|
||||||
|
|
||||||
|
export const useBookmarkStore = create<
|
||||||
|
BookmarkStoreState,
|
||||||
|
[["zustand/persist", BookmarkStoreState]]
|
||||||
|
>(
|
||||||
|
persist(
|
||||||
|
(set, get) => ({
|
||||||
|
bookmarks: [],
|
||||||
|
addBookmark: (item: ItemData) =>
|
||||||
|
set((state) => ({
|
||||||
|
bookmarks: [...state.bookmarks, item],
|
||||||
|
})),
|
||||||
|
removeBookmark: (item: ItemData) =>
|
||||||
|
set((state) => ({
|
||||||
|
bookmarks: state.bookmarks.filter(
|
||||||
|
(bookmark) => bookmark.id !== item.id,
|
||||||
|
),
|
||||||
|
})),
|
||||||
|
isBookmarked: (item: ItemData) =>
|
||||||
|
Boolean(get().bookmarks.find((bookmark) => bookmark.id === item.id)),
|
||||||
|
}),
|
||||||
|
{
|
||||||
|
name: "bookmarks",
|
||||||
|
storage: createJSONStorage(() => zustandStorage),
|
||||||
|
},
|
||||||
|
),
|
||||||
|
);
|
||||||
|
Reference in New Issue
Block a user