columns.tsx 1.77 KB
import type { ColumnsType } from 'antd/es/table';
import type { StoreListingRow } from '@/api';
import type { StoreDictionaries } from '@/features/store/dictionaries';
import { dictionaryLabel, formatDateTime } from '@/features/store/formatters';
import { COLUMN_LABELS, type ListingColumn } from '../tools/listing';

export function buildColumns({
  visible,
  spec,
}: {
  visible: ListingColumn[];
  spec?: StoreDictionaries;
}): ColumnsType<StoreListingRow> {
  const definitions: Record<ListingColumn, ColumnsType<StoreListingRow>[number]> = {
    storeName: {
      title: COLUMN_LABELS.storeName,
      dataIndex: 'storeName',
      key: 'storeName',
      width: 230,
      render: (value: string) => <span className="store-listing-name">{value || '—'}</span>,
    },
    merchantNo: {
      title: COLUMN_LABELS.merchantNo,
      dataIndex: 'merchantNo',
      key: 'merchantNo',
      width: 120,
      render: (value: string) => <span className="store-code">{value || '—'}</span>,
    },
    businessScene: {
      title: COLUMN_LABELS.businessScene,
      dataIndex: 'businessScene',
      key: 'businessScene',
      width: 230,
      render: (value: string) => (
        <span className="store-listing-scene">{dictionaryLabel(spec?.businessScenes, value)}</span>
      ),
    },
    effectiveTime: {
      title: COLUMN_LABELS.effectiveTime,
      dataIndex: 'effectiveTime',
      key: 'effectiveTime',
      width: 130,
      render: (value: string | null) => formatDateTime(value).split(' ')[0],
    },
    changeCount: {
      title: COLUMN_LABELS.changeCount,
      dataIndex: 'changeCount',
      key: 'changeCount',
      width: 90,
      align: 'right',
      render: (value: number | null | undefined) => value ?? '—',
    },
  };
  return visible.map((column) => definitions[column]);
}