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 created_at and updated_at columns to all dashboard tables (#14217)

Damian Legawiec7135f177c33e
diff --git a/packages/dashboard-core/src/index.ts b/packages/dashboard-core/src/index.ts
index 2fe5282064..ca90fc2230 100644
--- a/packages/dashboard-core/src/index.ts
+++ b/packages/dashboard-core/src/index.ts
@@ -73,6 +73,7 @@ export * from './lib/search-registry'
export * from './lib/settings-nav-registry'
export * from './lib/slot-registry'
export * from './lib/table-registry'
+export { ensureTimestampColumns } from './lib/timestamp-columns'
// ---------------------------------------------------------------------------
// Plugin facade — re-exported for convenience; same API as `/plugin` subpath
// ---------------------------------------------------------------------------
diff --git a/packages/dashboard-core/src/lib/table-registry.ts b/packages/dashboard-core/src/lib/table-registry.ts
index eb386492fd..84892bdbc6 100644
--- a/packages/dashboard-core/src/lib/table-registry.ts
+++ b/packages/dashboard-core/src/lib/table-registry.ts
@@ -1,4 +1,5 @@
import type { ReactNode } from 'react'
+import { ensureTimestampColumns } from './timestamp-columns'
// ============================================================================
// Types
@@ -168,7 +169,11 @@ function flushPending(tableKey: string, table: TableDef) {
}
export function defineTable<T = any>(key: string, def: Omit<TableDef<T>, 'key'>): TableDef<T> {
- const tableDef = { ...def, key } as TableDef<T>
+ const tableDef = {
+ ...def,
+ key,
+ columns: ensureTimestampColumns(def.columns),
+ } as TableDef<T>
registry.set(key, tableDef)
flushPending(key, tableDef as TableDef)
return tableDef
diff --git a/packages/dashboard-core/src/lib/timestamp-columns.tsx b/packages/dashboard-core/src/lib/timestamp-columns.tsx
new file mode 100644
index 0000000000..b543b4c550
--- /dev/null
+++ b/packages/dashboard-core/src/lib/timestamp-columns.tsx
@@ -0,0 +1,58 @@
+import { RelativeTime } from '@spree/dashboard-ui'
+import i18n from 'i18next'
+import type { ColumnDef } from './table-registry'
+
+const TIMESTAMP_KEYS = ['created_at', 'updated_at'] as const
+
+type TimestampKey = (typeof TIMESTAMP_KEYS)[number]
+
+const TIMESTAMP_CLASS = 'text-sm text-muted-foreground whitespace-nowrap'
+
+function createTimestampColumn(key: TimestampKey, visibleByDefault = false): ColumnDef {
+ return {
+ key,
+ label: i18n.t(`admin.fields.${key}.label`),
+ sortable: true,
+ filterable: true,
+ default: visibleByDefault,
+ filterType: 'date',
+ className: TIMESTAMP_CLASS,
+ render: (row) => <RelativeTime iso={row[key]} />,
+ }
+}
+
+function enhanceTimestampColumn(col: ColumnDef, key: TimestampKey): ColumnDef {
+ const sortable = col.sortable !== false
+ const filterable = col.filterable !== false
+
+ return {
+ ...col,
+ label: col.label || i18n.t(`admin.fields.${key}.label`),
+ sortable,
+ filterable,
+ ...(filterable ? { filterType: 'date' as const } : {}),
+ default: col.default ?? false,
+ className: col.className ?? TIMESTAMP_CLASS,
+ render: col.render ?? ((row) => <RelativeTime iso={row[key]} />),
+ } as ColumnDef
+}
+
+/**
+ * Ensures every table exposes `created_at` and `updated_at` as sortable,
+ * filterable date columns. Missing columns are appended (hidden by default);
+ * existing definitions are preserved and augmented with sort/filter when absent.
+ */
+export function ensureTimestampColumns(columns: ColumnDef[]): ColumnDef[] {
+ const result = [...columns]
+
+ for (const key of TIMESTAMP_KEYS) {
+ const index = result.findIndex((col) => col.key === key)
+ if (index === -1) {
+ result.push(createTimestampColumn(key))
+ } else {
+ result[index] = enhanceTimestampColumn(result[index], key)
+ }
+ }
+
+ return result
+}
diff --git a/packages/dashboard/src/tables/allowed-origins.tsx b/packages/dashboard/src/tables/allowed-origins.tsx
index d9c2818671..d145510185 100644
--- a/packages/dashboard/src/tables/allowed-origins.tsx
+++ b/packages/dashboard/src/tables/allowed-origins.tsx
@@ -26,7 +26,10 @@ defineTable<AllowedOrigin>('allowed-origins', {
key: 'created_at',
label: i18n.t('admin.fields.created_at.label'),
sortable: true,
+ filterable: true,
default: true,
+ filterType: 'date',
+ className: 'text-sm text-muted-foreground whitespace-nowrap',
render: (origin) => <RelativeTime iso={origin.created_at} />,
},
],
diff --git a/packages/dashboard/src/tables/customers.tsx b/packages/dashboard/src/tables/customers.tsx
index 7607a1a077..c571b359c9 100644
--- a/packages/dashboard/src/tables/customers.tsx
+++ b/packages/dashboard/src/tables/customers.tsx
@@ -109,14 +109,5 @@ defineTable('customers', {
filterType: 'boolean',
render: (c) => <ActiveBadge active={c.accepts_email_marketing} />,
},
- {
- key: 'created_at',
- label: i18n.t('admin.fields.created_at.label'),
- sortable: true,
- default: false,
- filterType: 'date',
- className: 'text-sm text-muted-foreground whitespace-nowrap',
- render: (c) => <RelativeTime iso={c.created_at} />,
- },
],
})
diff --git a/packages/dashboard/src/tables/gift-cards.tsx b/packages/dashboard/src/tables/gift-cards.tsx
index 0060245b08..2e5afa78ed 100644
--- a/packages/dashboard/src/tables/gift-cards.tsx
+++ b/packages/dashboard/src/tables/gift-cards.tsx
@@ -157,14 +157,5 @@ defineTable<GiftCard>('gift-cards', {
className: 'text-sm text-muted-foreground whitespace-nowrap',
render: (g) => (g.expires_at ? <RelativeTime iso={g.expires_at} /> : '—'),
},
- {
- key: 'created_at',
- label: i18n.t('admin.fields.created_at.label'),
- sortable: true,
- filterType: 'date',
- default: false,
- className: 'text-sm text-muted-foreground whitespace-nowrap',
- render: (g) => <RelativeTime iso={g.created_at} />,
- },
],
})
diff --git a/packages/dashboard/src/tables/option-types.tsx b/packages/dashboard/src/tables/option-types.tsx
index 869cd7bbd6..e94ceab41b 100644
--- a/packages/dashboard/src/tables/option-types.tsx
+++ b/packages/dashboard/src/tables/option-types.tsx
@@ -1,6 +1,6 @@
import type { OptionType } from '@spree/admin-sdk'
import { defineTable } from '@spree/dashboard-core'
-import { ActiveBadge, Badge, RelativeTime, ResourceNameCell } from '@spree/dashboard-ui'
+import { ActiveBadge, Badge, ResourceNameCell } from '@spree/dashboard-ui'
import i18n from 'i18next'
import { ListChecksIcon } from 'lucide-react'
@@ -62,11 +62,5 @@ defineTable<OptionType>('option-types', {
default: true,
render: (ot) => <ActiveBadge active={ot.filterable ?? false} dashWhenInactive />,
},
- {
- key: 'updated_at',
- label: i18n.t('admin.fields.updated_at.label'),
- sortable: true,
- render: (ot) => <RelativeTime iso={ot.updated_at} />,
- },
],
})
diff --git a/packages/dashboard/src/tables/orders.tsx b/packages/dashboard/src/tables/orders.tsx
index 01e384d8c0..c2dec26967 100644
--- a/packages/dashboard/src/tables/orders.tsx
+++ b/packages/dashboard/src/tables/orders.tsx
@@ -174,24 +174,6 @@ defineTable('orders', {
default: false,
render: (order) => <TagList tags={order.tags} />,
},
- {
- key: 'created_at',
- label: i18n.t('admin.fields.created_at.label'),
- sortable: true,
- default: false,
- filterType: 'date',
- className: 'text-sm text-muted-foreground whitespace-nowrap',
- render: (order) => <RelativeTime iso={order.created_at} />,
- },
- {
- key: 'updated_at',
- label: i18n.t('admin.fields.updated_at.label'),
- sortable: true,
- default: false,
- filterType: 'date',
- className: 'text-sm text-muted-foreground whitespace-nowrap',
- render: (order) => <RelativeTime iso={order.updated_at} />,
- },
// Filter-only columns
{
key: 'first_name',
diff --git a/packages/dashboard/src/tables/products.tsx b/packages/dashboard/src/tables/products.tsx
index 07a3001281..07a153061d 100644
--- a/packages/dashboard/src/tables/products.tsx
+++ b/packages/dashboard/src/tables/products.tsx
@@ -1,6 +1,6 @@
import type { Channel } from '@spree/admin-sdk'
import { defineTable, formatPrice, Subject } from '@spree/dashboard-core'
-import { RelativeTime, StatusBadge, TagList } from '@spree/dashboard-ui'
+import { StatusBadge, TagList } from '@spree/dashboard-ui'
import { Link } from '@tanstack/react-router'
import i18n from 'i18next'
import { PackageIcon } from 'lucide-react'
@@ -130,26 +130,6 @@ defineTable('products', {
default: false,
render: (product) => <TagList tags={product.tags} />,
},
- {
- key: 'created_at',
- label: i18n.t('admin.fields.created_at.label'),
- sortable: true,
- filterable: true,
- default: false,
- filterType: 'date',
- className: 'text-sm text-muted-foreground whitespace-nowrap',
- render: (product) => <RelativeTime iso={product.created_at} />,
- },
- {
- key: 'updated_at',
- label: i18n.t('admin.fields.updated_at.label'),
- sortable: true,
- filterable: true,
- default: false,
- filterType: 'date',
- className: 'text-sm text-muted-foreground whitespace-nowrap',
- render: (product) => <RelativeTime iso={product.updated_at} />,
- },
{
key: 'in_stock',
label: i18n.t('admin.products.columns.in_stock'),
diff --git a/packages/dashboard/src/tables/stock-transfers.tsx b/packages/dashboard/src/tables/stock-transfers.tsx
index ef0689ca13..b7e85a4cf4 100644
--- a/packages/dashboard/src/tables/stock-transfers.tsx
+++ b/packages/dashboard/src/tables/stock-transfers.tsx
@@ -51,6 +51,7 @@ defineTable<StockTransfer>('stock-transfers', {
key: 'created_at',
label: i18n.t('admin.fields.created_at.label'),
sortable: true,
+ filterable: true,
default: true,
filterType: 'date',
className: 'text-sm text-muted-foreground whitespace-nowrap',