Add product search with autocomplete, filters, and sort

- Header: search button opening a command-palette dialog with debounced
  product suggestions, a term chip, and View All
- Command: shadcn-shaped primitives built on the project's own Dialog so
  no cmdk/radix dependency is introduced
- /search: results grid with item count, sort (relevance, price asc/desc),
  and cursor-based load more
- Filters: driven by the API's productFilters facets — colour swatches,
  labelled lists with counts, and a price range — round-tripped through
  each facet's raw input string so no filter shape is hardcoded

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_016jWbNNJLksC1QG8z8845FX
This commit is contained in:
Rami Bitar
2026-08-01 12:15:49 -04:00
co-authored by Claude Opus 5
parent e04f1e0405
commit cc901b9ce8
8 changed files with 1084 additions and 0 deletions
+120
View File
@@ -0,0 +1,120 @@
'use client';
import { shopifyFetch } from '@/services/shopify/client';
import {
SEARCH_PRODUCTS_QUERY,
SEARCH_SUGGESTIONS_QUERY,
} from '@/graphql/search';
import type { Product } from '@/hooks/use-shopify-products';
export type SearchSortKey = 'RELEVANCE' | 'PRICE';
export interface SearchFilterValue {
id: string;
label: string;
count: number;
/** JSON string accepted back as a `ProductFilter` input. */
input: string;
}
export interface SearchFilter {
id: string;
label: string;
type: 'LIST' | 'PRICE_RANGE' | 'BOOLEAN';
values: SearchFilterValue[];
}
export interface SearchProductsResult {
products: Product[];
totalCount: number;
filters: SearchFilter[];
hasNextPage: boolean;
endCursor: string | null;
}
export interface SearchSuggestion {
id: string;
title: string;
handle: string;
featuredImage?: {
url: string;
altText?: string;
} | null;
priceRange: {
minVariantPrice: {
amount: string;
currencyCode: string;
};
};
}
interface SearchProductsOptions {
query: string;
first?: number;
after?: string | null;
sortKey?: SearchSortKey;
reverse?: boolean;
/** Raw `input` strings from the facets, parsed back into filter objects. */
filterInputs?: string[];
}
function parseFilterInputs(inputs: string[]): unknown[] {
return inputs.flatMap((input) => {
try {
return [JSON.parse(input)];
} catch {
console.warn('Ignoring malformed product filter input:', input);
return [];
}
});
}
export async function searchProducts({
query,
first = 24,
after = null,
sortKey = 'RELEVANCE',
reverse = false,
filterInputs = [],
}: SearchProductsOptions): Promise<SearchProductsResult> {
const response = await shopifyFetch({
query: SEARCH_PRODUCTS_QUERY,
variables: {
query,
first,
after,
sortKey,
reverse,
productFilters: filterInputs.length
? parseFilterInputs(filterInputs)
: null,
},
});
const search = response.data.search;
return {
products: search.edges.map((edge: { node: Product }) => edge.node),
totalCount: search.totalCount ?? 0,
filters: search.productFilters ?? [],
hasNextPage: Boolean(search.pageInfo?.hasNextPage),
endCursor: search.pageInfo?.endCursor ?? null,
};
}
export async function searchSuggestions(
query: string,
first = 3
): Promise<{ products: SearchSuggestion[]; totalCount: number }> {
const response = await shopifyFetch({
query: SEARCH_SUGGESTIONS_QUERY,
variables: { query, first },
});
const search = response.data.search;
return {
products: search.edges.map((edge: { node: SearchSuggestion }) => edge.node),
totalCount: search.totalCount ?? 0,
};
}