import { ExternalLinkIcon, TagIcon } from 'lucide-react';
import type { ReactNode } from 'react';
import { ClippedText } from '@/components/table/clipped-text';
import type { StatusPalette } from '@/components/table/status-badge';
import { StatusBadge } from '@/components/table/status-badge';
import { formatDate, formatDateTime, formatMoney } from '@/lib/format';
import { t } from '@/lib/ui-text';
import type { ColumnKey, ProductRow } from './types';
/**
* The products table as data — the frontend mirror of `App\Columns\ProductColumns`.
*
* The header row, the body cells and the loading skeleton are all rendered by
* mapping over this list, so a new column is one entry here (and one line in the
* PHP column set) rather than an edit in three places that drift apart.
*/
/**
* Classes that hide a cell until the TABLE, not the window, is wide enough for
* it — a container query, because the sidebar takes its width at the same
* breakpoints a viewport query would use.
*
* Each column's `reveal` below is the table width it needs, worked out by the
* generator from this module's own columns: statuses first, then relations,
* amounts and dates, each one once the table holds it and every column before
* it. A column that is not shown yet is still in the Columns menu (which shows
* it at any width), on the detail page and in every export. To change which columns come first,
* use the `show` / `collapse` / `no-index` hints and regenerate rather than
* editing the sizes by hand.
*/
export const REVEAL_CLASS = {
'576px': 'hidden @min-[576px]:table-cell',
'800px': 'hidden @min-[800px]:table-cell',
'896px': 'hidden @min-[896px]:table-cell',
'1056px': 'hidden @min-[1056px]:table-cell',
'1184px': 'hidden @min-[1184px]:table-cell',
'1440px': 'hidden @min-[1440px]:table-cell',
} as const;
/**
* Columns kept out of the table by default — still on the detail page and in
* every export, just not worth a column here.
*/
export const SECONDARY_COLUMNS: ColumnKey[] = [
'sku',
'compare_at_price',
'is_featured',
'published_at',
'publishedByRef',
];
export const STATUS_PALETTE: StatusPalette = {
draft: 'border-zinc-300/60 text-zinc-600 dark:border-zinc-700 dark:text-zinc-400',
active: 'border-emerald-300/60 text-emerald-700 dark:border-emerald-800 dark:text-emerald-400',
archived:
'border-violet-300/60 text-violet-700 dark:border-violet-800 dark:text-violet-400',
};
export type ProductColumn = {
/** `null` marks a column that is always shown. */
key: ColumnKey | null;
label: string;
/** Table width that reveals the column; omit for always visible. */
reveal?: keyof typeof REVEAL_CLASS;
/** Server-side sort key. Omit to render a plain, non-clickable header. */
sort?: string;
/** Placeholder shape while loading. */
skeleton?: 'text' | 'pill';
cell: (item: ProductRow) => ReactNode;
};
/** Ordinary muted cell text — the default look for most columns. */
function Muted({ children }: { children: ReactNode }) {
return (
{children}
);
}
/** Placeholder for an empty nullable value. */
function Empty() {
return —;
}
export const PRODUCT_COLUMNS: ProductColumn[] = [
{
key: 'category',
label: 'Category',
reveal: '800px',
sort: 'category',
cell: (item) =>
item.category ? (
) : (
),
},
{
key: 'sku',
label: 'SKU',
sort: 'sku',
cell: (item) => (
),
},
{
key: 'status',
label: 'Status',
reveal: '576px',
sort: 'status',
skeleton: 'pill',
cell: (item) => (
),
},
{
key: 'price',
label: 'Price',
reveal: '896px',
sort: 'price',
cell: (item) => (
{formatMoney(item.price)}
),
},
{
key: 'compare_at_price',
label: 'Compare-at price',
sort: 'compare_at_price',
cell: (item) => (
{item.compare_at_price !== null
? formatMoney(item.compare_at_price)
: '—'}
),
},
{
key: 'cost_price',
label: 'Cost Price',
reveal: '1056px',
sort: 'cost_price',
cell: (item) => (
{item.cost_price !== null ? formatMoney(item.cost_price) : '—'}
),
},
{
key: 'video_url',
label: 'Video',
reveal: '1440px',
cell: (item) =>
item.video_url ? (
e.stopPropagation()}
className="inline-flex max-w-50 items-center gap-1.5 text-[13px] text-primary hover:underline"
>
{
item.video_url
.replace(/^https?:\/\/(www\.)?/, '')
.split('/')[0]
}
) : (
),
},
{
key: 'is_featured',
label: 'Featured',
sort: 'is_featured',
cell: (item) => {item.is_featured ? t('yes') : '—'},
},
{
key: 'published_at',
label: 'Published',
sort: 'published_at',
cell: (item) => (
{item.published_at ? formatDateTime(item.published_at) : '—'}
),
},
{
key: 'publishedByRef',
label: 'Published by',
sort: 'publishedByRef',
cell: (item) =>
item.publishedByRef ? (
) : (
),
},
{
key: 'created',
get label() {
return t('createdAt');
},
reveal: '1184px',
sort: 'created_at',
cell: (item) =>
item.created_at ? (
{formatDate(item.created_at)}
) : (
),
},
];