Skip to content

Commit ea8ae1e

Browse files
author
Aasim Khan
committed
Add resize support
1 parent 5043d9e commit ea8ae1e

File tree

2 files changed

+43
-24
lines changed

2 files changed

+43
-24
lines changed

src/reactviews/pages/QueryResult/queryResultsGridView.tsx

Lines changed: 43 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,10 @@ const useStyles = makeStyles({
3939

4040
type GridItem = { batchId: number; resultId: number; index: number };
4141

42-
const MAX_GRID_HEIGHT_PX = 210;
4342
const ROW_HEIGHT = 26;
4443
const HEADER = 30;
4544
export const MARGIN_BOTTOM = 10;
45+
export const MAXIMUMG_GRID_HEIGHT_PX = 300;
4646

4747
export const QueryResultsGridView = () => {
4848
const classes = useStyles();
@@ -59,6 +59,7 @@ export const QueryResultsGridView = () => {
5959
const tabStates = useQueryResultSelector((state) => state.tabStates);
6060

6161
const gridViewContainerRef = useRef<HTMLDivElement>(null);
62+
const [gridViewContainerHeight, setGridViewContainerHeight] = useState<number>(0);
6263
const gridContainerRefs = useRef<Map<string, React.RefObject<HTMLDivElement>>>(new Map());
6364
const [maximizedGridKey, setMaximizedGridKey] = useState<string | undefined>(undefined);
6465
const gridRefs = useRef<Array<ResultGridHandle | undefined>>([]);
@@ -78,6 +79,29 @@ export const QueryResultsGridView = () => {
7879
return items;
7980
}, [resultSetSummaries]);
8081

82+
function naturalHeight(rowCount: number): number {
83+
return HEADER + rowCount * ROW_HEIGHT + MARGIN_BOTTOM;
84+
}
85+
86+
const gridHeights: number[] = useMemo(() => {
87+
if (!gridViewContainerHeight || gridList?.length === 0) {
88+
return [];
89+
}
90+
91+
const naturalHeights = gridList.map((it) =>
92+
naturalHeight(resultSetSummaries?.[it.batchId]?.[it.resultId]?.rowCount ?? 0),
93+
);
94+
95+
const equalHeights = gridViewContainerHeight / gridList.length;
96+
97+
return gridList.map((_, i) =>
98+
Math.min(
99+
naturalHeights[i],
100+
Math.max(equalHeights, Math.min(gridViewContainerHeight, MAXIMUMG_GRID_HEIGHT_PX)),
101+
),
102+
);
103+
}, [gridList, gridViewContainerHeight]);
104+
81105
// Restore grid view container scroll position on mount
82106
useEffect(() => {
83107
async function restoreGridViewContainerScrollPosition() {
@@ -137,6 +161,7 @@ export const QueryResultsGridView = () => {
137161
return undefined;
138162
}, [gridList, gridContainerRefs, gridRefs]);
139163

164+
// Keyboard shortcuts
140165
useEffect(() => {
141166
const handler = (event: KeyboardEvent) => {
142167
let handled = false;
@@ -241,28 +266,23 @@ export const QueryResultsGridView = () => {
241266
});
242267
};
243268

244-
// Calculate height for each grid based on row count
245-
const getGridHeight = (gridItem: GridItem, gridKey: string) => {
246-
const totalHeightAvailable = gridViewContainerRef.current?.clientHeight ?? 0;
247-
248-
if (maximizedGridKey === gridKey) {
249-
return `100%`;
250-
}
251-
252-
const equalHeights = totalHeightAvailable / gridList.length;
253-
const maxHeightAdjusted = Math.max(equalHeights, MAX_GRID_HEIGHT_PX);
254-
255-
const resultSetSummary = resultSetSummaries?.[gridItem.batchId]?.[gridItem.resultId];
256-
257-
let calculatedMaxHeight =
258-
HEADER + (resultSetSummary?.rowCount ?? 0) * ROW_HEIGHT + MARGIN_BOTTOM;
259-
if (resultSetSummary?.rowCount === 1) {
260-
calculatedMaxHeight = 80;
269+
// Observe container height
270+
useEffect(() => {
271+
const observer = new ResizeObserver((entries) => {
272+
for (let entry of entries) {
273+
if (entry.target === gridViewContainerRef.current) {
274+
const newHeight = entry.contentRect.height;
275+
setGridViewContainerHeight(newHeight);
276+
}
277+
}
278+
});
279+
if (gridViewContainerRef.current) {
280+
observer.observe(gridViewContainerRef.current);
261281
}
262-
263-
const finalHeight = Math.min(calculatedMaxHeight, maxHeightAdjusted);
264-
return `${finalHeight}px`;
265-
};
282+
return () => {
283+
observer.disconnect();
284+
};
285+
}, [gridViewContainerRef]);
266286

267287
return (
268288
<div
@@ -308,7 +328,7 @@ export const QueryResultsGridView = () => {
308328
? fontSettings.fontFamily
309329
: "var(--vscode-font-family)",
310330
fontSize: `${fontSettings.fontSize ?? 12}px`,
311-
height: `${getGridHeight(item, gridKey)}`,
331+
height: `${maximizedGridKey === gridKey ? `100%` : `${gridHeights[index]}px`}`,
312332
}}>
313333
<div style={{ flex: 1, minWidth: 0, overflow: "auto" }} ref={containerRef}>
314334
<ResultGrid

src/reactviews/pages/QueryResult/table/table.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,6 @@ export class Table<T extends Slick.SlickData> implements IThemable {
250250
);
251251

252252
this._isColumnWidthRestored = true;
253-
console.log("restored column widths: ", columnWidthArray);
254253

255254
if (!columnWidthArray) {
256255
return;

0 commit comments

Comments
 (0)