Optimize media manager polling and product info retrieval

- Remove recursive polling in MediaManager.html; context is now loaded once at startup.
- Optimize getSelectedProductInfo in mediaHandlers.ts to reduce SpreadsheetApp API calls.
- Update related tests to match new optimization.
This commit is contained in:
Ben Miller
2025-12-31 08:53:39 -07:00
parent ad67dd9ab5
commit 8487df3ea0
3 changed files with 57 additions and 5 deletions

View File

@ -17,14 +17,34 @@ export function showMediaManager() {
export function getSelectedProductInfo(): { sku: string, title: string } | null {
const ss = new GASSpreadsheetService()
// Optimization: Direct usage to avoid multiple service calls overhead
// Use SpreadsheetApp only once if possible to get active context
const sheet = SpreadsheetApp.getActiveSheet()
if (sheet.getName() !== "product_inventory") return null
const row = sheet.getActiveRange().getRow()
if (row <= 1) return null // Header
const sku = ss.getCellValueByColumnName("product_inventory", row, "sku")
const title = ss.getCellValueByColumnName("product_inventory", row, "title")
// Optimization: Get the whole row values in one go
// We need to know which index is SKU and Title.
// Getting headers once is cheaper than searching by name twice if we cache or just linear scan once.
// Actually, getCellValueByColumnName does: getSheet -> getHeaders (read) -> getRowData (read).
// Doing it twice = 6 operations.
// Let's do it manually efficiently:
const headers = sheet.getRange(1, 1, 1, sheet.getLastColumn()).getValues()[0] as string[];
const skuIdx = headers.indexOf("sku");
const titleIdx = headers.indexOf("title");
if (skuIdx === -1) return null; // No SKU column
// Read the specific row
// getRange(row, 1, 1, lastCol)
const rowValues = sheet.getRange(row, 1, 1, sheet.getLastColumn()).getValues()[0];
const sku = rowValues[skuIdx];
const title = titleIdx !== -1 ? rowValues[titleIdx] : "";
return sku ? { sku: String(sku), title: String(title || "") } : null
}