Skip to content

Commit c201fc2

Browse files
authored
revert: remove localStorage sync for search params (coder#9827)
1 parent f6ee08d commit c201fc2

File tree

2 files changed

+3
-64
lines changed

2 files changed

+3
-64
lines changed

site/src/components/Filter/filter.tsx

-21
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ import Divider from "@mui/material/Divider";
2626
import OpenInNewOutlined from "@mui/icons-material/OpenInNewOutlined";
2727

2828
import { useDebouncedFunction } from "hooks/debounce";
29-
import { useEffectEvent } from "hooks/hookPolyfills";
3029

3130
export type PresetFilter = {
3231
name: string;
@@ -56,26 +55,6 @@ export const useFilter = ({
5655
const [searchParams, setSearchParams] = searchParamsResult;
5756
const query = searchParams.get(useFilterParamsKey) ?? fallbackFilter;
5857

59-
// Stabilizing reference to setSearchParams from one central spot, just to be
60-
// on the extra careful side; don't want effects over-running. You would think
61-
// this would be overkill, but setSearchParams isn't stable out of the box
62-
const stableSetSearchParams = useEffectEvent(setSearchParams);
63-
64-
// Keep params synced with query, even as query changes from outside sources
65-
useEffect(() => {
66-
stableSetSearchParams((currentParams) => {
67-
const currentQuery = currentParams.get(useFilterParamsKey);
68-
69-
if (query === "") {
70-
currentParams.delete(useFilterParamsKey);
71-
} else if (currentQuery !== query) {
72-
currentParams.set(useFilterParamsKey, query);
73-
}
74-
75-
return currentParams;
76-
});
77-
}, [stableSetSearchParams, query]);
78-
7958
const update = (newValues: string | FilterValues) => {
8059
const serialized =
8160
typeof newValues === "string" ? newValues : stringifyFilter(newValues);

site/src/pages/WorkspacesPage/WorkspacesPage.tsx

+3-43
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import {
44
useDashboard,
55
useIsWorkspaceActionsEnabled,
66
} from "components/Dashboard/DashboardProvider";
7-
import { type FC, useEffect, useState, useSyncExternalStore } from "react";
7+
import { type FC, useEffect, useState } from "react";
88
import { Helmet } from "react-helmet-async";
99
import { pageTitle } from "utils/page";
1010
import { useWorkspacesData, useWorkspaceUpdate } from "./data";
@@ -140,29 +140,6 @@ const WorkspacesPage: FC = () => {
140140

141141
export default WorkspacesPage;
142142

143-
const workspaceFilterKey = "WorkspacesPage/filter";
144-
const defaultWorkspaceFilter = "owner:me";
145-
146-
// Function should stay outside components as much as possible; if declared
147-
// inside the component, React would add/remove event listeners every render
148-
function subscribeToFilterChanges(notifyReact: () => void) {
149-
const onStorageChange = (event: StorageEvent) => {
150-
const { key, storageArea, oldValue, newValue } = event;
151-
152-
const shouldNotify =
153-
key === workspaceFilterKey &&
154-
storageArea === window.localStorage &&
155-
newValue !== oldValue;
156-
157-
if (shouldNotify) {
158-
notifyReact();
159-
}
160-
};
161-
162-
window.addEventListener("storage", onStorageChange);
163-
return () => window.removeEventListener("storage", onStorageChange);
164-
}
165-
166143
type UseWorkspacesFilterOptions = {
167144
searchParamsResult: ReturnType<typeof useSearchParams>;
168145
onFilterChange: () => void;
@@ -172,27 +149,10 @@ const useWorkspacesFilter = ({
172149
searchParamsResult,
173150
onFilterChange,
174151
}: UseWorkspacesFilterOptions) => {
175-
// Using useSyncExternalStore store to safely access localStorage from the
176-
// first render; both snapshot callbacks return primitives, so no special
177-
// trickery needed to prevent hook from immediately blowing up in dev mode
178-
const localStorageFilter = useSyncExternalStore(
179-
subscribeToFilterChanges,
180-
() => {
181-
return (
182-
window.localStorage.getItem(workspaceFilterKey) ??
183-
defaultWorkspaceFilter
184-
);
185-
},
186-
() => defaultWorkspaceFilter,
187-
);
188-
189152
const filter = useFilter({
190-
fallbackFilter: localStorageFilter,
153+
fallbackFilter: "owner:me",
191154
searchParamsResult,
192-
onUpdate: (newValues) => {
193-
window.localStorage.setItem(workspaceFilterKey, newValues);
194-
onFilterChange();
195-
},
155+
onUpdate: onFilterChange,
196156
});
197157

198158
const permissions = usePermissions();

0 commit comments

Comments
 (0)