ChemPal Documentation - v1.6.0
    Preparing search index...

    Function setSearchResults

    • Persists the current search results (and the query that produced them) to the single "current" search_results row. Storing the query alongside data keeps 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.

      Parameters

      • results: Product[]

        The products to persist.

      • Optionalquery: string

        The originating search query. Omitted for callers with no query context; stored as undefined in that case.

      Returns Promise<void>

      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 });
      }
      }