-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzustandStore.js
99 lines (85 loc) · 3.01 KB
/
zustandStore.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import { create } from 'zustand';
import fetchStarred from './components/fetchStarred.js';
import fetchVisitHistory from './components/fetchVisitHistory.js';
import fetchBeingWatched from './components/fetchBeingWatched.js';
import fetchUnlockedSongs from './components/fetchUnlockedSongs.js';
const useStore = create((set, get) => ({
maximumObjects: 15,
objectLimit: 15,
groupMax: 4,
starredCount: 0,
visitHistoryCount: 0,
visitHistory: [],
fetchAndSetVisitHistory: async (userId, groupMax, ip) => {
try {
const { data, count } = await fetchVisitHistory(userId, groupMax, ip);
set(state => ({
visitHistory: data,
visitHistoryCount: count,
}));
// Explicitly call recalculateObjectLimit after setting new data
get().recalculateObjectLimit();
} catch (error) {
console.error('Failed to fetch visit history:', error);
}
},
starred: [],
fetchAndSetStarred: async (userId, groupMax, ip) => {
try {
const { data, count } = await fetchStarred(userId, groupMax, ip);
set(state => ({
starred: data,
starredCount: count,
}));
// Explicitly call recalculateObjectLimit after setting new data
get().recalculateObjectLimit();
} catch (error) {
console.error('Failed to fetch starred:', error);
}
},
beingWatched: [],
fetchAndSetBeingWatched: async (userId, ip, objectLimit) => {
if (objectLimit > 0) {
try {
const { data, count } = await fetchBeingWatched(userId, ip, objectLimit);
set({ beingWatched: data });
} catch (error) {
console.error('Failed to fetch being watched:', error);
}
}
},
unlockedSongs: [],
fetchAndSetUnlockedSongs: async (userId) => {
try {
const data = await fetchUnlockedSongs(userId); // Assuming fetchUnlockedSongs no longer returns count
set({ unlockedSongs: data });
} catch (error) {
console.error('Failed to fetch unlocked songs:', error);
}
},
// Method to recalculate the object limit based on the current counts
recalculateObjectLimit: () => set(state => {
const usedSpace = state.starredCount + state.visitHistoryCount;
const remainingSpace = state.maximumObjects - usedSpace;
return { objectLimit: Math.max(remainingSpace, 0) };
}),
refreshData: async (userId, ip) => {
await get().refreshVisitHistory(userId, ip);
await get().refreshStarred(userId, ip);
// Note: BeingWatched does not affect objectLimit calculation directly
await get().refreshBeingWatched(userId, ip);
},
refreshVisitHistory: async (userId, ip) => {
await get().fetchAndSetVisitHistory(userId, get().groupMax, ip);
},
refreshStarred: async (userId, ip) => {
await get().fetchAndSetStarred(userId, get().groupMax, ip);
},
refreshBeingWatched: async (userId, ip) => {
await get().fetchAndSetBeingWatched(userId, ip, get().objectLimit);
},
refreshUnlockedSongs: async (userId) => {
await get().fetchAndSetUnlockedSongs(userId);
},
}));
export default useStore;