103 lines
2.3 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
}
2023-11-19 20:03:35 +01:00
export interface BookmarkUpdateItem {
tmdbId: string;
title?: string;
year?: number;
id: string;
poster?: string;
type?: "show" | "movie";
action: "delete" | "add";
}
export interface BookmarkStore {
2023-10-17 16:14:46 +02:00
bookmarks: Record<string, BookmarkMediaItem>;
2023-11-19 20:03:35 +01:00
updateQueue: BookmarkUpdateItem[];
2023-10-17 16:14:46 +02:00
addBookmark(meta: PlayerMeta): void;
removeBookmark(id: string): void;
replaceBookmarks(items: Record<string, BookmarkMediaItem>): void;
clear(): void;
2023-11-19 20:03:35 +01:00
clearUpdateQueue(): void;
removeUpdateItem(id: string): void;
2023-10-17 16:14:46 +02:00
}
2023-11-19 20:03:35 +01:00
let updateId = 0;
2023-10-17 16:14:46 +02:00
export const useBookmarkStore = create(
persist(
immer<BookmarkStore>((set) => ({
2023-10-17 16:14:46 +02:00
bookmarks: {},
2023-11-19 20:03:35 +01:00
updateQueue: [],
2023-10-17 16:14:46 +02:00
removeBookmark(id) {
set((s) => {
2023-11-19 20:03:35 +01:00
updateId += 1;
s.updateQueue.push({
id: updateId.toString(),
action: "delete",
tmdbId: id,
});
2023-10-17 16:14:46 +02:00
delete s.bookmarks[id];
});
},
addBookmark(meta) {
set((s) => {
2023-11-19 20:03:35 +01:00
updateId += 1;
s.updateQueue.push({
id: updateId.toString(),
action: "add",
tmdbId: meta.tmdbId,
type: meta.type,
title: meta.title,
year: meta.releaseYear,
poster: meta.poster,
});
2023-10-17 16:14:46 +02:00
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;
});
},
clear() {
set((s) => {
s.bookmarks = {};
});
},
2023-11-19 20:03:35 +01:00
clearUpdateQueue() {
set((s) => {
s.updateQueue = [];
});
},
removeUpdateItem(id: string) {
set((s) => {
s.updateQueue = [...s.updateQueue.filter((v) => v.id !== id)];
});
},
2023-10-17 16:14:46 +02:00
})),
{
name: "__MW::bookmarks",
},
),
2023-10-17 16:14:46 +02:00
);