Implemented the new react version of the frontend
This commit is contained in:
1 parent
236e6e06fb
commit
ffe7e52527
65 files changed
+5228
-177
No files matched your search
@@ -0,0 +1,59 @@
|
||||
import { create } from 'zustand';
|
||||
import type { Entry } from '@/types';
|
||||
|
||||
interface LibraryState {
|
||||
entries: Map<number, Entry>;
|
||||
isLoaded: boolean;
|
||||
|
||||
// Actions
|
||||
setEntries: (entries: Entry[]) => void;
|
||||
addEntry: (entry: Entry) => void;
|
||||
updateEntry: (id: number, data: Partial<Entry>) => void;
|
||||
removeEntry: (id: number) => void;
|
||||
}
|
||||
|
||||
export const useLibraryStore = create<LibraryState>((set) => ({
|
||||
entries: new Map(),
|
||||
isLoaded: false,
|
||||
|
||||
setEntries: (entries) => {
|
||||
const map = new Map<number, Entry>();
|
||||
entries.forEach((entry) => {
|
||||
map.set(entry.id, { ...entry, isInLibrary: true });
|
||||
});
|
||||
set({ entries: map, isLoaded: true });
|
||||
},
|
||||
|
||||
addEntry: (entry) => {
|
||||
set((state) => {
|
||||
const newEntries = new Map(state.entries);
|
||||
newEntries.set(entry.id, { ...entry, isInLibrary: true });
|
||||
return { entries: newEntries };
|
||||
});
|
||||
},
|
||||
|
||||
updateEntry: (id, data) => {
|
||||
set((state) => {
|
||||
const existing = state.entries.get(id);
|
||||
if (!existing) return state;
|
||||
const newEntries = new Map(state.entries);
|
||||
newEntries.set(id, { ...existing, ...data });
|
||||
return { entries: newEntries };
|
||||
});
|
||||
},
|
||||
|
||||
removeEntry: (id) => {
|
||||
set((state) => {
|
||||
const newEntries = new Map(state.entries);
|
||||
newEntries.delete(id);
|
||||
return { entries: newEntries };
|
||||
});
|
||||
},
|
||||
}));
|
||||
|
||||
// Derived selectors — call these outside of Zustand selectors
|
||||
// Use with: const entries = useLibraryEntries();
|
||||
export function useLibraryEntries() {
|
||||
const entries = useLibraryStore((s) => s.entries);
|
||||
return Array.from(entries.values());
|
||||
}
|
||||
Reference in new issue
Block a user