From 1b280e0aa48fd7393b2eddbf9d31dd1cbe76b04e Mon Sep 17 00:00:00 2001 From: Sebastian Sdorra Date: Wed, 11 Aug 2021 09:41:10 +0200 Subject: [PATCH] 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. --- gradle/changelog/keep_type_on_search.yaml | 2 ++ scm-ui/ui-components/src/search/Hit.tsx | 2 +- .../ui-components/src/search/TextHitField.tsx | 8 ++++--- .../ui-webapp/src/containers/OmniSearch.tsx | 22 +++++++++++++++++-- 4 files changed, 28 insertions(+), 6 deletions(-) create mode 100644 gradle/changelog/keep_type_on_search.yaml diff --git a/gradle/changelog/keep_type_on_search.yaml b/gradle/changelog/keep_type_on_search.yaml new file mode 100644 index 0000000000..626ba4ee95 --- /dev/null +++ b/gradle/changelog/keep_type_on_search.yaml @@ -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)) diff --git a/scm-ui/ui-components/src/search/Hit.tsx b/scm-ui/ui-components/src/search/Hit.tsx index d5b47653a4..a6c1694832 100644 --- a/scm-ui/ui-components/src/search/Hit.tsx +++ b/scm-ui/ui-components/src/search/Hit.tsx @@ -42,7 +42,7 @@ type SearchResultType = FC & { }; const Hit: SearchResultType = ({ children }) => { - return
{children}
; + return
{children}
; }; Hit.Title = ({ className, children }) => ( diff --git a/scm-ui/ui-components/src/search/TextHitField.tsx b/scm-ui/ui-components/src/search/TextHitField.tsx index 8cb07a2276..3af01a1334 100644 --- a/scm-ui/ui-components/src/search/TextHitField.tsx +++ b/scm-ui/ui-components/src/search/TextHitField.tsx @@ -49,15 +49,17 @@ const HighlightedTextField: FC = ({ field }) => ( ); -const TextHitField: FC = ({ hit, field: fieldName, truncateValueAt = 0 }) => { +const TextHitField: FC = ({ hit, field: fieldName, children, truncateValueAt = 0 }) => { const field = hit.fields[fieldName]; if (!field) { - return null; + return <>{children}; } else if (isHighlightedHitField(field)) { return ; } 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}; diff --git a/scm-ui/ui-webapp/src/containers/OmniSearch.tsx b/scm-ui/ui-webapp/src/containers/OmniSearch.tsx index 2bedb86949..460609daa8 100644 --- a/scm-ui/ui-webapp/src/containers/OmniSearch.tsx +++ b/scm-ui/ui-webapp/src/containers/OmniSearch.tsx @@ -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(); };