react-magic-search-params 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,514 @@
1
+ import { useSearchParams } from 'react-router-dom'
2
+ import { useMemo, useEffect, useRef, useCallback } from 'react'
3
+
4
+
5
+ // Custom hook with advanced techniques to handle search parameters for any pagination
6
+
7
+ type CommonParams = {
8
+ page?: number
9
+ page_size?: number
10
+ }
11
+ /*
12
+ Maps all properties of M (mandatory) as required
13
+ and all properties of O (optional) as optional.
14
+ */
15
+ type MergeParams<M, O> = {
16
+ [K in keyof M]: M[K]
17
+ } & {
18
+ [K in keyof O]?: O[K]
19
+ }
20
+ /**
21
+ * Interface for the configuration object that the hook receives
22
+ */
23
+ interface UseMagicSearchParamsOptions<
24
+ M extends Record<string, unknown>,
25
+ O extends Record<string, unknown>
26
+ > {
27
+ mandatory: M
28
+ optional?: O
29
+ defaultParams?: Partial<MergeParams<M, O>>
30
+ forceParams?: Partial<MergeParams<M, O>> // transform all to partial to avoid errors
31
+ arraySerialization?: 'csv' | 'repeat' | 'brackets' // technical to serialize arrays in the URL
32
+ omitParamsByValues?: Array<'all' | 'default' | 'unknown' | 'none' | 'void '>
33
+ }
34
+
35
+ /**
36
+ Generic hook to handle search parameters in the URL
37
+ @param mandatory - Mandatory parameters (e.g., page=1, page_size=10, etc.)
38
+ @param optional - Optional parameters (e.g., order, search, etc.)
39
+ @param defaultParams - Default parameters sent in the URL on initialization
40
+ @param forceParams - Parameters forced into the URL regardless of user input
41
+ @param omitParamsByValues - Parameters omitted if they have specific values
42
+ */
43
+ export const useMagicSearchParams = <
44
+ M extends Record<string, unknown> & CommonParams,
45
+ O extends Record<string, unknown>,
46
+ >({
47
+ mandatory = {} as M,
48
+ optional = {} as O,
49
+ defaultParams = {} as Partial<MergeParams<M, O>>,
50
+ arraySerialization = 'csv',
51
+ forceParams = {} as {} as Partial<MergeParams<M, O>>,
52
+ omitParamsByValues = [] as Array<'all' | 'default' | 'unknown' | 'none' | 'void '>
53
+ }: UseMagicSearchParamsOptions<M, O>)=> {
54
+
55
+
56
+ const [searchParams, setSearchParams] = useSearchParams()
57
+ // Ref to store subscriptions: { paramName: [callback1, callback2, ...] }
58
+ const subscriptionsRef = useRef<Record<string, Array<() => unknown>>>({});
59
+ const previousParamsRef = useRef<Record<string, unknown>>({})
60
+
61
+ const TOTAL_PARAMS_PAGE: MergeParams<M, O> = useMemo(() => {
62
+ return { ...mandatory, ...optional };
63
+ }, [mandatory, optional]);
64
+
65
+ const PARAM_ORDER = useMemo(() => {
66
+ return Array.from(Object.keys(TOTAL_PARAMS_PAGE))
67
+ }, [TOTAL_PARAMS_PAGE])
68
+
69
+ // we get the keys that are arrays according to TOTAL_PARAMS_PAGE since these require special treatment in the URL due to serialization mode
70
+ const ARRAY_KEYS = useMemo(() => {
71
+ return Object.keys(TOTAL_PARAMS_PAGE).filter(
72
+ (key) => Array.isArray(TOTAL_PARAMS_PAGE[key])
73
+ );
74
+ }, [TOTAL_PARAMS_PAGE])
75
+
76
+ const appendArrayValues = (
77
+ finallyParams: Record<string, unknown>,
78
+ newParams: Record<string, string | string[] | unknown>
79
+ ): Record<string, unknown> => {
80
+
81
+ // Note: We cannot modify the object of the final parameters directly, as immutability must be maintained
82
+ const updatedParams = { ...finallyParams };
83
+
84
+
85
+ if (ARRAY_KEYS.length === 0) return updatedParams;
86
+
87
+ ARRAY_KEYS.forEach((key) => {
88
+ // We use the current values directly from searchParams (source of truth)
89
+ // This avoids depending on finallyParams in which the arrays have been omitted
90
+ let currentValues = [];
91
+ switch (arraySerialization) {
92
+ case 'csv': {
93
+ const raw = searchParams.get(key) || '';
94
+ // For csv we expect "value1,value2,..." (no prefix)
95
+ currentValues = raw.split(',')
96
+ .map((v) => v.trim())
97
+ .filter(Boolean) as Array<string>
98
+ break;
99
+ }
100
+ case 'repeat': {
101
+ // Here we get all ocurrences of key
102
+ const urlParams = searchParams.getAll(key) as Array<string>
103
+ currentValues = urlParams.length > 0 ? urlParams : []
104
+
105
+ console.log({REPEAT: currentValues})
106
+ break;
107
+ }
108
+ case 'brackets': {
109
+ // Build URLSearchParams from current parameters (to ensure no serialized values are taken previously)
110
+ const urlParams = searchParams.getAll(`${key}[]`) as Array<string>
111
+ currentValues = urlParams.length > 0 ? urlParams : []
112
+ console.log({BRACKETS: urlParams})
113
+
114
+
115
+ break;
116
+ }
117
+ default: {
118
+ // Mode by default works as csv
119
+ const raw = searchParams.get(key) ?? '';
120
+ currentValues = raw.split(',')
121
+ .map((v) => v.trim())
122
+ .filter(Boolean);
123
+ }
124
+ break;
125
+ }
126
+ // Update array values with new ones
127
+
128
+ if (newParams[key] !== undefined) {
129
+ const incoming = newParams[key];
130
+ let combined: string[] = []
131
+ if (typeof incoming === 'string') {
132
+ // If it is a string, it is toggled (add/remove)
133
+ combined = currentValues.includes(incoming)
134
+ ? currentValues.filter((v) => v !== incoming)
135
+ : [...currentValues, incoming];
136
+ console.log({currentValues})
137
+ console.log({incoming})
138
+ console.log({CONBINED_STRING: combined})
139
+ } else if (Array.isArray(incoming)) {
140
+ // if an array is passed, repeated values are merged into a single value
141
+ // Note: Set is used to remove duplicates
142
+ combined = Array.from(new Set([ ...incoming]));
143
+ console.log({incoming})
144
+ console.log({combined})
145
+ } else {
146
+
147
+ combined = currentValues;
148
+ }
149
+
150
+ updatedParams[key] = combined
151
+
152
+ }
153
+ });
154
+ console.log({updatedParams})
155
+ return updatedParams
156
+ };
157
+
158
+ const transformParamsToURLSearch = (params: Record<string, unknown>): URLSearchParams => {
159
+ console.log({PARAMS_RECIBIDOS_TRANSFORM: params})
160
+
161
+ const newParam: URLSearchParams = new URLSearchParams()
162
+
163
+ const paramsKeys = Object.keys(params)
164
+
165
+ for (const key of paramsKeys) {
166
+ if (Array.isArray(TOTAL_PARAMS_PAGE[key])) {
167
+ const arrayValue = params[key] as unknown[]
168
+ console.log({arrayValue})
169
+ switch (arraySerialization) {
170
+ case 'csv': {
171
+ const csvValue = arrayValue.join(',')
172
+ // set ensure that the previous value is replaced
173
+ newParam.set(key, csvValue)
174
+ break
175
+ } case 'repeat': {
176
+
177
+ for (const item of arrayValue) {
178
+ console.log({item})
179
+ // add new value to the key, instead of replacing it
180
+ newParam.append(key, item as string)
181
+
182
+ }
183
+ break
184
+ } case 'brackets': {
185
+ for (const item of arrayValue) {
186
+ newParam.append(`${key}[]`, item as string)
187
+ }
188
+ break
189
+ } default: {
190
+ const csvValue = arrayValue.join(',')
191
+ newParam.set(key, csvValue)
192
+ }
193
+ }
194
+ } else {
195
+ newParam.set(key, params[key] as string)
196
+ }
197
+ }
198
+ console.log({FINAL: newParam.toString()})
199
+ return newParam
200
+ }
201
+ // @ts-ignore
202
+ const hasForcedParamsValues = ({ paramsForced, compareParams }) => {
203
+
204
+ // Iterates over the forced parameters and verifies that they exist in the URL and match their values
205
+ // Ej: { page: 1, page_size: 10 } === { page: 1, page_size: 10 } => true
206
+ const allParamsMatch = Object.entries(paramsForced).every(
207
+ ([key, value]) => compareParams[key] === value
208
+ );
209
+
210
+ return allParamsMatch;
211
+ };
212
+
213
+ useEffect(() => {
214
+
215
+ const keysDefaultParams: string[] = Object.keys(defaultParams)
216
+ const keysForceParams: string[] = Object.keys(forceParams)
217
+ if(keysDefaultParams.length === 0 && keysForceParams.length === 0) return
218
+
219
+
220
+ function handleStartingParams() {
221
+
222
+ const defaultParamsString = transformParamsToURLSearch(defaultParams).toString()
223
+ const paramsUrl = getParams()
224
+ const paramsUrlString = transformParamsToURLSearch(paramsUrl).toString()
225
+ const forceParamsString = transformParamsToURLSearch(forceParams).toString()
226
+
227
+ console.log({defaultParamsString})
228
+
229
+ const isForcedParams: boolean = hasForcedParamsValues({ paramsForced: forceParams, compareParams: paramsUrl })
230
+
231
+ if (!isForcedParams) {
232
+
233
+ // In this case, the forced parameters take precedence over the default parameters and the parameters of the current URL (which could have been modified by the user, e.g., page_size=1000)
234
+
235
+ updateParams({ newParams: {
236
+ ...defaultParams,
237
+ ...forceParams
238
+ }})
239
+ return
240
+ }
241
+ // In this way it will be validated that the forced parameters keys and values are in the current URL
242
+ const isIncludesForcedParams = hasForcedParamsValues({ paramsForced: forceParamsString, compareParams: defaultParams })
243
+
244
+ if (keysDefaultParams.length > 0 && isIncludesForcedParams) {
245
+ if (defaultParamsString === paramsUrlString) return // this means that the URL already has the default parameters
246
+ updateParams({ newParams: defaultParams })
247
+ }
248
+
249
+ }
250
+ handleStartingParams()
251
+
252
+ // eslint-disable-next-line react-hooks/exhaustive-deps
253
+ }, [])
254
+
255
+ /**
256
+ * Convert a string value to its original type (number, boolean, array) according to TOTAL_PARAMS_PAGE
257
+ * @param value - Chain obtained from the URL
258
+ * @param key - Key of the parameter
259
+ */
260
+ const convertOriginalType = (value: string, key: string) => {
261
+ // Given that the parameters of a URL are recieved as strings, they are converted to their original type
262
+ if (typeof TOTAL_PARAMS_PAGE[key] === 'number') {
263
+ return parseInt(value)
264
+ } else if (typeof TOTAL_PARAMS_PAGE[key] === 'boolean') {
265
+ return value === 'true'
266
+ } else if (Array.isArray(TOTAL_PARAMS_PAGE[key])) {
267
+ // The result will be a valid array represented in the URL ej: tags=tag1,tag2,tag3 to ['tag1', 'tag2', 'tag3'], useful to combine the values of the arrays with the new ones
268
+
269
+ if (arraySerialization === 'csv') {
270
+ return searchParams.getAll(key).join('').split(',')
271
+ } else if (arraySerialization === 'repeat') {
272
+
273
+ console.log({SEARCH_PARAMS: searchParams.getAll(key)})
274
+ return searchParams.getAll(key)
275
+ } else if (arraySerialization === 'brackets') {
276
+ return searchParams.getAll(`${key}[]`)
277
+ }
278
+
279
+
280
+ }
281
+ // Note: dates are not converted as it is better to handle them directly in the component that receives them, using a library like < date-fns >
282
+ return value
283
+ }
284
+
285
+ /**
286
+ * Gets the current parameters from the URL and converts them to their original type if desired
287
+ * @param convert - If true, converts from string to the inferred type (number, boolean, ...)
288
+ */
289
+ const getStringUrl = (key: string, paramsUrl: Record<string, unknown>) => {
290
+ const isKeyArray = Array.isArray(TOTAL_PARAMS_PAGE[key])
291
+ if (isKeyArray) {
292
+
293
+ if (arraySerialization === 'brackets') {
294
+
295
+ const arrayUrl = searchParams.getAll(`${key}[]`)
296
+ const encodedQueryArray = transformParamsToURLSearch({ [key]: arrayUrl }).toString()
297
+ // in this way the array of the URL is decoded to its original form ej: tags[]=tag1&tags[]=tag2&tags[]=tag3
298
+ const unencodeQuery = decodeURIComponent(encodedQueryArray)
299
+ return unencodeQuery
300
+ } else if (arraySerialization === 'csv') {
301
+ const arrayValue = searchParams.getAll(key)
302
+ const encodedQueryArray = transformParamsToURLSearch({ [key]: arrayValue }).toString()
303
+ const unencodeQuery = decodeURIComponent(encodedQueryArray)
304
+ return unencodeQuery
305
+ }
306
+ const arrayValue = searchParams.getAll(key)
307
+ const stringResult = transformParamsToURLSearch({ [key]: arrayValue }).toString()
308
+ return stringResult
309
+ } else {
310
+
311
+ return paramsUrl[key] as string
312
+ }
313
+ }
314
+ const getParamsObj = (searchParams: URLSearchParams): Record<string, string | string[]> => {
315
+ const paramsObj: Record<string, string | string[]> = {};
316
+ // @ts-ignore
317
+ for (const [key, value] of searchParams.entries()) {
318
+ if (key.endsWith('[]')) {
319
+ const bareKey = key.replace('[]', '');
320
+ if (paramsObj[bareKey]) {
321
+ (paramsObj[bareKey] as string[]).push(value);
322
+ } else {
323
+ paramsObj[bareKey] = [value];
324
+ }
325
+ } else {
326
+ // If the key already exists, it is a repeated parameter
327
+ if (paramsObj[key]) {
328
+ if (Array.isArray(paramsObj[key])) {
329
+ (paramsObj[key] as string[]).push(value);
330
+ } else {
331
+ paramsObj[key] = [paramsObj[key] as string, value];
332
+ }
333
+ } else {
334
+ paramsObj[key] = value;
335
+ }
336
+ }
337
+ }
338
+ return paramsObj;
339
+ }
340
+ // Optimization: While the parameters are not updated, the current parameters of the URL are not recalculated
341
+ const CURRENT_PARAMS_URL: Record<string, unknown> = useMemo(() => {
342
+
343
+ return arraySerialization === 'brackets' ? getParamsObj(searchParams) : Object.fromEntries(searchParams.entries())
344
+ }, [searchParams, arraySerialization])
345
+
346
+ const getParams = ({ convert = true } = {}): MergeParams<M, O> => {
347
+ // All the paramteres are extracted from the URL and converted into an object
348
+
349
+ const params = Object.keys(CURRENT_PARAMS_URL).reduce((acc, key) => {
350
+ if (Object.prototype.hasOwnProperty.call(TOTAL_PARAMS_PAGE, key)) {
351
+ const realKey = arraySerialization === 'brackets' ? key.replace('[]', '') : key
352
+ // @ts-ignore
353
+ acc[realKey] = convert === true
354
+ ? convertOriginalType(CURRENT_PARAMS_URL[key] as string, key)
355
+ : getStringUrl(key, CURRENT_PARAMS_URL)
356
+ }
357
+ return acc
358
+ }, {})
359
+
360
+ return params as MergeParams<M, O>
361
+ }
362
+ type keys = keyof MergeParams<M, O>
363
+ // Note: in this way the return of the getParam function is typed dynamically, thus having autocomplete in the IDE (eg: value.split(','))
364
+ type TagReturn<T extends boolean> = T extends true ? string[] : string;
365
+ const getParam = <T extends boolean>(key: keys, options?: { convert: T }): TagReturn<T> => {
366
+
367
+ const keyStr = String(key)
368
+ // @ts-ignore
369
+ const value = options?.convert === true ? convertOriginalType(searchParams.get(keyStr), keyStr) : getStringUrl(keyStr, CURRENT_PARAMS_URL)
370
+ return value as TagReturn<T>
371
+ }
372
+
373
+ type OptionalParamsFiltered = Partial<O>
374
+
375
+ const calculateOmittedParameters = (newParams: Record<string, unknown | unknown[]>, keepParams: Record<string, boolean>) => {
376
+ // Calculate the ommited parameters, that is, the parameters that have not been sent in the request
377
+ const params = getParams()
378
+ // hasOw
379
+ // Note: it will be necessary to omit the parameters that are arrays because the idea is not to replace them but to add or remove some values
380
+ const newParamsWithoutArray = Object.entries(newParams).filter(([key,]) => !Array.isArray(TOTAL_PARAMS_PAGE[key]))
381
+ const result = Object.assign({
382
+ ...params,
383
+ ...Object.fromEntries(newParamsWithoutArray),
384
+ ...forceParams // the forced parameters will always be sent and will maintain their value
385
+ })
386
+ const paramsFiltered: OptionalParamsFiltered = Object.keys(result).reduce((acc, key) => {
387
+ // for default no parameters are omitted unless specified in the keepParams object
388
+ if (Object.prototype.hasOwnProperty.call(keepParams, key) && keepParams[key] === false) {
389
+ return acc
390
+ // Note: They array of parameters omitted by values (e.g., ['all', 'default']) are omitted since they are usually a default value that is not desired to be sent
391
+ } else if (!!result[key] !== false && !omitParamsByValues.includes(result[key])) {
392
+ // @ts-ignore
393
+ acc[key] = result[key]
394
+ }
395
+
396
+ return acc
397
+ }, {})
398
+
399
+ return {
400
+ ...mandatory,
401
+ ...paramsFiltered
402
+ }
403
+ }
404
+ // @ts-ignore
405
+ const sortParameters = (paramsFiltered) => {
406
+ // sort the parameters according to the structure so that it persists with each change in the URL, eg: localhost:3000/?page=1&page_size=10
407
+ // Note: this visibly improves the user experience
408
+ const orderedParams = PARAM_ORDER.reduce((acc, key) => {
409
+ if (Object.prototype.hasOwnProperty.call(paramsFiltered, key)) {
410
+ // @ts-ignore
411
+ acc[key] = paramsFiltered[key]
412
+ }
413
+
414
+ return acc
415
+ }, {})
416
+ return orderedParams
417
+ }
418
+
419
+ const mandatoryParameters = () => {
420
+ // Note: in case there are arrays in the URL, they are converted to their original form ej: tags=['tag1', 'tag2'] otherwise the parameters are extracted without converting to optimize performance
421
+ const isNecessaryConvert: boolean = ARRAY_KEYS.length > 0 ? true : false
422
+ const totalParametros: Record<string, unknown> = getParams({ convert: isNecessaryConvert })
423
+
424
+ const paramsUrlFound: Record<string, boolean> = Object.keys(totalParametros).reduce(
425
+ (acc, key) => {
426
+ if (Object.prototype.hasOwnProperty.call(mandatory, key)) {
427
+ // @ts-ignore
428
+ acc[key] = totalParametros[key]
429
+ }
430
+ return acc
431
+ },
432
+ {}
433
+ )
434
+
435
+ return paramsUrlFound
436
+ }
437
+
438
+ const clearParams = ({ keepMandatoryParams = true } = {}): void => {
439
+ // for default, the mandatory parameters are not cleared since the current pagination would be lost
440
+ const paramsTransformed = transformParamsToURLSearch(
441
+ {
442
+ ...mandatory,
443
+
444
+ ...(keepMandatoryParams && {
445
+ ...mandatoryParameters()
446
+ }),
447
+ ...forceParams
448
+ }
449
+ )
450
+ setSearchParams(paramsTransformed)
451
+ }
452
+
453
+ // transforms the keys to boolean to know which parameters to keep
454
+ type KeepParamsTransformedValuesBoolean = Partial<Record<keyof typeof TOTAL_PARAMS_PAGE, boolean>>
455
+ type NewParams = Partial<typeof TOTAL_PARAMS_PAGE>
456
+ type KeepParams = KeepParamsTransformedValuesBoolean
457
+ const updateParams = ({ newParams = {} as NewParams, keepParams = {} as KeepParams } = {}) => {
458
+
459
+ if (
460
+ Object.keys(newParams).length === 0 &&
461
+ Object.keys(keepParams).length === 0
462
+ ) {
463
+ clearParams()
464
+ return
465
+ }
466
+ // @ts-ignore
467
+ const finallyParamters = calculateOmittedParameters(newParams, keepParams)
468
+
469
+ const convertedArrayValues = appendArrayValues(finallyParamters, newParams)
470
+
471
+ const paramsSorted = sortParameters(convertedArrayValues)
472
+
473
+ setSearchParams(transformParamsToURLSearch(paramsSorted))
474
+
475
+ }
476
+
477
+ // only for the keys of the parameters to subscribe to changes in the URL to trigger the callback
478
+ const onChange = useCallback( (paramName: keys, callbacks: Array<() => void>) => {
479
+ const paramNameStr = String(paramName)
480
+ // replace the previous callbacks with the new ones so as not to accumulate callbacks
481
+ subscriptionsRef.current[paramNameStr] = callbacks;
482
+ }, [])
483
+
484
+ // each time searchParams changes, we notify the subscribers
485
+ useEffect(() => {
486
+
487
+ for (const [key, value] of Object.entries(subscriptionsRef.current)) {
488
+
489
+ const newValue = CURRENT_PARAMS_URL[key] ?? null
490
+ const oldValue = previousParamsRef.current[key] ?? null
491
+ if (newValue !== oldValue) {
492
+
493
+ for (const callback of value) {
494
+ console.log(value)
495
+
496
+ callback()
497
+
498
+ }
499
+ }
500
+ // once the callback is executed, the previous value is updated to ensure that the next time the value changes, the callback is executed
501
+ previousParamsRef.current[key] = newValue
502
+ }
503
+
504
+
505
+ }, [CURRENT_PARAMS_URL])
506
+ return {
507
+ searchParams,
508
+ updateParams,
509
+ clearParams,
510
+ getParams,
511
+ getParam,
512
+ onChange
513
+ }
514
+ }