Template
- /api/chat streams via AI SDK v7 through OpenRouter (OPENROUTER_API_KEY), with a store-specific system prompt and five read-only tools: searchCatalogue, getProductDetails, listCollections, getCollectionProducts, browseProducts - Sidebar assistant on the right that opens from a launcher and expands, built on ai-elements (conversation, message, prompt-input, tool) with shimmer on in-flight tool calls - Extract services/shopify/catalog.ts so the Storefront fetchers have no React imports and can run in a Route Handler; the client hooks now re-export from it - ai-elements pulled in the canonical shadcn primitives, replacing the hand-rolled command/dialog/button variants; search dialog moved to the cmdk-based Command and CommandDialog forwards shouldFilter - Pin shiki to ^3.19.0 to match streamdown and drop the duplicate copy - Add .env.example documenting the required env vars Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_016jWbNNJLksC1QG8z8845FX
100 lines
2.6 KiB
TypeScript
100 lines
2.6 KiB
TypeScript
'use client';
|
|
|
|
import { useState, useEffect, useCallback } from 'react';
|
|
import {
|
|
getCollections,
|
|
getCollectionProducts,
|
|
} from '@/services/shopify/catalog';
|
|
|
|
export {
|
|
getCollections,
|
|
getCollectionProducts,
|
|
getCollectionProductsPage,
|
|
} from '@/services/shopify/catalog';
|
|
export type {
|
|
Collection,
|
|
CollectionWithProducts,
|
|
CollectionSortKey,
|
|
CollectionProductsPage,
|
|
ProductFilterFacet,
|
|
} from '@/services/shopify/catalog';
|
|
|
|
import type {
|
|
Collection,
|
|
CollectionWithProducts,
|
|
CollectionSortKey,
|
|
} from '@/services/shopify/catalog';
|
|
|
|
interface UseCollectionProductsOptions {
|
|
first?: number;
|
|
after?: string | null;
|
|
sortKey?: CollectionSortKey;
|
|
reverse?: boolean;
|
|
filterInputs?: string[];
|
|
}
|
|
|
|
// Hook for fetching all collections
|
|
export function useCollections(first = 50) {
|
|
const [collections, setCollections] = useState<Collection[]>([]);
|
|
const [loading, setLoading] = useState(true);
|
|
const [error, setError] = useState<string | null>(null);
|
|
|
|
const fetchCollections = useCallback(async () => {
|
|
try {
|
|
setLoading(true);
|
|
setError(null);
|
|
const data = await getCollections(first);
|
|
setCollections(data);
|
|
} catch (err) {
|
|
console.error('Error fetching collections:', err);
|
|
setError(err instanceof Error ? err.message : 'Failed to load collections');
|
|
} finally {
|
|
setLoading(false);
|
|
}
|
|
}, [first]);
|
|
|
|
useEffect(() => {
|
|
fetchCollections();
|
|
}, [fetchCollections]);
|
|
|
|
return { collections, loading, error, refetch: fetchCollections };
|
|
}
|
|
|
|
// Hook for fetching products in a collection
|
|
export function useCollectionProducts(
|
|
handle: string | null,
|
|
options: UseCollectionProductsOptions = {}
|
|
) {
|
|
const [collection, setCollection] = useState<CollectionWithProducts | null>(null);
|
|
const [loading, setLoading] = useState(true);
|
|
const [error, setError] = useState<string | null>(null);
|
|
|
|
const fetchCollection = useCallback(async () => {
|
|
if (!handle) {
|
|
setLoading(false);
|
|
return;
|
|
}
|
|
|
|
try {
|
|
setLoading(true);
|
|
setError(null);
|
|
const data = await getCollectionProducts(handle, options);
|
|
setCollection(data);
|
|
if (!data) {
|
|
setError('Collection not found');
|
|
}
|
|
} catch (err) {
|
|
console.error('Error fetching collection products:', err);
|
|
setError(err instanceof Error ? err.message : 'Failed to load collection');
|
|
} finally {
|
|
setLoading(false);
|
|
}
|
|
}, [handle, options.first, options.sortKey, options.reverse]);
|
|
|
|
useEffect(() => {
|
|
fetchCollection();
|
|
}, [fetchCollection]);
|
|
|
|
return { collection, loading, error, refetch: fetchCollection };
|
|
}
|