The products to persist.
Optionalquery: stringThe originating search query. Omitted for callers with no query
context; stored as undefined in that case.
await setSearchResults([{ id: "A", ... }], "acetone");
export async function setSearchResults(results: Product[], query?: string): Promise<void> {
try {
const db = await getDB();
// Detect (do NOT silently remove) duplicates before persisting. The same
// product appearing twice means the search almost certainly fired twice —
// surface that loudly instead of masking the underlying double-search bug.
const duplicateIds = findDuplicateProductIds(results);
if (duplicateIds.length > 0) {
logger.warn('Duplicate products in search results — the search likely fired twice', {
duplicateCount: duplicateIds.length,
duplicateIds,
total: results.length,
});
}
await db.put(IDB_STORE.SEARCH_RESULTS, { id: 'current', data: results, query });
if (results.length === 0) {
emitSearchResultsCleared();
}
} catch (error) {
logger.error('Failed to set search results in IndexedDB', { error });
}
}
Persists the current search results (and the query that produced them) to the single
"current"search_resultsrow. Storing thequeryalongsidedatakeeps the two in lockstep, so the results header can rehydrate the query even when the transient session copy is gone. Duplicate products are logged (not removed); an empty result set dispatches IDB_SEARCH_RESULTS_CLEARED.