Skip to content

Commit

Permalink
Add comments, add isSubset()
Browse files Browse the repository at this point in the history
  • Loading branch information
noisysocks committed Jul 11, 2023
1 parent 2da548b commit b02c0e5
Showing 1 changed file with 10 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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 = {
Expand Down Expand Up @@ -112,6 +113,7 @@ export default function useSyncPathWithURL() {
[ navigatorLocation.path, navigatorParams ]
);

// Update navigator path when the URL params change.
useEffect(
() => {
const path = getPathFromURL( urlParams );
Expand Down

0 comments on commit b02c0e5

Please sign in to comment.