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 | 1x 1x 1x 17x 17x 17x 1x 1x 7x 7x 6x 6x 6x 6x 6x 6x 1x 1x 1x 1x 2x 2x 2x 2x 2x 2x 2x 2x 1x 2x 1x 1x 1x 1x 1x 1x 1x 7x 7x 7x 7x 4x 4x 4x 4x 4x 4x 4x 4x 4x 2x 2x 4x 1x 1x 1x 1x 1x 4x 4x 4x 4x 6x 6x 6x 4x 4x 4x 1x 1x 1x 7x 7x 7x 7x 6x 11x 11x 11x 11x 11x 6x 6x 9x 3x 3x 6x 1x 1x 1x 2x 2x 2x 2x 2x 2x 1x 1x 1x 2x 2x 2x 2x 2x 2x 1x 1x 24x 24x 24x 24x 1x 1x 1x 1x 1x 7x 7x 7x 15x 13x 13x 13x 13x 15x 15x 15x 15x 15x 11x 11x 11x 11x 21x 8x 8x 8x 13x 13x 15x 15x 7x 7x 1x 1x 1x 4x 4x 2x 4x 1x 1x 4x | import { type Column, type ColumnDef, type Table } from '@tanstack/react-table';
import { i18n } from '@/helpers/i18n';
/**
* Narrows an unknown value to the primitive column value types handled here.
*
* @param value - The value to check
* @returns True if `value` is a string or number
*/
function isStringOrNumber(value: unknown): value is string | number {
return typeof value === 'string' || typeof value === 'number';
}
/**
* Reads the `accessorKey` from a column definition when it is an accessor column.
*
* @param columnDef - The column definition to inspect
* @returns The accessor key, or undefined for non-accessor columns
*/
function getAccessorKey<TData>(columnDef: ColumnDef<TData, unknown>): keyof TData | undefined {
if ('accessorKey' in columnDef) {
const key = columnDef.accessorKey;
if (typeof key === 'string' || typeof key === 'number' || typeof key === 'symbol') {
// TanStack types accessorKey as its own deep-key union; narrow to this data's keys.
return key as keyof TData;
}
}
return undefined;
}
/**
* Extracts the string `children` from the element a functional header returns.
*
* @param rendered - The value produced by invoking a functional header template
* @returns The children string, or an empty string when it isn't a string-children element
*/
function getRenderedHeaderText(rendered: unknown): string {
if (
typeof rendered === 'object' &&
rendered !== null &&
'props' in rendered &&
typeof rendered.props === 'object' &&
rendered.props !== null &&
'children' in rendered.props &&
typeof rendered.props.children === 'string'
) {
return rendered.props.children;
}
return '';
}
/**
* Gets the displayable header text for a column.
* This is needed because the header text is not always a string.
* Sometimes it's an HTML or React element.
*
* @param column - The column to get the header text from
* @returns The displayable header text as a string
* @source
*/
export function getHeaderText<TData>(column: Column<TData, unknown>): string {
const header = column.columnDef.header;
if (header === undefined) return '';
if (typeof header === 'string') return header;
if (typeof header === 'function') {
// TanStack's functional header expects a `HeaderContext` we don't have here.
// Some headers read it (e.g. the price column derives its currency from
// `table.options.meta`) and throw when invoked context-free, so guard the
// call and fall back to the column-id label — the same label the
// column-visibility menu uses for functional headers.
try {
const rendered: unknown = (header as (...args: unknown[]) => unknown)();
return getRenderedHeaderText(rendered);
} catch {
return i18n(`column_${column.id}`);
}
}
return String(header);
}
/**
* Gets a sorted unique list of values for a column from visible rows.
* Only returns the visible values (e.g., if another filter has been applied,
* the filtered out results won't be included here).
*
* @param column - The column to get values from
* @param table - The table instance
* @returns A sorted array of unique values
* @source
*/
export function getVisibleUniqueValues<TData>(
column: Column<TData, unknown>,
table: Table<TData>,
): (string | number)[] {
const values = new Set<string | number>();
table.getRowModel().rows.forEach((row) => {
const value: unknown = row.getValue(column.id);
if (isStringOrNumber(value)) values.add(value);
});
return Array.from(values).sort((a, b) => {
if (typeof a === 'number' && typeof b === 'number') return a - b;
return String(a).localeCompare(String(b));
});
}
/**
* Gets a sorted unique list of values for a column from all rows.
* Returns all values, including the filtered out ones.
*
* @param column - The column to get values from
* @param table - The table instance
* @returns A sorted array of unique values
* @source
*/
export function getAllUniqueValues<TData>(
column: Column<TData, unknown>,
table: Table<TData>,
): (string | number)[] {
const accessorKey = getAccessorKey<TData>(column.columnDef);
if (accessorKey === undefined) return [];
const uniqueValues = table.options.data.reduce<(string | number)[]>((accu, row) => {
const value: unknown = row[accessorKey];
if (isStringOrNumber(value) && !accu.includes(value)) {
accu.push(value);
}
return accu;
}, []);
return uniqueValues.sort((a, b) => {
if (typeof a === 'number' && typeof b === 'number') return a - b;
return String(a).localeCompare(String(b));
});
}
/**
* Gets the range of values for a column from all rows.
* Returns all values, including the filtered out ones.
*
* @param column - The column to get the range from
* @param table - The table instance
* @returns A tuple containing the minimum and maximum values
* @source
*/
export function getFullRange<TData>(
column: Column<TData, unknown>,
table: Table<TData>,
): [number, number] {
const values = getAllUniqueValues(column, table);
// Range helpers are only called on numeric columns, so the endpoints are numbers.
return [values[0] as number, values.at(-1) as number];
}
/**
* Gets the range of values for a column from visible rows.
* Only returns the visible values (e.g., if another filter has been applied,
* the filtered out results won't be included here).
*
* @param column - The column to get the range from
* @param table - The table instance
* @returns A tuple containing the minimum and maximum values
* @source
*/
export function getVisibleRange<TData>(
column: Column<TData, unknown>,
table: Table<TData>,
): [number, number] {
const values = getVisibleUniqueValues(column, table);
// Range helpers are only called on numeric columns, so the endpoints are numbers.
return [values[0] as number, values.at(-1) as number];
}
/**
* Determines whether a value counts as data when deciding if a column is empty.
*
* @param value - The cell/field value to test
* @returns False for null/undefined, blank strings, and empty arrays; true
* otherwise (numbers including 0 and booleans count as data)
*/
function hasValue(value: unknown): boolean {
if (value == null) return false;
if (typeof value === 'string') return value.trim().length > 0;
if (Array.isArray(value)) return value.length > 0;
return true;
}
/**
* Returns the ids of hideable columns that contain no data in ANY row — across
* variant sub-rows and rows filtered out of the current view, not just the
* visible page — so callers can auto-hide columns irrelevant to a result set.
* A column with `meta.dataKeys` is judged from those raw product fields (this
* takes precedence, so an accessor's constant fallback can't mask an empty
* column); otherwise accessor columns are read via each row's value. Columns that
* can't be hidden, and non-accessor columns without `meta.dataKeys` (whose
* emptiness can't be determined), are never reported.
* @param table - The table instance to inspect
* @returns The ids of hideable columns that have no data in any row
* @example
* ```typescript
* // A result set where no product has a CAS number or SDS link:
* getEmptyHideableColumnIds(table); // ["cas", "sds"]
* ```
* @source
*/
export function getEmptyHideableColumnIds<TData>(table: Table<TData>): string[] {
const rows = table.getCoreRowModel().flatRows;
const emptyColumnIds: string[] = [];
for (const column of table.getAllColumns()) {
if (!column.getCanHide()) continue;
const columnDef = column.columnDef;
// Opt-out for accessor columns whose value is an optional, separately-loaded
// derivation (e.g. price-trend); auto-hiding would drop a column the user
// opted into whenever no row currently has a value.
if (columnDef.meta?.skipEmptyHide) continue;
const isAccessorColumn = 'accessorKey' in columnDef || 'accessorFn' in columnDef;
const dataKeys = columnDef.meta?.dataKeys ?? [];
// A display column with no accessor and no declared dataKeys can't be judged.
if (!isAccessorColumn && dataKeys.length === 0) continue;
// `dataKeys`, when present, take precedence over the accessor: they read the
// raw product fields directly, so an accessor's constant fallback (e.g. the
// purity column's "Ungraded") can't mask an otherwise-empty column.
const hasData = rows.some((row) => {
if (dataKeys.length > 0) {
const original = row.original as Record<string, unknown>;
return dataKeys.some((key) => hasValue(original[key]));
}
return hasValue(row.getValue(column.id));
});
if (!hasData) emptyColumnIds.push(column.id);
}
return emptyColumnIds;
}
/**
* Sets the visibility of a column.
*
* @param column - The column to set visibility for
* @param visible - Whether the column should be visible
* @source
*/
export function setColumnVisibility<TData>(column: Column<TData, unknown>, visible: boolean): void {
if (column.getCanHide() === false) return;
if (visible) {
if (!column.getIsVisible()) column.toggleVisibility(true);
} else {
if (column.getIsVisible()) column.toggleVisibility(false);
}
}
|