Skip to content

Commit c081aad

Browse files
committed
fix: improve handling of project query state
1 parent ba4890d commit c081aad

File tree

4 files changed

+28
-12
lines changed

4 files changed

+28
-12
lines changed

components/navigation/Logo.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export const Logo = () => {
1010
const { query } = useRouter();
1111

1212
return (
13-
<Link passHref href={{ pathname: "/", query: { project: query.project } }}>
13+
<Link passHref href={{ pathname: "/", query: query.project ? { project: query.project } : {} }}>
1414
<LogoLink>
1515
<Image
1616
alt="Squonk (animal) logo with title text 'Squonk' and subtitle 'Data Manager'"
Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,39 @@
11
import { useEffect } from "react";
22

3+
import compare from "just-compare";
34
import { useRouter } from "next/router";
45

56
import { PROJECT_LOCAL_STORAGE_KEY } from "../constants";
67
import { getFromLocalStorage } from "../utils/localStorage";
78
import type { ProjectLocalStoragePayload } from "./projectHooks";
89

9-
const PATHS_FOR_UPDATE = ["/datasets", "/project", "/executions", "/results"];
10-
10+
/**
11+
* Hook to synchronise the project local storage to query params.
12+
* E.g. if the user visits example-ui.com/data-manager-ui/sub/path, the url will be *replaced* with
13+
* example-ui.com/data-manager-ui/sub/path?project=project-blah-blah-blah-blah using the project ID
14+
* loaded from local storage.
15+
*
16+
* @private
17+
* It could be better to use a local storage event listener here but I find those unreliable
18+
* If the project was stored server-side this could be done in a middleware but alas.
19+
*/
1120
export const useBindProjectFromLSToQParams = () => {
1221
const router = useRouter();
1322

14-
const { isReady, pathname, query, push } = router;
15-
16-
const shouldNavigate = PATHS_FOR_UPDATE.map((s) => pathname.startsWith(s)).some((b) => b);
23+
const { isReady, pathname, query, replace } = router;
1724

1825
useEffect(() => {
1926
const { projectId } = getFromLocalStorage<
2027
ProjectLocalStoragePayload | Record<string, undefined>
2128
>(PROJECT_LOCAL_STORAGE_KEY, {});
2229

23-
if (isReady && shouldNavigate && projectId) {
24-
push({ pathname, query: { project: projectId, ...query } }, undefined, {
25-
shallow: true,
26-
});
30+
if (isReady && projectId) {
31+
const newQuery = { ...query, project: projectId };
32+
if (!compare(query, newQuery)) {
33+
replace({ pathname, query: newQuery }, undefined, {
34+
shallow: true,
35+
});
36+
}
2737
}
28-
// eslint-disable-next-line react-hooks/exhaustive-deps
29-
}, []);
38+
}, [isReady, pathname, replace, query]);
3039
};

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
"formik-mui-lab": "1.0.0",
6363
"immer": "9.0.15",
6464
"jotai": "1.7.6",
65+
"just-compare": "^2.2.2",
6566
"lodash-es": "4.17.21",
6667
"material-ui-popup-state": "4.0.1",
6768
"next": "12.2.4",

pnpm-lock.yaml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)