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.

6.0 KB
Raw Blame
import { defineTable, Money } from '@spree/dashboard-core'
import { ActiveBadge, Badge, RelativeTime, TagList } from '@spree/dashboard-ui'
import { UsersIcon } from '@spree/dashboard-ui/icons'
import { Link } from '@tanstack/react-router'
import i18n from 'i18next'
import { companyAutocompleteProps } from '../hooks/use-companies'
import { customerGroupAutocompleteProps } from '../hooks/use-customer-groups'
import { customerDisplayName, erasedFieldValue } from '../lib/erased-customer'

defineTable('customers', {
  title: i18n.t('admin.nav.customers'),
  docsPath: 'customers/customer-profile',
  description: i18n.t('admin.table_descriptions.customers'),
  searchParam: 'search',
  searchPlaceholder: i18n.t('admin.customers.table.search_placeholder'),
  defaultSort: { field: 'created_at', direction: 'desc' },
  emptyIcon: <UsersIcon className="size-8 text-muted-foreground" />,
  emptyMessage: i18n.t('admin.customers.table.empty'),
  columns: [
    {
      key: 'email',
      label: i18n.t('admin.fields.email.label'),
      sortable: true,
      default: true,
      render: (c) => (
        <Link
          to={'/$storeId/customers/$customerId' as string}
          params={{ customerId: c.id }}
          className="font-medium text-foreground no-underline"
        >
          {erasedFieldValue(c.email, c.anonymized)}
        </Link>
      ),
    },
    {
      key: 'first_name',
      label: i18n.t('admin.fields.name.label'),
      sortable: true,
      default: true,
      render: (c) => customerDisplayName(c, i18n.t('admin.customers.erased.name')),
    },
    {
      key: 'phone',
      label: i18n.t('admin.fields.phone.label'),
      sortable: true,
      // Kept filterable where email and name are not: the search scope matches
      // those two but never `phone`, so this is the only way to look a customer
      // up by number.
      filterable: true,
      default: false,
      render: (c) => erasedFieldValue(c.phone, c.anonymized),
    },
    {
      key: 'orders_count',
      label: i18n.t('admin.customers.columns.orders'),
      default: true,
      className: 'text-right tabular-nums',
      render: (c) => c.orders_count ?? 0,
    },
    {
      key: 'total_spent',
      label: i18n.t('admin.customers.columns.total_spent'),
      default: true,
      className: 'text-right tabular-nums whitespace-nowrap font-medium',
      render: (c) => <Money amount={c.total_spent} />,
    },
    {
      key: 'last_order_completed_at',
      label: i18n.t('admin.customers.columns.last_order'),
      default: true,
      className: 'text-sm text-muted-foreground whitespace-nowrap',
      render: (c) => <RelativeTime iso={c.last_order_completed_at} />,
    },
    {
      key: 'customer_groups',
      label: i18n.t('admin.customers.columns.groups'),
      default: false,
      filterable: true,
      filterType: 'resource',
      // `users.customer_groups.id` join — backed by the
      // `customer_groups` whitelisted_ransackable_association on User.
      ransackAttribute: 'customer_groups_id',
      filterResource: customerGroupAutocompleteProps('customer-groups-picker'),
      render: (c) => {
        const groups = c.customer_groups ?? []
        if (groups.length === 0) return '—'
        return (
          <div className="flex flex-wrap gap-1">
            {groups.map((g: { id: string; name: string }) => (
              <Badge key={g.id} variant="secondary">
                {g.name}
              </Badge>
            ))}
          </div>
        )
      },
    },
    {
      key: 'companies',
      label: i18n.t('admin.customers.columns.companies'),
      default: false,
      sortable: false,
      filterable: true,
      filterType: 'resource',
      expand: 'companies',
      // Scope, not `companies_id_in`: standing covers a node AND its ancestors.
      ransackAttribute: 'with_standing_for_company',
      ransackScope: true,
      filterResource: companyAutocompleteProps('companies-filter-picker'),
      render: (c) => {
        const companies = c.companies ?? []
        if (companies.length === 0) return '—'
        return (
          <div className="flex flex-wrap gap-1">
            {companies.map((company: { id: string; name: string }) => (
              <Badge key={company.id} variant="secondary">
                {company.name}
              </Badge>
            ))}
          </div>
        )
      },
    },
    {
      key: 'tags',
      label: i18n.t('admin.fields.customer.tags.label'),
      sortable: false,
      filterable: true,
      filterType: 'tags',
      taggableType: 'customer',
      default: false,
      render: (c) => <TagList tags={c.tags} />,
    },
    {
      // A person whose data was erased keeps their orders and their totals, so
      // the row still means something — but they are not a customer anyone can
      // contact, and leaving them in the list makes every search noisier. Off
      // by default (see `defaultParams` on the customers route), findable when
      // someone needs to confirm an erasure happened.
      key: 'anonymized',
      label: i18n.t('admin.customers.columns.erased'),
      // A scope, not a predicate on `anonymized_at`: the question is whether
      // an erasure happened, and a boolean control needs something that takes
      // one of its two states as a value.
      ransackAttribute: 'anonymized',
      ransackScope: true,
      sortable: false,
      filterable: true,
      filterType: 'boolean',
      booleanLabels: {
        true: i18n.t('admin.customers.filters.erased'),
        false: i18n.t('admin.customers.filters.not_erased'),
      },
      default: false,
      render: (c) =>
        c.anonymized_at ? (
          <Badge variant="secondary">
            <RelativeTime iso={c.anonymized_at} />
          </Badge>
        ) : (
          '—'
        ),
    },
    {
      key: 'accepts_email_marketing',
      label: i18n.t('admin.customers.columns.newsletter'),
      sortable: true,
      filterable: true,
      default: false,
      filterType: 'boolean',
      render: (c) => <ActiveBadge active={c.accepts_email_marketing} />,
    },
  ],
})