Skip to content

Commit

Permalink
Support provider docs
Browse files Browse the repository at this point in the history
Signed-off-by: Damian Stasik <[email protected]>
  • Loading branch information
damianstasik committed Aug 29, 2024
1 parent f7aa6fb commit a66b76f
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 15 deletions.
42 changes: 27 additions & 15 deletions frontend/src/components/Search/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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(() => {
Expand All @@ -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,
Expand Down
1 change: 1 addition & 0 deletions frontend/src/components/Search/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export interface ApiSearchResult {
description: string;
version: string;
link_variables: {
id?: string;
name: string;
version: string;
namespace: string;
Expand Down

0 comments on commit a66b76f

Please sign in to comment.