import { Copy, Expand } from 'lucide-react' import { useCallback, useMemo, useRef, useState } from 'react' import DataGrid, { CalculatedColumn } from 'react-data-grid' import { ContextMenu, ContextMenuContent, ContextMenuItem, ContextMenuTrigger, copyToClipboard, } from 'ui' import { CellDetailPanel } from './CellDetailPanel' import { ResultCell } from './ResultCell' import { formatClipboardValue } from './Results.utils' import { handleCellKeyDown } from '@/components/grid/BrivenGrid.utils' export const Results = ({ rows }: { rows: readonly any[] }) => { const [expandedCell, setExpandedCell] = useState<{ column: string; value: any } | null>(null) const contextMenuCellRef = useRef<{ column: string; value: any } | null>(null) const triggerRef = useRef(null) const handleContextMenu = useCallback((e: React.MouseEvent, column: string, value: any) => { contextMenuCellRef.current = { column, value } if (triggerRef.current) { // Position the hidden trigger at the mouse cursor so the context menu opens there triggerRef.current.style.position = 'fixed' triggerRef.current.style.left = `${e.clientX}px` triggerRef.current.style.top = `${e.clientY}px` const contextMenuEvent = new MouseEvent('contextmenu', { bubbles: true, clientX: e.clientX, clientY: e.clientY, }) triggerRef.current.dispatchEvent(contextMenuEvent) } }, []) const columnRender = (name: string) => { return
{name}
} const EST_CHAR_WIDTH = 8.25 const MIN_COLUMN_WIDTH = 100 const MAX_COLUMN_WIDTH = 500 const columns: CalculatedColumn[] = useMemo( () => Object.keys(rows?.[0] ?? []).map((key, idx) => { const maxColumnValueLength = rows .map((row) => String(row[key]).length) .reduce((a, b) => Math.max(a, b), 0) const columnWidth = Math.max( Math.min(maxColumnValueLength * EST_CHAR_WIDTH, MAX_COLUMN_WIDTH), MIN_COLUMN_WIDTH ) return { idx, key, name: key, resizable: true, parent: undefined, level: 0, width: columnWidth, minWidth: MIN_COLUMN_WIDTH, maxWidth: undefined, draggable: false, frozen: false, sortable: false, isLastFrozenColumn: false, renderCell: ({ row }: { row: any }) => ( setExpandedCell({ column, value })} /> ), renderHeaderCell: () => columnRender(key), } }), [rows, handleContextMenu] ) return ( <> {rows.length === 0 ? (

Success. No rows returned

) : ( <>
e.stopPropagation()}> { const value = formatClipboardValue(contextMenuCellRef.current?.value ?? '') copyToClipboard(value) }} onFocusCapture={(e) => e.stopPropagation()} > Copy cell content { const cell = contextMenuCellRef.current if (cell) setExpandedCell({ column: cell.column, value: cell.value }) }} onFocusCapture={(e) => e.stopPropagation()} > View cell content '[&>.rdg-cell]:items-center'} onCellKeyDown={handleCellKeyDown} /> setExpandedCell(null)} /> )} ) } export default Results