import { EntitySearchList } from '@components/common/page-builder/pickers/EntitySearchList.js'; import { _ } from '@evershop/evershop/lib/locale/translate/_'; import { listVirtualCollections } from '@evershop/evershop/lib/util/virtualCollection'; import React, { useEffect, useState } from 'react'; import { useQuery } from 'urql'; /** * Search-and-pick a Collection. The original collection-products widget * inlined this same query + list UI; this file is the canonical copy and * that widget should re-route through here in a follow-up. * * Collections don't have public URLs by default (they're merchandising * groupings, not navigable pages), so the picker returns the `code`. The * caller decides what to do with it — most widgets store the code as the * collection identifier in the widget setting. */ const SEARCH_QUERY = ` query CollectionPickerSearch($filters: [FilterInput]) { collections(filters: $filters) { items { collectionId uuid code name } total } } `; export interface CollectionPickResult { code: string; name: string; uuid: string; } export interface CollectionPickerProps { selectedCode?: string | null; onPick: (result: CollectionPickResult) => void; limit?: number; } export function CollectionPicker({ selectedCode, onPick, limit = 10 }: CollectionPickerProps) { const [search, setSearch] = useState(''); const [debounced, setDebounced] = useState(''); useEffect(() => { const t = setTimeout(() => setDebounced(search), 300); return () => clearTimeout(t); }, [search]); const filters = debounced ? [ { key: 'name', operation: 'like', value: debounced }, { key: 'limit', operation: 'eq', value: String(limit) } ] : [{ key: 'limit', operation: 'eq', value: String(limit) }]; const [result] = useQuery({ query: SEARCH_QUERY, variables: { filters } }); // Built-in collections have no row, so `collections` cannot return them; they // are merged in here and filtered by the same search box. A theme ships one // when it cannot know which collections a store has, and a merchant should be // able to pick it deliberately (catalog/services/virtualCollection.ts). const needle = debounced.trim().toLowerCase(); const virtualItems = listVirtualCollections() .filter((c) => !needle || c.name.toLowerCase().includes(needle)) .map((c) => ({ id: c.code, primary: c.name, secondary: _('built-in'), _uuid: c.uuid })); const items = [ ...virtualItems, ...(result.data?.collections?.items ?? []).map( (c: { uuid: string; code: string; name: string }) => ({ id: c.code, primary: c.name, secondary: c.code, _uuid: c.uuid }) ) ]; return ( onPick({ code: id, name: item.primary, uuid: (item as unknown as { _uuid: string })._uuid }) } caption={_('Pick a collection.')} emptyHint={ debounced ? _('No collections match "${query}".', { query: debounced }) : _('No collections yet.') } /> ); }