From da6a9b62643d1cf2f204978c0e28d2ea47f0f47c Mon Sep 17 00:00:00 2001 From: Lukas <lukas.budginas@gmail.com> Date: Thu, 19 Dec 2024 17:43:27 +0200 Subject: [PATCH] Update block search url build function --- .../home/searches/utils/blockSearchHelpers.ts | 55 ++++++++----------- 1 file changed, 24 insertions(+), 31 deletions(-) diff --git a/components/home/searches/utils/blockSearchHelpers.ts b/components/home/searches/utils/blockSearchHelpers.ts index 379ad0b3..a3ad14f1 100644 --- a/components/home/searches/utils/blockSearchHelpers.ts +++ b/components/home/searches/utils/blockSearchHelpers.ts @@ -1,6 +1,6 @@ -import { convertIdsToBooleanArray, getPageUrlParams } from "@/lib/utils"; import Explorer from "@/types/Explorer"; -import { dataToURL } from "@/utils/URLutils"; +import { convertIdsToBooleanArray } from "@/lib/utils"; +import { setParamIfPositive } from "./globalSearchHelpers"; export function startBlockSearch( blockSearchProps: Explorer.BlockSearchProps, @@ -13,43 +13,36 @@ export function startBlockSearch( export function getBlockPageLink( blockSearchProps: Explorer.BlockSearchProps | undefined, - operationsTypes: any + operationsTypes: Explorer.ExtendedOperationTypePattern[] | undefined ): (blockNumber: number) => string { return (blockNumber: number) => { if (!blockSearchProps) return "#"; - const urlParams: Explorer.UrlParam[] = [ - { - paramName: "accountName", - paramValue: dataToURL(blockSearchProps.accountName), - }, - { - paramName: "keyContent", - paramValue: dataToURL(blockSearchProps.deepProps.content), - }, - { - paramName: "setOfKeys", - paramValue: dataToURL(blockSearchProps.deepProps.keys), - }, - ]; - - if (blockSearchProps.operationTypes) { - const booleanTypesArray = convertIdsToBooleanArray( - blockSearchProps.operationTypes - ); - let isFull = !!blockSearchProps.operationTypes; + const { accountName, operationTypes } = blockSearchProps; + + const searchParams = new URLSearchParams(); + + setParamIfPositive(searchParams, "accountName", accountName); + + if (operationTypes) { + const booleanTypesArray = convertIdsToBooleanArray(operationTypes); + let isFull = !!operationTypes; + operationsTypes?.forEach((operationType: any) => { - if ( - !blockSearchProps.operationTypes?.includes(operationType.op_type_id) - ) + if (!operationTypes.includes(operationType.op_type_id)) { isFull = false; + } }); - urlParams.push({ - paramName: "filters", - paramValue: dataToURL(!isFull ? booleanTypesArray : []), - }); + + const filtersValue = !isFull ? booleanTypesArray : []; + setParamIfPositive(searchParams, "filters", filtersValue); } - return `/block/${blockNumber}${getPageUrlParams(urlParams)}`; + const queryString = searchParams.toString(); + const urlPath = `/block/${blockNumber}${ + queryString ? `?${queryString}` : "" + }`; + + return urlPath; }; } -- GitLab