@@ -50,25 +50,36 @@ export default function GamificationAnalytics() {
5050 queryFn : ( ) => base44 . entities . Recognition . list ( '-created_date' , 1000 ) ,
5151 } ) ;
5252
53- const isLoading = userLoading || pointsLoading || redemptionsLoading || ledgerLoading || tiersLoading || recognitionsLoading ;
53+ const isLoading = userLoading || pointsLoading || redemptionsLoading || ledgerLoading || tiersLoading || recognitionsLoading || badgesLoading || chalLoading ;
5454
5555 if ( isLoading ) {
5656 return < LoadingSpinner message = "Loading analytics..." /> ;
5757 }
5858
59+ // Null-safe data
60+ const safeUserPoints = allUserPoints || [ ] ;
61+ const safeBadgeAwards = allBadgeAwards || [ ] ;
62+ const safeChallengeParticipations = allChallengeParticipations || [ ] ;
63+ const safeRedemptions = allRedemptions || [ ] ;
64+
5965 // Calculate key metrics
60- const totalUsers = allUserPoints ?. length || 0 ;
61- const totalPoints = allUserPoints ?. reduce ( ( sum , up ) => sum + up . total_points , 0 ) || 0 ;
62- const avgPointsPerUser = totalUsers > 0 ? Math . round ( totalPoints / totalUsers ) : 0 ;
63- const totalRedemptions = allRedemptions ?. length || 0 ;
64- const completedRedemptions = allRedemptions ?. filter ( r => r . status === 'fulfilled' ) ?. length || 0 ;
65- const redemptionRate = totalRedemptions > 0 ? Math . round ( ( completedRedemptions / totalRedemptions ) * 100 ) : 0 ;
66-
67- // Users by tier
68- const usersByTier = { } ;
69- allTiers ?. forEach ( tier => {
70- usersByTier [ tier . tier_name ] = allUserPoints ?. filter ( up => up . current_tier_id === tier . id ) . length || 0 ;
71- } ) ;
66+ const totalPointsDistributed = safeUserPoints . reduce ( ( sum , up ) => sum + ( up . total_points || 0 ) , 0 ) || 0 ;
67+ const totalBadgesAwarded = safeBadgeAwards . length || 0 ;
68+ const totalChallengeJoins = safeChallengeParticipations . length || 0 ;
69+
70+ // Empty state check
71+ const hasData = safeUserPoints . length > 0 || safeBadgeAwards . length > 0 || safeChallengeParticipations . length > 0 ;
72+
73+ // Tier distribution for chart
74+ const tierDistributionData = ( allTiers || [ ] ) . map ( tier => {
75+ const pointsForTier = safeUserPoints
76+ . filter ( up => up . tier === tier . tier_name )
77+ . reduce ( ( sum , up ) => sum + ( up . total_points || 0 ) , 0 ) || 0 ;
78+ return {
79+ tier : tier . tier_name || 'Unknown' ,
80+ points : pointsForTier
81+ } ;
82+ } ) . filter ( d => d . points > 0 ) ;
7283
7384 return (
7485 < div className = "space-y-6" >
0 commit comments