fix(admin-web): handle undefined data in dashboard hooks

Add null-safe access and fallback to empty arrays to prevent
"Cannot read properties of undefined" errors when API returns
unexpected data structure.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
hailin 2026-01-02 19:05:54 -08:00
parent 2e7de8a1ef
commit 8c8a049f77
1 changed files with 6 additions and 3 deletions

View File

@ -40,7 +40,8 @@ export function useDashboardStats() {
queryKey: dashboardKeys.stats(),
queryFn: async () => {
const response = await dashboardService.getStats();
return response.data.stats;
// 确保返回数组,即使 API 返回 undefined 或非数组
return response?.data?.stats ?? [];
},
staleTime: 30 * 1000,
gcTime: 5 * 60 * 1000,
@ -71,7 +72,8 @@ export function useDashboardRegion() {
queryKey: dashboardKeys.region(),
queryFn: async () => {
const response = await dashboardService.getRegionDistribution();
return response.data.regions;
// 确保返回数组
return response?.data?.regions ?? [];
},
staleTime: 5 * 60 * 1000, // 5分钟后标记为过期
gcTime: 10 * 60 * 1000,
@ -87,7 +89,8 @@ export function useDashboardActivities(limit = 5) {
queryKey: dashboardKeys.activities(limit),
queryFn: async () => {
const response = await dashboardService.getRecentActivities(limit);
return response.data.activities;
// 确保返回数组
return response?.data?.activities ?? [];
},
staleTime: 30 * 1000, // 30秒后标记为过期
gcTime: 5 * 60 * 1000,