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
Show file tree
Hide file tree
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: clean up filter to be fully pure on mount
  • Loading branch information
Parkreiner committed Sep 18, 2023
commit df1f8df0ab9d5bb157432d76cef20a114c5d4f4f
40 changes: 27 additions & 13 deletions site/src/components/Filter/filter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,14 @@ export type PresetFilter = {
type FilterValues = Record<string, string | undefined>;

type UseFilterConfig = {
/**
* If initialValue is a string, that value will be used immediately from the
* first render.
*
* If it's a function, the function will be lazy-evaluated via an effect
* after the initial render. The initial render will use an empty string while
* waiting for the effect to run.
*/
initialValue?: string | (() => string);
searchParamsResult: ReturnType<typeof useSearchParams>;
onUpdate?: (newValue: string) => void;
Expand All @@ -49,25 +57,31 @@ export const useFilter = ({
}: UseFilterConfig) => {
const [searchParams, setSearchParams] = searchParamsResult;

// Fully expect the initialValue functions to have some impurity (e.g. reading
// from localStorage during a render path). (Ab)using useState's lazy
// initialization mode to guarantee impurities only exist on mount. Pattern
// has added benefit of locking down initialValue and ignoring any accidental
// value changes on re-renders
const [readonlyInitialQueryState] = useState(initialValue);
// Copying initialValue into state to lock the value down from the outside
const [initializedValue, setInitializedValue] = useState(() => {
return typeof initialValue === "string" ? initialValue : "";
});

// Sync the params with the value provided via the initialValue function;
// should behave only as an on-mount effect
// Lazy-evaluate initialValue only on mount; have to resolve via effect
// because initialValue is allowed to be impure (e.g., read from localStorage)
const lazyOnMountRef = useRef(initialValue);
useEffect(() => {
if (typeof lazyOnMountRef.current === "string") {
return;
}

const lazyInitialValue = lazyOnMountRef.current();
setInitializedValue(lazyInitialValue);

setSearchParams((current) => {
const currentFilter = current.get(useFilterParamsKey);
if (currentFilter !== readonlyInitialQueryState) {
current.set(useFilterParamsKey, readonlyInitialQueryState);
if (currentFilter !== lazyInitialValue) {
current.set(useFilterParamsKey, lazyInitialValue);
}

return current;
});
}, [setSearchParams, readonlyInitialQueryState]);
}, [setSearchParams]);

const update = (newValues: string | FilterValues) => {
const serialized =
Expand All @@ -82,11 +96,11 @@ export const useFilter = ({
};

const { debounced: debounceUpdate, cancelDebounce } = useDebouncedFunction(
(values: string | FilterValues) => update(values),
update,
500,
);

const query = searchParams.get("filter") ?? readonlyInitialQueryState;
const query = searchParams.get("filter") ?? initializedValue;
const values = parseFilterQuery(query);
const used = query !== "" && query !== initialValue;

Expand Down
3 changes: 0 additions & 3 deletions site/src/pages/WorkspacesPage/WorkspacesPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -151,9 +151,6 @@ const useWorkspacesFilter = ({
searchParamsResult,
initialValue: () => {
const fallbackValue = "owner:me";

// Have to include check because initialValue will be called during the
// render itself; future-proofing for SSR, if we ever need that
if (typeof window === "undefined") {
return fallbackValue;
}
Expand Down