From c3888d658e9a924f85261546de39361fb0badedd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tomek=20Wytr=C4=99bowicz?= Date: Mon, 17 Oct 2022 16:24:29 +0200 Subject: [PATCH] Remove `qs` dependency from layout Controller, use native `URLSearchParams` instead. --- .../client/layout/controller.js | 21 +++++++++++-------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/plugins/woocommerce-admin/client/layout/controller.js b/plugins/woocommerce-admin/client/layout/controller.js index 0ef2068d80f..720eb9215ee 100644 --- a/plugins/woocommerce-admin/client/layout/controller.js +++ b/plugins/woocommerce-admin/client/layout/controller.js @@ -3,7 +3,6 @@ */ import { Suspense, lazy } from '@wordpress/element'; import { useRef, useEffect } from 'react'; -import { parse, stringify } from 'qs'; import { find, isEqual, last, omit } from 'lodash'; import { applyFilters } from '@wordpress/hooks'; import { __ } from '@wordpress/i18n'; @@ -356,18 +355,22 @@ export const Controller = ( { ...props } ) => { */ export function updateLinkHref( item, nextQuery, excludedScreens ) { if ( isWCAdmin( item.href ) ) { + // If we accept a full HTMLAnchorElement, then we should be able to use `.search`. + // const query = new URLSearchParams( item.search ); + // but to remain backward compatible, we support any object with `href` property. const search = last( item.href.split( '?' ) ); - const query = parse( search ); - const path = query.path || 'homescreen'; + let query = new URLSearchParams( search ); + const path = query.get( 'path' ) || 'homescreen'; const screen = getScreenFromPath( path ); - const isExcludedScreen = excludedScreens.includes( screen ); + if ( ! excludedScreens.includes( screen ) ) { + query = new URLSearchParams( { + ...Object.fromEntries( query ), + ...nextQuery, + } ); + } - const href = - 'admin.php?' + - stringify( - Object.assign( query, isExcludedScreen ? {} : nextQuery ) - ); + const href = 'admin.php?' + query.toString(); // Replace the href so you can see the url on hover. item.href = href;