From b02c0e5d616ad01dfbe728791ae1cdff2b9177fb Mon Sep 17 00:00:00 2001 From: Robert Anderson Date: Tue, 11 Jul 2023 11:26:46 +1000 Subject: [PATCH] Add comments, add isSubset() --- .../use-sync-path-with-url.js | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/packages/edit-site/src/components/sync-state-with-url/use-sync-path-with-url.js b/packages/edit-site/src/components/sync-state-with-url/use-sync-path-with-url.js index e4254077583074..62a023c928e659 100644 --- a/packages/edit-site/src/components/sync-state-with-url/use-sync-path-with-url.js +++ b/packages/edit-site/src/components/sync-state-with-url/use-sync-path-with-url.js @@ -36,6 +36,12 @@ export function getPathFromURL( urlParams ) { return path; } +function isSubset( subset, superset ) { + return Object.entries( subset ).every( ( [ key, value ] ) => { + return superset[ key ] === value; + } ); +} + export default function useSyncPathWithURL() { const history = useHistory(); const { params: urlParams } = useLocation(); @@ -46,23 +52,18 @@ export default function useSyncPathWithURL() { } = useNavigator(); const isMounting = useRef( true ); + // Update URL params when the navigator path changes. useEffect( () => { // The navigatorParams are only initially filled properly when the - // navigator screens mount. so we ignore the first synchronisation. + // navigator screens mount, so we ignore the first synchronisation. if ( isMounting.current ) { isMounting.current = false; return; } function updateUrlParams( newUrlParams ) { - if ( - Object.entries( newUrlParams ).every( - ( [ key, value ] ) => { - return urlParams[ key ] === value; - } - ) - ) { + if ( isSubset( newUrlParams, urlParams ) ) { return; } const updatedParams = { @@ -112,6 +113,7 @@ export default function useSyncPathWithURL() { [ navigatorLocation.path, navigatorParams ] ); + // Update navigator path when the URL params change. useEffect( () => { const path = getPathFromURL( urlParams );