Skip to content

armgit / spree

Public Imported from GitHub
0

Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.

Fix catalog audience company picker browse limit and hierarchy (V-3571) (#14548)

Damian Legawiec5b066827bc1b
diff --git a/packages/dashboard-core/src/components/resource-combobox.tsx b/packages/dashboard-core/src/components/resource-combobox.tsx
index 7007036495..4ac7aec1c1 100644
--- a/packages/dashboard-core/src/components/resource-combobox.tsx
+++ b/packages/dashboard-core/src/components/resource-combobox.tsx
@@ -26,9 +26,10 @@ export interface ResourceComboboxProps<T extends ComboboxOption>
* Server-side search. Called as the user types (debounced via React's
* `useDeferredValue`). Receives the trimmed query string — returning
* `{ data: [] }` for empty queries is a fine default, as is showing
- * "recent records".
+ * "recent records". When the response includes `meta.count`, rows beyond
+ * `data.length` trigger a footer hint so a capped list does not read complete.
*/
- search: (query: string) => Promise<{ data: T[] }>
+ search: (query: string) => Promise<{ data: T[]; meta?: { count: number } }>
/**
* Hydrate the currently-selected ID into a record so the trigger can show
@@ -117,6 +118,9 @@ export function ResourceCombobox<T extends ComboboxOption>({
return filterOption ? merged.filter(filterOption) : merged
}, [searchData, hydratedData, filterOption])
+ const visibleSearchCount = searchData?.data.length ?? 0
+ const hiddenCount = Math.max(0, (searchData?.meta?.count ?? 0) - visibleSearchCount)
+
return (
<HeadlessResourceCombobox
value={value}
@@ -127,6 +131,13 @@ export function ResourceCombobox<T extends ComboboxOption>({
renderOption={renderOption}
placeholder={placeholderLabel}
emptyText={emptyLabel}
+ listFooter={
+ hiddenCount > 0 ? (
+ <p className="border-t border-border px-2.5 py-2 text-muted-foreground text-xs">
+ {t('admin.common.combobox_more_results', { count: hiddenCount })}
+ </p>
+ ) : undefined
+ }
disabled={disabled}
id={id}
/>
diff --git a/packages/dashboard-core/src/locales/ar.json b/packages/dashboard-core/src/locales/ar.json
index 2ce88db3d0..1ec57abbfd 100644
--- a/packages/dashboard-core/src/locales/ar.json
+++ b/packages/dashboard-core/src/locales/ar.json
@@ -160,6 +160,8 @@
"next": "التالي",
"no": "لا",
"no_results": "لا توجد نتائج",
+ "combobox_more_results_one": "لم تُعرض نتيجة واحدة أخرى. اكتب للبحث.",
+ "combobox_more_results_other": "لم تُعرض {{count}} نتائج أخرى. اكتب للبحث.",
"none": "لا شيء",
"optional": "اختياري",
"or": "أو",
diff --git a/packages/dashboard-core/src/locales/de.json b/packages/dashboard-core/src/locales/de.json
index fbe7e789fd..0508facdcb 100644
--- a/packages/dashboard-core/src/locales/de.json
+++ b/packages/dashboard-core/src/locales/de.json
@@ -160,6 +160,8 @@
"next": "Weiter",
"no": "Nein",
"no_results": "Keine Ergebnisse",
+ "combobox_more_results_one": "1 weiteres Ergebnis nicht angezeigt. Tippen zum Suchen.",
+ "combobox_more_results_other": "{{count}} weitere Ergebnisse nicht angezeigt. Tippen zum Suchen.",
"none": "Keine",
"optional": "Optional",
"or": "oder",
diff --git a/packages/dashboard-core/src/locales/en.json b/packages/dashboard-core/src/locales/en.json
index f296a02fae..531e9852bb 100644
--- a/packages/dashboard-core/src/locales/en.json
+++ b/packages/dashboard-core/src/locales/en.json
@@ -160,6 +160,8 @@
"next": "Next",
"no": "No",
"no_results": "No results",
+ "combobox_more_results_one": "1 more result not shown. Type to search.",
+ "combobox_more_results_other": "{{count}} more results not shown. Type to search.",
"none": "None",
"optional": "Optional",
"or": "or",
diff --git a/packages/dashboard-core/src/locales/fr.json b/packages/dashboard-core/src/locales/fr.json
index f1ac22f580..ab814f6fb3 100644
--- a/packages/dashboard-core/src/locales/fr.json
+++ b/packages/dashboard-core/src/locales/fr.json
@@ -160,6 +160,8 @@
"next": "Suivant",
"no": "Non",
"no_results": "Aucun résultat",
+ "combobox_more_results_one": "1 résultat de plus non affiché. Saisissez pour rechercher.",
+ "combobox_more_results_other": "{{count}} résultats de plus non affichés. Saisissez pour rechercher.",
"none": "Aucun",
"optional": "Facultatif",
"or": "ou",
diff --git a/packages/dashboard-core/src/locales/pl.json b/packages/dashboard-core/src/locales/pl.json
index c56bcf9343..a38c3650bf 100644
--- a/packages/dashboard-core/src/locales/pl.json
+++ b/packages/dashboard-core/src/locales/pl.json
@@ -160,6 +160,9 @@
"next": "Następna",
"no": "Nie",
"no_results": "Brak wyników",
+ "combobox_more_results_one": "Nie pokazano 1 kolejnego wyniku. Wpisz, aby wyszukać.",
+ "combobox_more_results_few": "Nie pokazano {{count}} kolejnych wyników. Wpisz, aby wyszukać.",
+ "combobox_more_results_other": "Nie pokazano {{count}} kolejnych wyników. Wpisz, aby wyszukać.",
"none": "Brak",
"optional": "Opcjonalne",
"or": "lub",
diff --git a/packages/dashboard-core/src/locales/zh-CN.json b/packages/dashboard-core/src/locales/zh-CN.json
index 1067f1db19..33b8b2b4d7 100644
--- a/packages/dashboard-core/src/locales/zh-CN.json
+++ b/packages/dashboard-core/src/locales/zh-CN.json
@@ -160,6 +160,8 @@
"next": "下一页",
"no": "否",
"no_results": "无结果",
+ "combobox_more_results_one": "还有 1 个结果未显示。输入以搜索。",
+ "combobox_more_results_other": "还有 {{count}} 个结果未显示。输入以搜索。",
"none": "无",
"optional": "可选",
"or": "或",
diff --git a/packages/dashboard-ui/src/spree/resource-combobox.tsx b/packages/dashboard-ui/src/spree/resource-combobox.tsx
index 4fe0c1631d..12c46bd3c0 100644
--- a/packages/dashboard-ui/src/spree/resource-combobox.tsx
+++ b/packages/dashboard-ui/src/spree/resource-combobox.tsx
@@ -49,6 +49,8 @@ export interface ResourceComboboxProps<T extends ComboboxOption> {
placeholder?: string
/** Empty state text shown in the dropdown when items is empty. */
emptyText?: string
+ /** Optional footer below the list — e.g. a count of rows not shown. */
+ listFooter?: ReactNode
disabled?: boolean
/**
@@ -78,6 +80,7 @@ export function ResourceCombobox<T extends ComboboxOption>({
renderOption,
placeholder,
emptyText,
+ listFooter,
disabled,
id,
}: ResourceComboboxProps<T>) {
@@ -109,6 +112,7 @@ export function ResourceCombobox<T extends ComboboxOption>({
</ComboboxItem>
)}
</ComboboxList>
+ {listFooter}
</ComboboxContent>
</Combobox>
)
diff --git a/packages/dashboard/src/components/spree/catalog-audience.tsx b/packages/dashboard/src/components/spree/catalog-audience.tsx
index 56ad6e62d6..841420e35b 100644
--- a/packages/dashboard/src/components/spree/catalog-audience.tsx
+++ b/packages/dashboard/src/components/spree/catalog-audience.tsx
@@ -1,3 +1,4 @@
+import type { Company } from '@spree/admin-sdk'
import { adminClient, ResourceCombobox, useStore } from '@spree/dashboard-core'
import {
Badge,
@@ -35,7 +36,9 @@ import { Link } from '@tanstack/react-router'
import { useState } from 'react'
import type { UseFormReturn } from 'react-hook-form'
import { useTranslation } from 'react-i18next'
+import { companyAutocompleteProps } from '../../hooks/use-companies'
import type { AssignmentEntry, CatalogFormValues } from '../../schemas/catalog'
+import { CompanyComboboxOption } from './company-combobox-option'
const ASSIGNABLE_TYPES = ['company', 'customer_group'] as const
type AssignableType = (typeof ASSIGNABLE_TYPES)[number]
@@ -275,22 +278,6 @@ interface AssignableOption {
email?: string | null
}
-function assignableSearch(type: AssignableType) {
- switch (type) {
- case 'company':
- return {
- search: (q: string) => adminClient.companies.list({ name_cont: q, limit: 10 }),
- hydrate: (ids: string[]) => adminClient.companies.list({ id_in: ids, limit: ids.length }),
- }
- case 'customer_group':
- return {
- search: (q: string) => adminClient.customerGroups.list({ name_cont: q, limit: 10 }),
- hydrate: (ids: string[]) =>
- adminClient.customerGroups.list({ id_in: ids, limit: ids.length }),
- }
- }
-}
-
/** Picking one audience. Its host decides whether this sits inline or in a dialog. */
function AssignCatalogForm({
assigned,
@@ -312,8 +299,6 @@ function AssignCatalogForm({
label: t(`admin.catalogs.assignable_types.${type}`),
}))
- const { search, hydrate } = assignableSearch(assignableType)
-
// A row staged for withdrawal is not a duplicate — picking it again is how
// the merchant takes the removal back.
const duplicate = assigned.some(
@@ -365,22 +350,35 @@ function AssignCatalogForm({
<Field>
<FieldLabel>{t('admin.catalogs.assignments.audience_label')}</FieldLabel>
- <ResourceCombobox<AssignableOption>
- key={assignableType}
- queryKey={`catalog-assignables-${assignableType}`}
- search={search}
- hydrate={hydrate}
- getOptionLabel={(option) => option.name ?? option.email ?? option.id}
- placeholder={t('admin.catalogs.assignments.audience_placeholder')}
- emptyText={t('admin.catalogs.assignments.audience_empty')}
- value={assignableId || undefined}
- // The record, not just the id: a staged row has to render a name
- // before the assignment exists server-side.
- onChange={(id, record) => {
- setAssignableId(id ?? '')
- setAssignableName(record ? (record.name ?? record.email ?? null) : null)
- }}
- />
+ {assignableType === 'company' ? (
+ <ResourceCombobox<Company>
+ key="company"
+ {...companyAutocompleteProps('catalog-assignables-company')}
+ renderOption={(company) => <CompanyComboboxOption company={company} />}
+ value={assignableId || undefined}
+ onChange={(id, record) => {
+ setAssignableId(id ?? '')
+ setAssignableName(record ? (record.name ?? null) : null)
+ }}
+ />
+ ) : (
+ <ResourceCombobox<AssignableOption>
+ key="customer_group"
+ queryKey="catalog-assignables-customer_group"
+ search={(q) =>
+ adminClient.customerGroups.list({ name_cont: q, limit: 100, sort: 'name' })
+ }
+ hydrate={(ids) => adminClient.customerGroups.list({ id_in: ids, limit: ids.length })}
+ getOptionLabel={(group) => group.name ?? group.id}
+ placeholder={t('admin.catalogs.assignments.audience_placeholder')}
+ emptyText={t('admin.catalogs.assignments.audience_empty')}
+ value={assignableId || undefined}
+ onChange={(id, record) => {
+ setAssignableId(id ?? '')
+ setAssignableName(record ? (record.name ?? record.email ?? null) : null)
+ }}
+ />
+ )}
{duplicate && <FieldError>{t('admin.catalogs.assignments.already_assigned')}</FieldError>}
{assignableType === 'company' && (
<FieldDescription>
diff --git a/packages/dashboard/src/components/spree/company-combobox-option.tsx b/packages/dashboard/src/components/spree/company-combobox-option.tsx
new file mode 100644
index 0000000000..18bda898f0
--- /dev/null
+++ b/packages/dashboard/src/components/spree/company-combobox-option.tsx
@@ -0,0 +1,19 @@
+import type { Company } from '@spree/admin-sdk'
+
+/** The ancestor breadcrumb shown under a company name in pickers and lists. */
+export function formatCompanyAncestorPath(company: Company): string | undefined {
+ if (company.ancestors.length === 0) return undefined
+ return company.ancestors.map((ancestor) => ancestor.name).join(' / ')
+}
+
+/** Dropdown row for a company picker — name plus its place in the tree. */
+export function CompanyComboboxOption({ company }: { company: Company }) {
+ const path = formatCompanyAncestorPath(company)
+
+ return (
+ <div>
+ <div className="font-medium">{company.name ?? company.id}</div>
+ {path && <div className="text-muted-foreground text-xs">{path}</div>}
+ </div>
+ )
+}
diff --git a/packages/dashboard/src/hooks/use-companies.ts b/packages/dashboard/src/hooks/use-companies.ts
index ec97d41a7b..ccc27b7991 100644
--- a/packages/dashboard/src/hooks/use-companies.ts
+++ b/packages/dashboard/src/hooks/use-companies.ts
@@ -36,7 +36,7 @@ export function useCompany(id: string | undefined) {
export function companyAutocompleteProps(queryKey: string) {
return {
queryKey,
- search: (q: string) => adminClient.companies.list({ name_cont: q, limit: 20, sort: 'name' }),
+ search: (q: string) => adminClient.companies.list({ name_cont: q, limit: 100, sort: 'name' }),
hydrate: (ids: string[]) => adminClient.companies.list({ id_in: ids, limit: ids.length }),
getOptionLabel: (company: Company) => company.name ?? company.id,
placeholder: i18n.t('admin.companies.autocomplete.placeholder'),
diff --git a/packages/dashboard/src/routes/_authenticated/$storeId/orders/new.tsx b/packages/dashboard/src/routes/_authenticated/$storeId/orders/new.tsx
index 691b8e2dc1..5b8d5ab5a5 100644
--- a/packages/dashboard/src/routes/_authenticated/$storeId/orders/new.tsx
+++ b/packages/dashboard/src/routes/_authenticated/$storeId/orders/new.tsx
@@ -39,6 +39,7 @@ import { useRef, useState } from 'react'
import { useForm } from 'react-hook-form'
import { useTranslation } from 'react-i18next'
import { ChannelSelect } from '../../../../components/spree/channel-select'
+import { CompanyComboboxOption } from '../../../../components/spree/company-combobox-option'
import { useChannels } from '../../../../hooks/use-channels'
import { companyAutocompleteProps } from '../../../../hooks/use-companies'
import { customerAutocompleteProps } from '../../../../hooks/use-customers'
@@ -204,14 +205,7 @@ function NewOrderPage() {
// for. Clearing is the honest reset.
if (record?.id !== company?.id) setCustomer(null)
}}
- renderOption={(c) => (
- <div>
- <div className="font-medium">{c.name}</div>
- <div className="text-xs text-muted-foreground">
- {t(`admin.companies.kind.${c.kind}`)}
- </div>
- </div>
- )}
+ renderOption={(company) => <CompanyComboboxOption company={company} />}
/>
<FieldDescription>{t('admin.pages.orders.new.company_help')}</FieldDescription>
</Field>