Skip to content

Commit

Permalink
feat: add SearchInput client-side component
Browse files Browse the repository at this point in the history
Create new /search page which uses it to display faceted search results
  • Loading branch information
kevinstadler committed Aug 13, 2024
1 parent 2a66a91 commit e25f7f7
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 7 deletions.
30 changes: 30 additions & 0 deletions components/search-input.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
"use client";

// eslint-disable-next-line no-restricted-imports
import { useRouter } from "next/navigation";
import { type ReactNode, useOptimistic } from "react";

interface SearchInputProps {
value?: Array<string> | string;
}

export function SearchInput(props: SearchInputProps): ReactNode {
const router = useRouter();
const [optimisticValue, addOptimistic] = useOptimistic(
props.value,
(_currentState, optimisticValue: string) => {
return optimisticValue;
},
);
return (
<input
onChange={(e) => {
addOptimistic(e.target.value);
router.push(`?search=${e.target.value}`);
}}
placeholder="Heldenplatz"
type="text"
value={optimisticValue}
/>
);
}
20 changes: 13 additions & 7 deletions lib/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const client = new Client({
connectionTimeoutSeconds: 2,
});

// needs to match the collection name in scripts/typesense-schema.json
const collection = client.collections("thomas-bernhard");

export async function getCounts(field: string): Promise<Record<string, number>> {
Expand Down Expand Up @@ -48,16 +49,21 @@ export async function getCounts(field: string): Promise<Record<string, number>>
}

export function getFaceted(
facet: string,
facetValue?: string,
facetFields: Array<string>,
search?: string,
page = 1,
sortAlphabetic = false,
): Promise<SearchResponse<Publication>> {
return collection.documents().search({
q: "*",
// facet_by: facet,
facet_by: `${facet}(sort_by: _alpha:asc)`,
filter_by: facetValue && `${facet}: ${facetValue}`,
max_facet_values: 1000,
q: search,
query_by: "*",
facet_by: facetFields
.map((name) => {
return sortAlphabetic ? `${name}(sort_by: _alpha:asc)` : name;
})
.join(),
// filter_by: facetValue && `${facet}: \`${facetValue}\``,
max_facet_values: 100,
page: page,
per_page: perPage,
}) as unknown as Promise<SearchResponse<Publication>>;
Expand Down

0 comments on commit e25f7f7

Please sign in to comment.