Skip to content

Commit

Permalink
feat: Algolia accepts hard-coded default search results (#2414)
Browse files Browse the repository at this point in the history
* As a hot fix for the bad Algolia search results, this allows a Plasmic
  user to hard code results to show if the search is empty
  • Loading branch information
ryscheng authored Oct 25, 2024
1 parent 0c71d5c commit dc9423b
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 8 deletions.
11 changes: 7 additions & 4 deletions apps/frontend/components/widgets/algolia.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ type AlgoliaSearchListProps = {
children?: ReactElement; // Show this per item
indexName?: string; // Choose a custom Algolia index
placeholder?: string; // Placeholder for search box
showResultsOnEmptyQuery?: boolean; // Show some results even if no query
defaultSearchResults?: any; // Show some results even if no query
};

function HitsContainer(props: AlgoliaSearchListProps) {
const { children, showResultsOnEmptyQuery } = props;
const { children, defaultSearchResults } = props;
const { results, items, isLastPage, showMore } = useInfiniteHits();
const sentinelRef = useRef(null);
//console.log(results);
Expand All @@ -48,10 +48,13 @@ function HitsContainer(props: AlgoliaSearchListProps) {
}
}, [isLastPage, showMore]);

const isResultsReady = results?.query || Array.isArray(defaultSearchResults);
const displayItems = results?.query ? items : defaultSearchResults;

return (
<div>
{(showResultsOnEmptyQuery || results?.query) &&
items.map((hit) => (
{isResultsReady &&
displayItems.map((hit: any) => (
<div key={hit.objectID}>
<DataProvider name={PLASMIC_KEY} data={hit}>
{children}
Expand Down
5 changes: 1 addition & 4 deletions apps/frontend/plasmic-init-client.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -160,10 +160,7 @@ PLASMIC.registerComponent(
helpText: "Comma-separated Algolia index names",
},
placeholder: "string",
showResultsOnEmptyQuery: {
type: "boolean",
helpText: "Show some results even if user has no query",
},
defaultSearchResults: "object",
},
providesData: true,
importPath: "./components/widgets/algolia",
Expand Down

0 comments on commit dc9423b

Please sign in to comment.