Skip to content

feat: make workspace search bar remember text #9759

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 17 commits into from
Sep 20, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
refactor: update code to use useEffectEvent
  • Loading branch information
Parkreiner committed Sep 20, 2023
commit dfad46f54c4151239798d29a62567a1c73dc738b
16 changes: 6 additions & 10 deletions site/src/components/Filter/filter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import Divider from "@mui/material/Divider";
import OpenInNewOutlined from "@mui/icons-material/OpenInNewOutlined";

import { useDebouncedFunction } from "hooks/debounce";
import { useEffectEvent } from "hooks/hookPolyfills";

export type PresetFilter = {
name: string;
Expand Down Expand Up @@ -55,18 +56,13 @@ export const useFilter = ({
const [searchParams, setSearchParams] = searchParamsResult;
const query = searchParams.get(useFilterParamsKey) ?? fallbackFilter;

// Using ref approach to be defensive against useSearchParams not returning a
// stable set function on each render. Effect must run before query effect
const setSearchParamsRef = useRef(setSearchParams);
useEffect(() => {
setSearchParamsRef.current = setSearchParams;
}, [setSearchParams]);
// Stabilizing reference to setSearchParams from one central spot, just to be
// on the extra careful side. Don't want effects over-running
const stableSetSearchParams = useEffectEvent(setSearchParams);

// Keep params synced with query, even as query changes from outside sources
useEffect(() => {
const setSearchParams = setSearchParamsRef.current;

setSearchParams((currentParams) => {
stableSetSearchParams((currentParams) => {
const currentQuery = currentParams.get(useFilterParamsKey);

if (query === "") {
Expand All @@ -77,7 +73,7 @@ export const useFilter = ({

return currentParams;
});
}, [query]);
}, [stableSetSearchParams, query]);

const update = (newValues: string | FilterValues) => {
const serialized =
Expand Down