-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathui-component.tsx
More file actions
252 lines (225 loc) · 7.01 KB
/
Copy pathui-component.tsx
File metadata and controls
252 lines (225 loc) · 7.01 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
/**
* Project Statistics Dashboard Widget
*
* Example React component for UI plugin that displays project statistics.
* Demonstrates:
* - React hooks (useState, useEffect)
* - IPC communication with backend
* - Auto-refresh with intervals
* - Error handling and loading states
* - Tailwind CSS styling
*/
import { useEffect, useState } from 'react';
import { RefreshCw, TrendingUp, CheckCircle2, Clock, AlertCircle, XCircle } from 'lucide-react';
interface ProjectStats {
totalSpecs: number;
completedSpecs: number;
pendingSpecs: number;
inProgressSpecs: number;
failedSpecs: number;
successRate: number;
lastBuildTime: string | null;
averageBuildTime: string | null;
}
interface ProjectStatsWidgetProps {
projectDir: string;
refreshInterval?: number;
showCharts?: boolean;
}
/**
* Project Statistics Widget Component
*
* Displays project statistics in a dashboard card with auto-refresh capability.
*/
export function ProjectStatsWidget({
projectDir,
refreshInterval = 30000,
showCharts = true
}: ProjectStatsWidgetProps) {
const [stats, setStats] = useState<ProjectStats | null>(null);
const [isLoading, setIsLoading] = useState(true);
const [error, setError] = useState<string | null>(null);
const [lastRefresh, setLastRefresh] = useState<Date>(new Date());
/**
* Fetch project statistics from backend via IPC
*/
const fetchStats = async (showLoadingState = true) => {
if (showLoadingState) {
setIsLoading(true);
}
setError(null);
try {
// Call IPC handler registered in plugin.py
const result = await window.electronAPI.invoke('ui-extension:get-stats', projectDir);
if (result.success) {
setStats(result.data);
setLastRefresh(new Date());
} else {
setError(result.error || 'Failed to fetch statistics');
}
} catch (err) {
setError(err instanceof Error ? err.message : 'Unknown error occurred');
} finally {
setIsLoading(false);
}
};
/**
* Manually refresh statistics (clears cache on backend)
*/
const handleRefresh = async () => {
setIsLoading(true);
setError(null);
try {
const result = await window.electronAPI.invoke('ui-extension:refresh-stats', projectDir);
if (result.success) {
setStats(result.data);
setLastRefresh(new Date());
} else {
setError(result.error || 'Failed to refresh statistics');
}
} catch (err) {
setError(err instanceof Error ? err.message : 'Unknown error occurred');
} finally {
setIsLoading(false);
}
};
// Fetch stats on mount
useEffect(() => {
fetchStats();
}, [projectDir]);
// Auto-refresh at specified interval
useEffect(() => {
if (!refreshInterval) return;
const interval = setInterval(() => {
fetchStats(false); // Don't show loading spinner for auto-refresh
}, refreshInterval);
return () => clearInterval(interval);
}, [refreshInterval, projectDir]);
// Loading state
if (isLoading && !stats) {
return (
<div className="rounded-lg border border-border bg-card p-6">
<div className="flex items-center justify-center h-48">
<RefreshCw className="h-8 w-8 animate-spin text-muted-foreground" />
</div>
</div>
);
}
// Error state
if (error) {
return (
<div className="rounded-lg border border-border bg-card p-6">
<div className="flex flex-col items-center justify-center h-48 space-y-4">
<AlertCircle className="h-12 w-12 text-destructive" />
<div className="text-center">
<p className="text-sm font-medium text-foreground">Failed to load statistics</p>
<p className="text-xs text-muted-foreground mt-1">{error}</p>
<button
onClick={() => fetchStats()}
className="mt-4 px-4 py-2 bg-primary text-primary-foreground rounded-md text-sm hover:bg-primary/90"
>
Try Again
</button>
</div>
</div>
</div>
);
}
// No stats available
if (!stats) {
return null;
}
return (
<div className="rounded-lg border border-border bg-card p-6">
{/* Header */}
<div className="flex items-center justify-between mb-6">
<div className="flex items-center space-x-2">
<TrendingUp className="h-5 w-5 text-primary" />
<h3 className="text-lg font-semibold text-foreground">Project Statistics</h3>
</div>
<button
onClick={handleRefresh}
disabled={isLoading}
className="p-2 hover:bg-accent rounded-md transition-colors disabled:opacity-50"
title="Refresh statistics"
>
<RefreshCw className={`h-4 w-4 text-muted-foreground ${isLoading ? 'animate-spin' : ''}`} />
</button>
</div>
{/* Stats Grid */}
<div className="grid grid-cols-2 md:grid-cols-4 gap-4 mb-6">
{/* Total Specs */}
<StatCard
icon={<TrendingUp className="h-5 w-5" />}
label="Total Specs"
value={stats.totalSpecs}
color="text-blue-500"
/>
{/* Completed */}
<StatCard
icon={<CheckCircle2 className="h-5 w-5" />}
label="Completed"
value={stats.completedSpecs}
color="text-green-500"
/>
{/* In Progress */}
<StatCard
icon={<Clock className="h-5 w-5" />}
label="In Progress"
value={stats.inProgressSpecs}
color="text-yellow-500"
/>
{/* Failed */}
<StatCard
icon={<XCircle className="h-5 w-5" />}
label="Failed"
value={stats.failedSpecs}
color="text-red-500"
/>
</div>
{/* Success Rate */}
<div className="bg-accent/50 rounded-lg p-4 mb-4">
<div className="flex items-center justify-between mb-2">
<span className="text-sm font-medium text-muted-foreground">Success Rate</span>
<span className="text-2xl font-bold text-foreground">{stats.successRate}%</span>
</div>
{/* Progress Bar */}
<div className="w-full bg-muted rounded-full h-2">
<div
className="bg-green-500 h-2 rounded-full transition-all duration-300"
style={{ width: `${stats.successRate}%` }}
/>
</div>
</div>
{/* Last Refresh */}
<div className="text-xs text-muted-foreground text-right">
Last updated: {lastRefresh.toLocaleTimeString()}
</div>
</div>
);
}
/**
* Individual stat card component
*/
interface StatCardProps {
icon: React.ReactNode;
label: string;
value: number;
color: string;
}
function StatCard({ icon, label, value, color }: StatCardProps) {
return (
<div className="bg-accent/30 rounded-lg p-4">
<div className={`${color} mb-2`}>
{icon}
</div>
<div className="text-2xl font-bold text-foreground mb-1">
{value}
</div>
<div className="text-xs text-muted-foreground">
{label}
</div>
</div>
);
}
export default ProjectStatsWidget;