All files / src/state resolveInitialPanel.ts

100% Statements 12/12
100% Branches 7/7
100% Functions 1/1
100% Lines 12/12

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 361x   1x                                       1x 1x 12x 12x   12x 12x 12x     12x 12x 12x  
import { CACHE, PANEL } from '@/constants/common';
 
/**
 * Decides which panel the app should land on when the popup/tab opens, given the
 * restored session data and the number of cached search results. A queued
 * context-menu search always wins (land on Results so it can execute); otherwise
 * cached results open the Results table, and an empty cache falls back to the
 * search home. A previously-saved Stats selection is deliberately never honored
 * — that panel only renders in the session-only advanced mode, which is always
 * off at mount, so restoring it would strand the user on a blank panel.
 * @param sessionData - The restored `cstorage.session` values (`CACHE.QUERY`, `CACHE.SEARCH_IS_NEW_SEARCH`).
 * @param resultCount - The number of cached search results (from IndexedDB).
 * @returns The panel to activate on open.
 * @category Utils
 * @group State
 * @example
 * ```ts
 * resolveInitialPanel({ is_new_search: true, query: 'acetone' }, 0); // PANEL.RESULTS
 * resolveInitialPanel({}, 5);                                        // PANEL.RESULTS
 * resolveInitialPanel({}, 0);                                        // PANEL.SEARCH_HOME
 * ```
 * @source
 */
export function resolveInitialPanel(
  sessionData: Record<string, unknown>,
  resultCount: number,
): PANEL {
  const query = sessionData[CACHE.QUERY];
  const hasPendingSearch = Boolean(
    sessionData[CACHE.SEARCH_IS_NEW_SEARCH] && typeof query === 'string' && query.trim(),
  );
 
  if (hasPendingSearch) return PANEL.RESULTS;
  return resultCount > 0 ? PANEL.RESULTS : PANEL.SEARCH_HOME;
}