53 lines
1.2 KiB
TypeScript
Raw Normal View History

2023-10-17 16:14:46 +02:00
import { create } from "zustand";
import { persist } from "zustand/middleware";
import { immer } from "zustand/middleware/immer";
import { PlayerMeta } from "@/stores/player/slices/source";
export interface BookmarkMediaItem {
title: string;
year?: number;
2023-10-17 16:14:46 +02:00
poster?: string;
type: "show" | "movie";
updatedAt: number;
2023-10-17 16:14:46 +02:00
}
export interface ProgressStore {
bookmarks: Record<string, BookmarkMediaItem>;
addBookmark(meta: PlayerMeta): void;
removeBookmark(id: string): void;
replaceBookmarks(items: Record<string, BookmarkMediaItem>): void;
2023-10-17 16:14:46 +02:00
}
export const useBookmarkStore = create(
persist(
immer<ProgressStore>((set) => ({
bookmarks: {},
removeBookmark(id) {
set((s) => {
delete s.bookmarks[id];
});
},
addBookmark(meta) {
set((s) => {
s.bookmarks[meta.tmdbId] = {
type: meta.type,
title: meta.title,
year: meta.releaseYear,
poster: meta.poster,
updatedAt: Date.now(),
2023-10-17 16:14:46 +02:00
};
});
},
replaceBookmarks(items: Record<string, BookmarkMediaItem>) {
set((s) => {
s.bookmarks = items;
});
},
2023-10-17 16:14:46 +02:00
})),
{
name: "__MW::bookmarks",
}
)
);