-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathVsumTabs.tsx
More file actions
244 lines (220 loc) · 8.78 KB
/
VsumTabs.tsx
File metadata and controls
244 lines (220 loc) · 8.78 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
import React, { useEffect, useMemo, useState } from 'react';
import { VsumDetails } from '../../types';
import { apiService } from '../../services/api';
interface VsumTabsProps {
openVsums: number[];
activeVsumId: number | null;
onActivate: (id: number) => void;
onClose: (id: number) => void;
}
export const VsumTabs: React.FC<VsumTabsProps> = ({ openVsums, activeVsumId, onActivate, onClose }) => {
const [detailsById, setDetailsById] = useState<Record<number, VsumDetails | undefined>>({});
const [error, setError] = useState<string>('');
const [edits, setEdits] = useState<Record<number, { metaModelSourceIds: number[] }>>({});
const [saving, setSaving] = useState(false);
// util
const areIdArraysEqual = (a: number[] = [], b: number[] = []) => {
if (a.length !== b.length) return false;
const sa = [...a].sort((x, y) => x - y);
const sb = [...b].sort((x, y) => x - y);
for (let i = 0; i < sa.length; i++) {
if (sa[i] !== sb[i]) return false;
}
return true;
};
// dirty = compare by sourceId arrays
const dirtyById = useMemo(() => {
const map: Record<number, boolean> = {};
openVsums.forEach((id) => {
const edit = edits[id];
const details = detailsById[id];
if (!edit || !details) { map[id] = false; return; }
map[id] = !areIdArraysEqual(edit.metaModelSourceIds);
});
return map;
}, [openVsums, edits, detailsById]);
// load details for active vsum and seed edits using sourceId
useEffect(() => {
const fetchDetails = async (id: number) => {
setError('');
try {
const res = await apiService.getVsumDetails(id);
setDetailsById(prev => ({ ...prev, [id]: res.data }));
} catch (e) {
setError(e instanceof Error ? e.message : 'Failed to load VSUM details');
}
};
if (activeVsumId && !detailsById[activeVsumId]) {
fetchDetails(activeVsumId);
}
}, [activeVsumId, detailsById]);
// save changes: send sourceIds in metaModelIds field + metaModelRelationRequests: null
const saveById = async (
id: number,
override?: { metaModelSourceIds?: number[] }
) => {
const edit = edits[id];
// fallback from details using sourceId
const fallbackSourceIds = (detailsById[id]?.metaModels);
const metaModelSourceIds =
override?.metaModelSourceIds ?? edit?.metaModelSourceIds ?? fallbackSourceIds;
if (!metaModelSourceIds || metaModelSourceIds.length === 0) {
setError('At least one MetaModel is required');
return;
}
setSaving(true);
setError('');
try {
// IMPORTANT: backend param is "metaModelIds" but we pass SOURCE IDs here
await apiService.updateVsumSyncChanges(id, {
metaModelIds: metaModelSourceIds,
metaModelRelationRequests: null, // not implemented yet
});
const res = await apiService.getVsumDetails(id);
setDetailsById(prev => ({ ...prev, [id]: res.data }));
window.dispatchEvent(new CustomEvent('vitruv.refreshVsums'));
} catch (e) {
setError(e instanceof Error ? e.message : 'Failed to save VSUM');
} finally {
setSaving(false);
}
};
const onSave = async () => {
if (!activeVsumId) return;
await saveById(activeVsumId);
};
// handle external "add meta model" event
useEffect(() => {
const onAdd = (e: Event) => {
const ce = e as CustomEvent<{ id?: number; sourceId?: number }>;
if (!activeVsumId) return;
// Prefer sourceId; if only id is provided and in your app that id is the global source id, this still works.
// If your event sends cloned ids instead, adjust the emitter to pass sourceId.
const sourceId = typeof ce.detail?.sourceId === 'number'
? ce.detail.sourceId
: (typeof ce.detail?.id === 'number' ? ce.detail.id : undefined);
if (typeof sourceId !== 'number') return;
const current = edits[activeVsumId];
if (current.metaModelSourceIds.includes(sourceId)) return;
setEdits(prev => ({
...prev,
[activeVsumId!]: { metaModelSourceIds: [...current.metaModelSourceIds, sourceId] }
}));
};
window.addEventListener('vitruv.addMetaModelToActiveVsum', onAdd as EventListener);
return () => window.removeEventListener('vitruv.addMetaModelToActiveVsum', onAdd as EventListener);
}, [activeVsumId, edits, detailsById]);
if (openVsums.length === 0) return null;
const anyDirty = activeVsumId ? !!dirtyById[activeVsumId] : false;
return (
<div
style={{
background: '#ffffff',
borderBottom: '1px solid #e5e7eb',
position: 'sticky',
top: 0,
zIndex: 20,
boxShadow: 'inset 0 -1px 0 #e5e7eb',
cursor: saving ? 'progress' : 'default'
}}
>
<div style={{ display: 'flex', alignItems: 'center', gap: 10, padding: '8px 10px' }}>
<div style={{ display: 'flex', alignItems: 'center', gap: 6, overflowX: 'auto', flex: 1 }}>
{openVsums.map(id => {
const isActive = id === activeVsumId;
const name = detailsById[id]?.name || `VSUM #${id}`;
const isDirty = !!dirtyById[id];
return (
<div
key={id}
style={{
display: 'flex',
alignItems: 'center',
gap: 8,
padding: '6px 12px',
border: isActive ? '1px solid #bfdbfe' : '1px solid #e5e7eb',
borderBottom: isActive ? '2px solid #3b82f6' : '1px solid #e5e7eb',
borderRadius: 8,
background: isActive ? '#f0f7ff' : '#ffffff',
cursor: 'pointer',
boxShadow: isActive ? '0 1px 2px rgba(0,0,0,0.04)' : 'none'
}}
onClick={() => onActivate(id)}
aria-current={isActive ? 'page' : undefined}
title={name}
>
{isDirty && (
<span
aria-label="Unsaved changes"
title="Unsaved changes"
style={{ width: 6, height: 6, borderRadius: 6, background: '#f59e0b', display: 'inline-block' }}
/>
)}
<span style={{ fontWeight: 700, color: '#1f2937', fontSize: 12, whiteSpace: 'nowrap' }}>{name}</span>
<button
onClick={(e) => {
e.stopPropagation();
onClose(id);
}}
style={{
border: '1px solid transparent',
background: 'transparent',
color: '#64748b',
cursor: 'pointer',
borderRadius: 4,
lineHeight: 1,
width: 16,
height: 16,
display: 'inline-flex',
alignItems: 'center',
justifyContent: 'center'
}}
aria-label={`Close ${name}`}
title="Close"
>
×
</button>
</div>
);
})}
</div>
{activeVsumId && (
<div style={{ display: 'flex', alignItems: 'center', gap: 8 }}>
{error && (
<div
role="alert"
style={{
padding: '4px 8px',
border: '1px solid #fecaca',
color: '#991b1b',
background: '#fef2f2',
borderRadius: 6,
fontSize: 11
}}
>
{error}
</div>
)}
{anyDirty && (
<button
onClick={onSave}
disabled={saving}
style={{
padding: '6px 10px',
border: '1px solid #3b82f6',
borderRadius: 8,
background: saving ? '#bfdbfe' : '#3b82f6',
color: '#ffffff',
fontWeight: 700,
cursor: saving ? 'not-allowed' : 'pointer'
}}
>
{saving ? 'Saving…' : 'Save changes'}
</button>
)}
</div>
)}
</div>
</div>
);
};