-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathFeedbackAnalytics.tsx
More file actions
363 lines (332 loc) · 13.5 KB
/
Copy pathFeedbackAnalytics.tsx
File metadata and controls
363 lines (332 loc) · 13.5 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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
'use client';
import React, { useState, useEffect } from 'react';
import { FeedbackAnalytics as FeedbackAnalyticsType } from '../types';
import { AnalyticsService } from '../services/analyticsService';
import { useTheme } from '@/contexts/ui/ThemeContext';
interface FeedbackAnalyticsProps {
className?: string;
}
export const FeedbackAnalytics: React.FC<FeedbackAnalyticsProps> = ({ className = '' }) => {
const { theme } = useTheme();
const [feedbackData, setFeedbackData] = useState<FeedbackAnalyticsType | null>(null);
const [loading, setLoading] = useState(true);
const [error, setError] = useState<string | null>(null);
// Theme classes
const getThemeClasses = () => {
if (theme === 'light') {
return {
container:
'bg-gradient-to-br from-white to-gray-50 border-gray-200/50 text-gray-800 backdrop-blur-sm rounded-2xl shadow-2xl',
card: 'bg-gradient-to-br from-white to-gray-50 border-gray-200/50 hover:from-gray-50 hover:to-gray-100 backdrop-blur-sm rounded-xl shadow-lg',
text: 'text-gray-800',
textSecondary: 'text-gray-600',
textMuted: 'text-gray-500',
};
} else {
return {
container:
'bg-gradient-to-br from-slate-900 to-slate-800 border-white/20 text-white backdrop-blur-sm rounded-2xl shadow-2xl',
card: 'bg-gradient-to-br from-slate-800/60 to-slate-900/60 border-white/20 hover:from-slate-700/60 hover:to-slate-800/60 backdrop-blur-sm rounded-xl shadow-lg',
text: 'text-white',
textSecondary: 'text-gray-300',
textMuted: 'text-gray-400',
};
}
};
const themeClasses = getThemeClasses();
const [selectedDemo, setSelectedDemo] = useState<string>('all');
useEffect(() => {
loadFeedbackAnalytics();
}, []);
const loadFeedbackAnalytics = async () => {
try {
setLoading(true);
setError(null);
const data = await AnalyticsService.getFeedbackAnalytics();
setFeedbackData(data);
} catch (err) {
console.error('Error loading feedback analytics:', err);
setError('Failed to load feedback analytics');
} finally {
setLoading(false);
}
};
if (loading) {
return (
<div className={`p-6 ${themeClasses.container} ${className}`}>
<div className='animate-pulse space-y-4'>
<div
className={`h-6 ${theme === 'light' ? 'bg-gray-200' : 'bg-white/10'} rounded w-1/3`}
></div>
<div className='grid grid-cols-2 md:grid-cols-4 gap-4'>
{[...Array(8)].map((_, i) => (
<div
key={i}
className={`h-20 ${theme === 'light' ? 'bg-gray-200' : 'bg-white/10'} rounded`}
></div>
))}
</div>
</div>
</div>
);
}
if (error) {
return (
<div className={`p-6 ${themeClasses.container} ${className}`}>
<div className='text-center'>
<p className='text-red-500 mb-4'>{error}</p>
<button
onClick={loadFeedbackAnalytics}
className='px-4 py-2 bg-blue-500/20 border border-blue-400/50 text-blue-600 rounded-lg hover:bg-blue-500/30'
>
Retry
</button>
</div>
</div>
);
}
if (!feedbackData) {
return (
<div className={`p-6 ${themeClasses.container} ${className}`}>
<p className={themeClasses.textMuted}>No feedback data available</p>
</div>
);
}
return (
<div className={`space-y-6 ${className}`}>
{/* Overview Stats */}
<div className={`p-6 ${themeClasses.container}`}>
<h2 className={`text-xl font-semibold ${themeClasses.text} mb-6`}>Feedback Overview</h2>
<div className='grid grid-cols-2 md:grid-cols-4 gap-4'>
<StatCard
title='Total Feedback'
value={feedbackData.totalFeedback.toLocaleString()}
icon='💬'
color='blue'
theme={theme}
/>
<StatCard
title='Average Rating'
value={`${feedbackData.averageRating.toFixed(1)}/5`}
icon='⭐'
color='yellow'
theme={theme}
/>
<StatCard
title='Avg Completion Time'
value={`${feedbackData.averageCompletionTime.toFixed(1)} min`}
icon='⏱️'
color='green'
theme={theme}
/>
<StatCard
title='Recommendation Rate'
value={`${feedbackData.recommendationRate.toFixed(1)}%`}
icon='👍'
color='purple'
theme={theme}
/>
</div>
</div>
{/* Rating Distribution */}
<div className={`p-6 ${themeClasses.container}`}>
<h2 className={`text-xl font-semibold ${themeClasses.text} mb-6`}>Rating Distribution</h2>
<div className='space-y-3'>
{Object.entries(feedbackData.ratingDistribution).map(([rating, count]) => {
const percentage =
feedbackData.totalFeedback > 0 ? (count / feedbackData.totalFeedback) * 100 : 0;
return (
<div key={rating} className='flex items-center space-x-4'>
<div className='w-8 text-center'>
<span className={`${themeClasses.text} font-medium`}>{rating}</span>
<span className='text-yellow-400'>⭐</span>
</div>
<div className='flex-1'>
<div
className={`w-full h-4 ${theme === 'light' ? 'bg-gray-200' : 'bg-white/10'} rounded-full overflow-hidden`}
>
<div
className='h-full bg-gradient-to-r from-yellow-400 to-yellow-600 rounded-full'
style={{ width: `${percentage}%` }}
></div>
</div>
</div>
<div className='w-16 text-right'>
<span className={`${themeClasses.text} font-medium`}>{count}</span>
<span className={`${themeClasses.textMuted} text-sm ml-1`}>
({percentage.toFixed(1)}%)
</span>
</div>
</div>
);
})}
</div>
</div>
{/* Difficulty Distribution */}
<div className={`p-6 ${themeClasses.container}`}>
<h2 className={`text-xl font-semibold ${themeClasses.text} mb-6`}>
Difficulty Distribution
</h2>
<div className='grid grid-cols-3 gap-4'>
{Object.entries(feedbackData.difficultyDistribution).map(([difficulty, count]) => {
const percentage =
feedbackData.totalFeedback > 0 ? (count / feedbackData.totalFeedback) * 100 : 0;
const colorClasses = {
easy: 'from-green-400 to-green-600',
medium: 'from-yellow-400 to-yellow-600',
hard: 'from-red-400 to-red-600',
};
return (
<div key={difficulty} className={`p-4 ${themeClasses.card}`}>
<div className='text-center'>
<p className={`text-2xl font-bold ${themeClasses.text}`}>{count}</p>
<p className={`text-sm ${themeClasses.textMuted} capitalize`}>{difficulty}</p>
<div
className={`mt-2 w-full h-2 ${theme === 'light' ? 'bg-gray-200' : 'bg-white/10'} rounded-full overflow-hidden`}
>
<div
className={`h-full bg-gradient-to-r ${colorClasses[difficulty as keyof typeof colorClasses]} rounded-full`}
style={{ width: `${percentage}%` }}
></div>
</div>
<p className={`text-xs ${themeClasses.textMuted} mt-1`}>
{percentage.toFixed(1)}%
</p>
</div>
</div>
);
})}
</div>
</div>
{/* Feedback by Demo */}
<div className={`p-6 ${themeClasses.container}`}>
<h2 className={`text-xl font-semibold ${themeClasses.text} mb-6`}>Feedback by Demo</h2>
<div className='space-y-4'>
{Object.entries(feedbackData.feedbackByDemo).map(([demoId, demoData]) => (
<div
key={demoId}
className={`p-4 ${themeClasses.card} backdrop-blur-sm rounded border`}
>
<div className='flex items-center justify-between mb-3'>
<h3 className={`text-lg font-medium ${themeClasses.text}`}>{demoData.demoName}</h3>
<span className={`text-sm ${themeClasses.textMuted}`}>
{demoData.totalFeedback} feedback
</span>
</div>
<div className='grid grid-cols-2 md:grid-cols-4 gap-4'>
<div className='text-center'>
<p className='text-2xl font-bold text-yellow-400'>
{demoData.averageRating.toFixed(1)}
</p>
<p className={`text-sm ${themeClasses.textMuted}`}>Avg Rating</p>
</div>
<div className='text-center'>
<p className='text-2xl font-bold text-green-400'>
{demoData.averageCompletionTime.toFixed(1)}
</p>
<p className={`text-sm ${themeClasses.textMuted}`}>Avg Time (min)</p>
</div>
<div className='text-center'>
<p className='text-2xl font-bold text-blue-400'>
{demoData.recommendationRate.toFixed(1)}%
</p>
<p className={`text-sm ${themeClasses.textMuted}`}>Recommend</p>
</div>
<div className='text-center'>
<p className='text-2xl font-bold text-purple-400'>{demoData.totalFeedback}</p>
<p className={`text-sm ${themeClasses.textMuted}`}>Total</p>
</div>
</div>
</div>
))}
</div>
</div>
{/* Recent Feedback */}
<div className={`p-6 ${themeClasses.container}`}>
<h2 className={`text-xl font-semibold ${themeClasses.text} mb-6`}>Recent Feedback</h2>
<div className='space-y-3'>
{feedbackData.recentFeedback.slice(0, 10).map(feedback => (
<div
key={feedback.id}
className={`p-4 ${themeClasses.card} backdrop-blur-sm rounded border`}
>
<div className='flex items-start justify-between mb-2'>
<div>
<p className={`${themeClasses.text} font-medium`}>{feedback.demoName}</p>
<p className={`text-sm ${themeClasses.textMuted}`}>
{feedback.userId.slice(0, 8)}...{feedback.userId.slice(-8)}
</p>
</div>
<div className='text-right'>
<div className='flex items-center space-x-1'>
{[...Array(5)].map((_, i) => (
<span
key={i}
className={`text-sm ${
i < feedback.rating ? 'text-yellow-400' : 'text-gray-600'
}`}
>
⭐
</span>
))}
</div>
<p className={`text-xs ${themeClasses.textMuted}`}>
{new Date(feedback.timestamp).toLocaleDateString()}
</p>
</div>
</div>
<p className={`${themeClasses.textSecondary} text-sm mb-2`}>{feedback.feedback}</p>
<div className={`flex items-center space-x-4 text-xs ${themeClasses.textMuted}`}>
<span>Difficulty: {feedback.difficulty}</span>
<span>Time: {feedback.completionTime} min</span>
<span className={feedback.wouldRecommend ? 'text-green-400' : 'text-red-400'}>
{feedback.wouldRecommend ? '👍 Recommends' : '👎 Not recommended'}
</span>
</div>
</div>
))}
</div>
</div>
</div>
);
};
interface StatCardProps {
title: string;
value: string;
icon: string;
color: 'blue' | 'green' | 'purple' | 'yellow' | 'red';
theme: 'light' | 'dark';
}
const StatCard: React.FC<StatCardProps> = ({ title, value, icon, color, theme }) => {
const colorClasses = {
blue: 'from-blue-500 to-blue-600',
green: 'from-green-500 to-green-600',
purple: 'from-purple-500 to-purple-600',
yellow: 'from-yellow-500 to-yellow-600',
red: 'from-red-500 to-red-600',
};
const themeClasses =
theme === 'light'
? {
container:
'bg-gradient-to-br from-white to-gray-50 border-gray-200/50 hover:from-gray-50 hover:to-gray-100 backdrop-blur-sm rounded-xl shadow-lg',
text: 'text-gray-800',
textSecondary: 'text-gray-600',
}
: {
container:
'bg-gradient-to-br from-slate-800/60 to-slate-900/60 border-white/20 hover:from-slate-700/60 hover:to-slate-800/60 backdrop-blur-sm rounded-xl shadow-lg',
text: 'text-white',
textSecondary: 'text-gray-300',
};
return (
<div className={`p-4 ${themeClasses.container} transition-colors`}>
<div className='flex items-center justify-between mb-2'>
<span className='text-2xl'>{icon}</span>
<div className={`w-3 h-3 rounded-full bg-gradient-to-r ${colorClasses[color]}`}></div>
</div>
<p className={`text-2xl font-bold ${themeClasses.text} mb-1`}>{value}</p>
<p className={`text-sm ${themeClasses.textSecondary}`}>{title}</p>
</div>
);
};