-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuseDashboard.ts
More file actions
164 lines (145 loc) · 4.27 KB
/
Copy pathuseDashboard.ts
File metadata and controls
164 lines (145 loc) · 4.27 KB
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import { useQuery, useQueryClient } from '@tanstack/react-query';
import { useAuthStore } from '@/stores/authStore';
import { useMessStore } from '@/stores/messStore';
import {
dashboardService,
AdminStats,
ManagerSummary,
MemberSummary,
QuickStats,
PaginatedResponse,
} from '../services/dashboardService';
/**
* useDashboard Hook
* Manages dashboard data fetching and caching
*/
export const useDashboard = () => {
const authStore = useAuthStore();
const messStore = useMessStore();
const queryClient = useQueryClient();
/**
* Fetch admin stats
*/
const adminStatsQuery = useQuery<AdminStats>({
queryKey: ['dashboard', 'admin-stats'],
queryFn: () => dashboardService.getAdminStats(),
enabled: authStore.user?.id !== undefined,
staleTime: 1000 * 60 * 5, // 5 minutes
});
/**
* Fetch manager summary
*/
const managerSummaryQuery = useQuery<ManagerSummary>({
queryKey: ['dashboard', 'manager-summary', messStore.messId],
queryFn: () => dashboardService.getManagerSummary(messStore.messId!),
enabled: messStore.messId !== null,
staleTime: 1000 * 60 * 5,
});
/**
* Fetch members summary with pagination
*/
const membersSummaryQuery = useQuery<PaginatedResponse<MemberSummary>>({
queryKey: ['dashboard', 'members-summary', messStore.messId],
queryFn: () => dashboardService.getMembersSummary(messStore.messId!, 20),
enabled: messStore.messId !== null,
staleTime: 1000 * 60 * 5,
});
/**
* Fetch quick stats
*/
const quickStatsQuery = useQuery<QuickStats>({
queryKey: ['dashboard', 'quick-stats', messStore.messId],
queryFn: () => dashboardService.getQuickStats(messStore.messId!),
enabled: messStore.messId !== null,
staleTime: 1000 * 60 * 5,
});
/**
* Fetch recent activities with pagination
*/
const recentActivitiesQuery = useQuery<PaginatedResponse<any>>({
queryKey: ['dashboard', 'recent-activities', messStore.messId],
queryFn: () => dashboardService.getRecentActivities(messStore.messId!, 20),
enabled: messStore.messId !== null,
staleTime: 1000 * 60 * 5,
});
/**
* Refetch all dashboard data
*/
const refetchAll = async () => {
await Promise.all([
adminStatsQuery.refetch(),
managerSummaryQuery.refetch(),
membersSummaryQuery.refetch(),
quickStatsQuery.refetch(),
recentActivitiesQuery.refetch(),
]);
};
/**
* Load more members
*/
const loadMoreMembers = async () => {
const currentData = membersSummaryQuery.data;
if (!currentData?.cursor || !messStore.messId) return;
const newData = await dashboardService.getMembersSummary(
messStore.messId,
20,
currentData.cursor,
);
queryClient.setQueryData(
['dashboard', 'members-summary', messStore.messId],
{
...newData,
data: [...(currentData.data || []), ...newData.data],
},
);
};
/**
* Load more activities
*/
const loadMoreActivities = async () => {
const currentData = recentActivitiesQuery.data;
if (!currentData?.cursor || !messStore.messId) return;
const newData = await dashboardService.getRecentActivities(
messStore.messId,
20,
currentData.cursor,
);
queryClient.setQueryData(
['dashboard', 'recent-activities', messStore.messId],
{
...newData,
data: [...(currentData.data || []), ...newData.data],
},
);
};
const isLoading =
adminStatsQuery.isLoading ||
managerSummaryQuery.isLoading ||
membersSummaryQuery.isLoading ||
quickStatsQuery.isLoading ||
recentActivitiesQuery.isLoading;
const error =
adminStatsQuery.error ||
managerSummaryQuery.error ||
membersSummaryQuery.error ||
quickStatsQuery.error ||
recentActivitiesQuery.error;
return {
// Data
adminStats: adminStatsQuery.data,
managerSummary: managerSummaryQuery.data,
members: membersSummaryQuery.data?.data || [],
quickStats: quickStatsQuery.data,
recentActivities: recentActivitiesQuery.data?.data || [],
// Pagination
membersHasMore: membersSummaryQuery.data?.hasMore || false,
activitiesHasMore: recentActivitiesQuery.data?.hasMore || false,
// Status
isLoading,
error,
// Actions
refetchAll,
loadMoreMembers,
loadMoreActivities,
};
};