|
| 1 | +import { useRef, useState, useCallback, useMemo } from 'react'; |
| 2 | +import { useVirtualizer } from '@tanstack/react-virtual'; |
| 3 | +import { ArrowUpDown, Copy, Loader2 } from 'lucide-react'; |
| 4 | +import { copyTextToClipboard } from '../../utils/clipboard'; |
| 5 | + |
| 6 | +interface MiniResultGridProps { |
| 7 | + columns: string[]; |
| 8 | + rows: unknown[][]; |
| 9 | + loading?: boolean; |
| 10 | + message?: string; |
| 11 | +} |
| 12 | + |
| 13 | +export function MiniResultGrid({ columns, rows, loading, message }: MiniResultGridProps) { |
| 14 | + const parentRef = useRef<HTMLDivElement>(null); |
| 15 | + const [sortCol, setSortCol] = useState<string | null>(null); |
| 16 | + const [sortDir, setSortDir] = useState<'asc' | 'desc'>('asc'); |
| 17 | + |
| 18 | + const handleSort = useCallback((col: string) => { |
| 19 | + if (sortCol === col) { |
| 20 | + setSortDir((d) => (d === 'asc' ? 'desc' : 'asc')); |
| 21 | + } else { |
| 22 | + setSortCol(col); |
| 23 | + setSortDir('asc'); |
| 24 | + } |
| 25 | + }, [sortCol]); |
| 26 | + |
| 27 | + const displayRows = useMemo(() => { |
| 28 | + if (!sortCol) return rows; |
| 29 | + const idx = columns.indexOf(sortCol); |
| 30 | + if (idx === -1) return rows; |
| 31 | + const sorted = [...rows].sort((a, b) => { |
| 32 | + const av = a[idx] ?? ''; |
| 33 | + const bv = b[idx] ?? ''; |
| 34 | + if (av < bv) return sortDir === 'asc' ? -1 : 1; |
| 35 | + if (av > bv) return sortDir === 'asc' ? 1 : -1; |
| 36 | + return 0; |
| 37 | + }); |
| 38 | + return sorted; |
| 39 | + }, [rows, sortCol, sortDir, columns]); |
| 40 | + |
| 41 | + const virtualizer = useVirtualizer({ |
| 42 | + count: displayRows.length, |
| 43 | + getScrollElement: () => parentRef.current, |
| 44 | + estimateSize: () => 32, |
| 45 | + overscan: 10, |
| 46 | + }); |
| 47 | + |
| 48 | + if (loading) { |
| 49 | + return ( |
| 50 | + <div className="h-full flex items-center justify-center text-muted gap-2"> |
| 51 | + <Loader2 size={18} className="animate-spin" /> |
| 52 | + <span className="text-sm">Running query...</span> |
| 53 | + </div> |
| 54 | + ); |
| 55 | + } |
| 56 | + |
| 57 | + if (message) { |
| 58 | + return ( |
| 59 | + <div className="h-full flex items-center justify-center text-muted text-sm"> |
| 60 | + {message} |
| 61 | + </div> |
| 62 | + ); |
| 63 | + } |
| 64 | + |
| 65 | + if (columns.length === 0) { |
| 66 | + return ( |
| 67 | + <div className="h-full flex items-center justify-center text-muted text-sm"> |
| 68 | + No results |
| 69 | + </div> |
| 70 | + ); |
| 71 | + } |
| 72 | + |
| 73 | + return ( |
| 74 | + <div className="h-full flex flex-col overflow-hidden text-sm"> |
| 75 | + <div className="flex-1 overflow-auto" ref={parentRef}> |
| 76 | + <table className="w-full border-collapse"> |
| 77 | + <thead className="sticky top-0 z-10 bg-elevated"> |
| 78 | + <tr> |
| 79 | + {columns.map((col) => ( |
| 80 | + <th |
| 81 | + key={col} |
| 82 | + className="text-left px-3 py-1.5 text-xs font-semibold text-secondary border-b border-strong whitespace-nowrap cursor-pointer select-none hover:text-primary transition-colors" |
| 83 | + onClick={() => handleSort(col)} |
| 84 | + > |
| 85 | + <div className="flex items-center gap-1"> |
| 86 | + {col} |
| 87 | + <ArrowUpDown size={12} className={sortCol === col ? 'text-blue-400' : 'text-muted opacity-50'} /> |
| 88 | + </div> |
| 89 | + </th> |
| 90 | + ))} |
| 91 | + </tr> |
| 92 | + </thead> |
| 93 | + <tbody> |
| 94 | + <tr style={{ height: virtualizer.getTotalSize() }}> |
| 95 | + <td colSpan={columns.length} className="p-0"> |
| 96 | + <div style={{ position: 'relative', height: `${virtualizer.getTotalSize()}px` }}> |
| 97 | + {virtualizer.getVirtualItems().map((virtualRow) => { |
| 98 | + const row = displayRows[virtualRow.index]; |
| 99 | + return ( |
| 100 | + <div |
| 101 | + key={virtualRow.key} |
| 102 | + className="flex absolute left-0 w-full border-b border-strong/30 hover:bg-surface-secondary/50 transition-colors" |
| 103 | + style={{ |
| 104 | + height: `${virtualRow.size}px`, |
| 105 | + transform: `translateY(${virtualRow.start}px)`, |
| 106 | + }} |
| 107 | + > |
| 108 | + {columns.map((col, colIdx) => { |
| 109 | + const value = row[colIdx]; |
| 110 | + const display = value === null ? 'NULL' : String(value); |
| 111 | + return ( |
| 112 | + <div |
| 113 | + key={col} |
| 114 | + className="px-3 py-1.5 text-primary whitespace-nowrap overflow-hidden text-ellipsis flex-1 min-w-[80px]" |
| 115 | + title={display} |
| 116 | + > |
| 117 | + <span className={value === null ? 'text-muted italic' : ''}>{display}</span> |
| 118 | + </div> |
| 119 | + ); |
| 120 | + })} |
| 121 | + </div> |
| 122 | + ); |
| 123 | + })} |
| 124 | + </div> |
| 125 | + </td> |
| 126 | + </tr> |
| 127 | + </tbody> |
| 128 | + </table> |
| 129 | + </div> |
| 130 | + <div className="px-3 py-1.5 text-xs text-muted border-t border-strong bg-elevated flex items-center justify-between shrink-0"> |
| 131 | + <span>{rows.length} rows</span> |
| 132 | + <button |
| 133 | + onClick={() => copyTextToClipboard(columns.join('\t') + '\n' + rows.map((r) => r.join('\t')).join('\n'))} |
| 134 | + className="flex items-center gap-1 hover:text-primary transition-colors" |
| 135 | + title="Copy all" |
| 136 | + > |
| 137 | + <Copy size={12} /> |
| 138 | + Copy |
| 139 | + </button> |
| 140 | + </div> |
| 141 | + </div> |
| 142 | + ); |
| 143 | +} |
0 commit comments