urlState.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. export const SEARCH_SCOPE = Object.freeze({
  2. SINGLE: "single",
  3. MULTI: "multi",
  4. ALL: "all",
  5. });
  6. /**
  7. * Read a query parameter from either:
  8. * - URLSearchParams (client-side `useSearchParams()`)
  9. * - Next.js server `searchParams` object (plain object with string or string[])
  10. *
  11. * @param {any} searchParams
  12. * @param {string} key
  13. * @returns {string|null}
  14. */
  15. function readParam(searchParams, key) {
  16. if (!searchParams) return null;
  17. // URLSearchParams-like object: has .get()
  18. if (typeof searchParams.get === "function") {
  19. const value = searchParams.get(key);
  20. return typeof value === "string" ? value : null;
  21. }
  22. // Next.js server searchParams: plain object
  23. const raw = searchParams[key];
  24. if (Array.isArray(raw)) {
  25. return typeof raw[0] === "string" ? raw[0] : null;
  26. }
  27. return typeof raw === "string" ? raw : null;
  28. }
  29. function normalizeTrimmedOrNull(value) {
  30. if (typeof value !== "string") return null;
  31. const s = value.trim();
  32. return s ? s : null;
  33. }
  34. function uniqueStable(items) {
  35. const out = [];
  36. const seen = new Set();
  37. for (const it of items) {
  38. const s = String(it);
  39. if (!s) continue;
  40. if (seen.has(s)) continue;
  41. seen.add(s);
  42. out.push(s);
  43. }
  44. return out;
  45. }
  46. /**
  47. * Parse comma-separated branches param (branches=NL01,NL02).
  48. *
  49. * @param {string|null} raw
  50. * @returns {string[]}
  51. */
  52. export function parseBranchesCsv(raw) {
  53. const s = typeof raw === "string" ? raw.trim() : "";
  54. if (!s) return [];
  55. const parts = s
  56. .split(",")
  57. .map((x) => String(x).trim())
  58. .filter(Boolean);
  59. return uniqueStable(parts);
  60. }
  61. /**
  62. * Serialize branches as comma-separated string.
  63. *
  64. * @param {string[]|null|undefined} branches
  65. * @returns {string|null}
  66. */
  67. export function serializeBranchesCsv(branches) {
  68. if (!Array.isArray(branches) || branches.length === 0) return null;
  69. const cleaned = uniqueStable(
  70. branches.map((b) => String(b).trim()).filter(Boolean)
  71. );
  72. return cleaned.length > 0 ? cleaned.join(",") : null;
  73. }
  74. /**
  75. * Parse search URL state from query params.
  76. *
  77. * Precedence (robust + shareable):
  78. * 1) scope=all -> ALL
  79. * 2) scope=multi OR branches=... -> MULTI
  80. * 3) branch=... or fallback routeBranch -> SINGLE
  81. *
  82. * Notes:
  83. * - For SINGLE we default to the route branch if branch param is missing.
  84. * - For MULTI/ALL we intentionally return branch=null (UI is route-context but API scope is not single).
  85. *
  86. * @param {URLSearchParams|Record<string, any>|any} searchParams
  87. * @param {{ routeBranch?: string|null }=} options
  88. * @returns {{
  89. * q: string|null,
  90. * scope: "single"|"multi"|"all",
  91. * branch: string|null,
  92. * branches: string[],
  93. * from: string|null,
  94. * to: string|null
  95. * }}
  96. */
  97. export function parseSearchUrlState(searchParams, { routeBranch = null } = {}) {
  98. const q = normalizeTrimmedOrNull(readParam(searchParams, "q"));
  99. const scopeRaw = normalizeTrimmedOrNull(readParam(searchParams, "scope"));
  100. const branchRaw = normalizeTrimmedOrNull(readParam(searchParams, "branch"));
  101. const branches = parseBranchesCsv(readParam(searchParams, "branches"));
  102. const from = normalizeTrimmedOrNull(readParam(searchParams, "from"));
  103. const to = normalizeTrimmedOrNull(readParam(searchParams, "to"));
  104. let scope = SEARCH_SCOPE.SINGLE;
  105. if (scopeRaw === "all") {
  106. scope = SEARCH_SCOPE.ALL;
  107. } else if (scopeRaw === "multi" || branches.length > 0) {
  108. scope = SEARCH_SCOPE.MULTI;
  109. }
  110. if (scope === SEARCH_SCOPE.SINGLE) {
  111. const fallbackRouteBranch = normalizeTrimmedOrNull(routeBranch);
  112. const branch = branchRaw || fallbackRouteBranch || null;
  113. return {
  114. q,
  115. scope,
  116. branch,
  117. branches: [],
  118. from,
  119. to,
  120. };
  121. }
  122. if (scope === SEARCH_SCOPE.MULTI) {
  123. return {
  124. q,
  125. scope,
  126. branch: null,
  127. branches,
  128. from,
  129. to,
  130. };
  131. }
  132. // ALL
  133. return {
  134. q,
  135. scope: SEARCH_SCOPE.ALL,
  136. branch: null,
  137. branches: [],
  138. from,
  139. to,
  140. };
  141. }
  142. /**
  143. * Serialize search URL state into a stable query string (no leading "?").
  144. *
  145. * Stable param ordering:
  146. * - q
  147. * - scope
  148. * - branch
  149. * - branches
  150. * - from
  151. * - to
  152. *
  153. * @param {{
  154. * q?: string|null,
  155. * scope?: "single"|"multi"|"all"|string|null,
  156. * branch?: string|null,
  157. * branches?: string[]|null,
  158. * from?: string|null,
  159. * to?: string|null
  160. * }} state
  161. * @returns {string}
  162. */
  163. export function serializeSearchUrlState(state) {
  164. const s = state || {};
  165. const params = new URLSearchParams();
  166. const q = normalizeTrimmedOrNull(s.q);
  167. const from = normalizeTrimmedOrNull(s.from);
  168. const to = normalizeTrimmedOrNull(s.to);
  169. if (q) params.set("q", q);
  170. // Scope: we only emit what the UI needs for shareability.
  171. if (s.scope === SEARCH_SCOPE.ALL) {
  172. params.set("scope", "all");
  173. } else if (s.scope === SEARCH_SCOPE.MULTI) {
  174. params.set("scope", "multi");
  175. const csv = serializeBranchesCsv(s.branches);
  176. if (csv) params.set("branches", csv);
  177. } else {
  178. // SINGLE
  179. const branch = normalizeTrimmedOrNull(s.branch);
  180. if (branch) params.set("branch", branch);
  181. }
  182. // from/to are additive for RHL-025. We allow carrying them already.
  183. if (from) params.set("from", from);
  184. if (to) params.set("to", to);
  185. return params.toString();
  186. }