2023-05-02 06:42:16 +00:00
|
|
|
import { ColumnDef, CellContext } from '@tanstack/react-table';
|
2022-09-02 15:30:34 +00:00
|
|
|
|
|
|
|
import { Link } from '@@/Link';
|
|
|
|
|
|
|
|
export function buildNameColumn<T extends Record<string, unknown>>(
|
2023-05-02 06:42:16 +00:00
|
|
|
nameKey: keyof T,
|
2022-09-02 15:30:34 +00:00
|
|
|
idKey: string,
|
2023-07-12 14:26:52 +00:00
|
|
|
path: string,
|
|
|
|
idParam = 'id'
|
2023-05-02 06:42:16 +00:00
|
|
|
): ColumnDef<T> {
|
|
|
|
const cell = createCell<T>();
|
|
|
|
|
|
|
|
return {
|
|
|
|
header: 'Name',
|
|
|
|
accessorKey: nameKey,
|
2022-09-02 15:30:34 +00:00
|
|
|
id: 'name',
|
2023-05-02 06:42:16 +00:00
|
|
|
cell,
|
|
|
|
enableSorting: true,
|
2023-07-12 14:26:52 +00:00
|
|
|
enableHiding: false,
|
2022-09-02 15:30:34 +00:00
|
|
|
};
|
|
|
|
|
2023-05-02 06:42:16 +00:00
|
|
|
function createCell<T extends Record<string, unknown>>() {
|
|
|
|
return function NameCell({ renderValue, row }: CellContext<T, unknown>) {
|
|
|
|
const name = renderValue() || '';
|
|
|
|
|
|
|
|
if (typeof name !== 'string') {
|
|
|
|
return null;
|
|
|
|
}
|
2022-09-02 15:30:34 +00:00
|
|
|
|
2023-05-02 06:42:16 +00:00
|
|
|
return (
|
2023-07-12 14:26:52 +00:00
|
|
|
<Link
|
|
|
|
to={path}
|
|
|
|
params={{ [idParam]: row.original[idKey] }}
|
|
|
|
title={name}
|
|
|
|
>
|
2023-05-02 06:42:16 +00:00
|
|
|
{name}
|
|
|
|
</Link>
|
|
|
|
);
|
|
|
|
};
|
2022-09-02 15:30:34 +00:00
|
|
|
}
|
|
|
|
}
|