{"version":3,"file":"tableState.cjs","sources":["../../../../../../src/components/ui/tables/utils/tableState.ts"],"sourcesContent":["import { FetchParamsType } from \"../../../../types\";\n\n/**\n * Persisted table state helpers for IHubTableServer.\n *\n * The table remembers where the user was (page, rows per page, sort, search)\n * in `sessionStorage`, so that navigating to a detail page and coming back\n * lands on the same page instead of page 1. Session storage is per tab and\n * is cleared when the tab closes, which matches the \"resume where I was\"\n * expectation without leaking state across sessions.\n */\n\nexport const TABLE_STATE_STORAGE_PREFIX = \"ihub-table-state:\";\n\n/** The subset of request params that is worth remembering between visits. */\nexport type PersistedTableParamsType = Pick<\n  FetchParamsType,\n  \"page\" | \"limit\" | \"search\" | \"sort\" | \"direction\"\n>;\n\nexport interface PersistedTableStateType {\n  params: PersistedTableParamsType;\n  /**\n   * Value-based key of the consumer's `searchParams` at the time of saving.\n   * A page number only makes sense for the filter set it was reached with,\n   * so `page` and `search` are only restored when this key still matches.\n   */\n  searchParamsKey: string;\n  savedAt: number;\n  /**\n   * `keyExtractor` value of the row the user last opened from this table, so\n   * the table can highlight and focus it when they come back.\n   */\n  rowKey?: string | number;\n}\n\nconst isPositiveInteger = (value: unknown): value is number =>\n  typeof value === \"number\" && Number.isInteger(value) && value > 0;\n\n/**\n * Build the storage key that identifies one table.\n *\n * Tables are identified by the page they live on plus the endpoint they read\n * from. That is enough for the common case (one list per page) without any\n * consumer changes. Two tables on one page reading the same endpoint should\n * pass an explicit `persistKey` to keep their state apart.\n */\nexport const buildTableStateKey = (\n  persistKey: string | undefined,\n  endpointPath: string\n): string => {\n  if (persistKey) return `${TABLE_STATE_STORAGE_PREFIX}${persistKey}`;\n\n  const pathname =\n    typeof window !== \"undefined\" && window.location\n      ? window.location.pathname\n      : \"\";\n  return `${TABLE_STATE_STORAGE_PREFIX}${pathname}|${endpointPath}`;\n};\n\nconst getStorage = (): Storage | null => {\n  // Storage can be missing (SSR) or throw (blocked cookies, private mode).\n  try {\n    if (typeof window === \"undefined\" || !window.sessionStorage) return null;\n    return window.sessionStorage;\n  } catch {\n    return null;\n  }\n};\n\n/**\n * Validate a raw stored value into a `PersistedTableStateType`, dropping any\n * field that is not the right shape. Returns null when nothing usable is left.\n */\nexport const parseTableState = (raw: unknown): PersistedTableStateType | null => {\n  if (!raw || typeof raw !== \"object\") return null;\n  const record = raw as Record<string, unknown>;\n  const rawParams =\n    record.params && typeof record.params === \"object\"\n      ? (record.params as Record<string, unknown>)\n      : null;\n  if (!rawParams) return null;\n\n  const params: PersistedTableParamsType = {\n    page: isPositiveInteger(rawParams.page) ? rawParams.page : 1,\n    limit: isPositiveInteger(rawParams.limit) ? rawParams.limit : 10,\n  };\n  if (typeof rawParams.search === \"string\" && rawParams.search) {\n    params.search = rawParams.search;\n  }\n  if (typeof rawParams.sort === \"string\" && rawParams.sort) {\n    params.sort = rawParams.sort;\n  }\n  if (rawParams.direction === \"asc\" || rawParams.direction === \"desc\") {\n    params.direction = rawParams.direction;\n  }\n\n  const parsed: PersistedTableStateType = {\n    params,\n    searchParamsKey:\n      typeof record.searchParamsKey === \"string\" ? record.searchParamsKey : \"\",\n    savedAt: typeof record.savedAt === \"number\" ? record.savedAt : 0,\n  };\n  if (\n    (typeof record.rowKey === \"string\" && record.rowKey) ||\n    typeof record.rowKey === \"number\"\n  ) {\n    parsed.rowKey = record.rowKey;\n  }\n  return parsed;\n};\n\n/**\n * The row to highlight on return, if the table is showing the same list it\n * was left on. A row key from a different filter set is not on screen anyway.\n */\nexport const resolveRestoredRowKey = (\n  stored: PersistedTableStateType | null,\n  currentSearchParamsKey: string\n): string | number | null => {\n  if (!stored || stored.searchParamsKey !== currentSearchParamsKey) return null;\n  return stored.rowKey ?? null;\n};\n\n/** Read and validate the persisted state for a table. Never throws. */\nexport const readTableState = (\n  storageKey: string\n): PersistedTableStateType | null => {\n  const storage = getStorage();\n  if (!storage) return null;\n  try {\n    const raw = storage.getItem(storageKey);\n    if (!raw) return null;\n    return parseTableState(JSON.parse(raw));\n  } catch {\n    return null;\n  }\n};\n\n/** Persist the current state for a table. Never throws. */\nexport const writeTableState = (\n  storageKey: string,\n  state: PersistedTableStateType\n): void => {\n  const storage = getStorage();\n  if (!storage) return;\n  try {\n    storage.setItem(storageKey, JSON.stringify(state));\n  } catch {\n    // Quota exceeded or storage blocked: losing the memory is acceptable.\n  }\n};\n\n/** Remove the persisted state for a table. Never throws. */\nexport const clearTableState = (storageKey: string): void => {\n  const storage = getStorage();\n  if (!storage) return;\n  try {\n    storage.removeItem(storageKey);\n  } catch {\n    // Ignore: nothing to recover from.\n  }\n};\n\n/** Pick only the fields we persist out of the live request params. */\nexport const pickPersistedParams = (\n  params: FetchParamsType\n): PersistedTableParamsType => {\n  const picked: PersistedTableParamsType = {\n    page: params.page,\n    limit: params.limit,\n  };\n  if (params.search) picked.search = params.search;\n  if (params.sort) picked.sort = params.sort;\n  if (params.direction) picked.direction = params.direction;\n  return picked;\n};\n\n/**\n * Work out which persisted fields apply to the current filter set.\n *\n * - Same `searchParamsKey`: everything is restored, the user is looking at the\n *   same list they left.\n * - Different key: only the filter-independent preferences (rows per page and\n *   sort) are restored. The page and search term belonged to another list.\n */\nexport const resolveRestoredParams = (\n  stored: PersistedTableStateType | null,\n  currentSearchParamsKey: string\n): Partial<PersistedTableParamsType> => {\n  if (!stored) return {};\n  if (stored.searchParamsKey === currentSearchParamsKey) {\n    return { ...stored.params };\n  }\n  const { limit, sort, direction } = stored.params;\n  return {\n    limit,\n    ...(sort ? { sort } : {}),\n    ...(direction ? { direction } : {}),\n  };\n};\n"],"names":["TABLE_STATE_STORAGE_PREFIX","isPositiveInteger","value","Number","isInteger","getStorage","window","sessionStorage","_unused","parseTableState","raw","_typeof","record","rawParams","params","page","limit","search","sort","direction","parsed","searchParamsKey","savedAt","rowKey","persistKey","endpointPath","concat","pathname","location","storageKey","storage","removeItem","_unused4","picked","getItem","JSON","parse","_unused2","stored","currentSearchParamsKey","_objectSpread","_stored$params","_stored$rowKey","state","setItem","stringify","_unused3"],"mappings":"2IAYaA,EAA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