Keep search result type if searched from result page (#1764)

Whenever OmniSearch was used, the user was redirected to the repository results. However, if you are looking for a different type and want to refine your search after the first results, it makes sense to stay on the same type when searching again.
So whenever a search is started from the search result page the selected type keeps selected.
This commit is contained in:
Sebastian Sdorra
2021-08-11 09:41:10 +02:00
committed by GitHub
parent 3e6ce4e814
commit 1b280e0aa4
4 changed files with 28 additions and 6 deletions

View File

@@ -0,0 +1,2 @@
- type: Changed
description: Keep search result type if searched from result page ([#1764](https://github.com/scm-manager/scm-manager/pull/1764))

View File

@@ -42,7 +42,7 @@ type SearchResultType = FC & {
};
const Hit: SearchResultType = ({ children }) => {
return <article className="media p-1">{children}</article>;
return <article className="media">{children}</article>;
};
Hit.Title = ({ className, children }) => (

View File

@@ -49,15 +49,17 @@ const HighlightedTextField: FC<HighlightedTextFieldProps> = ({ field }) => (
</>
);
const TextHitField: FC<Props> = ({ hit, field: fieldName, truncateValueAt = 0 }) => {
const TextHitField: FC<Props> = ({ hit, field: fieldName, children, truncateValueAt = 0 }) => {
const field = hit.fields[fieldName];
if (!field) {
return null;
return <>{children}</>;
} else if (isHighlightedHitField(field)) {
return <HighlightedTextField field={field} />;
} else {
let value = field.value;
if (typeof value === "string" && truncateValueAt > 0 && value.length > truncateValueAt) {
if (value === "") {
return <>{children}</>;
} else if (typeof value === "string" && truncateValueAt > 0 && value.length > truncateValueAt) {
value = value.substring(0, truncateValueAt) + "...";
}
return <>{value}</>;

View File

@@ -26,7 +26,7 @@ import { Hit, Links, ValueHitField } from "@scm-manager/ui-types";
import styled from "styled-components";
import { BackendError, useSearch } from "@scm-manager/ui-api";
import classNames from "classnames";
import { Link, useHistory } from "react-router-dom";
import { Link, useHistory, useLocation } from "react-router-dom";
import { useTranslation } from "react-i18next";
import {
Button,
@@ -322,6 +322,23 @@ const useShowResultsOnFocus = () => {
};
};
const useSearchType = () => {
const location = useLocation();
const pathname = location.pathname;
let type = "repository";
if (pathname.startsWith("/search/")) {
const path = pathname.substring("/search/".length);
const index = path.indexOf("/");
if (index > 0) {
type = path.substring(0, index);
} else {
type = path;
}
}
return type;
};
const OmniSearch: FC = () => {
const [query, setQuery] = useState("");
const debouncedQuery = useDebounce(query, 250);
@@ -329,13 +346,14 @@ const OmniSearch: FC = () => {
const { showResults, hideResults, ...handlers } = useShowResultsOnFocus();
const [showHelp, setShowHelp] = useState(false);
const history = useHistory();
const searchType = useSearchType();
const openHelp = () => setShowHelp(true);
const closeHelp = () => setShowHelp(false);
const clearQuery = () => setQuery("");
const gotoDetailSearch = () => {
history.push(`/search/repository/?q=${query}`);
history.push(`/search/${searchType}/?q=${query}`);
hideResults();
};