Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/components/ui/SidebarTabs.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ export const SidebarTabs: React.FC<SidebarTabsProps> = ({ width = 350, showBorde
<div style={{ height: '100%', display: 'flex', flexDirection: 'column', padding: 16, color: '#6b7280' }}>
<div style={{ fontWeight: 700, color: '#111827', marginBottom: 8 }}>Choose a workspace</div>
<div style={{ fontSize: 13, lineHeight: 1.6 }}>
Select <span style={{ fontWeight: 700 }}>MML</span> to browse and upload Meta Models, or
select <span style={{ fontWeight: 700 }}>Project</span> to open vSUMS project tools.
Select <span style={{ fontWeight: 700 }}>Meta Model</span> to browse and upload Meta Models, or
select <span style={{ fontWeight: 700 }}>Projects</span> to open vSUMS project tools.
</div>
</div>
)}
Expand Down
323 changes: 149 additions & 174 deletions src/components/ui/VsumTabs.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useEffect, useMemo, useRef, useState } from 'react';
import React, { useEffect, useMemo, useState } from 'react';
import { VsumDetails } from '../../types';
import { apiService } from '../../services/api';

Expand All @@ -12,11 +12,8 @@ interface VsumTabsProps {
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, { name: string; metaModelIds: number[] }>>({});
const [edits, setEdits] = useState<Record<number, { metaModelIds: number[] }>>({});
const [saving, setSaving] = useState(false);
const [renamingId, setRenamingId] = useState<number | null>(null);
const [renameValue, setRenameValue] = useState<string>('');
const renameCommitRef = useRef(false);

const areIdArraysEqual = (a: number[] = [], b: number[] = []) => {
if (a.length !== b.length) return false;
Expand All @@ -35,8 +32,7 @@ export const VsumTabs: React.FC<VsumTabsProps> = ({ openVsums, activeVsumId, onA
const details = detailsById[id];
if (!edit || !details) { map[id] = false; return; }
const detailsIds = (details.metaModels || []).map(m => m.id);
const isDirty = edit.name.trim() !== (details.name || '').trim() || !areIdArraysEqual(edit.metaModelIds, detailsIds);
map[id] = isDirty;
map[id] = !areIdArraysEqual(edit.metaModelIds, detailsIds);
});
return map;
}, [openVsums, edits, detailsById]);
Expand All @@ -47,7 +43,10 @@ export const VsumTabs: React.FC<VsumTabsProps> = ({ openVsums, activeVsumId, onA
try {
const res = await apiService.getVsumDetails(id);
setDetailsById(prev => ({ ...prev, [id]: res.data }));
setEdits(prev => ({ ...prev, [id]: { name: res.data.name, metaModelIds: (res.data.metaModels || []).map(m => m.id) } }));
setEdits(prev => ({
...prev,
[id]: { metaModelIds: (res.data.metaModels || []).map(m => m.id) }
}));
} catch (e) {
setError(e instanceof Error ? e.message : 'Failed to load VSUM details');
}
Expand All @@ -57,21 +56,38 @@ export const VsumTabs: React.FC<VsumTabsProps> = ({ openVsums, activeVsumId, onA
}
}, [activeVsumId, detailsById]);

// Derived states no longer needed: activeEdit, activeDetails

const saveById = async (id: number, override?: { name?: string; metaModelIds?: number[] }) => {
const saveById = async (
id: number,
override?: { metaModelIds?: number[] }
) => {
const edit = edits[id];
const fallbackMetaIds = (detailsById[id]?.metaModels || []).map(m => m.id);
const name = (override?.name ?? edit?.name ?? '').trim();
const metaModelIds = override?.metaModelIds ?? edit?.metaModelIds ?? fallbackMetaIds;
if (!name) { setError('Name is required'); return; }

if (!metaModelIds || metaModelIds.length === 0) {
setError('At least one MetaModel is required');
return;
}

setSaving(true);
setError('');
try {
await apiService.updateVsum(id, { name, metaModelIds });
await apiService.updateVsumSyncChanges(id, {
metaModelIds,
metaModelRelationRequests: null, // not implemented yet
});

const res = await apiService.getVsumDetails(id);
setDetailsById(prev => ({ ...prev, [id]: res.data }));
setEdits(prev => ({ ...prev, [id]: { name: res.data.name, metaModelIds: (res.data.metaModels || []).map(m => m.id) } }));

// keep edit state in sync with server
setEdits(prev => ({
...prev,
[id]: {
metaModelIds: (res.data.metaModels || []).map(m => m.id),
},
}));

window.dispatchEvent(new CustomEvent('vitruv.refreshVsums'));
} catch (e) {
setError(e instanceof Error ? e.message : 'Failed to save VSUM');
Expand All @@ -85,45 +101,23 @@ export const VsumTabs: React.FC<VsumTabsProps> = ({ openVsums, activeVsumId, onA
await saveById(activeVsumId);
};

const ensureDetails = async (id: number) => {
if (detailsById[id]) return detailsById[id];
try {
const res = await apiService.getVsumDetails(id);
setDetailsById(prev => ({ ...prev, [id]: res.data }));
setEdits(prev => ({ ...prev, [id]: { name: res.data.name, metaModelIds: (res.data.metaModels || []).map(m => m.id) } }));
return res.data;
} catch (e) {
setError(e instanceof Error ? e.message : 'Failed to load VSUM details');
return undefined;
}
};

const beginRename = async (id: number) => {
await ensureDetails(id);
const currentName = edits[id]?.name || detailsById[id]?.name || `VSUM #${id}`;
setRenamingId(id);
setRenameValue(currentName);
};

const commitRenameById = async (id: number) => {
const current = edits[id] || { name: renameValue, metaModelIds: (detailsById[id]?.metaModels || []).map(m => m.id) };
const nextName = renameValue.trim();
if (!nextName) { setError('Name is required'); return; }
setEdits(prev => ({ ...prev, [id]: { ...current, name: nextName } }));
setRenamingId(null);
await saveById(id, { name: nextName });
};

// handle external "add meta model" event
useEffect(() => {
const onAdd = (e: Event) => {
const ce = e as CustomEvent<{ id: number }>; // meta model id
if (!activeVsumId) return;
const mmId = ce.detail?.id;
if (typeof mmId !== 'number') return;
const current = edits[activeVsumId] || { name: detailsById[activeVsumId!]?.name || '', metaModelIds: [] };

const current = edits[activeVsumId] || { metaModelIds: (detailsById[activeVsumId!]?.metaModels || []).map(m => m.id) };
if (current.metaModelIds.includes(mmId)) return;
setEdits(prev => ({ ...prev, [activeVsumId!]: { ...current, metaModelIds: [...current.metaModelIds, mmId] } }));

setEdits(prev => ({
...prev,
[activeVsumId!]: { metaModelIds: [...current.metaModelIds, mmId] }
}));
};

window.addEventListener('vitruv.addMetaModelToActiveVsum', onAdd as EventListener);
return () => window.removeEventListener('vitruv.addMetaModelToActiveVsum', onAdd as EventListener);
}, [activeVsumId, edits, detailsById]);
Expand All @@ -133,134 +127,115 @@ export const VsumTabs: React.FC<VsumTabsProps> = ({ openVsums, activeVsumId, onA
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)}
onContextMenu={(e) => { e.preventDefault(); beginRename(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' }} />}
{renamingId === id ? (
<input
value={renameValue}
onChange={(e) => setRenameValue(e.target.value)}
onKeyDown={(e) => {
if (e.key === 'Enter') {
e.preventDefault();
renameCommitRef.current = true;
void (async () => {
try {
await commitRenameById(id);
} finally {
// allow blur after commit
setTimeout(() => { renameCommitRef.current = false; }, 0);
}
})();
}
if (e.key === 'Escape') { setRenamingId(null); }
}}
onBlur={() => { if (!renameCommitRef.current) { setRenamingId(null); } }}
autoFocus
disabled={saving}
style={{
padding: '1px 4px',
border: 'none',
borderBottom: '1px solid #94a3b8',
background: 'transparent',
fontSize: 12,
minWidth: 80,
color: '#111827',
outline: 'none'
}}
/>
) : (
<span style={{ fontWeight: 700, color: '#1f2937', fontSize: 12, whiteSpace: 'nowrap' }}>{name}</span>
)}
{renamingId === id && (
<button
onMouseDown={() => { renameCommitRef.current = true; }}
onClick={(e) => {
e.stopPropagation();
void (async () => {
try {
await commitRenameById(id);
} finally {
setTimeout(() => { renameCommitRef.current = false; }, 0);
}
})();
}}
disabled={saving || !renameValue.trim() || renameValue.trim() === (edits[id]?.name || detailsById[id]?.name || '')}
style={{ border: '1px solid transparent', background: 'transparent', color: saving ? '#93c5fd' : '#3b82f6', cursor: saving ? 'not-allowed' : 'pointer', borderRadius: 4, lineHeight: 1, width: 16, height: 16, display: 'inline-flex', alignItems: 'center', justifyContent: 'center' }}
aria-label={`Save name for ${name}`}
title="Save"
>
</button>
)}
<button
onMouseDown={() => { renameCommitRef.current = true; }}
onClick={(e) => {
e.stopPropagation();
setRenamingId(null);
onClose(id);
setTimeout(() => { renameCommitRef.current = false; }, 0);
}}
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
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>
</div>
);
};


};
Loading
Loading