Skip to content

The complete open source platform to run your entire marketplace. Storefront, vendor panel, admin console, and integrations in one enterprise-grade platform you own.

fix(admin): search order list filter options on the server (#1640)

Viktor Holik786718ae3829
diff --git a/packages/admin/src/components/table/data-table/data-table-filter/data-table-filter.tsx b/packages/admin/src/components/table/data-table/data-table-filter/data-table-filter.tsx
index 83609b036..9db174736 100644
--- a/packages/admin/src/components/table/data-table/data-table-filter/data-table-filter.tsx
+++ b/packages/admin/src/components/table/data-table/data-table-filter/data-table-filter.tsx
@@ -24,6 +24,13 @@ export type Filter = {
options: Option[]
multiple?: boolean
searchable?: boolean
+ /**
+ * Hands the search text to the owner so options can be fetched from the
+ * server instead of filtered client-side. Selected values must still be
+ * present in `options` for their labels to render.
+ */
+ onSearch?: (value: string) => void
+ isLoading?: boolean
}
| {
type: "date"
@@ -136,6 +143,8 @@ export const DataTableFilter = ({
options={filter.options}
multiple={filter.multiple}
searchable={filter.searchable}
+ onSearch={filter.onSearch}
+ isLoading={filter.isLoading}
openOnMount={filter.openOnMount}
/>
)
diff --git a/packages/admin/src/components/table/data-table/data-table-filter/select-filter.tsx b/packages/admin/src/components/table/data-table/data-table-filter/select-filter.tsx
index 7f7a4703d..3ede8f6e6 100644
--- a/packages/admin/src/components/table/data-table/data-table-filter/select-filter.tsx
+++ b/packages/admin/src/components/table/data-table/data-table-filter/select-filter.tsx
@@ -15,6 +15,8 @@ interface SelectFilterProps extends IFilter {
readonly?: boolean
multiple?: boolean
searchable?: boolean
+ onSearch?: (value: string) => void
+ isLoading?: boolean
}
export const SelectFilter = ({
@@ -23,6 +25,8 @@ export const SelectFilter = ({
readonly,
multiple,
searchable,
+ onSearch,
+ isLoading,
options,
openOnMount,
}: SelectFilterProps) => {
@@ -68,8 +72,13 @@ export const SelectFilter = ({
}
}
+ const handleSearchChange = (value: string) => {
+ setSearch(value)
+ onSearch?.(value)
+ }
+
const handleClearSearch = () => {
- setSearch("")
+ handleSearchChange("")
if (searchRef) {
searchRef.focus()
@@ -129,14 +138,14 @@ export const SelectFilter = ({
}
}}
>
- <Command className="h-full">
+ <Command className="h-full" shouldFilter={!onSearch}>
{searchable && (
<div className="border-b p-1">
<div className="grid grid-cols-[1fr_20px] gap-x-2 rounded-md px-2 py-1">
<Command.Input
ref={setSearchRef}
value={search}
- onValueChange={setSearch}
+ onValueChange={handleSearchChange}
className="txt-compact-small placeholder:text-ui-fg-muted bg-transparent outline-none"
placeholder={t("general.search")}
/>
@@ -159,7 +168,9 @@ export const SelectFilter = ({
)}
<Command.Empty className="txt-compact-small flex items-center justify-center p-1">
<span className="w-full px-2 py-1 text-center">
- {t("general.noResultsTitle")}
+ {isLoading
+ ? t("general.loading")
+ : t("general.noResultsTitle")}
</span>
</Command.Empty>
<Command.List className="h-full max-h-[163px] min-h-[0] overflow-auto p-1 outline-none">
diff --git a/packages/admin/src/pages/orders/order-list/components/order-list-table/use-order-table-filters.tsx b/packages/admin/src/pages/orders/order-list/components/order-list-table/use-order-table-filters.tsx
index 5a0fdefe2..50eaa4520 100644
--- a/packages/admin/src/pages/orders/order-list/components/order-list-table/use-order-table-filters.tsx
+++ b/packages/admin/src/pages/orders/order-list/components/order-list-table/use-order-table-filters.tsx
@@ -1,82 +1,170 @@
import { usePermissions } from "@mercurjs/dashboard-shared"
+import { keepPreviousData } from "@tanstack/react-query"
import { useMemo } from "react"
import { useTranslation } from "react-i18next"
+import { useSearchParams } from "react-router-dom"
import { Filter } from "@components/table/data-table/data-table-filter"
import { useSalesChannels } from "@hooks/api/sales-channels"
import { useCustomers } from "@hooks/api/customers"
import { useSellers } from "@hooks/api/sellers"
+import { useDebouncedSearch } from "@hooks/use-debounced-search"
+
+// Options are searched server-side, so only a page of candidates is loaded
+// at a time. Entities already selected in the URL are fetched separately so
+// their labels render even when they fall outside the current search.
+const OPTIONS_PAGE_SIZE = 50
+
+type Option = { label: string; value: string }
+
+const mergeOptions = (selected: Option[], found: Option[]) => {
+ const seen = new Set<string>()
+ return [...selected, ...found].filter((option) => {
+ if (seen.has(option.value)) {
+ return false
+ }
+ seen.add(option.value)
+ return true
+ })
+}
+
+const useSelectedIds = (key: string) => {
+ const [searchParams] = useSearchParams()
+ const raw = searchParams.get(key)
+ return useMemo(() => raw?.split(",").filter(Boolean) ?? [], [raw])
+}
export const useOrderGroupTableFilters = () => {
const { t } = useTranslation()
const { can } = usePermissions()
- const { customers } = useCustomers(
+ const customerSearch = useDebouncedSearch()
+ const sellerSearch = useDebouncedSearch()
+ const salesChannelSearch = useDebouncedSearch()
+
+ const selectedCustomerIds = useSelectedIds("customer_id")
+ const selectedSellerIds = useSelectedIds("seller_id")
+ const selectedSalesChannelIds = useSelectedIds("sales_channel_id")
+
+ const canViewCustomers = can("customers")
+ const canViewSellers = can("sellers")
+ const canViewSalesChannels = can("sales_channels")
+
+ const customerFields = "id,first_name,last_name,email"
+ const { customers, isFetching: isFetchingCustomers } = useCustomers(
{
- limit: 1000,
- fields: "id,first_name,last_name,email",
+ limit: OPTIONS_PAGE_SIZE,
+ fields: customerFields,
+ ...(customerSearch.query ? { q: customerSearch.query } : {}),
},
- { enabled: can("customers") }
+ { enabled: canViewCustomers, placeholderData: keepPreviousData }
+ )
+ const { customers: selectedCustomers } = useCustomers(
+ {
+ id: selectedCustomerIds,
+ limit: selectedCustomerIds.length,
+ fields: customerFields,
+ },
+ { enabled: canViewCustomers && selectedCustomerIds.length > 0 }
)
- const { sellers } = useSellers(
+ const { sellers, isFetching: isFetchingSellers } = useSellers(
+ {
+ limit: OPTIONS_PAGE_SIZE,
+ fields: "id,name",
+ ...(sellerSearch.query ? { q: sellerSearch.query } : {}),
+ },
+ { enabled: canViewSellers, placeholderData: keepPreviousData }
+ )
+ const { sellers: selectedSellers } = useSellers(
{
- limit: 1000,
+ id: selectedSellerIds,
+ limit: selectedSellerIds.length,
fields: "id,name",
},
- { enabled: can("sellers") }
+ { enabled: canViewSellers && selectedSellerIds.length > 0 }
)
- const { sales_channels } = useSalesChannels(
+ const { sales_channels, isFetching: isFetchingSalesChannels } =
+ useSalesChannels(
+ {
+ limit: OPTIONS_PAGE_SIZE,
+ fields: "id,name",
+ ...(salesChannelSearch.query ? { q: salesChannelSearch.query } : {}),
+ },
+ { enabled: canViewSalesChannels, placeholderData: keepPreviousData }
+ )
+ const { sales_channels: selectedSalesChannels } = useSalesChannels(
{
- limit: 1000,
+ id: selectedSalesChannelIds,
+ limit: selectedSalesChannelIds.length,
fields: "id,name",
},
- { enabled: can("sales_channels") }
+ { enabled: canViewSalesChannels && selectedSalesChannelIds.length > 0 }
)
return useMemo(() => {
const filters: Filter[] = []
- if (customers?.length) {
+ const customerOption = (c: {
+ id: string
+ first_name?: string | null
+ last_name?: string | null
+ email?: string | null
+ }): Option => ({
+ label:
+ [c.first_name, c.last_name].filter(Boolean).join(" ") || c.email || c.id,
+ value: c.id,
+ })
+ const namedOption = (s: { id: string; name?: string | null }): Option => ({
+ label: s.name || s.id,
+ value: s.id,
+ })
+
+ if (canViewCustomers) {
filters.push({
key: "customer_id",
label: t("fields.customer"),
type: "select",
multiple: true,
searchable: true,
- options: customers.map((c) => ({
- label:
- [c.first_name, c.last_name].filter(Boolean).join(" ") || c.email,
- value: c.id,
- })),
+ onSearch: customerSearch.onSearchValueChange,
+ isLoading: isFetchingCustomers,
+ options: mergeOptions(
+ (selectedCustomers ?? []).map(customerOption),
+ (customers ?? []).map(customerOption)
+ ),
})
}
- if (sellers?.length) {
+ if (canViewSellers) {
filters.push({
key: "seller_id",
label: t("fields.store"),
type: "select",
multiple: true,
searchable: true,
- options: sellers.map((s) => ({
- label: s.name,
- value: s.id,
- })),
+ onSearch: sellerSearch.onSearchValueChange,
+ isLoading: isFetchingSellers,
+ options: mergeOptions(
+ (selectedSellers ?? []).map(namedOption),
+ (sellers ?? []).map(namedOption)
+ ),
})
}
- if (sales_channels?.length) {
+ if (canViewSalesChannels) {
filters.push({
key: "sales_channel_id",
label: t("fields.salesChannel"),
type: "select",
multiple: true,
searchable: true,
- options: sales_channels.map((s) => ({
- label: s.name,
- value: s.id,
- })),
+ onSearch: salesChannelSearch.onSearchValueChange,
+ isLoading: isFetchingSalesChannels,
+ options: mergeOptions(
+ (selectedSalesChannels ?? []).map(namedOption),
+ (sales_channels ?? []).map(namedOption)
+ ),
})
}
@@ -94,5 +182,22 @@ export const useOrderGroupTableFilters = () => {
)
return filters
- }, [customers, sellers, sales_channels, t])
+ }, [
+ t,
+ canViewCustomers,
+ canViewSellers,
+ canViewSalesChannels,
+ customers,
+ selectedCustomers,
+ isFetchingCustomers,
+ customerSearch.onSearchValueChange,
+ sellers,
+ selectedSellers,
+ isFetchingSellers,
+ sellerSearch.onSearchValueChange,
+ sales_channels,
+ selectedSalesChannels,
+ isFetchingSalesChannels,
+ salesChannelSearch.onSearchValueChange,
+ ])
}