Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rd-332 add search results type #334

Merged
merged 1 commit into from
Oct 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions docs/03-customization/03-search.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,9 @@ If you prefer to handle search logic entirely on the client side, you can provid
Here’s an example callback function:

```ts filename="utils/searcher.ts"
const searcher = async (
search: string,
abortController: AbortController
): { href: string; content: string; title: string }[] => {
import { type Searcher } from "robindoc/lib/core/types/search";

const searcher: Searcher = async (search, abortController) => {
const results = await advancedSearcher(pagesData, search, abortController);
return results;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import React, { useEffect, useMemo, useRef, useState } from "react";

import { type Searcher, type SearchItem } from "@src/core/types/search";
import { type Searcher, type SearchResults } from "@src/core/types/search";
import { createBaseSearcher } from "@src/core/utils/create-base-searcher";
import { NavLink } from "@src/components/blocks/nav-link";
import { useDebouncer } from "@src/core/hooks/use-debouncer";
Expand All @@ -25,7 +25,7 @@ export interface SearchModalProps {
export const SearchModal: React.FC<SearchModalProps> = ({ translations, searcher, open, onClose, onInput }) => {
const { typeSomething = "Type something...", nothingFound = "Nothing found" } = translations || {};
const inputRef = useRef<HTMLInputElement | null>(null);
const [results, setResults] = useState<SearchItem[] | null>(null);
const [results, setResults] = useState<SearchResults | null>(null);
const targetSearcher = useMemo(
() => (typeof searcher === "string" ? createBaseSearcher(searcher) : searcher),
[searcher],
Expand Down
2 changes: 2 additions & 0 deletions packages/robindoc/src/core/types/search.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
export type SearchItem = { title: string; href: string; description?: string };

export type SearchResults = SearchItem[];

export type Searcher = (search: string, abortController: AbortController) => Promise<SearchItem[]>;