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.

Add `<ResourceNameCell>` component to standardize displaying resource names in tables

Damian Legawiecb85b6b89b45c
diff --git a/packages/admin/src/components/spree/resource-name-cell.tsx b/packages/admin/src/components/spree/resource-name-cell.tsx
new file mode 100644
index 0000000000..df64ef93f4
--- /dev/null
+++ b/packages/admin/src/components/spree/resource-name-cell.tsx
@@ -0,0 +1,44 @@
+import type { ReactNode } from 'react'
+import { cn } from '@/lib/utils'
+
+interface ResourceNameCellProps {
+ /** Resource identifier, mirrored into the bridge data attribute. */
+ id: string
+ /** Data attribute the `useRowClickBridge` listener targets (e.g. `data-payment-method-id`). */
+ dataAttr: `data-${string}`
+ /** Primary text — the resource's display name. */
+ name: ReactNode
+ /** Optional secondary line. Falsy values render nothing. */
+ secondary?: ReactNode
+ /** Extra classes for the primary `<span>` — useful for `tabular-nums` on ID/number columns. */
+ nameClassName?: string
+}
+
+/**
+ * Standard "name + secondary" cell used as the primary clickable target in
+ * resource index tables. The wrapping `<button>` carries the bridge data
+ * attribute so `useRowClickBridge` resolves the row → detail navigation.
+ *
+ * Stays purely presentational — routing is the parent table's job via the
+ * bridge hook, not this component's. If you need a custom layout (icon
+ * thumbnail, status pill inline with name, etc.), compose it inline rather
+ * than overloading this cell with props.
+ */
+export function ResourceNameCell({
+ id,
+ dataAttr,
+ name,
+ secondary,
+ nameClassName,
+}: ResourceNameCellProps) {
+ return (
+ <button
+ type="button"
+ {...{ [dataAttr]: id }}
+ className="flex h-full w-full cursor-pointer flex-col items-start rounded text-left focus-visible:outline-none"
+ >
+ <span className={cn('font-medium', nameClassName)}>{name}</span>
+ {secondary && <span className="text-xs text-muted-foreground">{secondary}</span>}
+ </button>
+ )
+}
diff --git a/packages/admin/src/tables/payment-methods.tsx b/packages/admin/src/tables/payment-methods.tsx
index 261f881fcc..c5c0d290bb 100644
--- a/packages/admin/src/tables/payment-methods.tsx
+++ b/packages/admin/src/tables/payment-methods.tsx
@@ -1,6 +1,7 @@
import type { PaymentMethod } from '@spree/admin-sdk'
import { CreditCardIcon } from 'lucide-react'
-import { ActiveBadge } from '@/components/ui/badge'
+import { ResourceNameCell } from '@/components/spree/resource-name-cell'
+import { ActiveBadge, Badge } from '@/components/ui/badge'
import { defineTable } from '@/lib/table-registry'
defineTable<PaymentMethod>('payment-methods', {
@@ -18,18 +19,12 @@ defineTable<PaymentMethod>('payment-methods', {
filterable: true,
default: true,
render: (pm) => (
- <button
- type="button"
- data-payment-method-id={pm.id}
- className="flex flex-col items-start text-left hover:underline focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring rounded"
- >
- <span className="font-medium">{pm.name}</span>
- {pm.description && (
- <span data-payment-method-id={pm.id} className="text-xs text-muted-foreground">
- {pm.description}
- </span>
- )}
- </button>
+ <ResourceNameCell
+ id={pm.id}
+ dataAttr="data-payment-method-id"
+ name={pm.name}
+ secondary={pm.description}
+ />
),
},
{
@@ -38,7 +33,7 @@ defineTable<PaymentMethod>('payment-methods', {
sortable: true,
filterable: true,
default: true,
- render: (pm) => pm.type,
+ render: (pm) => <Badge variant="outline">{pm.type}</Badge>,
},
{
key: 'storefront_visible',
diff --git a/packages/admin/src/tables/promotions.tsx b/packages/admin/src/tables/promotions.tsx
index 16156c9f7d..9d10f161d0 100644
--- a/packages/admin/src/tables/promotions.tsx
+++ b/packages/admin/src/tables/promotions.tsx
@@ -1,6 +1,7 @@
import type { Promotion } from '@spree/admin-sdk'
import { TagIcon } from 'lucide-react'
import { RelativeTime } from '@/components/spree/relative-time'
+import { ResourceNameCell } from '@/components/spree/resource-name-cell'
import { Badge } from '@/components/ui/badge'
import { defineTable } from '@/lib/table-registry'
@@ -19,14 +20,12 @@ defineTable<Promotion>('promotions', {
filterable: true,
default: true,
render: (p) => (
- <button
- type="button"
- data-promotion-id={p.id}
- className="flex flex-col items-start text-left hover:underline focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring rounded"
- >
- <span className="font-medium">{p.name}</span>
- {p.description && <span className="text-xs text-muted-foreground">{p.description}</span>}
- </button>
+ <ResourceNameCell
+ id={p.id}
+ dataAttr="data-promotion-id"
+ name={p.name}
+ secondary={p.description}
+ />
),
},
{
diff --git a/packages/admin/src/tables/stock-locations.tsx b/packages/admin/src/tables/stock-locations.tsx
index 115ebf59bc..bc0b7804ce 100644
--- a/packages/admin/src/tables/stock-locations.tsx
+++ b/packages/admin/src/tables/stock-locations.tsx
@@ -1,5 +1,6 @@
import type { StockLocation } from '@spree/admin-sdk'
import { PencilIcon, WarehouseIcon } from 'lucide-react'
+import { ResourceNameCell } from '@/components/spree/resource-name-cell'
import { ActiveBadge } from '@/components/ui/badge'
import { Button } from '@/components/ui/button'
import { defineTable } from '@/lib/table-registry'
@@ -18,22 +19,13 @@ defineTable<StockLocation>('stock-locations', {
sortable: true,
filterable: true,
default: true,
- // The button carries `data-stock-location-id`. The route page mounts a
- // delegated click listener (RowClickBridge) that opens the edit Sheet
- // when any descendant marked with that attribute is clicked.
render: (sl) => (
- <button
- type="button"
- data-stock-location-id={sl.id}
- className="flex flex-col items-start text-left hover:underline focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring rounded cursor-pointer w-full"
- >
- <span className="font-medium">{sl.name}</span>
- {sl.admin_name && (
- <span data-stock-location-id={sl.id} className="text-xs text-muted-foreground">
- {sl.admin_name}
- </span>
- )}
- </button>
+ <ResourceNameCell
+ id={sl.id}
+ dataAttr="data-stock-location-id"
+ name={sl.name}
+ secondary={sl.admin_name}
+ />
),
},
{
diff --git a/packages/admin/src/tables/stock-transfers.tsx b/packages/admin/src/tables/stock-transfers.tsx
index 4c57f4cb5f..f4844a6a40 100644
--- a/packages/admin/src/tables/stock-transfers.tsx
+++ b/packages/admin/src/tables/stock-transfers.tsx
@@ -1,6 +1,7 @@
import type { StockTransfer } from '@spree/admin-sdk'
import { ArrowLeftRightIcon } from 'lucide-react'
import { RelativeTime } from '@/components/spree/relative-time'
+import { ResourceNameCell } from '@/components/spree/resource-name-cell'
import { Badge } from '@/components/ui/badge'
import { defineTable } from '@/lib/table-registry'
@@ -19,13 +20,12 @@ defineTable<StockTransfer>('stock-transfers', {
filterable: true,
default: true,
render: (st) => (
- <button
- type="button"
- data-stock-transfer-id={st.id}
- className="font-medium tabular-nums hover:underline focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring rounded"
- >
- {st.number}
- </button>
+ <ResourceNameCell
+ id={st.id}
+ dataAttr="data-stock-transfer-id"
+ name={st.number}
+ nameClassName="tabular-nums"
+ />
),
},
{
diff --git a/packages/admin/src/tables/tax-categories.tsx b/packages/admin/src/tables/tax-categories.tsx
index 7cc73f47c8..b91857b237 100644
--- a/packages/admin/src/tables/tax-categories.tsx
+++ b/packages/admin/src/tables/tax-categories.tsx
@@ -1,5 +1,6 @@
import type { TaxCategory } from '@spree/admin-sdk'
import { PercentIcon } from 'lucide-react'
+import { ResourceNameCell } from '@/components/spree/resource-name-cell'
import { ActiveBadge } from '@/components/ui/badge'
import { defineTable } from '@/lib/table-registry'
@@ -18,18 +19,12 @@ defineTable<TaxCategory>('tax-categories', {
filterable: true,
default: true,
render: (tc) => (
- <button
- type="button"
- data-tax-category-id={tc.id}
- className="flex flex-col items-start text-left hover:underline focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring rounded"
- >
- <span className="font-medium">{tc.name}</span>
- {tc.description && (
- <span data-tax-category-id={tc.id} className="text-xs text-muted-foreground">
- {tc.description}
- </span>
- )}
- </button>
+ <ResourceNameCell
+ id={tc.id}
+ dataAttr="data-tax-category-id"
+ name={tc.name}
+ secondary={tc.description}
+ />
),
},
{