183 lines
4.9 KiB
TypeScript
183 lines
4.9 KiB
TypeScript
import {Link} from 'react-router';
|
|
import {urlWithTrackingParams, type RegularSearchReturn} from '~/lib/search';
|
|
import {AbiirProductListItem} from '~/components/AbiirProductListItem';
|
|
import {PaginatedResourceSection} from '~/components/PaginatedResourceSection';
|
|
import {
|
|
applyDisplayRatingsToListItemData,
|
|
mapStorefrontProductToAbiirProductListItemData,
|
|
type StorefrontProductNode,
|
|
} from '~/lib/featuredProducts';
|
|
import {ABIIR_PRODUCT_LIST_GRID_CLASS} from '~/lib/abiirProductListGrid';
|
|
import type {DisplayRating} from '~/lib/productDisplayRatings.server';
|
|
import type {RegularSearchQuery} from 'storefrontapi.generated';
|
|
|
|
type SearchProductRow = NonNullable<
|
|
NonNullable<RegularSearchQuery['products']>['nodes']
|
|
>[number];
|
|
|
|
type SearchItems = RegularSearchReturn['result']['items'];
|
|
type PartialSearchResult<ItemType extends keyof SearchItems> = Pick<
|
|
SearchItems,
|
|
ItemType
|
|
> &
|
|
Pick<RegularSearchReturn, 'term'>;
|
|
|
|
type SearchResultsProps = RegularSearchReturn & {
|
|
children: (
|
|
args: SearchItems & {term: string; reviewStatsByHandle: Record<string, DisplayRating>},
|
|
) => React.ReactNode;
|
|
};
|
|
|
|
export function SearchResults({
|
|
term,
|
|
result,
|
|
reviewStatsByHandle = {},
|
|
children,
|
|
}: Omit<SearchResultsProps, 'error' | 'type'>) {
|
|
if (!result?.total) {
|
|
return null;
|
|
}
|
|
|
|
return children({
|
|
...result.items,
|
|
term,
|
|
reviewStatsByHandle,
|
|
});
|
|
}
|
|
|
|
SearchResults.Articles = SearchResultsArticles;
|
|
SearchResults.Pages = SearchResultsPages;
|
|
SearchResults.Products = SearchResultsProducts;
|
|
SearchResults.Empty = SearchResultsEmpty;
|
|
|
|
function SearchResultsArticles({
|
|
term,
|
|
articles,
|
|
}: PartialSearchResult<'articles'>) {
|
|
if (!articles?.nodes.length) {
|
|
return null;
|
|
}
|
|
|
|
return (
|
|
<div className="search-page__section" key="articles">
|
|
<h2 className="search-page__section-title">Articles</h2>
|
|
<ul className="search-page__link-list">
|
|
{articles?.nodes?.map((article) => {
|
|
const blogHandle = article.blog?.handle;
|
|
if (!blogHandle) return null;
|
|
const articleUrl = urlWithTrackingParams({
|
|
baseUrl: `/blogs/${blogHandle}/${article.handle}`,
|
|
trackingParams: article.trackingParameters,
|
|
term,
|
|
});
|
|
|
|
return (
|
|
<li key={article.id}>
|
|
<Link
|
|
className="search-page__link"
|
|
prefetch="intent"
|
|
to={articleUrl}
|
|
>
|
|
{article.title}
|
|
</Link>
|
|
</li>
|
|
);
|
|
})}
|
|
</ul>
|
|
</div>
|
|
);
|
|
}
|
|
|
|
function SearchResultsPages({term, pages}: PartialSearchResult<'pages'>) {
|
|
if (!pages?.nodes.length) {
|
|
return null;
|
|
}
|
|
|
|
return (
|
|
<div className="search-page__section" key="pages">
|
|
<h2 className="search-page__section-title">Pages</h2>
|
|
<ul className="search-page__link-list">
|
|
{pages?.nodes?.map((page) => {
|
|
const pageUrl = urlWithTrackingParams({
|
|
baseUrl: `/pages/${page.handle}`,
|
|
trackingParams: page.trackingParameters,
|
|
term,
|
|
});
|
|
|
|
return (
|
|
<li key={page.id}>
|
|
<Link
|
|
className="search-page__link"
|
|
prefetch="intent"
|
|
to={pageUrl}
|
|
>
|
|
{page.title}
|
|
</Link>
|
|
</li>
|
|
);
|
|
})}
|
|
</ul>
|
|
</div>
|
|
);
|
|
}
|
|
|
|
function SearchResultsProducts({
|
|
term,
|
|
products,
|
|
reviewStatsByHandle = {},
|
|
}: PartialSearchResult<'products'> & {
|
|
reviewStatsByHandle?: Record<string, DisplayRating>;
|
|
}) {
|
|
if (!products || !('nodes' in products)) {
|
|
return null;
|
|
}
|
|
|
|
return (
|
|
<div className="search-page__products">
|
|
<PaginatedResourceSection<SearchProductRow>
|
|
connection={products}
|
|
resourcesClassName={ABIIR_PRODUCT_LIST_GRID_CLASS}
|
|
ariaLabel="Search products"
|
|
>
|
|
{({node: product, index}) => {
|
|
const handle = product.handle?.trim() ?? '';
|
|
const base = mapStorefrontProductToAbiirProductListItemData(
|
|
product as StorefrontProductNode,
|
|
);
|
|
const item = applyDisplayRatingsToListItemData(
|
|
base,
|
|
handle ? reviewStatsByHandle[handle] : undefined,
|
|
);
|
|
return (
|
|
<AbiirProductListItem
|
|
key={product.id}
|
|
index={index}
|
|
item={item}
|
|
/>
|
|
);
|
|
}}
|
|
</PaginatedResourceSection>
|
|
</div>
|
|
);
|
|
}
|
|
|
|
function SearchResultsEmpty({term}: {term: string}) {
|
|
if (term) {
|
|
return (
|
|
<p className="search-page__empty">
|
|
No results for <q>{term}</q>. Try a different search or browse our{' '}
|
|
<Link className="search-page__empty-link" to="/collections/all">
|
|
full collection
|
|
</Link>
|
|
.
|
|
</p>
|
|
);
|
|
}
|
|
|
|
return (
|
|
<p className="search-page__empty">
|
|
Enter a few keywords above to search products and content.
|
|
</p>
|
|
);
|
|
}
|