Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 | 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 129x 129x 129x 129x 129x 3x 3x 3x 2x 2x 2x 2x 2x 1x 2x 2x 3x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 3x 3x 3x 3x 3x 3x 3x 3x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 2x 2x 2x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 2x 2x 2x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 2x 129x 129x 129x 129x 129x 129x 129x 129x 1x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 1x 1x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 2x 2x 2x 1x 1x 1x 2x 2x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 2x 2x 1x 1x 1x 1x 1x 1x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 2x 2x 1x 1x 1x 2x 2x 2x 2x 2x 2x 2x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 2x 2x 1x 1x 1x 1x 1x 1x 1x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 2x 2x 1x 1x 1x 1x 1x 1x 1x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 2x 2x 1x 1x 1x 1x 1x 1x 1x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 3x 3x 2x 3x 3x 3x 3x 3x 3x 3x 3x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 2x 2x 1x 1x 1x 1x 1x 1x 1x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 4x 4x 4x 4x 4x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 129x 1x 1x 1x 35x 35x 770x 770x 490x 490x 490x 490x 490x 490x 35x 35x | import { PriceSparkline, PriceTrend } from '@/components/SearchPanel/PriceTrendGraph';
import { CountryFlagTooltip, PriceHistoryTooltip } from '@/components/StyledComponents';
import { default as Link } from '@/components/TabLink';
import { AVAILABILITY_OPTIONS, SHIPPING_OPTIONS } from '@/constants/common';
import { SUPPLIER_COUNTRY_OPTIONS } from '@/constants/countries';
import { DRAWER_ADORNMENT, DRAWER_BINDING, DRAWER_WIDGET } from '@/constants/drawer';
import { supplierDisplayNames } from '@/constants/supplierMeta';
import { SUPPLIER_CLASS_NAMES } from '@/constants/suppliers';
import { omit } from '@/helpers/collectionUtils';
import { getCountryName } from '@/helpers/country';
import { i18n } from '@/helpers/i18n';
import {
formatDisplayPrice,
formatUnitPrice,
formatUnitPriceExact,
getUnitPrice,
} from '@/helpers/price';
import { resolveRowTrendPoints } from '@/helpers/priceHistory';
import { resolveDisplayedVariants } from '@/helpers/product';
import { availabilityLabel, shippingLabel } from '@/helpers/productLabels';
import { pubchemCasSearchUrl, pubchemCompoundUrl } from '@/helpers/pubchem';
import { formatUomForDisplay } from '@/helpers/quantity';
import ArrowDropDownIcon from '@/icons/ArrowDropDownIcon';
import ArrowRightIcon from '@/icons/ArrowRightIcon';
import COAIcon from '@/icons/COAIcon';
import SDSIcon from '@/icons/SDSIcon';
import TDSIcon from '@/icons/TDSIcon';
import { ColumnDef, type CellContext, type HeaderContext } from '@tanstack/react-table';
import { hasFlag } from 'country-flag-icons';
import getUnicodeFlagIcon from 'country-flag-icons/unicode';
import styles from './TableColumns.module.scss';
/**
* Defines the column configuration for the product results table. Each
* column declares its accessor, cell renderer, sort/filter functions, and
* (optionally) `meta.drawer` to opt into a pre-search drawer accordion.
*
* The returned order is the render order in both the table header and the
* drawer's column-backed sections (see `DrawerSearchPanel`).
* @returns Ordered TanStack column definitions for the `Product` row type.
* @example
* ```tsx
* const columns = TableColumns();
* useReactTable({ columns, data, ... });
* // columns.map(c => c.id) →
* // ["expander", "title", "supplier", "country", "shipping",
* // "availability", "description", "price", "quantity", "uom",
* // "unitPrice", "priceTrend", "priceChange", "sds", "specs", "coa", "cas",
* // "pubchem", "formula", "moleweight", "purity", "concentration"]
* // columns.filter(c => c.meta?.drawer).map(c => c.id) →
* // ["supplier", "country", "shipping", "availability", "price"]
* ```
* @source
*/
export default function TableColumns(): ColumnDef<Product, unknown>[] {
return [
{
id: 'expander',
header: () => null,
cell: ({ row }: ProductRow) => {
// `getCanExpand` (backed by hasExpandableDetail) already gates whether
// there's anything to show in the panel — image, variants, or details.
return row.getCanExpand() ? (
<button
{...{
onClick: row.getToggleExpandedHandler(),
style: { cursor: 'pointer' },
}}
className={styles['svg-button-icon']}
>
{row.getIsExpanded() ? (
<ArrowDropDownIcon fontSize="small" />
) : (
<ArrowRightIcon fontSize="small" />
)}
</button>
) : null;
},
enableHiding: false,
// Fixed width sized to fit the 20px toggle button (.svg-button-icon) so
// the expand/collapse chevron isn't clipped under table-layout: fixed.
minSize: 24,
maxSize: 24,
size: 24,
enableSorting: false,
enableColumnFilter: false,
enableResizing: false,
},
{
id: 'title',
accessorKey: 'title',
header: i18n('column_title'),
cell: ({ row }: ProductRow) => {
// Indent variant rows so the hierarchy is visually obvious — 16px per
// depth level, matching the chevron column width.
const indent = row.depth > 0 ? row.depth * 16 : 0;
return (
<span style={{ paddingLeft: indent, display: 'inline-block' }}>
<Link
history={{
type: 'product',
data: omit(row.original, 'variants'),
}}
href={row.original.permalink ?? row.original.url}
>
{row.original.title}
</Link>
</span>
);
},
enableHiding: false,
filterFn: 'includeHierarchy',
meta: {
filterPlaceholder: i18n('filter_placeholder_title'),
filterVariant: 'text',
// Titles are long; let the column grow wider than most (to this cap),
// then word-wrap within that width.
autoSizeMax: 240,
style: {
textAlign: 'left',
},
},
},
{
id: 'supplier',
header: i18n('column_supplier'),
accessorKey: 'supplier',
cell: (info) => info.getValue(),
filterFn: 'multiSelect',
meta: {
filterPlaceholder: i18n('filter_placeholder_supplier'),
filterVariant: 'select',
style: {
textAlign: 'left',
},
drawer: {
label: i18n('drawer_supplier_label'),
widget: DRAWER_WIDGET.AUTOCOMPLETE_STRINGS,
options: SUPPLIER_CLASS_NAMES,
optionLabels: supplierDisplayNames(),
emptyHelperText: i18n('drawer_supplier_empty_helper'),
placeholder: i18n('drawer_supplier_placeholder'),
bind: { kind: DRAWER_BINDING.SELECTED_SUPPLIERS },
},
},
},
{
id: 'country',
header: i18n('column_country'),
accessorKey: 'supplierCountry',
cell: ({ row }: ProductRow) => {
const country = row.original.supplierCountry;
if (!country) return null;
if (!hasFlag(country)) return country;
const countryName = getCountryName(country) ?? country;
return (
<CountryFlagTooltip title={countryName} placement="top">
<span>{getUnicodeFlagIcon(country)}</span>
</CountryFlagTooltip>
);
},
filterFn: 'multiSelect',
meta: {
filterPlaceholder: i18n('filter_placeholder_country'),
filterVariant: 'select',
style: {
textAlign: 'center',
},
renderSelectOption: (code) => (hasFlag(code) ? getUnicodeFlagIcon(code) : code),
drawer: {
label: i18n('drawer_country_label'),
widget: DRAWER_WIDGET.AUTOCOMPLETE_OBJECTS,
options: SUPPLIER_COUNTRY_OPTIONS,
emptyHelperText: i18n('drawer_country_empty_helper'),
placeholder: i18n('drawer_country_placeholder'),
bind: { kind: DRAWER_BINDING.SEARCH_FILTERS, key: 'country' },
},
},
},
{
id: 'shipping',
header: i18n('column_shipping'),
accessorKey: 'supplierShipping',
cell: ({ row }: ProductRow) => {
const shipping = row.original.supplierShipping;
return shipping ? shippingLabel(shipping) : null;
},
filterFn: 'multiSelect',
meta: {
filterPlaceholder: i18n('filter_placeholder_shipping'),
filterVariant: 'select',
renderSelectOption: (value) => shippingLabel(value),
drawer: {
label: i18n('drawer_shipping_label'),
widget: DRAWER_WIDGET.CHIPS,
options: SHIPPING_OPTIONS,
formatChipLabel: (option) => shippingLabel(option),
bind: { kind: DRAWER_BINDING.SEARCH_FILTERS, key: 'shippingType' },
},
},
},
{
id: 'availability',
header: i18n('column_availability'),
accessorKey: 'availability',
cell: ({ row }: ProductRow) => {
const availability = row.original.availability;
return availability ? availabilityLabel(availability) : null;
},
filterFn: 'multiSelect',
meta: {
filterPlaceholder: i18n('filter_placeholder_availability'),
filterVariant: 'select',
renderSelectOption: (value) => availabilityLabel(value),
style: {
textAlign: 'left',
},
drawer: {
label: i18n('drawer_availability_label'),
widget: DRAWER_WIDGET.CHIPS,
options: AVAILABILITY_OPTIONS,
formatChipLabel: (option) => availabilityLabel(option),
bind: { kind: DRAWER_BINDING.SEARCH_FILTERS, key: 'availability' },
},
},
},
{
accessorKey: 'description',
header: i18n('column_description'),
cell: ({ row }: CellContext<Product, unknown>) => row.original.description,
meta: {
truncate: true,
// Descriptions can be very long; cap the content-fit width so the
// column truncates with an ellipsis instead of stretching the table.
autoSizeMax: 180,
style: {
textAlign: 'left',
},
},
},
{
id: 'price',
// Prices are all converted to the user's selected currency, so surface that
// code in the header — it disambiguates symbols shared across currencies
// (e.g. "£" = GBP/GIP/…, "$" = USD/SRD).
header: ({ table }: HeaderContext<Product, unknown>) =>
i18n('column_price_currency', [table.options.meta?.userSettings?.currency ?? 'USD']),
accessorKey: 'price',
// Read userSettings from table meta rather than context so TableColumns()
// stays hook-free — it's called from both React renders and non-render
// code paths (getColumnFilterConfig, DrawerSearchPanel's useMemo), where
// calling useAppContext() would violate the Rules of Hooks. The shared
// formatDisplayPrice helper keeps this in sync with the variant list in
// the expanded detail panel.
cell: ({ row, table }: CellContext<Product, unknown>) =>
formatDisplayPrice(row.original, table.options.meta?.userSettings),
sortingFn: 'priceSortingFn',
filterFn: 'inNumberRangeHierarchy',
meta: {
filterPlaceholder: i18n('filter_placeholder_price'),
filterVariant: 'range',
style: {
textAlign: 'left',
},
drawer: {
label: i18n('drawer_price_label'),
widget: DRAWER_WIDGET.NUMBER_RANGE,
adornment: DRAWER_ADORNMENT.CURRENCY,
bind: {
kind: DRAWER_BINDING.USER_SETTINGS_RANGE,
minKey: 'priceMin',
maxKey: 'priceMax',
},
},
},
},
{
id: 'quantity',
header: i18n('column_quantity'),
accessorKey: 'quantity',
meta: {
filterPlaceholder: i18n('filter_placeholder_quantity'),
filterVariant: 'range',
style: {
textAlign: 'left',
},
},
cell: ({ row }: ProductRow) => {
return `${row.original.quantity} ${formatUomForDisplay(row.original.uom)}`;
},
sortingFn: 'quantitySortingFn',
filterFn: 'inNumberRangeHierarchy',
minSize: 50,
},
{
id: 'uom',
header: i18n('column_unit'),
cell: (info) => formatUomForDisplay(info.getValue()),
accessorKey: 'uom',
filterFn: 'multiSelect',
meta: {
filterPlaceholder: i18n('filter_placeholder_unit'),
filterVariant: 'select',
renderSelectOption: (value) => formatUomForDisplay(value),
style: {
textAlign: 'left',
},
},
},
{
id: 'unitPrice',
header: i18n('column_unit_price'),
// Numeric value (per base unit) so the column sorts/filters/auto-hides on
// the amount; the cell renders the "$0.08/g" display string.
accessorFn: (product) => getUnitPrice(product),
cell: ({ row, table }: CellContext<Product, unknown>) => {
const settings = table.options.meta?.userSettings;
const display = formatUnitPrice(row.original, settings);
if (!display) return '';
// When the display was rounded or collapsed to "<$0.01", reveal the
// full-precision value on hover via a native title tooltip.
const exact = formatUnitPriceExact(row.original, settings);
return exact && exact !== display ? <span title={exact}>{display}</span> : display;
},
sortingFn: 'unitPriceSortingFn',
filterFn: 'inNumberRangeHierarchy',
meta: {
filterPlaceholder: i18n('filter_placeholder_unit_price'),
filterVariant: 'range',
style: {
textAlign: 'left',
},
},
},
{
id: 'priceTrend',
header: i18n('column_price_trend'),
// Sortable by the trend's signed percent change (rising = +, falling = −),
// stamped onto the row as `priceTrendValue` by useResultsTable — TanStack
// can't sort a column without an accessor, and the price history it derives
// from isn't on the row. `sortUndefined: 'last'` parks trend-less rows at the
// bottom. The empty-column auto-hide drops this column when no result has a
// trend, but only once price history has loaded (see ResultsTable).
accessorFn: (product) => product.priceTrendValue,
// The cell still draws from the preloaded history map in table meta (the
// accessor value above is only the number the column sorts on).
cell: ({ row, table }: CellContext<Product, unknown>) => {
const history = table.options.meta?.priceHistory;
if (history === undefined) return null;
const product = row.original;
// Match the detail panel's variant set: filtered sub-rows when grouped,
// else the product's own variants. A flattened variant row (ungrouped) has
// neither and is resolved by its stamped priceSeriesKey inside the helper.
const variants: Variant[] =
row.subRows.length > 0
? row.subRows.map((sub) => sub.original)
: (product.variants ?? []);
const points = resolveRowTrendPoints(
product,
resolveDisplayedVariants(product, variants),
history,
);
if (!points) return null;
// Hover reveals the same delta/percent badge shown in the variant list.
// PriceHistoryTooltip's light surface keeps the red/green trend colors
// legible — the default dark tooltip bubble washes them out.
return (
<PriceHistoryTooltip
title={<PriceTrend points={points} userSettings={table.options.meta?.userSettings} />}
arrow
placement="top"
>
<span style={{ display: 'inline-flex' }}>
<PriceSparkline points={points} colorByTrend />
</span>
</PriceHistoryTooltip>
);
},
enableColumnFilter: false,
sortUndefined: 'last',
size: 96,
minSize: 96,
maxSize: 140,
meta: {
style: {
textAlign: 'center',
},
},
},
{
id: 'priceChange',
header: i18n('column_price_change'),
// The numeric form of the price trend: the same signed delta and percent
// shown in the sparkline column's hover tooltip. Sorts by the stamped
// `priceTrendValue` (signed percent change), matching the sparkline column;
// `sortUndefined: 'last'` behaves the same way, and it likewise auto-hides
// when no result has a trend (once price history has loaded).
accessorFn: (product) => product.priceTrendValue,
cell: ({ row, table }: CellContext<Product, unknown>) => {
const history = table.options.meta?.priceHistory;
if (history === undefined) return null;
const product = row.original;
const variants: Variant[] =
row.subRows.length > 0
? row.subRows.map((sub) => sub.original)
: (product.variants ?? []);
const points = resolveRowTrendPoints(
product,
resolveDisplayedVariants(product, variants),
history,
);
if (!points) return null;
return <PriceTrend points={points} userSettings={table.options.meta?.userSettings} />;
},
enableColumnFilter: false,
sortUndefined: 'last',
size: 150,
minSize: 120,
maxSize: 200,
meta: {
style: {
textAlign: 'center',
},
},
},
{
id: 'sds',
header: i18n('column_sds'),
cell: ({ row }: ProductRow) => {
const url = row.original.sdsUrl;
if (!url) return null;
return (
<Link
href={url}
aria-label={i18n('product_detail_sds')}
title={i18n('product_detail_sds')}
>
<SDSIcon fontSize="small" />
</Link>
);
},
//enableSorting: true,
enableColumnFilter: false,
minSize: 40,
maxSize: 40,
meta: {
dataKeys: ['sdsUrl'],
style: {
textAlign: 'center',
},
},
},
{
id: 'specs',
header: i18n('column_specs'),
cell: ({ row }: ProductRow) => {
const url = row.original.specSheetUrl;
if (!url) return null;
return (
<Link
href={url}
aria-label={i18n('product_detail_tds')}
title={i18n('product_detail_tds')}
>
<TDSIcon fontSize="small" />
</Link>
);
},
//enableSorting: true,
enableColumnFilter: false,
minSize: 40,
maxSize: 40,
meta: {
dataKeys: ['specSheetUrl'],
style: {
textAlign: 'center',
},
},
},
{
id: 'coa',
header: i18n('column_coa'),
cell: ({ row }: ProductRow) => {
const url = row.original.coaUrl;
if (!url) return null;
return (
<Link
href={url}
aria-label={i18n('product_detail_coa')}
title={i18n('product_detail_coa')}
>
<COAIcon fontSize="small" />
</Link>
);
},
//enableSorting: true,
enableColumnFilter: false,
minSize: 40,
maxSize: 40,
meta: {
dataKeys: ['coaUrl'],
style: {
textAlign: 'center',
},
},
},
{
id: 'cas',
header: i18n('column_cas'),
accessorKey: 'cas',
// Link the CAS number to PubChem: straight to the compound page when the CID is known,
// otherwise to a CAS search that resolves to the matching compound.
cell: ({ row }: ProductRow) => {
const cas = row.original.cas;
if (!cas) return null;
const cid = row.original.pubchemId;
const href = cid ? pubchemCompoundUrl(cid) : pubchemCasSearchUrl(cas);
return (
<Link
href={href}
title={i18n('product_detail_pubchem_view')}
aria-label={`${cas} — ${i18n('product_detail_pubchem_view')}`}
>
{cas}
</Link>
);
},
filterFn: 'includeHierarchy',
meta: {
filterPlaceholder: i18n('filter_placeholder_cas'),
filterVariant: 'text',
style: {
textAlign: 'left',
},
},
},
{
id: 'pubchem',
header: i18n('column_pubchem'),
accessorKey: 'pubchemId',
cell: ({ row }: ProductRow) => {
const cid = row.original.pubchemId;
if (!cid) return null;
return (
<Link
href={pubchemCompoundUrl(cid)}
aria-label={i18n('product_detail_pubchem_cid', [String(cid)])}
title={i18n('product_detail_pubchem_view')}
>
{cid}
</Link>
);
},
enableColumnFilter: true,
meta: {
style: {
textAlign: 'left',
},
},
},
{
id: 'formula',
header: i18n('column_formula'),
accessorKey: 'formula',
// The formula is stored with sub/superscripts already converted to
// unicode (see ProductBuilder.setFormula), so render it as plain text.
cell: (info) => info.getValue(),
filterFn: 'includeHierarchy',
meta: {
filterPlaceholder: i18n('filter_placeholder_formula'),
filterVariant: 'text',
style: {
textAlign: 'left',
},
},
},
{
id: 'moleweight',
header: i18n('column_moleweight'),
accessorKey: 'moleweight',
cell: (info) => info.getValue(),
filterFn: 'inNumberRangeHierarchy',
meta: {
filterPlaceholder: i18n('filter_placeholder_moleweight'),
filterVariant: 'range',
style: {
textAlign: 'left',
},
},
},
{
id: 'purity',
header: i18n('column_purity'),
// Prefer the chemical grade (e.g. "ACS"); fall back to the purity % when no grade is
// set. Using an accessor keeps the displayed value, sorting, and filtering in sync.
accessorFn: (product) => product.grade ?? product.purity ?? 'Ungraded',
cell: (info) => {
const value = info.getValue();
if (!value || value === 'Ungraded')
return <span className={styles.ungraded}>{i18n('purity_ungraded')}</span>;
return value;
},
filterFn: 'includeHierarchy',
// The column mixes grades and percentages, so a string sort would interleave them
// ("ACS Grade" before "95%"). puritySortingFn ranks both on one numeric scale.
sortingFn: 'puritySortingFn',
meta: {
filterPlaceholder: i18n('filter_placeholder_purity'),
filterVariant: 'text',
// Judge emptiness from the raw fields, not the accessor: its 'Ungraded'
// fallback is never empty and would otherwise pin this column visible.
dataKeys: ['grade', 'purity'],
style: {
textAlign: 'left',
},
},
},
{
id: 'concentration',
header: i18n('column_concentration'),
accessorKey: 'concentration',
cell: (info) => info.getValue(),
filterFn: 'includeHierarchy',
meta: {
filterPlaceholder: i18n('filter_placeholder_concentration'),
filterVariant: 'text',
style: {
textAlign: 'left',
},
},
},
];
}
/**
* Builds a seed filter-config object from the column definitions, keyed by
* column id. Each filterable column contributes one entry with its
* `filterVariant` and an empty `filterData` array ready to be populated as
* search results stream in.
* @returns Record of `{ [columnId]: { filterVariant, filterData: [] } }` for
* every column whose `meta.filterVariant` is defined and that has
* a non-empty `id`.
* @example
* ```tsx
* const filterConfig = getColumnFilterConfig();
* // => {
* // title: { filterVariant: "text", filterData: [] },
* // supplier: { filterVariant: "select", filterData: [] },
* // country: { filterVariant: "select", filterData: [] },
* // shipping: { filterVariant: "select", filterData: [] },
* // availability: { filterVariant: "select", filterData: [] },
* // price: { filterVariant: "range", filterData: [] },
* // quantity: { filterVariant: "range", filterData: [] },
* // uom: { filterVariant: "select", filterData: [] },
* // unitPrice: { filterVariant: "range", filterData: [] },
* // cas: { filterVariant: "text", filterData: [] },
* // formula: { filterVariant: "text", filterData: [] },
* // moleweight: { filterVariant: "range", filterData: [] },
* // purity: { filterVariant: "text", filterData: [] },
* // concentration: { filterVariant: "text", filterData: [] },
* // }
* ```
* @source
*/
export function getColumnFilterConfig() {
return TableColumns().reduce<Record<string, { filterVariant: string; filterData: unknown[] }>>(
(accu, column) => {
const filterVariant = column.meta?.filterVariant;
if (filterVariant === undefined || !column.id) return accu;
accu[column.id] = {
filterVariant,
filterData: [],
};
return accu;
},
{},
);
}
|