Implement smart merge of synced data to preserve external updates

This commit is contained in:
Christian Vidal Wolf
2026-04-10 12:31:57 +02:00
parent 8849303f58
commit dbcd6d622f
+28 -3
View File
@@ -113,14 +113,39 @@ export default function App() {
console.log('Fetching synced data from Supabase...'); console.log('Fetching synced data from Supabase...');
const syncedData = await getAllSyncedRows(session?.access_token); const syncedData = await getAllSyncedRows(session?.access_token);
const editableColumns = new Set([
COLUMNS.LONG_DE, COLUMNS.LONG_EN,
COLUMNS.SHORT_DE, COLUMNS.SHORT_EN,
COLUMNS.DETAILS_DE, COLUMNS.DETAILS_EN,
COLUMNS.INNER_L, COLUMNS.INNER_W, COLUMNS.INNER_H,
COLUMNS.OUTER_L, COLUMNS.OUTER_W, COLUMNS.OUTER_H,
COLUMNS.UNITS_OUTER, COLUMNS.MOQ
]);
headers.forEach((h: any, i: number) => {
const hl = String(h || '').toLowerCase();
if (hl.includes('srp') || hl.includes('uvp') || hl.includes('40') || hl.includes('price')) {
editableColumns.add(i);
}
});
const articleNoIdx = COLUMNS.ARTICLE_NO; const articleNoIdx = COLUMNS.ARTICLE_NO;
const processedRows = rows.map(row => { const processedRows = rows.map(row => {
const articleNo = String(row[articleNoIdx]); const articleNo = String(row[articleNoIdx]);
const synced = syncedData[articleNo]; const synced = syncedData[articleNo];
const finalRow = synced ? synced.data : row;
if (synced && synced.status === 'pending') { const finalRow = [...row];
setRowStatuses(prev => ({ ...prev, [articleNo]: 'pending' })); if (synced) {
// Smart Merge: Only overlay fields we logically "own" via this app's editors
editableColumns.forEach(idx => {
if (synced.data[idx] !== undefined && synced.data[idx] !== null) {
finalRow[idx] = synced.data[idx];
}
});
if (synced.status === 'pending') {
setRowStatuses(prev => ({ ...prev, [articleNo]: 'pending' }));
}
} }
return finalRow.map((val: any, idx: number) => { return finalRow.map((val: any, idx: number) => {