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 @@
1
+ {"version":3,"file":"react-magic-search-params.cjs.production.min.js","sources":["../src/useMagicSearchParams.ts"],"sourcesContent":["import { useSearchParams } from 'react-router-dom'\r\nimport { useMemo, useEffect, useRef, useCallback } from 'react'\r\n\r\n\r\n// Custom hook with advanced techniques to handle search parameters for any pagination\r\n\r\ntype CommonParams = {\r\n page?: number\r\n page_size?: number\r\n}\r\n/*\r\nMaps all properties of M (mandatory) as required\r\nand all properties of O (optional) as optional. \r\n*/\r\ntype MergeParams<M, O> = {\r\n [K in keyof M]: M[K]\r\n} & {\r\n [K in keyof O]?: O[K]\r\n}\r\n/**\r\n * Interface for the configuration object that the hook receives \r\n */\r\ninterface UseMagicSearchParamsOptions<\r\n M extends Record<string, unknown>,\r\n O extends Record<string, unknown>\r\n> {\r\n mandatory: M \r\n optional?: O\r\n defaultParams?: Partial<MergeParams<M, O>>\r\n forceParams?: Partial<MergeParams<M, O>> // transform all to partial to avoid errors\r\n arraySerialization?: 'csv' | 'repeat' | 'brackets' // technical to serialize arrays in the URL\r\n omitParamsByValues?: Array<'all' | 'default' | 'unknown' | 'none' | 'void '> \r\n}\r\n\r\n/** \r\nGeneric hook to handle search parameters in the URL\r\n@param mandatory - Mandatory parameters (e.g., page=1, page_size=10, etc.)\r\n@param optional - Optional parameters (e.g., order, search, etc.)\r\n@param defaultParams - Default parameters sent in the URL on initialization\r\n@param forceParams - Parameters forced into the URL regardless of user input\r\n@param omitParamsByValues - Parameters omitted if they have specific values \r\n*/\r\nexport const useMagicSearchParams = <\r\n M extends Record<string, unknown> & CommonParams,\r\n O extends Record<string, unknown>,\r\n>({\r\n mandatory = {} as M,\r\n optional = {} as O,\r\n defaultParams = {} as Partial<MergeParams<M, O>>,\r\n arraySerialization = 'csv',\r\n forceParams = {} as {} as Partial<MergeParams<M, O>>,\r\n omitParamsByValues = [] as Array<'all' | 'default' | 'unknown' | 'none' | 'void '>\r\n}: UseMagicSearchParamsOptions<M, O>)=> {\r\n\r\n\r\n const [searchParams, setSearchParams] = useSearchParams() \r\n // Ref to store subscriptions: { paramName: [callback1, callback2, ...] }\r\n const subscriptionsRef = useRef<Record<string, Array<() => unknown>>>({}); \r\n const previousParamsRef = useRef<Record<string, unknown>>({})\r\n\r\n const TOTAL_PARAMS_PAGE: MergeParams<M, O> = useMemo(() => {\r\n return { ...mandatory, ...optional };\r\n }, [mandatory, optional]);\r\n\r\n const PARAM_ORDER = useMemo(() => {\r\n return Array.from(Object.keys(TOTAL_PARAMS_PAGE))\r\n }, [TOTAL_PARAMS_PAGE])\r\n\r\n // we get the keys that are arrays according to TOTAL_PARAMS_PAGE since these require special treatment in the URL due to serialization mode\r\n const ARRAY_KEYS = useMemo(() => {\r\n return Object.keys(TOTAL_PARAMS_PAGE).filter(\r\n (key) => Array.isArray(TOTAL_PARAMS_PAGE[key])\r\n );\r\n }, [TOTAL_PARAMS_PAGE])\r\n\r\n const appendArrayValues = (\r\n finallyParams: Record<string, unknown>,\r\n newParams: Record<string, string | string[] | unknown>\r\n ): Record<string, unknown> => {\r\n \r\n // Note: We cannot modify the object of the final parameters directly, as immutability must be maintained\r\n const updatedParams = { ...finallyParams };\r\n \r\n\r\n if (ARRAY_KEYS.length === 0) return updatedParams;\r\n \r\n ARRAY_KEYS.forEach((key) => {\r\n // We use the current values directly from searchParams (source of truth)\r\n // This avoids depending on finallyParams in which the arrays have been omitted\r\n let currentValues = []; \r\n switch (arraySerialization) {\r\n case 'csv': {\r\n const raw = searchParams.get(key) || '';\r\n // For csv we expect \"value1,value2,...\" (no prefix)\r\n currentValues = raw.split(',')\r\n .map((v) => v.trim())\r\n .filter(Boolean) as Array<string>\r\n break;\r\n }\r\n case 'repeat': {\r\n // Here we get all ocurrences of key\r\n const urlParams = searchParams.getAll(key) as Array<string>\r\n currentValues = urlParams.length > 0 ? urlParams : []\r\n \r\n console.log({REPEAT: currentValues})\r\n break;\r\n }\r\n case 'brackets': {\r\n // Build URLSearchParams from current parameters (to ensure no serialized values are taken previously)\r\n const urlParams = searchParams.getAll(`${key}[]`) as Array<string>\r\n currentValues = urlParams.length > 0 ? urlParams : []\r\n console.log({BRACKETS: urlParams})\r\n \r\n \r\n break;\r\n }\r\n default: {\r\n // Mode by default works as csv\r\n const raw = searchParams.get(key) ?? '';\r\n currentValues = raw.split(',')\r\n .map((v) => v.trim())\r\n .filter(Boolean);\r\n }\r\n break; \r\n }\r\n // Update array values with new ones\r\n \r\n if (newParams[key] !== undefined) {\r\n const incoming = newParams[key];\r\n let combined: string[] = []\r\n if (typeof incoming === 'string') {\r\n // If it is a string, it is toggled (add/remove)\r\n combined = currentValues.includes(incoming)\r\n ? currentValues.filter((v) => v !== incoming)\r\n : [...currentValues, incoming];\r\n console.log({currentValues})\r\n console.log({incoming})\r\n console.log({CONBINED_STRING: combined})\r\n } else if (Array.isArray(incoming)) {\r\n // if an array is passed, repeated values are merged into a single value\r\n // Note: Set is used to remove duplicates\r\n combined = Array.from(new Set([ ...incoming]));\r\n console.log({incoming})\r\n console.log({combined})\r\n } else {\r\n \r\n combined = currentValues;\r\n }\r\n\r\n updatedParams[key] = combined\r\n\r\n }\r\n });\r\n console.log({updatedParams})\r\n return updatedParams\r\n };\r\n\r\n const transformParamsToURLSearch = (params: Record<string, unknown>): URLSearchParams => {\r\n console.log({PARAMS_RECIBIDOS_TRANSFORM: params})\r\n\r\n const newParam: URLSearchParams = new URLSearchParams()\r\n\r\n const paramsKeys = Object.keys(params)\r\n\r\n for (const key of paramsKeys) {\r\n if (Array.isArray(TOTAL_PARAMS_PAGE[key])) {\r\n const arrayValue = params[key] as unknown[]\r\n console.log({arrayValue})\r\n switch (arraySerialization) {\r\n case 'csv': {\r\n const csvValue = arrayValue.join(',')\r\n // set ensure that the previous value is replaced\r\n newParam.set(key, csvValue)\r\n break\r\n } case 'repeat': {\r\n \r\n for (const item of arrayValue) {\r\n console.log({item})\r\n // add new value to the key, instead of replacing it\r\n newParam.append(key, item as string)\r\n \r\n }\r\n break\r\n } case 'brackets': {\r\n for (const item of arrayValue) {\r\n newParam.append(`${key}[]`, item as string)\r\n }\r\n break\r\n } default: {\r\n const csvValue = arrayValue.join(',')\r\n newParam.set(key, csvValue)\r\n }\r\n }\r\n } else {\r\n newParam.set(key, params[key] as string)\r\n }\r\n }\r\n console.log({FINAL: newParam.toString()})\r\n return newParam\r\n }\r\n // @ts-ignore\r\n const hasForcedParamsValues = ({ paramsForced, compareParams }) => {\r\n\r\n // Iterates over the forced parameters and verifies that they exist in the URL and match their values\r\n // Ej: { page: 1, page_size: 10 } === { page: 1, page_size: 10 } => true\r\n const allParamsMatch = Object.entries(paramsForced).every(\r\n ([key, value]) => compareParams[key] === value\r\n );\r\n\r\n return allParamsMatch;\r\n };\r\n \r\n useEffect(() => {\r\n\r\n const keysDefaultParams: string[] = Object.keys(defaultParams)\r\n const keysForceParams: string[] = Object.keys(forceParams)\r\n if(keysDefaultParams.length === 0 && keysForceParams.length === 0) return\r\n \r\n\r\n function handleStartingParams() {\r\n\r\n const defaultParamsString = transformParamsToURLSearch(defaultParams).toString()\r\n const paramsUrl = getParams()\r\n const paramsUrlString = transformParamsToURLSearch(paramsUrl).toString()\r\n const forceParamsString = transformParamsToURLSearch(forceParams).toString()\r\n\r\n console.log({defaultParamsString})\r\n\r\n const isForcedParams: boolean = hasForcedParamsValues({ paramsForced: forceParams, compareParams: paramsUrl })\r\n\r\n if (!isForcedParams) {\r\n\r\n // 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)\r\n\r\n updateParams({ newParams: {\r\n ...defaultParams,\r\n ...forceParams\r\n }})\r\n return\r\n }\r\n // In this way it will be validated that the forced parameters keys and values are in the current URL\r\n const isIncludesForcedParams = hasForcedParamsValues({ paramsForced: forceParamsString, compareParams: defaultParams })\r\n\r\n if (keysDefaultParams.length > 0 && isIncludesForcedParams) {\r\n if (defaultParamsString === paramsUrlString) return // this means that the URL already has the default parameters\r\n updateParams({ newParams: defaultParams })\r\n }\r\n\r\n }\r\n handleStartingParams()\r\n\r\n // eslint-disable-next-line react-hooks/exhaustive-deps\r\n }, [])\r\n\r\n /**\r\n * Convert a string value to its original type (number, boolean, array) according to TOTAL_PARAMS_PAGE\r\n * @param value - Chain obtained from the URL\r\n * @param key - Key of the parameter\r\n */\r\n const convertOriginalType = (value: string, key: string) => {\r\n // Given that the parameters of a URL are recieved as strings, they are converted to their original type\r\n if (typeof TOTAL_PARAMS_PAGE[key] === 'number') {\r\n return parseInt(value)\r\n } else if (typeof TOTAL_PARAMS_PAGE[key] === 'boolean') {\r\n return value === 'true'\r\n } else if (Array.isArray(TOTAL_PARAMS_PAGE[key])) {\r\n // 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\r\n \r\n if (arraySerialization === 'csv') {\r\n return searchParams.getAll(key).join('').split(',')\r\n } else if (arraySerialization === 'repeat') {\r\n \r\n console.log({SEARCH_PARAMS: searchParams.getAll(key)})\r\n return searchParams.getAll(key)\r\n } else if (arraySerialization === 'brackets') {\r\n return searchParams.getAll(`${key}[]`)\r\n }\r\n \r\n \r\n }\r\n // 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 >\r\n return value\r\n }\r\n \r\n /**\r\n * Gets the current parameters from the URL and converts them to their original type if desired\r\n * @param convert - If true, converts from string to the inferred type (number, boolean, ...)\r\n */\r\n const getStringUrl = (key: string, paramsUrl: Record<string, unknown>) => {\r\n const isKeyArray = Array.isArray(TOTAL_PARAMS_PAGE[key])\r\n if (isKeyArray) {\r\n\r\n if (arraySerialization === 'brackets') {\r\n\r\n const arrayUrl = searchParams.getAll(`${key}[]`)\r\n const encodedQueryArray = transformParamsToURLSearch({ [key]: arrayUrl }).toString()\r\n // in this way the array of the URL is decoded to its original form ej: tags[]=tag1&tags[]=tag2&tags[]=tag3\r\n const unencodeQuery = decodeURIComponent(encodedQueryArray)\r\n return unencodeQuery\r\n } else if (arraySerialization === 'csv') {\r\n const arrayValue = searchParams.getAll(key)\r\n const encodedQueryArray = transformParamsToURLSearch({ [key]: arrayValue }).toString()\r\n const unencodeQuery = decodeURIComponent(encodedQueryArray)\r\n return unencodeQuery\r\n }\r\n const arrayValue = searchParams.getAll(key)\r\n const stringResult = transformParamsToURLSearch({ [key]: arrayValue }).toString()\r\n return stringResult\r\n } else {\r\n \r\n return paramsUrl[key] as string\r\n }\r\n }\r\n const getParamsObj = (searchParams: URLSearchParams): Record<string, string | string[]> => {\r\n const paramsObj: Record<string, string | string[]> = {};\r\n // @ts-ignore\r\n for (const [key, value] of searchParams.entries()) {\r\n if (key.endsWith('[]')) {\r\n const bareKey = key.replace('[]', '');\r\n if (paramsObj[bareKey]) {\r\n (paramsObj[bareKey] as string[]).push(value);\r\n } else {\r\n paramsObj[bareKey] = [value];\r\n }\r\n } else {\r\n // If the key already exists, it is a repeated parameter\r\n if (paramsObj[key]) {\r\n if (Array.isArray(paramsObj[key])) {\r\n (paramsObj[key] as string[]).push(value);\r\n } else {\r\n paramsObj[key] = [paramsObj[key] as string, value];\r\n }\r\n } else {\r\n paramsObj[key] = value;\r\n }\r\n }\r\n }\r\n return paramsObj;\r\n }\r\n // Optimization: While the parameters are not updated, the current parameters of the URL are not recalculated\r\n const CURRENT_PARAMS_URL: Record<string, unknown> = useMemo(() => {\r\n\r\n return arraySerialization === 'brackets' ? getParamsObj(searchParams) : Object.fromEntries(searchParams.entries())\r\n }, [searchParams, arraySerialization])\r\n\r\n const getParams = ({ convert = true } = {}): MergeParams<M, O> => {\r\n // All the paramteres are extracted from the URL and converted into an object\r\n\r\n const params = Object.keys(CURRENT_PARAMS_URL).reduce((acc, key) => {\r\n if (Object.prototype.hasOwnProperty.call(TOTAL_PARAMS_PAGE, key)) {\r\n const realKey = arraySerialization === 'brackets' ? key.replace('[]', '') : key\r\n // @ts-ignore\r\n acc[realKey] = convert === true\r\n ? convertOriginalType(CURRENT_PARAMS_URL[key] as string, key)\r\n : getStringUrl(key, CURRENT_PARAMS_URL)\r\n }\r\n return acc\r\n }, {})\r\n \r\n return params as MergeParams<M, O>\r\n }\r\n type keys = keyof MergeParams<M, O>\r\n // Note: in this way the return of the getParam function is typed dynamically, thus having autocomplete in the IDE (eg: value.split(','))\r\n type TagReturn<T extends boolean> = T extends true ? string[] : string;\r\n const getParam = <T extends boolean>(key: keys, options?: { convert: T }): TagReturn<T> => {\r\n\r\n const keyStr = String(key)\r\n // @ts-ignore\r\n const value = options?.convert === true ? convertOriginalType(searchParams.get(keyStr), keyStr) : getStringUrl(keyStr, CURRENT_PARAMS_URL)\r\n return value as TagReturn<T> \r\n }\r\n \r\n type OptionalParamsFiltered = Partial<O>\r\n\r\n const calculateOmittedParameters = (newParams: Record<string, unknown | unknown[]>, keepParams: Record<string, boolean>) => {\r\n // Calculate the ommited parameters, that is, the parameters that have not been sent in the request\r\n const params = getParams()\r\n // hasOw\r\n // 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\r\n const newParamsWithoutArray = Object.entries(newParams).filter(([key,]) => !Array.isArray(TOTAL_PARAMS_PAGE[key]))\r\n const result = Object.assign({\r\n ...params,\r\n ...Object.fromEntries(newParamsWithoutArray),\r\n ...forceParams // the forced parameters will always be sent and will maintain their value\r\n })\r\n const paramsFiltered: OptionalParamsFiltered = Object.keys(result).reduce((acc, key) => {\r\n // for default no parameters are omitted unless specified in the keepParams object\r\n if (Object.prototype.hasOwnProperty.call(keepParams, key) && keepParams[key] === false) {\r\n return acc\r\n // 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\r\n } else if (!!result[key] !== false && !omitParamsByValues.includes(result[key])) {\r\n // @ts-ignore\r\n acc[key] = result[key]\r\n }\r\n\r\n return acc\r\n }, {})\r\n\r\n return {\r\n ...mandatory,\r\n ...paramsFiltered\r\n } \r\n }\r\n // @ts-ignore\r\n const sortParameters = (paramsFiltered) => {\r\n // 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\r\n // Note: this visibly improves the user experience\r\n const orderedParams = PARAM_ORDER.reduce((acc, key) => {\r\n if (Object.prototype.hasOwnProperty.call(paramsFiltered, key)) {\r\n // @ts-ignore\r\n acc[key] = paramsFiltered[key]\r\n }\r\n\r\n return acc\r\n }, {})\r\n return orderedParams\r\n }\r\n\r\n const mandatoryParameters = () => {\r\n // 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\r\n const isNecessaryConvert: boolean = ARRAY_KEYS.length > 0 ? true : false\r\n const totalParametros: Record<string, unknown> = getParams({ convert: isNecessaryConvert })\r\n\r\n const paramsUrlFound: Record<string, boolean> = Object.keys(totalParametros).reduce(\r\n (acc, key) => {\r\n if (Object.prototype.hasOwnProperty.call(mandatory, key)) {\r\n // @ts-ignore\r\n acc[key] = totalParametros[key]\r\n }\r\n return acc\r\n },\r\n {}\r\n )\r\n\r\n return paramsUrlFound\r\n }\r\n\r\n const clearParams = ({ keepMandatoryParams = true } = {}): void => {\r\n // for default, the mandatory parameters are not cleared since the current pagination would be lost\r\n const paramsTransformed = transformParamsToURLSearch(\r\n {\r\n ...mandatory,\r\n \r\n ...(keepMandatoryParams && {\r\n ...mandatoryParameters()\r\n }),\r\n ...forceParams \r\n }\r\n )\r\n setSearchParams(paramsTransformed) \r\n }\r\n\r\n // transforms the keys to boolean to know which parameters to keep\r\n type KeepParamsTransformedValuesBoolean = Partial<Record<keyof typeof TOTAL_PARAMS_PAGE, boolean>>\r\n type NewParams = Partial<typeof TOTAL_PARAMS_PAGE> \r\n type KeepParams = KeepParamsTransformedValuesBoolean\r\n const updateParams = ({ newParams = {} as NewParams, keepParams = {} as KeepParams } = {}) => {\r\n\r\n if (\r\n Object.keys(newParams).length === 0 &&\r\n Object.keys(keepParams).length === 0\r\n ) {\r\n clearParams()\r\n return\r\n }\r\n // @ts-ignore\r\n const finallyParamters = calculateOmittedParameters(newParams, keepParams)\r\n\r\n const convertedArrayValues = appendArrayValues(finallyParamters, newParams)\r\n\r\n const paramsSorted = sortParameters(convertedArrayValues)\r\n\r\n setSearchParams(transformParamsToURLSearch(paramsSorted))\r\n\r\n }\r\n\r\n // only for the keys of the parameters to subscribe to changes in the URL to trigger the callback\r\n const onChange = useCallback( (paramName: keys, callbacks: Array<() => void>) => {\r\n const paramNameStr = String(paramName)\r\n // replace the previous callbacks with the new ones so as not to accumulate callbacks\r\n subscriptionsRef.current[paramNameStr] = callbacks;\r\n }, [])\r\n \r\n // each time searchParams changes, we notify the subscribers\r\n useEffect(() => {\r\n\r\n for (const [key, value] of Object.entries(subscriptionsRef.current)) {\r\n\r\n const newValue = CURRENT_PARAMS_URL[key] ?? null \r\n const oldValue = previousParamsRef.current[key] ?? null\r\n if (newValue !== oldValue) {\r\n \r\n for (const callback of value) {\r\n console.log(value)\r\n\r\n callback()\r\n\r\n }\r\n }\r\n // once the callback is executed, the previous value is updated to ensure that the next time the value changes, the callback is executed\r\n previousParamsRef.current[key] = newValue\r\n }\r\n\r\n \r\n }, [CURRENT_PARAMS_URL])\r\n return {\r\n searchParams,\r\n updateParams,\r\n clearParams,\r\n getParams,\r\n getParam,\r\n onChange\r\n }\r\n}\r\n"],"names":["_ref","mandatory","_ref$mandatory","_ref$optional","optional","_ref$defaultParams","defaultParams","_ref$arraySerializati","arraySerialization","_ref$forceParams","forceParams","_ref$omitParamsByValu","omitParamsByValues","_useSearchParams","useSearchParams","searchParams","setSearchParams","subscriptionsRef","useRef","previousParamsRef","TOTAL_PARAMS_PAGE","useMemo","_extends","PARAM_ORDER","Array","from","Object","keys","ARRAY_KEYS","filter","key","isArray","transformParamsToURLSearch","params","console","log","PARAMS_RECIBIDOS_TRANSFORM","newParam","URLSearchParams","_i","_paramsKeys","length","arrayValue","csvValue","join","set","_step","_iterator","_createForOfIteratorHelperLoose","done","item","value","append","_step2","_iterator2","FINAL","toString","hasForcedParamsValues","_ref2","compareParams","entries","paramsForced","every","_ref3","useEffect","keysDefaultParams","keysForceParams","defaultParamsString","paramsUrl","getParams","paramsUrlString","forceParamsString","isIncludesForcedParams","updateParams","newParams","handleStartingParams","convertOriginalType","parseInt","getAll","split","SEARCH_PARAMS","getStringUrl","_transformParamsToURL3","_transformParamsToURL","arrayUrl","encodedQueryArray","decodeURIComponent","_transformParamsToURL2","CURRENT_PARAMS_URL","_step3","paramsObj","_iterator3","_step3$value","endsWith","bareKey","replace","push","getParamsObj","fromEntries","_temp","_ref4$convert","convert","reduce","acc","prototype","hasOwnProperty","call","clearParams","_temp2","totalParametros","_ref6$keepMandatoryPa","keepMandatoryParams","paramsTransformed","_temp3","_ref7$newParams","_ref7","_ref7$keepParams","keepParams","paramsFiltered","convertedArrayValues","finallyParams","updatedParams","forEach","currentValues","get","map","v","trim","Boolean","urlParams","REPEAT","BRACKETS","_searchParams$get","undefined","incoming","combined","includes","concat","CONBINED_STRING","Set","appendArrayValues","newParamsWithoutArray","_ref5","result","assign","calculateOmittedParameters","paramsSorted","onChange","useCallback","paramName","callbacks","paramNameStr","String","current","_i2","_Object$entries","_CURRENT_PARAMS_URL$k","_previousParamsRef$cu","_Object$entries$_i","newValue","_step4","_iterator4","callback","getParam","options","keyStr"],"mappings":"ipCA0CoC,SAAHA,WAI/BC,UAAAA,WAASC,EAAG,GAAOA,EAAAC,EAAAH,EACnBI,SAAAA,WAAQD,EAAG,GAAOA,EAAAE,EAAAL,EAClBM,cAAAA,WAAaD,EAAG,GAAgCA,EAAAE,EAAAP,EAChDQ,mBAAAA,WAAkBD,EAAG,MAAKA,EAAAE,EAAAT,EAC1BU,YAAAA,WAAWD,EAAG,GAAuCA,EAAAE,EAAAX,EACrDY,mBAAAA,WAAkBD,EAAG,GAA6DA,EAIlFE,EAAwCC,oBAAjCC,EAAYF,KAAEG,EAAeH,KAE9BI,EAAmBC,SAA6C,IAChEC,EAAoB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,IAEH,OAAAjF,KACKrB,EACAqH,GAkEoB2B,CAA2BvE,EAAW2C,GAEE3C,GAE3DwE,GAlEgB5B,EAkEcC,EA/DdhG,EAAY+E,QAAO,SAACC,EAAKzE,GAM7C,OALIJ,OAAO8E,UAAUC,eAAeC,KAAKY,EAAgBxF,KAEvDyE,EAAIzE,GAAOwF,EAAexF,IAGrByE,IACN,KA0DHvF,EAAgBgB,EAA2BkH,SAVzCvC,KAeIwC,EAAWC,eAAa,SAACC,EAAiBC,GAC9C,IAAMC,EAAeC,OAAOH,GAE5BpI,EAAiBwI,QAAQF,GAAgBD,IACxC,IAwBL,OArBEtF,aAAU,WAER,QAAA0F,IAAAC,EAA2BjI,OAAOkC,QAAQ3C,EAAiBwI,SAAQC,EAAAC,EAAAlH,OAAAiH,IAAE,CAAA,IAAAE,EAAAC,EAAhEC,EAAAH,EAAAD,GAAO5H,EAAGgI,KAAE3G,EAAK2G,KAEdC,SAAQH,EAAGpE,EAAmB1D,IAAI8H,EAAI,KAE5C,GAAIG,YADUF,EAAG1I,EAAkBsI,QAAQ3H,IAAI+H,EAAI,MAGjD,QAA4BG,EAA5BC,EAAAjH,EAAuBG,KAAK6G,EAAAC,KAAAhH,MAAE,CAAA,IAAnBiH,EAAQF,EAAA7G,MACjBjB,QAAQC,IAAIgB,GAEZ+G,IAKJ/I,EAAkBsI,QAAQ3H,GAAOiI,KAIlC,CAACvE,IACC,CACLzE,aAAAA,EACA0D,aAAAA,EACAkC,YAAAA,EACAtC,UAAAA,EACA8F,SAlJe,SAAoBrI,EAAWsI,GAE9C,IAAMC,EAASb,OAAO1H,GAGtB,OADmC,WAArBsI,SAAAA,EAAS/D,SAAmBzB,EAAoB7D,EAAa6G,IAAIyC,GAASA,GAAUpF,EAAaoF,EAAS7E,IA+IxH2D,SAAAA"}
@@ -0,0 +1,499 @@
1
+ import { useSearchParams } from 'react-router-dom';
2
+ import { useRef, useMemo, useEffect, useCallback } from 'react';
3
+
4
+ function _arrayLikeToArray(r, a) {
5
+ (null == a || a > r.length) && (a = r.length);
6
+ for (var e = 0, n = Array(a); e < a; e++) n[e] = r[e];
7
+ return n;
8
+ }
9
+ function _createForOfIteratorHelperLoose(r, e) {
10
+ var t = "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"];
11
+ if (t) return (t = t.call(r)).next.bind(t);
12
+ if (Array.isArray(r) || (t = _unsupportedIterableToArray(r)) || e && r && "number" == typeof r.length) {
13
+ t && (r = t);
14
+ var o = 0;
15
+ return function () {
16
+ return o >= r.length ? {
17
+ done: !0
18
+ } : {
19
+ done: !1,
20
+ value: r[o++]
21
+ };
22
+ };
23
+ }
24
+ throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
25
+ }
26
+ function _extends() {
27
+ return _extends = Object.assign ? Object.assign.bind() : function (n) {
28
+ for (var e = 1; e < arguments.length; e++) {
29
+ var t = arguments[e];
30
+ for (var r in t) ({}).hasOwnProperty.call(t, r) && (n[r] = t[r]);
31
+ }
32
+ return n;
33
+ }, _extends.apply(null, arguments);
34
+ }
35
+ function _unsupportedIterableToArray(r, a) {
36
+ if (r) {
37
+ if ("string" == typeof r) return _arrayLikeToArray(r, a);
38
+ var t = {}.toString.call(r).slice(8, -1);
39
+ return "Object" === t && r.constructor && (t = r.constructor.name), "Map" === t || "Set" === t ? Array.from(r) : "Arguments" === t || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(t) ? _arrayLikeToArray(r, a) : void 0;
40
+ }
41
+ }
42
+
43
+ /**
44
+ Generic hook to handle search parameters in the URL
45
+ @param mandatory - Mandatory parameters (e.g., page=1, page_size=10, etc.)
46
+ @param optional - Optional parameters (e.g., order, search, etc.)
47
+ @param defaultParams - Default parameters sent in the URL on initialization
48
+ @param forceParams - Parameters forced into the URL regardless of user input
49
+ @param omitParamsByValues - Parameters omitted if they have specific values
50
+ */
51
+ var useMagicSearchParams = function useMagicSearchParams(_ref) {
52
+ var _ref$mandatory = _ref.mandatory,
53
+ mandatory = _ref$mandatory === void 0 ? {} : _ref$mandatory,
54
+ _ref$optional = _ref.optional,
55
+ optional = _ref$optional === void 0 ? {} : _ref$optional,
56
+ _ref$defaultParams = _ref.defaultParams,
57
+ defaultParams = _ref$defaultParams === void 0 ? {} : _ref$defaultParams,
58
+ _ref$arraySerializati = _ref.arraySerialization,
59
+ arraySerialization = _ref$arraySerializati === void 0 ? 'csv' : _ref$arraySerializati,
60
+ _ref$forceParams = _ref.forceParams,
61
+ forceParams = _ref$forceParams === void 0 ? {} : _ref$forceParams,
62
+ _ref$omitParamsByValu = _ref.omitParamsByValues,
63
+ omitParamsByValues = _ref$omitParamsByValu === void 0 ? [] : _ref$omitParamsByValu;
64
+ var _useSearchParams = useSearchParams(),
65
+ searchParams = _useSearchParams[0],
66
+ setSearchParams = _useSearchParams[1];
67
+ // Ref to store subscriptions: { paramName: [callback1, callback2, ...] }
68
+ var subscriptionsRef = useRef({});
69
+ var previousParamsRef = useRef({});
70
+ var TOTAL_PARAMS_PAGE = useMemo(function () {
71
+ return _extends({}, mandatory, optional);
72
+ }, [mandatory, optional]);
73
+ var PARAM_ORDER = useMemo(function () {
74
+ return Array.from(Object.keys(TOTAL_PARAMS_PAGE));
75
+ }, [TOTAL_PARAMS_PAGE]);
76
+ // we get the keys that are arrays according to TOTAL_PARAMS_PAGE since these require special treatment in the URL due to serialization mode
77
+ var ARRAY_KEYS = useMemo(function () {
78
+ return Object.keys(TOTAL_PARAMS_PAGE).filter(function (key) {
79
+ return Array.isArray(TOTAL_PARAMS_PAGE[key]);
80
+ });
81
+ }, [TOTAL_PARAMS_PAGE]);
82
+ var appendArrayValues = function appendArrayValues(finallyParams, newParams) {
83
+ // Note: We cannot modify the object of the final parameters directly, as immutability must be maintained
84
+ var updatedParams = _extends({}, finallyParams);
85
+ if (ARRAY_KEYS.length === 0) return updatedParams;
86
+ ARRAY_KEYS.forEach(function (key) {
87
+ // We use the current values directly from searchParams (source of truth)
88
+ // This avoids depending on finallyParams in which the arrays have been omitted
89
+ var currentValues = [];
90
+ switch (arraySerialization) {
91
+ case 'csv':
92
+ {
93
+ var raw = searchParams.get(key) || '';
94
+ // For csv we expect "value1,value2,..." (no prefix)
95
+ currentValues = raw.split(',').map(function (v) {
96
+ return v.trim();
97
+ }).filter(Boolean);
98
+ break;
99
+ }
100
+ case 'repeat':
101
+ {
102
+ // Here we get all ocurrences of key
103
+ var urlParams = searchParams.getAll(key);
104
+ currentValues = urlParams.length > 0 ? urlParams : [];
105
+ console.log({
106
+ REPEAT: currentValues
107
+ });
108
+ break;
109
+ }
110
+ case 'brackets':
111
+ {
112
+ // Build URLSearchParams from current parameters (to ensure no serialized values are taken previously)
113
+ var _urlParams = searchParams.getAll(key + "[]");
114
+ currentValues = _urlParams.length > 0 ? _urlParams : [];
115
+ console.log({
116
+ BRACKETS: _urlParams
117
+ });
118
+ break;
119
+ }
120
+ default:
121
+ {
122
+ var _searchParams$get;
123
+ // Mode by default works as csv
124
+ var _raw = (_searchParams$get = searchParams.get(key)) != null ? _searchParams$get : '';
125
+ currentValues = _raw.split(',').map(function (v) {
126
+ return v.trim();
127
+ }).filter(Boolean);
128
+ }
129
+ break;
130
+ }
131
+ // Update array values with new ones
132
+ if (newParams[key] !== undefined) {
133
+ var incoming = newParams[key];
134
+ var combined = [];
135
+ if (typeof incoming === 'string') {
136
+ // If it is a string, it is toggled (add/remove)
137
+ combined = currentValues.includes(incoming) ? currentValues.filter(function (v) {
138
+ return v !== incoming;
139
+ }) : [].concat(currentValues, [incoming]);
140
+ console.log({
141
+ currentValues: currentValues
142
+ });
143
+ console.log({
144
+ incoming: incoming
145
+ });
146
+ console.log({
147
+ CONBINED_STRING: combined
148
+ });
149
+ } else if (Array.isArray(incoming)) {
150
+ // if an array is passed, repeated values are merged into a single value
151
+ // Note: Set is used to remove duplicates
152
+ combined = Array.from(new Set([].concat(incoming)));
153
+ console.log({
154
+ incoming: incoming
155
+ });
156
+ console.log({
157
+ combined: combined
158
+ });
159
+ } else {
160
+ combined = currentValues;
161
+ }
162
+ updatedParams[key] = combined;
163
+ }
164
+ });
165
+ console.log({
166
+ updatedParams: updatedParams
167
+ });
168
+ return updatedParams;
169
+ };
170
+ var transformParamsToURLSearch = function transformParamsToURLSearch(params) {
171
+ console.log({
172
+ PARAMS_RECIBIDOS_TRANSFORM: params
173
+ });
174
+ var newParam = new URLSearchParams();
175
+ var paramsKeys = Object.keys(params);
176
+ for (var _i = 0, _paramsKeys = paramsKeys; _i < _paramsKeys.length; _i++) {
177
+ var key = _paramsKeys[_i];
178
+ if (Array.isArray(TOTAL_PARAMS_PAGE[key])) {
179
+ var arrayValue = params[key];
180
+ console.log({
181
+ arrayValue: arrayValue
182
+ });
183
+ switch (arraySerialization) {
184
+ case 'csv':
185
+ {
186
+ var csvValue = arrayValue.join(',');
187
+ // set ensure that the previous value is replaced
188
+ newParam.set(key, csvValue);
189
+ break;
190
+ }
191
+ case 'repeat':
192
+ {
193
+ for (var _iterator = _createForOfIteratorHelperLoose(arrayValue), _step; !(_step = _iterator()).done;) {
194
+ var item = _step.value;
195
+ console.log({
196
+ item: item
197
+ });
198
+ // add new value to the key, instead of replacing it
199
+ newParam.append(key, item);
200
+ }
201
+ break;
202
+ }
203
+ case 'brackets':
204
+ {
205
+ for (var _iterator2 = _createForOfIteratorHelperLoose(arrayValue), _step2; !(_step2 = _iterator2()).done;) {
206
+ var _item = _step2.value;
207
+ newParam.append(key + "[]", _item);
208
+ }
209
+ break;
210
+ }
211
+ default:
212
+ {
213
+ var _csvValue = arrayValue.join(',');
214
+ newParam.set(key, _csvValue);
215
+ }
216
+ }
217
+ } else {
218
+ newParam.set(key, params[key]);
219
+ }
220
+ }
221
+ console.log({
222
+ FINAL: newParam.toString()
223
+ });
224
+ return newParam;
225
+ };
226
+ // @ts-ignore
227
+ var hasForcedParamsValues = function hasForcedParamsValues(_ref2) {
228
+ var paramsForced = _ref2.paramsForced,
229
+ compareParams = _ref2.compareParams;
230
+ // Iterates over the forced parameters and verifies that they exist in the URL and match their values
231
+ // Ej: { page: 1, page_size: 10 } === { page: 1, page_size: 10 } => true
232
+ var allParamsMatch = Object.entries(paramsForced).every(function (_ref3) {
233
+ var key = _ref3[0],
234
+ value = _ref3[1];
235
+ return compareParams[key] === value;
236
+ });
237
+ return allParamsMatch;
238
+ };
239
+ useEffect(function () {
240
+ var keysDefaultParams = Object.keys(defaultParams);
241
+ var keysForceParams = Object.keys(forceParams);
242
+ if (keysDefaultParams.length === 0 && keysForceParams.length === 0) return;
243
+ function handleStartingParams() {
244
+ var defaultParamsString = transformParamsToURLSearch(defaultParams).toString();
245
+ var paramsUrl = getParams();
246
+ var paramsUrlString = transformParamsToURLSearch(paramsUrl).toString();
247
+ var forceParamsString = transformParamsToURLSearch(forceParams).toString();
248
+ console.log({
249
+ defaultParamsString: defaultParamsString
250
+ });
251
+ var isForcedParams = hasForcedParamsValues({
252
+ paramsForced: forceParams,
253
+ compareParams: paramsUrl
254
+ });
255
+ if (!isForcedParams) {
256
+ // 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)
257
+ updateParams({
258
+ newParams: _extends({}, defaultParams, forceParams)
259
+ });
260
+ return;
261
+ }
262
+ // In this way it will be validated that the forced parameters keys and values are in the current URL
263
+ var isIncludesForcedParams = hasForcedParamsValues({
264
+ paramsForced: forceParamsString,
265
+ compareParams: defaultParams
266
+ });
267
+ if (keysDefaultParams.length > 0 && isIncludesForcedParams) {
268
+ if (defaultParamsString === paramsUrlString) return; // this means that the URL already has the default parameters
269
+ updateParams({
270
+ newParams: defaultParams
271
+ });
272
+ }
273
+ }
274
+ handleStartingParams();
275
+ // eslint-disable-next-line react-hooks/exhaustive-deps
276
+ }, []);
277
+ /**
278
+ * Convert a string value to its original type (number, boolean, array) according to TOTAL_PARAMS_PAGE
279
+ * @param value - Chain obtained from the URL
280
+ * @param key - Key of the parameter
281
+ */
282
+ var convertOriginalType = function convertOriginalType(value, key) {
283
+ // Given that the parameters of a URL are recieved as strings, they are converted to their original type
284
+ if (typeof TOTAL_PARAMS_PAGE[key] === 'number') {
285
+ return parseInt(value);
286
+ } else if (typeof TOTAL_PARAMS_PAGE[key] === 'boolean') {
287
+ return value === 'true';
288
+ } else if (Array.isArray(TOTAL_PARAMS_PAGE[key])) {
289
+ // 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
290
+ if (arraySerialization === 'csv') {
291
+ return searchParams.getAll(key).join('').split(',');
292
+ } else if (arraySerialization === 'repeat') {
293
+ console.log({
294
+ SEARCH_PARAMS: searchParams.getAll(key)
295
+ });
296
+ return searchParams.getAll(key);
297
+ } else if (arraySerialization === 'brackets') {
298
+ return searchParams.getAll(key + "[]");
299
+ }
300
+ }
301
+ // 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 >
302
+ return value;
303
+ };
304
+ /**
305
+ * Gets the current parameters from the URL and converts them to their original type if desired
306
+ * @param convert - If true, converts from string to the inferred type (number, boolean, ...)
307
+ */
308
+ var getStringUrl = function getStringUrl(key, paramsUrl) {
309
+ var isKeyArray = Array.isArray(TOTAL_PARAMS_PAGE[key]);
310
+ if (isKeyArray) {
311
+ var _transformParamsToURL3;
312
+ if (arraySerialization === 'brackets') {
313
+ var _transformParamsToURL;
314
+ var arrayUrl = searchParams.getAll(key + "[]");
315
+ var encodedQueryArray = transformParamsToURLSearch((_transformParamsToURL = {}, _transformParamsToURL[key] = arrayUrl, _transformParamsToURL)).toString();
316
+ // in this way the array of the URL is decoded to its original form ej: tags[]=tag1&tags[]=tag2&tags[]=tag3
317
+ var unencodeQuery = decodeURIComponent(encodedQueryArray);
318
+ return unencodeQuery;
319
+ } else if (arraySerialization === 'csv') {
320
+ var _transformParamsToURL2;
321
+ var _arrayValue = searchParams.getAll(key);
322
+ var _encodedQueryArray = transformParamsToURLSearch((_transformParamsToURL2 = {}, _transformParamsToURL2[key] = _arrayValue, _transformParamsToURL2)).toString();
323
+ var _unencodeQuery = decodeURIComponent(_encodedQueryArray);
324
+ return _unencodeQuery;
325
+ }
326
+ var arrayValue = searchParams.getAll(key);
327
+ var stringResult = transformParamsToURLSearch((_transformParamsToURL3 = {}, _transformParamsToURL3[key] = arrayValue, _transformParamsToURL3)).toString();
328
+ return stringResult;
329
+ } else {
330
+ return paramsUrl[key];
331
+ }
332
+ };
333
+ var getParamsObj = function getParamsObj(searchParams) {
334
+ var paramsObj = {};
335
+ // @ts-ignore
336
+ for (var _iterator3 = _createForOfIteratorHelperLoose(searchParams.entries()), _step3; !(_step3 = _iterator3()).done;) {
337
+ var _step3$value = _step3.value,
338
+ key = _step3$value[0],
339
+ value = _step3$value[1];
340
+ if (key.endsWith('[]')) {
341
+ var bareKey = key.replace('[]', '');
342
+ if (paramsObj[bareKey]) {
343
+ paramsObj[bareKey].push(value);
344
+ } else {
345
+ paramsObj[bareKey] = [value];
346
+ }
347
+ } else {
348
+ // If the key already exists, it is a repeated parameter
349
+ if (paramsObj[key]) {
350
+ if (Array.isArray(paramsObj[key])) {
351
+ paramsObj[key].push(value);
352
+ } else {
353
+ paramsObj[key] = [paramsObj[key], value];
354
+ }
355
+ } else {
356
+ paramsObj[key] = value;
357
+ }
358
+ }
359
+ }
360
+ return paramsObj;
361
+ };
362
+ // Optimization: While the parameters are not updated, the current parameters of the URL are not recalculated
363
+ var CURRENT_PARAMS_URL = useMemo(function () {
364
+ return arraySerialization === 'brackets' ? getParamsObj(searchParams) : Object.fromEntries(searchParams.entries());
365
+ }, [searchParams, arraySerialization]);
366
+ var getParams = function getParams(_temp) {
367
+ var _ref4 = _temp === void 0 ? {} : _temp,
368
+ _ref4$convert = _ref4.convert,
369
+ convert = _ref4$convert === void 0 ? true : _ref4$convert;
370
+ // All the paramteres are extracted from the URL and converted into an object
371
+ var params = Object.keys(CURRENT_PARAMS_URL).reduce(function (acc, key) {
372
+ if (Object.prototype.hasOwnProperty.call(TOTAL_PARAMS_PAGE, key)) {
373
+ var realKey = arraySerialization === 'brackets' ? key.replace('[]', '') : key;
374
+ // @ts-ignore
375
+ acc[realKey] = convert === true ? convertOriginalType(CURRENT_PARAMS_URL[key], key) : getStringUrl(key, CURRENT_PARAMS_URL);
376
+ }
377
+ return acc;
378
+ }, {});
379
+ return params;
380
+ };
381
+ var getParam = function getParam(key, options) {
382
+ var keyStr = String(key);
383
+ // @ts-ignore
384
+ var value = (options == null ? void 0 : options.convert) === true ? convertOriginalType(searchParams.get(keyStr), keyStr) : getStringUrl(keyStr, CURRENT_PARAMS_URL);
385
+ return value;
386
+ };
387
+ var calculateOmittedParameters = function calculateOmittedParameters(newParams, keepParams) {
388
+ // Calculate the ommited parameters, that is, the parameters that have not been sent in the request
389
+ var params = getParams();
390
+ // hasOw
391
+ // 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
392
+ var newParamsWithoutArray = Object.entries(newParams).filter(function (_ref5) {
393
+ var key = _ref5[0];
394
+ return !Array.isArray(TOTAL_PARAMS_PAGE[key]);
395
+ });
396
+ var result = Object.assign(_extends({}, params, Object.fromEntries(newParamsWithoutArray), forceParams));
397
+ var paramsFiltered = Object.keys(result).reduce(function (acc, key) {
398
+ // for default no parameters are omitted unless specified in the keepParams object
399
+ if (Object.prototype.hasOwnProperty.call(keepParams, key) && keepParams[key] === false) {
400
+ return acc;
401
+ // 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
402
+ } else if (!!result[key] !== false && !omitParamsByValues.includes(result[key])) {
403
+ // @ts-ignore
404
+ acc[key] = result[key];
405
+ }
406
+ return acc;
407
+ }, {});
408
+ return _extends({}, mandatory, paramsFiltered);
409
+ };
410
+ // @ts-ignore
411
+ var sortParameters = function sortParameters(paramsFiltered) {
412
+ // 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
413
+ // Note: this visibly improves the user experience
414
+ var orderedParams = PARAM_ORDER.reduce(function (acc, key) {
415
+ if (Object.prototype.hasOwnProperty.call(paramsFiltered, key)) {
416
+ // @ts-ignore
417
+ acc[key] = paramsFiltered[key];
418
+ }
419
+ return acc;
420
+ }, {});
421
+ return orderedParams;
422
+ };
423
+ var mandatoryParameters = function mandatoryParameters() {
424
+ // 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
425
+ var isNecessaryConvert = ARRAY_KEYS.length > 0 ? true : false;
426
+ var totalParametros = getParams({
427
+ convert: isNecessaryConvert
428
+ });
429
+ var paramsUrlFound = Object.keys(totalParametros).reduce(function (acc, key) {
430
+ if (Object.prototype.hasOwnProperty.call(mandatory, key)) {
431
+ // @ts-ignore
432
+ acc[key] = totalParametros[key];
433
+ }
434
+ return acc;
435
+ }, {});
436
+ return paramsUrlFound;
437
+ };
438
+ var clearParams = function clearParams(_temp2) {
439
+ var _ref6 = _temp2 === void 0 ? {} : _temp2,
440
+ _ref6$keepMandatoryPa = _ref6.keepMandatoryParams,
441
+ keepMandatoryParams = _ref6$keepMandatoryPa === void 0 ? true : _ref6$keepMandatoryPa;
442
+ // for default, the mandatory parameters are not cleared since the current pagination would be lost
443
+ var paramsTransformed = transformParamsToURLSearch(_extends({}, mandatory, keepMandatoryParams && _extends({}, mandatoryParameters()), forceParams));
444
+ setSearchParams(paramsTransformed);
445
+ };
446
+ var updateParams = function updateParams(_temp3) {
447
+ var _ref7 = _temp3 === void 0 ? {} : _temp3,
448
+ _ref7$newParams = _ref7.newParams,
449
+ newParams = _ref7$newParams === void 0 ? {} : _ref7$newParams,
450
+ _ref7$keepParams = _ref7.keepParams,
451
+ keepParams = _ref7$keepParams === void 0 ? {} : _ref7$keepParams;
452
+ if (Object.keys(newParams).length === 0 && Object.keys(keepParams).length === 0) {
453
+ clearParams();
454
+ return;
455
+ }
456
+ // @ts-ignore
457
+ var finallyParamters = calculateOmittedParameters(newParams, keepParams);
458
+ var convertedArrayValues = appendArrayValues(finallyParamters, newParams);
459
+ var paramsSorted = sortParameters(convertedArrayValues);
460
+ setSearchParams(transformParamsToURLSearch(paramsSorted));
461
+ };
462
+ // only for the keys of the parameters to subscribe to changes in the URL to trigger the callback
463
+ var onChange = useCallback(function (paramName, callbacks) {
464
+ var paramNameStr = String(paramName);
465
+ // replace the previous callbacks with the new ones so as not to accumulate callbacks
466
+ subscriptionsRef.current[paramNameStr] = callbacks;
467
+ }, []);
468
+ // each time searchParams changes, we notify the subscribers
469
+ useEffect(function () {
470
+ for (var _i2 = 0, _Object$entries = Object.entries(subscriptionsRef.current); _i2 < _Object$entries.length; _i2++) {
471
+ var _CURRENT_PARAMS_URL$k, _previousParamsRef$cu;
472
+ var _Object$entries$_i = _Object$entries[_i2],
473
+ key = _Object$entries$_i[0],
474
+ value = _Object$entries$_i[1];
475
+ var newValue = (_CURRENT_PARAMS_URL$k = CURRENT_PARAMS_URL[key]) != null ? _CURRENT_PARAMS_URL$k : null;
476
+ var oldValue = (_previousParamsRef$cu = previousParamsRef.current[key]) != null ? _previousParamsRef$cu : null;
477
+ if (newValue !== oldValue) {
478
+ for (var _iterator4 = _createForOfIteratorHelperLoose(value), _step4; !(_step4 = _iterator4()).done;) {
479
+ var callback = _step4.value;
480
+ console.log(value);
481
+ callback();
482
+ }
483
+ }
484
+ // once the callback is executed, the previous value is updated to ensure that the next time the value changes, the callback is executed
485
+ previousParamsRef.current[key] = newValue;
486
+ }
487
+ }, [CURRENT_PARAMS_URL]);
488
+ return {
489
+ searchParams: searchParams,
490
+ updateParams: updateParams,
491
+ clearParams: clearParams,
492
+ getParams: getParams,
493
+ getParam: getParam,
494
+ onChange: onChange
495
+ };
496
+ };
497
+
498
+ export { useMagicSearchParams };
499
+ //# sourceMappingURL=react-magic-search-params.esm.js.map