-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathVsumsPanel.tsx
More file actions
300 lines (275 loc) · 10.7 KB
/
VsumsPanel.tsx
File metadata and controls
300 lines (275 loc) · 10.7 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
import React, { useEffect, useRef, useState, useCallback } from 'react';
import { apiService } from '../../services/api';
import { Vsum } from '../../types';
import { CreateVsumModal } from './CreateVsumModal';
import { VsumDetailsModal } from './VsumDetailsModal';
const containerStyle: React.CSSProperties = {
userSelect: 'none',
width: '100%',
background: '#f8f9fa',
padding: '16px',
boxSizing: 'border-box',
height: '100%',
overflowY: 'auto',
borderRight: '1px solid #e9ecef',
};
const titleStyle: React.CSSProperties = {
fontSize: '18px',
fontWeight: 700,
marginBottom: '16px',
color: '#2c3e50',
textAlign: 'left',
padding: '8px 0',
borderBottom: '2px solid #3498db',
fontFamily: 'Georgia, serif',
};
const createButtonStyle: React.CSSProperties = {
width: '100%',
padding: '14px 18px',
marginBottom: '12px',
border: 'none',
borderRadius: '6px',
background: 'linear-gradient(135deg, #3498db 0%, #2980b9 100%)',
color: '#ffffff',
fontSize: '15px',
fontWeight: 600,
cursor: 'pointer',
transition: 'all 0.3s ease',
display: 'inline-flex',
alignItems: 'center',
justifyContent: 'center',
gap: '10px',
userSelect: 'none',
boxShadow: '0 3px 10px rgba(52, 152, 219, 0.3)',
fontFamily: 'Georgia, serif',
};
const createButtonHoverStyle: React.CSSProperties = {
transform: 'translateY(-1px)',
boxShadow: '0 5px 15px rgba(52, 152, 219, 0.4)',
background: 'linear-gradient(135deg, #2980b9 0%, #1f5f8b 100%)',
};
const sectionStyle: React.CSSProperties = {
marginTop: '16px',
marginBottom: '8px',
fontWeight: 700,
fontSize: '13px',
color: '#2c3e50',
borderBottom: '1px solid #3498db',
paddingBottom: '6px',
fontFamily: 'Georgia, serif',
};
const cardStyle: React.CSSProperties = {
background: '#ffffff',
border: '1px solid #d1ecf1',
borderRadius: '6px',
padding: '12px',
marginBottom: '12px',
boxShadow: '0 2px 6px rgba(0,0,0,0.08)',
};
const errorStyle: React.CSSProperties = {
padding: '8px 12px',
margin: '8px 0',
borderRadius: '6px',
fontSize: '12px',
fontWeight: 500,
backgroundColor: '#f8d7da',
color: '#721c24',
border: '1px solid #f5c6cb',
};
export const VsumsPanel: React.FC = () => {
const [items, setItems] = useState<Vsum[]>([]);
const [loading, setLoading] = useState(false);
const [error, setError] = useState<string>('');
const [search, setSearch] = useState('');
const [page, setPage] = useState(0);
const [hasMore, setHasMore] = useState(true);
const [showCreate, setShowCreate] = useState(false);
const [detailsId, setDetailsId] = useState<number | null>(null);
const containerRef = useRef<HTMLDivElement | null>(null);
const rafRef = useRef<number | null>(null);
const requestSeq = useRef(0);
const PAGE_SIZE = 10;
const formatDateTime = (iso: string) => {
const d = new Date(iso);
return `${d.toLocaleDateString()} ${d.toLocaleTimeString([], { hour: '2-digit', minute: '2-digit' })}`;
};
const loadFirstPage = useCallback(async () => {
const mySeq = ++requestSeq.current;
setLoading(true);
setError('');
setItems([]);
setPage(0);
setHasMore(true);
if (containerRef.current) containerRef.current.scrollTop = 0;
try {
const res = await apiService.getVsumsPaginated(search, 0, PAGE_SIZE);
if (mySeq !== requestSeq.current) return;
const newData: Vsum[] = res.data || [];
setItems(newData);
setPage(1);
setHasMore(newData.length === PAGE_SIZE);
} catch (e) {
if (mySeq !== requestSeq.current) return;
setError(e instanceof Error ? e.message : 'Failed to load VSUMs');
} finally {
if (mySeq === requestSeq.current) setLoading(false);
}
}, [search]);
const loadNextPage = useCallback(async () => {
if (loading || !hasMore) return;
const mySeq = requestSeq.current;
setLoading(true);
setError('');
try {
const res = await apiService.getVsumsPaginated(search, page, PAGE_SIZE);
if (mySeq !== requestSeq.current) return;
const newData: Vsum[] = res.data || [];
setItems(prev => [...prev, ...newData]);
setPage(prev => prev + 1);
setHasMore(newData.length === PAGE_SIZE);
} catch (e) {
if (mySeq !== requestSeq.current) return;
setError(e instanceof Error ? e.message : 'Failed to load VSUMs');
} finally {
if (mySeq === requestSeq.current) setLoading(false);
}
}, [search, page, hasMore, loading]);
useEffect(() => {
loadFirstPage();
}, [loadFirstPage]);
useEffect(() => {
const el = containerRef.current;
if (!el) return;
const onScroll = () => {
if (rafRef.current) cancelAnimationFrame(rafRef.current);
rafRef.current = requestAnimationFrame(() => {
if (loading || !hasMore) return;
const nearBottom = el.scrollHeight - el.scrollTop - el.clientHeight < 150;
if (nearBottom) loadNextPage();
});
};
el.addEventListener('scroll', onScroll);
return () => {
el.removeEventListener('scroll', onScroll);
if (rafRef.current) cancelAnimationFrame(rafRef.current);
};
}, [hasMore, loading, loadNextPage]);
return (
<div ref={containerRef} style={containerStyle}>
<div style={titleStyle}>Projects</div>
<CreateVsumModal
isOpen={showCreate}
onClose={() => setShowCreate(false)}
onSuccess={() => loadFirstPage()}
/>
{error && <div style={errorStyle}>{error}</div>}
<button
onClick={() => setShowCreate(true)}
style={createButtonStyle}
onMouseEnter={(e) => Object.assign(e.currentTarget.style, createButtonHoverStyle)}
onMouseLeave={(e) => Object.assign(e.currentTarget.style, createButtonStyle)}
>
Create
</button>
<div style={{ display: 'flex', gap: 8, marginBottom: 12 }}>
<input
type="text"
value={search}
onChange={(e) => setSearch(e.target.value)}
placeholder="Search by name..."
style={{
flex: 1,
padding: '8px 12px',
borderRadius: 6,
border: '1px solid #cbd5e1',
fontSize: 14,
outline: 'none',
}}
/>
<button
onClick={() => loadFirstPage()}
style={{
padding: '8px 14px',
borderRadius: 6,
background: '#3b82f6',
color: '#fff',
border: 'none',
cursor: 'pointer',
fontWeight: 600,
}}
>
Search
</button>
</div>
<div style={sectionStyle}>All</div>
{items.map((item) => {
const role = (item as any).role as string | undefined;
const canManage = role === 'OWNER';
return (
<div
key={item.id}
style={cardStyle}
onDoubleClick={() =>
window.dispatchEvent(new CustomEvent('vitruv.openVsum', { detail: { id: item.id } }))
}
>
<div style={{ display: 'flex', justifyContent: 'space-between', alignItems: 'center', gap: 8 }}>
<div style={{ display: 'flex', flexDirection: 'column' }}>
<div style={{ fontWeight: 700, color: '#2c3e50', display: 'flex', alignItems: 'center', gap: 8 }}>
{item.name}
{role && (
<span
style={{
padding: '2px 6px',
borderRadius: 6,
fontSize: 11,
fontWeight: 700,
border: '1px solid #e5e7eb',
background: role === 'OWNER' ? '#ecfdf5' : '#f3f4f6',
color: role === 'OWNER' ? '#065f46' : '#374151',
}}
>
{role}
</span>
)}
</div>
<div style={{ fontSize: 12, color: '#5a6c7d' }}>Created: {formatDateTime(item.createdAt)}</div>
</div>
{canManage && (
<button
onClick={() => setDetailsId(item.id)}
style={{
padding: '6px 10px',
border: '1px solid #dee2e6',
borderRadius: 6,
background: '#ffffff',
cursor: 'pointer',
fontWeight: 600,
}}
>
Details
</button>
)}
</div>
</div>
);
})}
{!loading && items.length === 0 && (
<div style={{ textAlign: 'center', color: '#6b7280', marginTop: 40, fontStyle: 'italic' }}>
No VSUMs found. Create one to get started.
</div>
)}
{loading && (
<div style={{ padding: 12, fontStyle: 'italic', color: '#5a6c7d', marginTop: 12 }}>
Loading...
</div>
)}
<VsumDetailsModal
isOpen={detailsId !== null}
vsumId={detailsId}
onClose={() => setDetailsId(null)}
onSaved={() => loadFirstPage()}
/>
</div>
);
};