1+ import  {  Box ,  Card ,  Grid ,  Text ,  Title ,  Group ,  Stack  }  from  '@mantine/core' ; 
2+ import  {  IconActivity ,  IconAlertTriangle ,  IconCalendar ,  IconChartBar  }  from  '@tabler/icons-react' ; 
3+ import  {  BarChart ,  Bar ,  XAxis ,  YAxis ,  CartesianGrid ,  Tooltip ,  ResponsiveContainer ,  PieChart ,  Pie ,  Cell  }  from  'recharts' ; 
4+ import  useQueryDashboard  from  '../hooks/useQueryDashboard' ; 
5+ 
6+ const  LEVEL_COLORS  =  { 
7+   Verbose : '#868e96' , 
8+   Debug : '#4c6ef5' , 
9+   Information : '#12b886' , 
10+   Warning : '#fd7e14' , 
11+   Error : '#fa5252' , 
12+   Fatal : '#c92a2a' , 
13+   Unknown : '#adb5bd' , 
14+ } ; 
15+ 
16+ const  StatCard  =  ( {  icon,  title,  value,  color } : {  
17+   icon : React . ReactNode ;  
18+   title : string ;  
19+   value : number ;  
20+   color : string ; 
21+ } )  =>  ( 
22+   < Card  shadow = "sm"  padding = "lg"  radius = "md"  withBorder > 
23+     < Group  justify = "space-between" > 
24+       < Stack  gap = "xs" > 
25+         < Text  size = "sm"  c = "dimmed" > 
26+           { title } 
27+         </ Text > 
28+         < Text  size = "xl"  fw = { 700 }  c = { color } > 
29+           { value . toLocaleString ( ) } 
30+         </ Text > 
31+       </ Stack > 
32+       < Box  c = { color } > 
33+         { icon } 
34+       </ Box > 
35+     </ Group > 
36+   </ Card > 
37+ ) ; 
38+ 
39+ export  const  Dashboard  =  ( )  =>  { 
40+   const  {  data : dashboard ,  isLoading,  error }  =  useQueryDashboard ( ) ; 
41+ 
42+   if  ( isLoading )  { 
43+     return  ( 
44+       < Box  p = "md" > 
45+         < Text > Loading dashboard...</ Text > 
46+       </ Box > 
47+     ) ; 
48+   } 
49+ 
50+   if  ( error  ||  ! dashboard )  { 
51+     return  ( 
52+       < Box  p = "md" > 
53+         < Text  c = "red" > Error loading dashboard data</ Text > 
54+       </ Box > 
55+     ) ; 
56+   } 
57+ 
58+   // Prepare data for charts 
59+   const  levelData  =  Object . entries ( dashboard . logsByLevel ) . map ( ( [ level ,  count ] )  =>  ( { 
60+     level, 
61+     count, 
62+     color : LEVEL_COLORS [ level  as  keyof  typeof  LEVEL_COLORS ]  ||  LEVEL_COLORS . Unknown , 
63+   } ) ) ; 
64+ 
65+   return  ( 
66+     < Box  p = "md" > 
67+       < Title  order = { 2 }  mb = "lg" > 
68+         Log Dashboard
69+       </ Title > 
70+ 
71+       { /* Stats Cards */ } 
72+       < Grid  gutter = "md"  mb = "xl" > 
73+         < Grid . Col  span = { {  base : 12 ,  md : 3  } } > 
74+           < StatCard 
75+             icon = { < IconChartBar  size = { 32 }  /> } 
76+             title = "Total Logs" 
77+             value = { dashboard . totalLogs } 
78+             color = "blue" 
79+           /> 
80+         </ Grid . Col > 
81+         < Grid . Col  span = { {  base : 12 ,  md : 3  } } > 
82+           < StatCard 
83+             icon = { < IconCalendar  size = { 32 }  /> } 
84+             title = "Today's Logs" 
85+             value = { dashboard . todayLogs } 
86+             color = "green" 
87+           /> 
88+         </ Grid . Col > 
89+         < Grid . Col  span = { {  base : 12 ,  md : 3  } } > 
90+           < StatCard 
91+             icon = { < IconAlertTriangle  size = { 32 }  /> } 
92+             title = "Today's Errors" 
93+             value = { dashboard . todayErrorLogs } 
94+             color = "red" 
95+           /> 
96+         </ Grid . Col > 
97+         < Grid . Col  span = { {  base : 12 ,  md : 3  } } > 
98+           < StatCard 
99+             icon = { < IconActivity  size = { 32 }  /> } 
100+             title = "Log Levels" 
101+             value = { Object . keys ( dashboard . logsByLevel ) . length } 
102+             color = "grape" 
103+           /> 
104+         </ Grid . Col > 
105+       </ Grid > 
106+ 
107+       { /* Charts */ } 
108+       < Grid  gutter = "md" > 
109+         < Grid . Col  span = { {  base : 12 ,  md : 8  } } > 
110+           < Card  shadow = "sm"  padding = "lg"  radius = "md"  withBorder > 
111+             < Title  order = { 4 }  mb = "md" > 
112+               Logs by Level
113+             </ Title > 
114+             < Box  h = { 300 } > 
115+               < ResponsiveContainer  width = "100%"  height = "100%" > 
116+                 < BarChart  data = { levelData } > 
117+                   < CartesianGrid  strokeDasharray = "3 3"  /> 
118+                   < XAxis  dataKey = "level"  /> 
119+                   < YAxis  /> 
120+                   < Tooltip  /> 
121+                   < Bar  dataKey = "count"  fill = "#4c6ef5"  /> 
122+                 </ BarChart > 
123+               </ ResponsiveContainer > 
124+             </ Box > 
125+           </ Card > 
126+         </ Grid . Col > 
127+         < Grid . Col  span = { {  base : 12 ,  md : 4  } } > 
128+           < Card  shadow = "sm"  padding = "lg"  radius = "md"  withBorder > 
129+             < Title  order = { 4 }  mb = "md" > 
130+               Level Distribution
131+             </ Title > 
132+             < Box  h = { 300 } > 
133+               < ResponsiveContainer  width = "100%"  height = "100%" > 
134+                 < PieChart > 
135+                   < Pie 
136+                     data = { levelData } 
137+                     cx = "50%" 
138+                     cy = "50%" 
139+                     labelLine = { false } 
140+                     label = { ( {  level,  percent } )  =>  `${ level } ${ ( ( percent  ||  0 )  *  100 ) . toFixed ( 0 ) }  } 
141+                     outerRadius = { 80 } 
142+                     fill = "#8884d8" 
143+                     dataKey = "count" 
144+                   > 
145+                     { levelData . map ( ( entry ,  index )  =>  ( 
146+                       < Cell  key = { `cell-${ index }  }  fill = { entry . color }  /> 
147+                     ) ) } 
148+                   </ Pie > 
149+                   < Tooltip  /> 
150+                 </ PieChart > 
151+               </ ResponsiveContainer > 
152+             </ Box > 
153+           </ Card > 
154+         </ Grid . Col > 
155+       </ Grid > 
156+     </ Box > 
157+   ) ; 
158+ } ; 
159+ 
160+ export  default  Dashboard ; 
0 commit comments