import { Box, Center, Checkbox, Flex } from '@mantine/core';
import { flexRender } from '@tanstack/react-table';
import { useCallback, useMemo } from 'react';
import MultiSelectHeaderWrapper from './MultiSelectHeaderWrapper';
import useChannelsTableStore from '../../../store/channelsTable';
const CustomTableHeader = ({
getHeaderGroups,
allRowIds,
selectedTableIds,
headerCellRenderFns,
onSelectAllChange,
tableCellProps,
headerPinned = true,
enableDragDrop = false,
}) => {
const isUnlocked = useChannelsTableStore((s) => s.isUnlocked);
const shouldEnableDrag = enableDragDrop && isUnlocked;
const renderHeaderCell = (header) => {
let content;
if (headerCellRenderFns[header.id]) {
content = headerCellRenderFns[header.id](header);
} else {
switch (header.id) {
case 'select':
content = (
0 &&
selectedTableIds.length !== allRowIds.length
}
onChange={onSelectAllChange}
/>
);
break;
default:
content = flexRender(
header.column.columnDef.header,
header.getContext()
);
}
}
// Automatically wrap content to enhance MultiSelect components
return {content};
};
// Get header groups for dependency tracking
const headerGroups = getHeaderGroups();
// Calculate minimum width based only on fixed-size columns
const minTableWidth = useMemo(() => {
if (!headerGroups || headerGroups.length === 0) return 0;
const width =
headerGroups[0]?.headers.reduce((total, header) => {
// Only count columns with fixed sizes, flexible columns will expand
const columnSize = header.column.columnDef.size
? header.getSize()
: header.column.columnDef.minSize || 150; // Default min for flexible columns
return total + columnSize;
}, 0) || 0;
return width;
}, [headerGroups]);
// Memoize the style object to ensure it updates when headerPinned changes
const headerStyle = useMemo(
() => ({
position: headerPinned ? 'sticky' : 'relative',
top: headerPinned ? 0 : 'auto',
backgroundColor: '#3E3E45',
zIndex: headerPinned ? 10 : 1,
}),
[headerPinned]
);
return (
{getHeaderGroups().map((headerGroup) => (
{headerGroup.headers.map((header) => {
return (
{renderHeaderCell(header)}
{header.column.getCanResize() && (
{
e.target.style.opacity = '1';
e.target.style.backgroundColor = '#6b7280';
}}
onMouseLeave={(e) => {
if (!header.column.getIsResizing()) {
e.target.style.opacity = '0.5';
e.target.style.backgroundColor = 'transparent';
}
}}
/>
)}
);
})}
))}
);
};
export default CustomTableHeader;