feat(media-manager): Implement batch manual linking and duplicate prevention

- **Batch Linking UI**: Added 'queueing' mechanism for links, allowing multiple manual links to be defined before saving.
- **Critical Save Fix**: Intercept saveChanges to strictly enforce the 'Confirmation Wizard' for pending links, ensuring items are merged in memory before backend processing to prevent duplication.
- **Adoption Persistence**: Updated MediaService to explicitly write shopify_media_id to Drive file properties during save, fixing race conditions where linked items were re-adopted as orphans.
- **Plan Accuracy**: Updated calculateDiff to exclude pending link items from generating duplicate 'Sync' or 'Adopt' actions.
- **Order Preservation**: Implemented logic to ensure the 'Synced' item creates/persists at the position of the *first* item in the linked pair.
- **Testing**: Added src/MediaStateLogic.test.ts as a permanent test suite for complex frontend state logic, covering queuing, plan generation, and invariant safety.
This commit is contained in:
Ben Miller
2025-12-31 23:55:10 -07:00
parent 61db262082
commit 09995d0d05
3 changed files with 586 additions and 40 deletions

View File

@ -522,6 +522,50 @@
.btn-link:hover {
background-color: var(--primary-hover);
}
/* Combined Card Styles */
.combined-item {
display: flex;
flex-direction: column;
background: #f0f9ff; /* Light blue tint */
border: 2px solid var(--primary);
}
.combined-images {
display: flex;
flex: 1;
overflow: hidden;
border-bottom: 1px solid var(--border);
}
.combined-part {
flex: 1;
position: relative;
border-right: 1px solid var(--border);
}
.combined-part:last-child {
border-right: none;
}
.combined-part .media-content {
aspect-ratio: auto; /* Allow filling height */
height: 100px;
}
.unlink-btn {
width: 100%;
background: white;
border: none;
padding: 6px;
color: var(--danger);
cursor: pointer;
display: flex;
align-items: center;
justify-content: center;
gap: 6px;
font-size: 11px;
font-weight: 500;
transition: background 0.1s;
}
.unlink-btn:hover {
background: #fee2e2;
}
</style>
</head>
@ -553,7 +597,7 @@
<div id="quick-links" style="font-size:12px; display:flex; gap:8px;"></div>
<button id="btn-link-selected" onclick="controller.linkSelectedMedia()" class="btn btn-link"
style="display:none; width:auto; padding: 4px 12px; font-size:12px; height:28px;">
Link Selected
Link These!
</button>
</div>
<div style="display: flex; gap: 8px;">
@ -766,6 +810,7 @@
this.items = [];
this.initialState = [];
this.selectedIds = new Set();
this.tentativeLinks = [];
}
MediaState.prototype.setSku = function (info) {
@ -780,6 +825,7 @@
this.items = items || [];
this.initialState = JSON.parse(JSON.stringify(this.items));
this.selectedIds.clear();
this.tentativeLinks = [];
ui.render(this.items);
this.checkDirty();
};
@ -798,27 +844,28 @@
var isDrive = (item.source === 'drive_only');
var isShopify = (item.source === 'shopify_only');
if (isDrive) {
// Clear other Drive selections
var _this = this;
this.items.forEach(function (i) {
if (i.source === 'drive_only' && _this.selectedIds.has(i.id) && i.id !== id) {
_this.selectedIds.delete(i.id);
affectedIds.push(i.id);
if (isDrive) {
// Clear other Drive selections
var _this = this;
this.items.forEach(function (i) {
// Simplified clearing logic
if (i.source === 'drive_only' && _this.selectedIds.has(i.id) && i.id !== id) {
_this.selectedIds.delete(i.id);
affectedIds.push(i.id);
}
});
} else if (isShopify) {
// Clear other Shopify selections
var _this = this;
this.items.forEach(function (i) {
if (i.source === 'shopify_only' && _this.selectedIds.has(i.id) && i.id !== id) {
_this.selectedIds.delete(i.id);
affectedIds.push(i.id);
}
});
}
});
} else if (isShopify) {
// Clear other Shopify selections
var _this = this;
this.items.forEach(function (i) {
if (i.source === 'shopify_only' && _this.selectedIds.has(i.id) && i.id !== id) {
_this.selectedIds.delete(i.id);
affectedIds.push(i.id);
}
});
this.selectedIds.add(id);
}
this.selectedIds.add(id);
}
// Targeted updates
affectedIds.forEach(function (aid) { ui.updateCardState(aid); });
@ -859,6 +906,14 @@
// Handled by Sortable
};
MediaState.prototype.unlinkMedia = function (driveId, shopifyId) {
this.tentativeLinks = this.tentativeLinks.filter(function (l) {
return !(l.driveId === driveId && l.shopifyId === shopifyId);
});
ui.render(this.items);
this.checkDirty();
};
MediaState.prototype.checkDirty = function () {
var plan = this.calculateDiff();
var isDirty = plan.hasChanges;
@ -872,16 +927,43 @@
var actions = [];
// Collect IDs involved in tentative links to exclude them from individual actions
var linkedIds = new Set();
this.tentativeLinks.forEach(function (l) {
linkedIds.add(l.driveId);
linkedIds.add(l.shopifyId);
});
this.items.forEach(function (i) {
if (i._deleted) actions.push({ type: 'delete', name: i.filename || 'Item' });
});
this.items.forEach(function (i) {
if (i._deleted) return;
// Skip items that are pending link (they will be handled by 'link' action)
if (linkedIds.has(i.id)) return;
if (!initialIds.has(i.id)) {
actions.push({ type: 'upload', name: i.filename || 'New Item' });
} else if (i.status === 'drive_only') {
actions.push({ type: 'sync_upload', name: i.filename || 'Item' });
} else if (i.status === 'shopify_only') {
actions.push({ type: 'adopt', name: i.filename || 'Item' });
}
});
// Tentative Links
var _this = this;
this.tentativeLinks.forEach(function (link) {
var dItem = _this.items.find(function (i) { return i.id === link.driveId; });
var sItem = _this.items.find(function (i) { return i.id === link.shopifyId; });
if (dItem && sItem) {
actions.push({
type: 'link',
name: 'Link Sync: ' + (dItem.filename || 'Item'),
driveId: link.driveId,
shopifyId: link.shopifyId
});
}
});
@ -1086,7 +1168,40 @@
return;
}
items.forEach(function (item, index) {
// Pre-process items for Tentative Links (Visual Merge)
var displayItems = [];
var handledIds = new Set();
items.forEach(function (item) {
if (handledIds.has(item.id)) return;
// Check if part of tentative link
var link = state.tentativeLinks.find(function (l) { return l.driveId === item.id || l.shopifyId === item.id; });
if (link) {
var dItem = items.find(function (i) { return i.id === link.driveId; });
var sItem = items.find(function (i) { return i.id === link.shopifyId; });
if (dItem && sItem) {
// Create merged display item
displayItems.push({
type: 'combined',
id: 'link-' + link.driveId + '-' + link.shopifyId,
drive: dItem,
shopify: sItem,
_deleted: false // Combined item is alive unless constituent parts deleted? (Shouldn't be deleted if linked)
});
handledIds.add(dItem.id);
handledIds.add(sItem.id);
return;
}
}
displayItems.push(item);
handledIds.add(item.id);
});
displayItems.forEach(function (item, index) {
var el = _this.createCard(item, index);
_this.grid.appendChild(el);
});
@ -1098,11 +1213,50 @@
ghostClass: 'sortable-ghost',
dragClass: 'sortable-drag',
onEnd: function () {
// Reordering Logic needs update for combined items?
// If we drag a combined item, what happens?
// The sortable list contains IDs.
// If we drag 'link-d1-s1', we can't easily map back to single item reorder.
// Ideally, we treat it as reordering the associated Drive file (and sync Shopify).
// But 'state.items' expects flat list.
// Complexity: Reordering combined items.
// Logic: Update state.items order based on displayItems order.
var newOrderIds = Array.from(_this.grid.children).map(function (el) { return el.dataset.id; });
var newItems = newOrderIds.map(function (id) {
return state.items.find(function (i) { return i.id === id; });
}).filter(Boolean);
state.items = newItems;
var newItems = [];
newOrderIds.forEach(function (id) {
if (id.startsWith('link-')) {
// Find the combined item in displayItems (or reconstruct)
var parts = id.replace('link-', '').split('-');
// DriveID is parts[0] ? No, id might contain hyphens?
// Use lookup map or careful logic.
// Actually, we tracked it in displayItems.
var dItem = state.items.find(i => i.id === parts[0]); // dangerous if ID has hyphen
// Better: find in cached displayItems?
// Let's rely on finding in state.items by matching tentativeLinks.
var link = state.tentativeLinks.find(l => 'link-' + l.driveId + '-' + l.shopifyId === id);
if (link) {
var d = state.items.find(i => i.id === link.driveId);
var s = state.items.find(i => i.id === link.shopifyId);
if (d) newItems.push(d);
if (s) newItems.push(s);
}
} else {
var item = state.items.find(i => i.id === id);
if (item) newItems.push(item);
}
});
// Append any items that were not in grid (e.g. filtered out? No, all active shown)
// What about handledIds?
// Simplest: just map the specific moved items.
// Reordering combined items is tricky.
// Let's assume for now user drags combined item, we place both d&s in that slot in correct relative order?
// Or just place Drive item there, and Shopify item follows?
// For simplicity: We reconstruct state.items based on new grid order.
state.items = newItems.concat(state.items.filter(i => !newItems.includes(i))); // Append missing?
state.checkDirty();
}
});
@ -1182,6 +1336,32 @@
};
UI.prototype.createCard = function (item, index) {
// Combined Card (Tentative Link)
if (item.type === 'combined') {
var div = document.createElement('div');
div.className = 'media-item combined-item';
div.dataset.id = item.id;
var parts = '<div class="combined-images">';
// Drive Part
parts += '<div class="combined-part">';
parts += '<img class="media-content" src="' + (item.drive.thumbnail || "") + '">';
parts += '</div>';
// Shopify Part
parts += '<div class="combined-part">';
parts += '<img class="media-content" src="' + (item.shopify.thumbnail || "") + '">';
parts += '</div>';
parts += '</div>'; // end combined-images
parts += '<button class="unlink-btn" onclick="state.unlinkMedia(\'' + item.drive.id + '\', \'' + item.shopify.id + '\')">Unlink</button>';
div.innerHTML = parts;
return div;
}
var div = document.createElement('div');
var isSelected = state.selectedIds.has(item.id);
div.className = 'media-item ' + (item._deleted ? 'deleted-item' : '') + (isSelected ? ' selected' : '');
@ -1279,7 +1459,7 @@
if (isVideo) {
console.log("Falling back to generic video icon...");
// Base64 Video Icon (Blue) to avoid network issues
this.src = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAEAAAABACAYAAACqaXHeAAAABmJLR0QA/wD/AP+gvaeTAAAHGUlEQVR42u2beWwUVRzHP293W2i33JRSChQoBAQkQkFO4Q8ET4wKBI0KCJ6JEARPUC5FjIkKCJFDoCIiNwoioCIiBExEIlEKBQu0hRZarcvutttd/xwz7fR2d3ZmO7uz2d9kM7Mz7733/T7z+733ezMHKlSokA/lYg9QDiwG5gPdwAigH9AP6Oow9gL7gX3ALqAZqANqgS/i8fi+XJ9IrgG4BFgMLAP6u4wVwH7gI6AKeD3XJ5RLABYAS4BvgWH5+nI9MBV4D6gHGrN9QDYBuBR4Hfi8gL5/AAuBp7J5WDYAuAp4GxhWZF8/A68Cl2fbsKwCcCHwIfBskX37BDCR9I1clDUArgXWASOL7NOTwDqy476sAHANsA4YWmRfPgKsJRvuMwvAdcBHwLAi++4RYCPZcd9mAdjE4/E9wO9F9uXfwB4S3qAvEwBcAXwGjCiy7x4FNpId92UcgI3AIsBXZN8tAkbke49kFIDLgdeBMUX23WPAO8Cl+faRDgAbSM/9w0X23cPAWnI7B6QDwKvAkCL76lFgGbnlQDIAXAa8AnQsso8eA94ALs71HkgGgI+A/kW2+SPg41zvgUQAGAP0LLLNjwJjcr3/iQAwFuhRZJt3AqNzve+JADAAyL+XtwuMzvW+JwJAd6B7kW3eHeia631PBIBuRe65QNeCAhAAChAAChAAChAAChAAChAAChCArG+C7wS8B4wHOgM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421OAQDwO+0l0v/1p6h+vUX64XibkwAAuAX4hPR/+SuqX38m/XC8zUkAADwArCT933+K6tdK0g/H25wGAMB1wFekd4CK6teXpB+OtzkNQJz0g/BcopP79RzpB+S2XJ5c6D0gHo9vAeaS3hEqql/nkB3x+woAgK8S3REqql9/kB7xFxQAAD8kuiNUVD/8JD3idyQAAG4guiNUVD/cSHrE71gAANxAdEeoeP/wkPSIv6AAALiO6I5Q8f6xnPSIv6AAALie6I5Q8f5xPekRf8EBAPD78DqiO0LF+8cPpEf8rgAA+J34I9EdoeL940fSI35XAABcS3RHqHj/uJb0iN+1AAD4nfhzojtCxXvH/wCwAPQCOgGdgG5AF8CbhP0A8DPwF+l/8Xg8vrcQAAi63yfdAA8rwn/vIWApuS8GQf8L+iHpR/yTivDf+wnpiN/Vl6CjgLeA74GLi/Dfuxj4FniL9I6w0H8CBgHPAp8DI4rw3z0KfAbMIHu3gKAAxEn/l/4U8E4R/nufAt4GppC9W0CQAAC4AHgB+BwYVoT/7mHA56R3hM+CagBx0v+lrwXeAMYX4b83HniD9I6wNswGEIf0C80LwAfAmCL898YBHyC9I2wJuwE0AReR/u/9OrCoCP+9xcA60jvClsq0/wD1uJ+s6hC8IQAAAABJRU5ErkJggg==";
// Ensure visibility if processing
if (item.isProcessing) {
this.style.opacity = "0.5";
@ -1305,6 +1485,7 @@
var isSelected = state.selectedIds.has(item.id);
// 1. Update Container Class
// Combined Items don't use this update path (they are re-rendered if unlinked)
el.className = 'media-item ' + (item._deleted ? 'deleted-item' : '') + (isSelected ? ' selected' : '');
if (item.isProcessing) el.className += ' processing-card';
@ -1437,12 +1618,16 @@
if (a.type === 'upload') icon = '📤';
if (a.type === 'sync_upload') icon = '☁️';
if (a.type === 'reorder') icon = '🔢';
if (a.type === 'link') icon = '🔗';
if (a.type === 'adopt') icon = '📥';
var label = "";
if (a.type === 'delete') label = 'Delete <b>' + a.name + '</b>';
if (a.type === 'upload') label = 'Upload New <b>' + a.name + '</b>';
if (a.type === 'sync_upload') label = 'Sync Drive File <b>' + a.name + '</b>';
if (a.type === 'reorder') label = 'Update Order';
if (a.type === 'link') label = '<b>' + a.name + '</b>';
if (a.type === 'adopt') label = 'Adopt <b>' + a.name + '</b> from Shopify';
return '<div style="margin-bottom:8px;">' + (i + 1) + '. ' + icon + ' ' + label + '</div>';
}).join('');
@ -1583,21 +1768,41 @@
return;
}
// Prepare a single match for the modal
this.matches = [{
drive: driveItem,
shopify: shopifyItem
}];
this.currentMatchIndex = 0;
// Custom text for manual link
document.getElementById('match-modal-title').innerText = "Confirm Manual Link";
document.getElementById('match-modal-text').innerText = "Are you sure you want to link these two items?";
this.startMatching();
// Queue Link
state.tentativeLinks.push({ driveId: driveItem.id, shopifyId: shopifyItem.id });
state.selectedIds.clear();
ui.render(state.items);
state.checkDirty();
},
saveChanges() {
// Intercept for Tentative Links (Batch Manual Linking)
if (state.tentativeLinks.length > 0) {
var manualMatches = [];
state.tentativeLinks.forEach(function (l) {
var d = state.items.find(function (i) { return i.id === l.driveId; });
var s = state.items.find(function (i) { return i.id === l.shopifyId; });
if (d && s) {
manualMatches.push({ drive: d, shopify: s });
}
});
if (manualMatches.length > 0) {
this.matches = manualMatches;
this.currentMatchIndex = 0;
this.postMatchAction = 'save';
ui.logStatus('info', 'Processing ' + manualMatches.length + ' pending links before saving...', 'info');
// Update Modal Text
document.getElementById('match-modal-title').innerText = "Confirm Manual Links";
document.getElementById('match-modal-text').innerText = "Please confirm the links you selected.";
this.startMatching();
return;
}
}
ui.toggleSave(false);
ui.saveBtn.innerText = "Saving...";
ui.setSavingState(true);
@ -2026,6 +2231,39 @@
google.script.run
.withSuccessHandler(function () {
ui.logStatus('link', 'Linked ' + match.drive.filename, 'success');
// Update Local State to prevent save conflicts
var d = state.items.find(function (i) { return i.id === match.drive.id; });
var s = state.items.find(function (i) { return i.id === match.shopify.id; });
if (d && s) {
var dIdx = state.items.indexOf(d);
var sIdx = state.items.indexOf(s);
if (dIdx !== -1 && sIdx !== -1) {
var targetIdx = Math.min(dIdx, sIdx);
// Update d
d.source = 'synced';
d.shopifyId = s.id;
d.status = 'synced';
// Remove both from current positions
// We filter by reference to handle duplicate ID edge cases safely, though ID should be unique
// But 'indexOf' found specific objects.
// Easiest is to splice high then low to avoid index shift?
// Or just filter out s then move d?
// Logic:
// 1. Remove s.
// 2. If s was before d, d's index decreased.
// 3. Move d to targetIdx.
// Simpler: Filter both out, then insert d at targetIdx.
state.items = state.items.filter(function (i) { return i !== d && i !== s; });
state.items.splice(targetIdx, 0, d);
}
}
_this.pendingLinks--;
_this.checkMatchingDone();
})
@ -2078,6 +2316,18 @@
// All Done
document.getElementById('matching-modal').style.display = 'none';
// Clear tentative links as they are now processed (linked or skipped)
state.tentativeLinks = [];
// Check Post-Match Action
if (this.postMatchAction === 'save') {
this.postMatchAction = null;
ui.logStatus('info', 'Matching complete. Proceeding to save...', 'info');
this.saveChanges();
return;
}
ui.logStatus('info', 'Matching complete. Finalizing gallery...', 'info');
// Show main UI immediately if not already showing