Keep quick search input on page reload (#1788)

This commit is contained in:
Sebastian Sdorra
2021-08-31 13:30:15 +02:00
committed by GitHub
parent 571025032c
commit 61c2ebe80e
2 changed files with 18 additions and 7 deletions

View File

@@ -0,0 +1,2 @@
- type: fixed
description: Keep quicks earch input on page reload ([#1788](https://github.com/scm-manager/scm-manager/pull/1788))

View File

@@ -32,6 +32,7 @@ import { Button, HitProps, Notification, RepositoryAvatar, useStringHitFieldValu
import SyntaxHelp from "../search/SyntaxHelp";
import SyntaxModal from "../search/SyntaxModal";
import SearchErrorNotification from "../search/SearchErrorNotification";
import queryString from "query-string";
const Field = styled.div`
margin-bottom: 0 !important;
@@ -282,31 +283,39 @@ const useShowResultsOnFocus = () => {
};
};
const useSearchType = () => {
const useSearchParams = () => {
const location = useLocation();
const pathname = location.pathname;
let type = "repository";
let searchType = "repository";
let initialQuery = "";
if (pathname.startsWith("/search/")) {
const path = pathname.substring("/search/".length);
const index = path.indexOf("/");
if (index > 0) {
type = path.substring(0, index);
searchType = path.substring(0, index);
} else {
type = path;
searchType = path;
}
const queryParams = queryString.parse(location.search);
initialQuery = queryParams.q || "";
}
return type;
return {
searchType,
initialQuery,
};
};
const OmniSearch: FC = () => {
const [query, setQuery] = useState("");
const { searchType, initialQuery } = useSearchParams();
const [query, setQuery] = useState(initialQuery);
const debouncedQuery = useDebounce(query, 250);
const { data, isLoading, error } = useSearch(debouncedQuery, { type: "repository", pageSize: 5 });
const { showResults, hideResults, ...handlers } = useShowResultsOnFocus();
const [showHelp, setShowHelp] = useState(false);
const history = useHistory();
const searchType = useSearchType();
const openHelp = () => setShowHelp(true);
const closeHelp = () => setShowHelp(false);