import { Popover, PopoverTrigger, Button, PopoverContent, PopoverBody, Stack, IconButton, Flex, HStack, Text, Portal, } from '@chakra-ui/react' import { ToolIcon, EyeIcon, EyeOffIcon, GripIcon } from 'assets/icons' import { ResultHeaderCell } from 'models' import React, { forwardRef, useState } from 'react' import { isNotDefined } from 'utils' import { HeaderIcon } from './ResultsTable' import { DndContext, closestCenter, KeyboardSensor, PointerSensor, useSensor, useSensors, DragEndEvent, DragStartEvent, DragOverlay, } from '@dnd-kit/core' import { CSS } from '@dnd-kit/utilities' import { SortableContext, sortableKeyboardCoordinates, verticalListSortingStrategy, useSortable, arrayMove, } from '@dnd-kit/sortable' type Props = { resultHeader: ResultHeaderCell[] columnVisibility: { [key: string]: boolean } columnOrder: string[] onColumnOrderChange: (columnOrder: string[]) => void setColumnVisibility: (columnVisibility: { [key: string]: boolean }) => void } export const ColumnSettingsButton = ({ resultHeader, columnVisibility, setColumnVisibility, columnOrder, onColumnOrderChange, }: Props) => { const sensors = useSensors( useSensor(PointerSensor), useSensor(KeyboardSensor, { coordinateGetter: sortableKeyboardCoordinates, }) ) const [draggingColumnId, setDraggingColumnId] = useState(null) const onEyeClick = (id: string) => () => { columnVisibility[id] === false ? setColumnVisibility({ ...columnVisibility, [id]: true }) : setColumnVisibility({ ...columnVisibility, [id]: false }) } const visibleHeaders = resultHeader .filter( (header) => isNotDefined(columnVisibility[header.id]) || columnVisibility[header.id] ) .sort((a, b) => columnOrder.indexOf(a.id) - columnOrder.indexOf(b.id)) const hiddenHeaders = resultHeader.filter( (header) => columnVisibility[header.id] === false ) const handleDragStart = (event: DragStartEvent) => { const { active } = event setDraggingColumnId(active.id as string) } const handleDragEnd = (event: DragEndEvent) => { const { active, over } = event if (active.id !== over?.id) { onColumnOrderChange const oldIndex = columnOrder.indexOf(active.id as string) const newIndex = columnOrder.indexOf(over?.id as string) const newColumnOrder = arrayMove(columnOrder, oldIndex, newIndex) onColumnOrderChange(newColumnOrder) } } return ( Shown in table: {visibleHeaders.map((header) => ( ))} {draggingColumnId ? : null} {hiddenHeaders.length > 0 && ( Hidden in table: {hiddenHeaders.map((header) => ( {header.label} } size="sm" aria-label={'Hide column'} onClick={onEyeClick(header.id)} /> ))} )} ) } const SortableColumns = ({ header, onEyeClick, }: { header: ResultHeaderCell onEyeClick: (key: string) => () => void }) => { const { attributes, listeners, setNodeRef, transform, transition, isDragging, } = useSortable({ id: header.id }) const style = { transform: CSS.Transform.toString(transform), transition, } return ( } aria-label={'Drag'} variant="ghost" {...listeners} /> {header.label} } size="sm" aria-label={'Hide column'} onClick={onEyeClick(header.id)} /> ) } const SortableColumnOverlay = forwardRef( (_, ref: React.LegacyRef) => { return } )