From a66b76fe0ef961796167b6e5f03da98e74e41b7d Mon Sep 17 00:00:00 2001 From: Damian Stasik <920747+damianstasik@users.noreply.github.com> Date: Mon, 26 Aug 2024 14:49:35 +0200 Subject: [PATCH] Support provider docs Signed-off-by: Damian Stasik <920747+damianstasik@users.noreply.github.com> --- frontend/src/components/Search/index.tsx | 42 +++++++++++++++--------- frontend/src/components/Search/types.ts | 1 + 2 files changed, 28 insertions(+), 15 deletions(-) diff --git a/frontend/src/components/Search/index.tsx b/frontend/src/components/Search/index.tsx index afab0ef3..34220106 100644 --- a/frontend/src/components/Search/index.tsx +++ b/frontend/src/components/Search/index.tsx @@ -44,6 +44,23 @@ const getTypeLabel = (type: SearchResultType) => { } }; +const getTypeLink = (type: SearchResultType, result: ApiSearchResult) => { + switch (type) { + case SearchResultType.Module: + return `/module/${result.link_variables.namespace}/${result.link_variables.name}/${result.link_variables.target_system}/${result.link_variables.version}`; + case SearchResultType.Provider: + return `/provider/${result.link_variables.namespace}/${result.link_variables.name}/${result.link_variables.version}`; + case SearchResultType.ProviderResource: + return `/provider/${result.link_variables.namespace}/${result.link_variables.name}/${result.link_variables.version}/docs/resources/${result.link_variables.id}`; + case SearchResultType.ProviderDatasource: + return `/provider/${result.link_variables.namespace}/${result.link_variables.name}/${result.link_variables.version}/docs/datasources/${result.link_variables.id}`; + case SearchResultType.ProviderFunction: + return `/provider/${result.link_variables.namespace}/${result.link_variables.name}/${result.link_variables.version}/docs/functions/${result.link_variables.id}`; + default: + return ""; + } +}; + type Results = Array<{ label: string; type: SearchResultType; @@ -54,7 +71,7 @@ export function Search() { const [query, setQuery] = useState(""); const deferredQuery = useDeferredValue(query); const { data, isLoading } = useQuery(getSearchQuery(deferredQuery)); - const inputRef = useRef<HTMLInputElement>(); + const inputRef = useRef<HTMLInputElement>(null); const navigate = useNavigate(); const filtered = useMemo(() => { @@ -71,22 +88,17 @@ export function Search() { const result = data[i] as ApiSearchResult; const order = getTypeOrder(result.type); - - const group = (results[order] = results[order] || { - type: result.type, - label: getTypeLabel(result.type), - results: [], - }); - - let link = ""; - - if (result.type === SearchResultType.Module) { - link = `/module/${result.link_variables.namespace}/${result.link_variables.name}/${result.link_variables.target_system}/${result.version}`; - } else if (result.type === SearchResultType.Provider) { - link = `/provider/${result.link_variables.namespace}/${result.link_variables.name}/${result.version}`; + const link = getTypeLink(result.type, result); + + if (!results[order]) { + results[order] = { + type: result.type, + label: getTypeLabel(result.type), + results: [], + }; } - group.results.push({ + results[order].results.push({ id: result.id, title: result.title, addr: result.addr, diff --git a/frontend/src/components/Search/types.ts b/frontend/src/components/Search/types.ts index 7c071d7e..d4bc59e5 100644 --- a/frontend/src/components/Search/types.ts +++ b/frontend/src/components/Search/types.ts @@ -15,6 +15,7 @@ export interface ApiSearchResult { description: string; version: string; link_variables: { + id?: string; name: string; version: string; namespace: string;