Skip to content
Closed
Changes from all commits
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
19 changes: 18 additions & 1 deletion frontend/src/components/Nav/Search.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useEffect, useRef, useState } from "react";

import { useRouter } from "next/navigation";
import { useRouter, useSearchParams } from "next/navigation";

import { SearchIcon } from "@primer/octicons-react";
import clsx from "clsx";
Expand Down Expand Up @@ -32,10 +32,12 @@ function MountedSearch({ className }: { className?: string }) {
console.info("<Search> close");
setInputValue("");
setQuery("");
updateUrl("");
setIsFocused(false);
};

const router = useRouter();
const searchParams = useSearchParams();

const pushRoute = (item: api.SearchResult) => {
switch (item.type) {
Expand All @@ -51,6 +53,16 @@ function MountedSearch({ className }: { className?: string }) {
}
};

const updateUrl = (query: string) => {
const params = new URLSearchParams(searchParams.toString());
if (query) {
params.set("search", query);
} else {
params.delete("search");
}
router.replace(`?${params.toString()}`);
};

const { isOpen, getMenuProps, getInputProps, getItemProps, setInputValue } =
useCombobox({
items,
Expand All @@ -72,6 +84,7 @@ function MountedSearch({ className }: { className?: string }) {
onInputValueChange({ inputValue }) {
clearTimeout(debouncedTimeout);
setQuery(inputValue);
updateUrl(inputValue);

if (inputValue.length === 0) {
setSearchItems([]);
Expand Down Expand Up @@ -100,6 +113,10 @@ function MountedSearch({ className }: { className?: string }) {
},
});

useEffect(() => {
setInputValue(searchParams.get("search") || "");
}, []);

const { renderLayer, triggerProps, layerProps, triggerBounds } = useLayer({
isOpen,
overflowContainer: false,
Expand Down