@mhjuma/traverse 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/package.json +46 -0
- package/src/bootstrap.spec.tsx +290 -0
- package/src/bootstrap.tsx +195 -0
- package/src/colors.ts +23 -0
- package/src/components/ChartPanel/ChartPanel.logic.spec.ts +358 -0
- package/src/components/ChartPanel/ChartPanel.logic.ts +318 -0
- package/src/components/ChartPanel/ChartPanel.spec.tsx +331 -0
- package/src/components/ChartPanel/ChartPanel.styles.ts +14 -0
- package/src/components/ChartPanel/ChartPanel.tsx +151 -0
- package/src/components/ConnectionDialog/ConnectionDialog.spec.tsx +767 -0
- package/src/components/ConnectionDialog/ConnectionDialog.tsx +652 -0
- package/src/components/ConnectionDialog/ConnectionRow.spec.tsx +104 -0
- package/src/components/ConnectionDialog/ConnectionRow.tsx +177 -0
- package/src/components/EditorToolbar/EditorToolbar.spec.tsx +599 -0
- package/src/components/EditorToolbar/EditorToolbar.tsx +259 -0
- package/src/components/Explorer/Explorer.spec.tsx +615 -0
- package/src/components/Explorer/Explorer.tsx +49 -0
- package/src/components/ExplorerWorkspace/ExplorerWorkspace.logic.spec.ts +715 -0
- package/src/components/ExplorerWorkspace/ExplorerWorkspace.logic.ts +324 -0
- package/src/components/ExplorerWorkspace/ExplorerWorkspace.spec.tsx +450 -0
- package/src/components/ExplorerWorkspace/ExplorerWorkspace.styles.ts +478 -0
- package/src/components/ExplorerWorkspace/ExplorerWorkspace.tsx +648 -0
- package/src/components/ExplorerWorkspace/kqlLanguage.spec.ts +321 -0
- package/src/components/ExplorerWorkspace/kqlLanguage.ts +155 -0
- package/src/components/ResultsPanel/ElapsedTimer.spec.tsx +51 -0
- package/src/components/ResultsPanel/ElapsedTimer.tsx +15 -0
- package/src/components/ResultsPanel/HistoryItem.spec.tsx +123 -0
- package/src/components/ResultsPanel/HistoryItem.tsx +56 -0
- package/src/components/ResultsPanel/ResultSetTab.tsx +18 -0
- package/src/components/ResultsPanel/ResultsPanel.spec.tsx +1194 -0
- package/src/components/ResultsPanel/ResultsPanel.tsx +321 -0
- package/src/components/ResultsPanel/TabButton.tsx +11 -0
- package/src/components/ResultsTable/ColumnHeader.tsx +42 -0
- package/src/components/ResultsTable/ColumnPickerItem.tsx +16 -0
- package/src/components/ResultsTable/FilterCell.tsx +24 -0
- package/src/components/ResultsTable/ResultsTable.logic.spec.ts +655 -0
- package/src/components/ResultsTable/ResultsTable.logic.ts +264 -0
- package/src/components/ResultsTable/ResultsTable.spec.tsx +930 -0
- package/src/components/ResultsTable/ResultsTable.styles.ts +108 -0
- package/src/components/ResultsTable/ResultsTable.tsx +501 -0
- package/src/components/ResultsTable/VirtualCell.spec.tsx +55 -0
- package/src/components/ResultsTable/VirtualCell.tsx +32 -0
- package/src/components/ResultsTable/VirtualRow.tsx +55 -0
- package/src/components/SchemaSidebar/FolderRow.tsx +35 -0
- package/src/components/SchemaSidebar/SchemaColumnItem.tsx +41 -0
- package/src/components/SchemaSidebar/SchemaContextMenu.spec.tsx +148 -0
- package/src/components/SchemaSidebar/SchemaContextMenu.tsx +49 -0
- package/src/components/SchemaSidebar/SchemaSidebar.logic.spec.ts +230 -0
- package/src/components/SchemaSidebar/SchemaSidebar.logic.ts +74 -0
- package/src/components/SchemaSidebar/SchemaSidebar.spec.tsx +1111 -0
- package/src/components/SchemaSidebar/SchemaSidebar.tsx +244 -0
- package/src/components/SchemaSidebar/SchemaTableItem.tsx +77 -0
- package/src/components/TabBar/TabBar.logic.spec.ts +127 -0
- package/src/components/TabBar/TabBar.logic.ts +35 -0
- package/src/components/TabBar/TabBar.spec.tsx +929 -0
- package/src/components/TabBar/TabBar.tsx +284 -0
- package/src/components/TabBar/TabItem.tsx +166 -0
- package/src/components/VegaChart/VegaChart.spec.tsx +126 -0
- package/src/components/VegaChart/VegaChart.tsx +224 -0
- package/src/config.spec.ts +44 -0
- package/src/config.ts +47 -0
- package/src/context/ExplorerColorContext.ts +14 -0
- package/src/context/ExplorerStateContext.ts +49 -0
- package/src/context/KustoClientContext.ts +12 -0
- package/src/context/context.spec.tsx +183 -0
- package/src/index.ts +14 -0
- package/src/services/cache.spec.ts +31 -0
- package/src/services/cache.ts +16 -0
- package/src/services/concurrency.spec.ts +139 -0
- package/src/services/concurrency.ts +46 -0
- package/src/services/kusto.spec.ts +919 -0
- package/src/services/kusto.ts +472 -0
- package/src/services/queryHistory.spec.ts +287 -0
- package/src/services/queryHistory.ts +146 -0
- package/src/services/schema.spec.ts +618 -0
- package/src/services/schema.ts +294 -0
- package/src/services/state-service.spec.ts +670 -0
- package/src/services/state-service.ts +602 -0
- package/src/services/useKustoQuery.spec.ts +234 -0
- package/src/services/useKustoQuery.ts +124 -0
- package/src/state/actions.ts +50 -0
- package/src/state/index.ts +3 -0
- package/src/state/reducer.spec.ts +644 -0
- package/src/state/reducer.ts +207 -0
- package/src/state/types.ts +54 -0
- package/tsconfig.json +7 -0
- package/tsconfig.lint.json +4 -0
- package/tsconfig.spec.json +7 -0
- package/vitest.config.ts +34 -0
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import React, { useCallback, useMemo } from 'react';
|
|
2
|
+
|
|
3
|
+
import { formatCellDisplayValue } from './ResultsTable.logic';
|
|
4
|
+
import { useStyles } from './ResultsTable.styles';
|
|
5
|
+
|
|
6
|
+
type Styles = ReturnType<typeof useStyles>;
|
|
7
|
+
|
|
8
|
+
export interface VirtualCellProps {
|
|
9
|
+
col: string;
|
|
10
|
+
colIdx: number;
|
|
11
|
+
rowIdx: number;
|
|
12
|
+
row: Record<string, unknown>;
|
|
13
|
+
width: number;
|
|
14
|
+
selected: boolean;
|
|
15
|
+
styles: Styles;
|
|
16
|
+
handleCellMouseDown: (rowIdx: number, colIdx: number, e: React.MouseEvent) => void;
|
|
17
|
+
handleCellMouseEnter: (rowIdx: number, colIdx: number) => void;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export function VirtualCell({ col, colIdx, rowIdx, row, width, selected, styles, handleCellMouseDown, handleCellMouseEnter }: VirtualCellProps) {
|
|
21
|
+
const val = row[col];
|
|
22
|
+
const display = formatCellDisplayValue(val);
|
|
23
|
+
const style = useMemo(() => ({ width }), [width]);
|
|
24
|
+
const onMouseDown = useCallback((e: React.MouseEvent) => handleCellMouseDown(rowIdx, colIdx, e), [handleCellMouseDown, rowIdx, colIdx]);
|
|
25
|
+
const onMouseEnter = useCallback(() => handleCellMouseEnter(rowIdx, colIdx), [handleCellMouseEnter, rowIdx, colIdx]);
|
|
26
|
+
return (
|
|
27
|
+
<div className={selected ? styles.tdSelected : styles.td} role="gridcell" style={style} title={display}
|
|
28
|
+
onMouseDown={onMouseDown} onMouseEnter={onMouseEnter}>
|
|
29
|
+
{display}
|
|
30
|
+
</div>
|
|
31
|
+
);
|
|
32
|
+
}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { ChevronRightRegular } from '@fluentui/react-icons';
|
|
2
|
+
import React, { useCallback, useMemo } from 'react';
|
|
3
|
+
|
|
4
|
+
import { isCellInRange } from './ResultsTable.logic';
|
|
5
|
+
import type { CellRange } from './ResultsTable.logic';
|
|
6
|
+
import { useStyles } from './ResultsTable.styles';
|
|
7
|
+
import { VirtualCell } from './VirtualCell';
|
|
8
|
+
|
|
9
|
+
type Styles = ReturnType<typeof useStyles>;
|
|
10
|
+
|
|
11
|
+
const ROW_HEIGHT = 28;
|
|
12
|
+
const chevronCollapsedStyle = { transition: 'transform 0.15s' };
|
|
13
|
+
const chevronExpandedStyle = { transform: 'rotate(90deg)', transition: 'transform 0.15s' };
|
|
14
|
+
|
|
15
|
+
export interface VirtualRowProps {
|
|
16
|
+
vRow: { index: number; start: number; key: string | number | bigint };
|
|
17
|
+
row: Record<string, unknown>;
|
|
18
|
+
isDetailOpen: boolean;
|
|
19
|
+
exprMatch: boolean;
|
|
20
|
+
visibleColumns: string[];
|
|
21
|
+
columnWidths: Record<string, number>;
|
|
22
|
+
selection: CellRange | null;
|
|
23
|
+
totalWidth: number;
|
|
24
|
+
styles: Styles;
|
|
25
|
+
setDetailRowIdx: React.Dispatch<React.SetStateAction<number | null>>;
|
|
26
|
+
handleCellMouseDown: (rowIdx: number, colIdx: number, e: React.MouseEvent) => void;
|
|
27
|
+
handleCellMouseEnter: (rowIdx: number, colIdx: number) => void;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export function VirtualRow({ vRow, row, isDetailOpen, exprMatch, visibleColumns, columnWidths, selection, totalWidth, styles, setDetailRowIdx, handleCellMouseDown, handleCellMouseEnter }: VirtualRowProps) {
|
|
31
|
+
const rowIdx = vRow.index;
|
|
32
|
+
const rowClass = isDetailOpen ? styles.trDetailOpen : exprMatch ? styles.trExprHighlight : styles.tr;
|
|
33
|
+
const rowStyle = useMemo(() => ({
|
|
34
|
+
position: 'absolute' as const, top: 0, left: 0, width: totalWidth,
|
|
35
|
+
height: ROW_HEIGHT, transform: `translateY(${vRow.start}px)`,
|
|
36
|
+
}), [totalWidth, vRow.start]);
|
|
37
|
+
const handleExpandClick = useCallback(() => {
|
|
38
|
+
setDetailRowIdx(isDetailOpen ? null : rowIdx);
|
|
39
|
+
}, [setDetailRowIdx, isDetailOpen, rowIdx]);
|
|
40
|
+
const handleExpandKeyDown = useCallback((e: React.KeyboardEvent) => {
|
|
41
|
+
if (e.key === 'Enter' || e.key === ' ') { e.preventDefault(); handleExpandClick(); }
|
|
42
|
+
}, [handleExpandClick]);
|
|
43
|
+
return (
|
|
44
|
+
<div className={rowClass} style={rowStyle}>
|
|
45
|
+
<button type="button" className={styles.expandBtn} onClick={handleExpandClick} onKeyDown={handleExpandKeyDown}>
|
|
46
|
+
<ChevronRightRegular fontSize={12} style={isDetailOpen ? chevronExpandedStyle : chevronCollapsedStyle} />
|
|
47
|
+
</button>
|
|
48
|
+
{visibleColumns.map((col, colIdx) => (
|
|
49
|
+
<VirtualCell key={col} col={col} colIdx={colIdx} rowIdx={rowIdx} row={row}
|
|
50
|
+
width={columnWidths[col]} selected={selection ? isCellInRange(rowIdx, colIdx, selection) : false}
|
|
51
|
+
styles={styles} handleCellMouseDown={handleCellMouseDown} handleCellMouseEnter={handleCellMouseEnter} />
|
|
52
|
+
))}
|
|
53
|
+
</div>
|
|
54
|
+
);
|
|
55
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import React, { useCallback } from 'react';
|
|
2
|
+
import {
|
|
3
|
+
FolderRegular,
|
|
4
|
+
FolderOpenRegular,
|
|
5
|
+
ChevronRightRegular,
|
|
6
|
+
ChevronDownRegular,
|
|
7
|
+
} from '@fluentui/react-icons';
|
|
8
|
+
|
|
9
|
+
export interface FolderRowProps {
|
|
10
|
+
folder: string;
|
|
11
|
+
folderKey?: string;
|
|
12
|
+
isExpanded: boolean;
|
|
13
|
+
toggleFolder: (folder: string) => void;
|
|
14
|
+
className: string;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export function FolderRow({ folder, folderKey, isExpanded, toggleFolder, className }: FolderRowProps) {
|
|
18
|
+
const key = folderKey ?? folder;
|
|
19
|
+
const handleClick = useCallback(() => toggleFolder(key), [toggleFolder, key]);
|
|
20
|
+
const handleKeyDown = useCallback((e: React.KeyboardEvent) => {
|
|
21
|
+
if (e.key === 'Enter' || e.key === ' ') {
|
|
22
|
+
e.preventDefault();
|
|
23
|
+
toggleFolder(key);
|
|
24
|
+
}
|
|
25
|
+
}, [toggleFolder, key]);
|
|
26
|
+
|
|
27
|
+
return (
|
|
28
|
+
<div className={className} role="treeitem" onClick={handleClick} onKeyDown={handleKeyDown} tabIndex={0}>
|
|
29
|
+
{isExpanded
|
|
30
|
+
? <><ChevronDownRegular fontSize={12} /><FolderOpenRegular fontSize={14} /></>
|
|
31
|
+
: <><ChevronRightRegular fontSize={12} /><FolderRegular fontSize={14} /></>}
|
|
32
|
+
<span>{folder}</span>
|
|
33
|
+
</div>
|
|
34
|
+
);
|
|
35
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import React, { useCallback } from 'react';
|
|
2
|
+
import { tokens } from '@fluentui/react-components';
|
|
3
|
+
|
|
4
|
+
const matchHighlightStyle: React.CSSProperties = { color: tokens.colorBrandForeground1 };
|
|
5
|
+
|
|
6
|
+
export interface SchemaColumnItemProps {
|
|
7
|
+
column: { name: string; type: string };
|
|
8
|
+
insertText: (text: string) => void;
|
|
9
|
+
schemaColumnClassName: string;
|
|
10
|
+
schemaColTypeClassName: string;
|
|
11
|
+
isMatch?: boolean;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export function SchemaColumnItem({ column, insertText, schemaColumnClassName, schemaColTypeClassName, isMatch }: SchemaColumnItemProps) {
|
|
15
|
+
const handleClick = useCallback(() => insertText(column.name), [insertText, column.name]);
|
|
16
|
+
const handleDragStart = useCallback((e: React.DragEvent) => {
|
|
17
|
+
e.dataTransfer.setData('text/plain', column.name);
|
|
18
|
+
e.dataTransfer.effectAllowed = 'copy';
|
|
19
|
+
e.stopPropagation();
|
|
20
|
+
}, [column.name]);
|
|
21
|
+
const handleKeyDown = useCallback((e: React.KeyboardEvent) => {
|
|
22
|
+
if (e.key === 'Enter' || e.key === ' ') {
|
|
23
|
+
e.preventDefault();
|
|
24
|
+
insertText(column.name);
|
|
25
|
+
}
|
|
26
|
+
}, [insertText, column.name]);
|
|
27
|
+
|
|
28
|
+
return (
|
|
29
|
+
<div className={schemaColumnClassName}
|
|
30
|
+
role="treeitem"
|
|
31
|
+
onClick={handleClick}
|
|
32
|
+
onKeyDown={handleKeyDown}
|
|
33
|
+
tabIndex={0}
|
|
34
|
+
draggable
|
|
35
|
+
onDragStart={handleDragStart}
|
|
36
|
+
style={isMatch ? matchHighlightStyle : undefined}>
|
|
37
|
+
<span>{column.name}</span>
|
|
38
|
+
<span className={schemaColTypeClassName}>{column.type}</span>
|
|
39
|
+
</div>
|
|
40
|
+
);
|
|
41
|
+
}
|
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
|
|
2
|
+
import React from 'react';
|
|
3
|
+
import { createRoot } from 'react-dom/client';
|
|
4
|
+
import { act } from 'react';
|
|
5
|
+
import { SchemaContextMenu } from './SchemaContextMenu';
|
|
6
|
+
|
|
7
|
+
vi.mock('@fluentui/react-components', () => ({
|
|
8
|
+
Menu: (p: Record<string, unknown>) => {
|
|
9
|
+
const onOpenChange = p['onOpenChange'] as ((_: unknown, data: { open: boolean }) => void) | undefined;
|
|
10
|
+
return p['open'] ? React.createElement('div', {
|
|
11
|
+
'data-testid': 'menu',
|
|
12
|
+
'data-has-open-change': onOpenChange ? 'true' : 'false',
|
|
13
|
+
onClick: () => onOpenChange?.(null, { open: false }),
|
|
14
|
+
}, p['children'] as React.ReactNode) : null;
|
|
15
|
+
},
|
|
16
|
+
MenuList: (p: Record<string, unknown>) => React.createElement('div', null, p['children'] as React.ReactNode),
|
|
17
|
+
MenuItem: (p: Record<string, unknown>) => React.createElement('div', { onClick: p['onClick'] as () => void, role: 'menuitem' }, p['children'] as React.ReactNode),
|
|
18
|
+
MenuPopover: (p: Record<string, unknown>) => React.createElement('div', null, p['children'] as React.ReactNode),
|
|
19
|
+
}));
|
|
20
|
+
vi.mock('@fluentui/react-icons', () => ({
|
|
21
|
+
TextAddRegular: () => React.createElement('span', null, 'text-add'),
|
|
22
|
+
TableSearchRegular: () => React.createElement('span', null, 'table-search'),
|
|
23
|
+
NumberSymbolRegular: () => React.createElement('span', null, 'number'),
|
|
24
|
+
TextDescriptionRegular: () => React.createElement('span', null, 'text-desc'),
|
|
25
|
+
CalendarRegular: () => React.createElement('span', null, 'calendar'),
|
|
26
|
+
}));
|
|
27
|
+
|
|
28
|
+
describe('SchemaContextMenu', () => {
|
|
29
|
+
let container: HTMLDivElement;
|
|
30
|
+
let root: ReturnType<typeof createRoot>;
|
|
31
|
+
|
|
32
|
+
beforeEach(() => {
|
|
33
|
+
vi.spyOn(console, 'error').mockImplementation(() => {});
|
|
34
|
+
vi.spyOn(console, 'warn').mockImplementation(() => {});
|
|
35
|
+
container = document.createElement('div');
|
|
36
|
+
document.body.appendChild(container);
|
|
37
|
+
root = createRoot(container);
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
afterEach(() => {
|
|
41
|
+
act(() => { root.unmount(); });
|
|
42
|
+
container.remove();
|
|
43
|
+
vi.restoreAllMocks();
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
const defaultProps = {
|
|
47
|
+
tableName: 'StormEvents',
|
|
48
|
+
x: 100,
|
|
49
|
+
y: 200,
|
|
50
|
+
insertText: vi.fn(),
|
|
51
|
+
schemaContextRunQuery: vi.fn(),
|
|
52
|
+
setSchemaContextMenu: vi.fn(),
|
|
53
|
+
contextMenuClassName: 'ctx-menu',
|
|
54
|
+
contextMenuItemClassName: 'ctx-item',
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
function findMenuItem(c: HTMLElement, label: string): HTMLElement | undefined {
|
|
58
|
+
return Array.from(c.querySelectorAll('[role="menuitem"]')).find(
|
|
59
|
+
(el) => el.textContent?.includes(label),
|
|
60
|
+
) as HTMLElement | undefined;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
it('renders all five menu items', () => {
|
|
64
|
+
act(() => { root.render(React.createElement(SchemaContextMenu, defaultProps)); });
|
|
65
|
+
expect(container.textContent).toContain('Insert name');
|
|
66
|
+
expect(container.textContent).toContain('Take 100');
|
|
67
|
+
expect(container.textContent).toContain('Count rows');
|
|
68
|
+
expect(container.textContent).toContain('Show schema');
|
|
69
|
+
expect(container.textContent).toContain('Date range');
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
it('Insert name calls insertText with table name', () => {
|
|
73
|
+
const insertText = vi.fn();
|
|
74
|
+
const setMenu = vi.fn();
|
|
75
|
+
act(() => { root.render(React.createElement(SchemaContextMenu, { ...defaultProps, insertText, setSchemaContextMenu: setMenu })); });
|
|
76
|
+
const item = findMenuItem(container, 'Insert name');
|
|
77
|
+
act(() => { item!.click(); });
|
|
78
|
+
expect(insertText).toHaveBeenCalledWith('StormEvents');
|
|
79
|
+
expect(setMenu).toHaveBeenCalledWith(null);
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
it('Take 100 runs the correct query', () => {
|
|
83
|
+
const runQuery = vi.fn();
|
|
84
|
+
const setMenu = vi.fn();
|
|
85
|
+
act(() => { root.render(React.createElement(SchemaContextMenu, { ...defaultProps, schemaContextRunQuery: runQuery, setSchemaContextMenu: setMenu })); });
|
|
86
|
+
const item = findMenuItem(container, 'Take 100');
|
|
87
|
+
act(() => { item!.click(); });
|
|
88
|
+
expect(runQuery).toHaveBeenCalledWith('StormEvents\n| take 100');
|
|
89
|
+
expect(setMenu).toHaveBeenCalledWith(null);
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
it('Count rows runs the correct query', () => {
|
|
93
|
+
const runQuery = vi.fn();
|
|
94
|
+
const setMenu = vi.fn();
|
|
95
|
+
act(() => { root.render(React.createElement(SchemaContextMenu, { ...defaultProps, schemaContextRunQuery: runQuery, setSchemaContextMenu: setMenu })); });
|
|
96
|
+
const item = findMenuItem(container, 'Count rows');
|
|
97
|
+
act(() => { item!.click(); });
|
|
98
|
+
expect(runQuery).toHaveBeenCalledWith('StormEvents\n| count');
|
|
99
|
+
expect(setMenu).toHaveBeenCalledWith(null);
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
it('Show schema runs the correct query', () => {
|
|
103
|
+
const runQuery = vi.fn();
|
|
104
|
+
const setMenu = vi.fn();
|
|
105
|
+
act(() => { root.render(React.createElement(SchemaContextMenu, { ...defaultProps, schemaContextRunQuery: runQuery, setSchemaContextMenu: setMenu })); });
|
|
106
|
+
const item = findMenuItem(container, 'Show schema');
|
|
107
|
+
act(() => { item!.click(); });
|
|
108
|
+
expect(runQuery).toHaveBeenCalledWith('StormEvents\n| getschema');
|
|
109
|
+
expect(setMenu).toHaveBeenCalledWith(null);
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
it('Date range runs the correct query', () => {
|
|
113
|
+
const runQuery = vi.fn();
|
|
114
|
+
const setMenu = vi.fn();
|
|
115
|
+
act(() => { root.render(React.createElement(SchemaContextMenu, { ...defaultProps, schemaContextRunQuery: runQuery, setSchemaContextMenu: setMenu })); });
|
|
116
|
+
const item = findMenuItem(container, 'Date range');
|
|
117
|
+
act(() => { item!.click(); });
|
|
118
|
+
expect(runQuery).toHaveBeenCalledWith('StormEvents\n| take 1000\n| summarize min(Timestamp), max(Timestamp)');
|
|
119
|
+
expect(setMenu).toHaveBeenCalledWith(null);
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
it('onOpenChange with open=false closes the menu', () => {
|
|
123
|
+
const setMenu = vi.fn();
|
|
124
|
+
act(() => { root.render(React.createElement(SchemaContextMenu, { ...defaultProps, setSchemaContextMenu: setMenu })); });
|
|
125
|
+
const menu = container.querySelector('[data-testid="menu"]') as HTMLElement;
|
|
126
|
+
expect(menu).toBeTruthy();
|
|
127
|
+
// Clicking the menu div triggers onOpenChange({ open: false })
|
|
128
|
+
act(() => { menu.click(); });
|
|
129
|
+
expect(setMenu).toHaveBeenCalledWith(null);
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
it('onOpenChange with open=true does not close the menu', () => {
|
|
133
|
+
const setMenu = vi.fn();
|
|
134
|
+
act(() => { root.render(React.createElement(SchemaContextMenu, { ...defaultProps, setSchemaContextMenu: setMenu })); });
|
|
135
|
+
// The menu is open — the handleOpenChange only calls setSchemaContextMenu when open=false
|
|
136
|
+
// Since the menu is rendering (open=true), the setMenu should not have been called yet
|
|
137
|
+
expect(setMenu).not.toHaveBeenCalled();
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
it('renders with different table name', () => {
|
|
141
|
+
const runQuery = vi.fn();
|
|
142
|
+
const setMenu = vi.fn();
|
|
143
|
+
act(() => { root.render(React.createElement(SchemaContextMenu, { ...defaultProps, tableName: 'MyTable', schemaContextRunQuery: runQuery, setSchemaContextMenu: setMenu })); });
|
|
144
|
+
const item = findMenuItem(container, 'Take 100');
|
|
145
|
+
act(() => { item!.click(); });
|
|
146
|
+
expect(runQuery).toHaveBeenCalledWith('MyTable\n| take 100');
|
|
147
|
+
});
|
|
148
|
+
});
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { Menu, MenuList, MenuItem, MenuPopover } from '@fluentui/react-components';
|
|
2
|
+
import { TextAddRegular, TableSearchRegular, NumberSymbolRegular, TextDescriptionRegular, CalendarRegular } from '@fluentui/react-icons';
|
|
3
|
+
import { useCallback, useMemo } from 'react';
|
|
4
|
+
|
|
5
|
+
const textAddIcon = <TextAddRegular />;
|
|
6
|
+
const tableSearchIcon = <TableSearchRegular />;
|
|
7
|
+
const numberSymbolIcon = <NumberSymbolRegular />;
|
|
8
|
+
const textDescriptionIcon = <TextDescriptionRegular />;
|
|
9
|
+
const calendarIcon = <CalendarRegular />;
|
|
10
|
+
|
|
11
|
+
export interface SchemaContextMenuProps {
|
|
12
|
+
tableName: string;
|
|
13
|
+
x: number;
|
|
14
|
+
y: number;
|
|
15
|
+
insertText: (text: string) => void;
|
|
16
|
+
schemaContextRunQuery: (kql: string) => void;
|
|
17
|
+
setSchemaContextMenu: (menu: null) => void;
|
|
18
|
+
contextMenuClassName: string;
|
|
19
|
+
contextMenuItemClassName: string;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export function SchemaContextMenu({ tableName, x, y, insertText, schemaContextRunQuery, setSchemaContextMenu }: SchemaContextMenuProps) {
|
|
23
|
+
const handleInsertName = useCallback(() => { insertText(tableName); setSchemaContextMenu(null); }, [insertText, tableName, setSchemaContextMenu]);
|
|
24
|
+
const handleTake100 = useCallback(() => { schemaContextRunQuery(`${tableName}\n| take 100`); setSchemaContextMenu(null); }, [schemaContextRunQuery, tableName, setSchemaContextMenu]);
|
|
25
|
+
const handleCount = useCallback(() => { schemaContextRunQuery(`${tableName}\n| count`); setSchemaContextMenu(null); }, [schemaContextRunQuery, tableName, setSchemaContextMenu]);
|
|
26
|
+
const handleShowSchema = useCallback(() => { schemaContextRunQuery(`${tableName}\n| getschema`); setSchemaContextMenu(null); }, [schemaContextRunQuery, tableName, setSchemaContextMenu]);
|
|
27
|
+
const handleDateRange = useCallback(() => { schemaContextRunQuery(`${tableName}\n| take 1000\n| summarize min(Timestamp), max(Timestamp)`); setSchemaContextMenu(null); }, [schemaContextRunQuery, tableName, setSchemaContextMenu]);
|
|
28
|
+
const handleOpenChange = useCallback((_: unknown, data: { open: boolean }) => {
|
|
29
|
+
if (!data.open) {
|
|
30
|
+
setSchemaContextMenu(null);
|
|
31
|
+
}
|
|
32
|
+
}, [setSchemaContextMenu]);
|
|
33
|
+
|
|
34
|
+
const positioning = useMemo(() => ({ target: { getBoundingClientRect: () => ({ x, y, top: y, left: x, bottom: y, right: x, width: 0, height: 0, toJSON: () => ({}) }) } }), [x, y]);
|
|
35
|
+
|
|
36
|
+
return (
|
|
37
|
+
<Menu open positioning={positioning} onOpenChange={handleOpenChange}>
|
|
38
|
+
<MenuPopover>
|
|
39
|
+
<MenuList>
|
|
40
|
+
<MenuItem icon={textAddIcon} onClick={handleInsertName}>Insert name</MenuItem>
|
|
41
|
+
<MenuItem icon={tableSearchIcon} onClick={handleTake100}>Take 100</MenuItem>
|
|
42
|
+
<MenuItem icon={numberSymbolIcon} onClick={handleCount}>Count rows</MenuItem>
|
|
43
|
+
<MenuItem icon={textDescriptionIcon} onClick={handleShowSchema}>Show schema</MenuItem>
|
|
44
|
+
<MenuItem icon={calendarIcon} onClick={handleDateRange}>Date range</MenuItem>
|
|
45
|
+
</MenuList>
|
|
46
|
+
</MenuPopover>
|
|
47
|
+
</Menu>
|
|
48
|
+
);
|
|
49
|
+
}
|
|
@@ -0,0 +1,230 @@
|
|
|
1
|
+
import { describe, it, expect, vi, beforeEach } from 'vitest';
|
|
2
|
+
|
|
3
|
+
import { groupSchemaByKindAndFolder, filterSchemaBySearch, computeExpandAllState } from './SchemaSidebar.logic';
|
|
4
|
+
import type { SchemaGroup } from './SchemaSidebar.logic';
|
|
5
|
+
import type { SchemaTable } from '../../services/schema';
|
|
6
|
+
|
|
7
|
+
beforeEach(() => {
|
|
8
|
+
vi.spyOn(console, 'error').mockImplementation(() => {});
|
|
9
|
+
vi.spyOn(console, 'warn').mockImplementation(() => {});
|
|
10
|
+
});
|
|
11
|
+
|
|
12
|
+
function makeTable(name: string, kind: SchemaTable['kind'], folder = '', columns: { name: string; type: string }[] = []): SchemaTable {
|
|
13
|
+
return { name, kind, folder, description: '', columns };
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
// ---------------------------------------------------------------------------
|
|
17
|
+
// groupSchemaByKindAndFolder
|
|
18
|
+
// ---------------------------------------------------------------------------
|
|
19
|
+
|
|
20
|
+
describe('groupSchemaByKindAndFolder', () => {
|
|
21
|
+
it('groups tables by kind', () => {
|
|
22
|
+
const schema: SchemaTable[] = [
|
|
23
|
+
makeTable('T1', 'table'),
|
|
24
|
+
makeTable('F1', 'function'),
|
|
25
|
+
makeTable('MV1', 'materializedView'),
|
|
26
|
+
];
|
|
27
|
+
const groups = groupSchemaByKindAndFolder(schema);
|
|
28
|
+
|
|
29
|
+
const labels = groups.map((g) => g.label);
|
|
30
|
+
expect(labels).toContain('Tables');
|
|
31
|
+
expect(labels).toContain('Functions');
|
|
32
|
+
expect(labels).toContain('Materialized Views');
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
it('omits kinds with no items', () => {
|
|
36
|
+
const schema: SchemaTable[] = [makeTable('T1', 'table')];
|
|
37
|
+
const groups = groupSchemaByKindAndFolder(schema);
|
|
38
|
+
|
|
39
|
+
expect(groups).toHaveLength(1);
|
|
40
|
+
expect(groups[0].label).toBe('Tables');
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
it('groups items by folder within each kind', () => {
|
|
44
|
+
const schema: SchemaTable[] = [
|
|
45
|
+
makeTable('T1', 'table', 'FolderA'),
|
|
46
|
+
makeTable('T2', 'table', 'FolderB'),
|
|
47
|
+
makeTable('T3', 'table', 'FolderA'),
|
|
48
|
+
];
|
|
49
|
+
const groups = groupSchemaByKindAndFolder(schema);
|
|
50
|
+
const tableGroup = groups.find((g) => g.kind === 'table')!;
|
|
51
|
+
|
|
52
|
+
expect(Object.keys(tableGroup.folders)).toHaveLength(2);
|
|
53
|
+
expect(tableGroup.folders['FolderA']).toHaveLength(2);
|
|
54
|
+
expect(tableGroup.folders['FolderB']).toHaveLength(1);
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
it('uses empty string key for items without a folder', () => {
|
|
58
|
+
const schema: SchemaTable[] = [makeTable('T1', 'table', '')];
|
|
59
|
+
const groups = groupSchemaByKindAndFolder(schema);
|
|
60
|
+
const tableGroup = groups.find((g) => g.kind === 'table')!;
|
|
61
|
+
|
|
62
|
+
expect(tableGroup.folders['']).toHaveLength(1);
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
it('returns empty array for empty schema', () => {
|
|
66
|
+
expect(groupSchemaByKindAndFolder([])).toEqual([]);
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
it('handles external tables', () => {
|
|
70
|
+
const schema: SchemaTable[] = [makeTable('ET1', 'externalTable', 'External')];
|
|
71
|
+
const groups = groupSchemaByKindAndFolder(schema);
|
|
72
|
+
|
|
73
|
+
expect(groups).toHaveLength(1);
|
|
74
|
+
expect(groups[0].label).toBe('External Tables');
|
|
75
|
+
expect(groups[0].kind).toBe('externalTable');
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
it('maintains kind order: Functions, MVs, Tables, External Tables', () => {
|
|
79
|
+
const schema: SchemaTable[] = [
|
|
80
|
+
makeTable('T1', 'table'),
|
|
81
|
+
makeTable('ET1', 'externalTable'),
|
|
82
|
+
makeTable('F1', 'function'),
|
|
83
|
+
makeTable('MV1', 'materializedView'),
|
|
84
|
+
];
|
|
85
|
+
const groups = groupSchemaByKindAndFolder(schema);
|
|
86
|
+
const labels = groups.map((g) => g.label);
|
|
87
|
+
|
|
88
|
+
expect(labels).toEqual(['Functions', 'Materialized Views', 'Tables', 'External Tables']);
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
it('handles mixed kinds in same folder', () => {
|
|
92
|
+
const schema: SchemaTable[] = [
|
|
93
|
+
makeTable('T1', 'table', 'SharedFolder'),
|
|
94
|
+
makeTable('F1', 'function', 'SharedFolder'),
|
|
95
|
+
];
|
|
96
|
+
const groups = groupSchemaByKindAndFolder(schema);
|
|
97
|
+
|
|
98
|
+
// Each kind gets its own group even if folder names overlap
|
|
99
|
+
expect(groups).toHaveLength(2);
|
|
100
|
+
const tableGroup = groups.find((g) => g.kind === 'table')!;
|
|
101
|
+
const funcGroup = groups.find((g) => g.kind === 'function')!;
|
|
102
|
+
expect(tableGroup.folders['SharedFolder']).toHaveLength(1);
|
|
103
|
+
expect(funcGroup.folders['SharedFolder']).toHaveLength(1);
|
|
104
|
+
});
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
// ---------------------------------------------------------------------------
|
|
108
|
+
// filterSchemaBySearch
|
|
109
|
+
// ---------------------------------------------------------------------------
|
|
110
|
+
|
|
111
|
+
describe('filterSchemaBySearch', () => {
|
|
112
|
+
const schema: SchemaTable[] = [
|
|
113
|
+
makeTable('StormEvents', 'table', '', [
|
|
114
|
+
{ name: 'StartTime', type: 'datetime' },
|
|
115
|
+
{ name: 'EventType', type: 'string' },
|
|
116
|
+
]),
|
|
117
|
+
makeTable('Heartbeat', 'table', '', [
|
|
118
|
+
{ name: 'Computer', type: 'string' },
|
|
119
|
+
]),
|
|
120
|
+
makeTable('MyFunction', 'function'),
|
|
121
|
+
];
|
|
122
|
+
|
|
123
|
+
it('returns null for empty query', () => {
|
|
124
|
+
expect(filterSchemaBySearch(schema, '')).toBeNull();
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
it('returns null for whitespace-only query', () => {
|
|
128
|
+
expect(filterSchemaBySearch(schema, ' ')).toBeNull();
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
it('matches by table name (case insensitive)', () => {
|
|
132
|
+
const result = filterSchemaBySearch(schema, 'storm');
|
|
133
|
+
expect(result).not.toBeNull();
|
|
134
|
+
expect(result).toHaveLength(1);
|
|
135
|
+
expect(result![0].name).toBe('StormEvents');
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
it('matches by column name (case insensitive)', () => {
|
|
139
|
+
const result = filterSchemaBySearch(schema, 'computer');
|
|
140
|
+
expect(result).not.toBeNull();
|
|
141
|
+
expect(result).toHaveLength(1);
|
|
142
|
+
expect(result![0].name).toBe('Heartbeat');
|
|
143
|
+
});
|
|
144
|
+
|
|
145
|
+
it('matches partial table name', () => {
|
|
146
|
+
const result = filterSchemaBySearch(schema, 'heart');
|
|
147
|
+
expect(result).not.toBeNull();
|
|
148
|
+
expect(result).toHaveLength(1);
|
|
149
|
+
expect(result![0].name).toBe('Heartbeat');
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
it('matches partial column name', () => {
|
|
153
|
+
const result = filterSchemaBySearch(schema, 'event');
|
|
154
|
+
expect(result).not.toBeNull();
|
|
155
|
+
// Matches StormEvents (table name) and its EventType column
|
|
156
|
+
expect(result!.some((t) => t.name === 'StormEvents')).toBe(true);
|
|
157
|
+
});
|
|
158
|
+
|
|
159
|
+
it('returns empty array for no matches', () => {
|
|
160
|
+
const result = filterSchemaBySearch(schema, 'zzzzz');
|
|
161
|
+
expect(result).not.toBeNull();
|
|
162
|
+
expect(result).toHaveLength(0);
|
|
163
|
+
});
|
|
164
|
+
|
|
165
|
+
it('matches function names', () => {
|
|
166
|
+
const result = filterSchemaBySearch(schema, 'myfunc');
|
|
167
|
+
expect(result).not.toBeNull();
|
|
168
|
+
expect(result).toHaveLength(1);
|
|
169
|
+
expect(result![0].name).toBe('MyFunction');
|
|
170
|
+
});
|
|
171
|
+
});
|
|
172
|
+
|
|
173
|
+
// ---------------------------------------------------------------------------
|
|
174
|
+
// computeExpandAllState
|
|
175
|
+
// ---------------------------------------------------------------------------
|
|
176
|
+
|
|
177
|
+
describe('computeExpandAllState', () => {
|
|
178
|
+
it('returns all folder and table names', () => {
|
|
179
|
+
const schema: SchemaTable[] = [
|
|
180
|
+
makeTable('T1', 'table', 'FolderA'),
|
|
181
|
+
makeTable('T2', 'table', 'FolderB'),
|
|
182
|
+
makeTable('F1', 'function', ''),
|
|
183
|
+
];
|
|
184
|
+
const groups: SchemaGroup[] = [
|
|
185
|
+
{ label: 'Functions', kind: 'function', folders: { '': [schema[2]] } },
|
|
186
|
+
{ label: 'Tables', kind: 'table', folders: { FolderA: [schema[0]], FolderB: [schema[1]] } },
|
|
187
|
+
];
|
|
188
|
+
|
|
189
|
+
const { allFolders, allTables } = computeExpandAllState(groups, schema);
|
|
190
|
+
|
|
191
|
+
expect(allFolders.has('Functions')).toBe(true);
|
|
192
|
+
expect(allFolders.has('Tables')).toBe(true);
|
|
193
|
+
expect(allFolders.has('Tables/FolderA')).toBe(true);
|
|
194
|
+
expect(allFolders.has('Tables/FolderB')).toBe(true);
|
|
195
|
+
expect(allTables.has('T1')).toBe(true);
|
|
196
|
+
expect(allTables.has('T2')).toBe(true);
|
|
197
|
+
expect(allTables.has('F1')).toBe(true);
|
|
198
|
+
});
|
|
199
|
+
|
|
200
|
+
it('does not include empty folder string as subfolder', () => {
|
|
201
|
+
const schema: SchemaTable[] = [makeTable('T1', 'table', '')];
|
|
202
|
+
const groups: SchemaGroup[] = [
|
|
203
|
+
{ label: 'Tables', kind: 'table', folders: { '': [schema[0]] } },
|
|
204
|
+
];
|
|
205
|
+
|
|
206
|
+
const { allFolders } = computeExpandAllState(groups, schema);
|
|
207
|
+
|
|
208
|
+
// 'Tables' should be present but 'Tables/' should not
|
|
209
|
+
expect(allFolders.has('Tables')).toBe(true);
|
|
210
|
+
expect(allFolders.has('Tables/')).toBe(false);
|
|
211
|
+
});
|
|
212
|
+
|
|
213
|
+
it('returns empty sets for empty input', () => {
|
|
214
|
+
const { allFolders, allTables } = computeExpandAllState([], []);
|
|
215
|
+
expect(allFolders.size).toBe(0);
|
|
216
|
+
expect(allTables.size).toBe(0);
|
|
217
|
+
});
|
|
218
|
+
|
|
219
|
+
it('includes group labels as expandable folders', () => {
|
|
220
|
+
const schema: SchemaTable[] = [
|
|
221
|
+
makeTable('MV1', 'materializedView'),
|
|
222
|
+
];
|
|
223
|
+
const groups: SchemaGroup[] = [
|
|
224
|
+
{ label: 'Materialized Views', kind: 'materializedView', folders: { '': [schema[0]] } },
|
|
225
|
+
];
|
|
226
|
+
|
|
227
|
+
const { allFolders } = computeExpandAllState(groups, schema);
|
|
228
|
+
expect(allFolders.has('Materialized Views')).toBe(true);
|
|
229
|
+
});
|
|
230
|
+
});
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Pure functions for SchemaSidebar — no React dependencies.
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import type { SchemaTable } from '../../services/schema';
|
|
6
|
+
|
|
7
|
+
export interface SchemaGroup {
|
|
8
|
+
label: string;
|
|
9
|
+
kind: SchemaTable['kind'];
|
|
10
|
+
folders: Record<string, SchemaTable[]>;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
const KIND_LABELS: { kind: SchemaTable['kind']; label: string }[] = [
|
|
14
|
+
{ kind: 'function', label: 'Functions' },
|
|
15
|
+
{ kind: 'materializedView', label: 'Materialized Views' },
|
|
16
|
+
{ kind: 'table', label: 'Tables' },
|
|
17
|
+
{ kind: 'externalTable', label: 'External Tables' },
|
|
18
|
+
];
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* Group schema items by kind, then by folder within each kind.
|
|
22
|
+
* Only includes kinds that have at least one item.
|
|
23
|
+
*/
|
|
24
|
+
export function groupSchemaByKindAndFolder(schema: SchemaTable[]): SchemaGroup[] {
|
|
25
|
+
const groups: SchemaGroup[] = [];
|
|
26
|
+
for (const { kind, label } of KIND_LABELS) {
|
|
27
|
+
const items = schema.filter((t) => t.kind === kind);
|
|
28
|
+
if (items.length === 0) {
|
|
29
|
+
continue;
|
|
30
|
+
}
|
|
31
|
+
const folders: Record<string, SchemaTable[]> = {};
|
|
32
|
+
for (const item of items) {
|
|
33
|
+
const folder = item.folder || '';
|
|
34
|
+
if (!folders[folder]) {
|
|
35
|
+
folders[folder] = [];
|
|
36
|
+
}
|
|
37
|
+
folders[folder].push(item);
|
|
38
|
+
}
|
|
39
|
+
groups.push({ label, kind, folders });
|
|
40
|
+
}
|
|
41
|
+
return groups;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Filter schema tables by a search query, matching on table name
|
|
46
|
+
* or column name (case-insensitive). Returns null when query is empty.
|
|
47
|
+
*/
|
|
48
|
+
export function filterSchemaBySearch(schema: SchemaTable[], query: string): SchemaTable[] | null {
|
|
49
|
+
if (!query.trim()) {
|
|
50
|
+
return null;
|
|
51
|
+
}
|
|
52
|
+
const q = query.toLowerCase();
|
|
53
|
+
return schema.filter((t) =>
|
|
54
|
+
t.name.toLowerCase().includes(q) ||
|
|
55
|
+
t.columns.some((c) => c.name.toLowerCase().includes(q)),
|
|
56
|
+
);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* Compute the sets of folder and table names for the "expand all" action.
|
|
61
|
+
*/
|
|
62
|
+
export function computeExpandAllState(groups: SchemaGroup[], schema: SchemaTable[]): { allFolders: Set<string>; allTables: Set<string> } {
|
|
63
|
+
const allFolders = new Set<string>();
|
|
64
|
+
for (const group of groups) {
|
|
65
|
+
allFolders.add(group.label);
|
|
66
|
+
for (const folder of Object.keys(group.folders)) {
|
|
67
|
+
if (folder) {
|
|
68
|
+
allFolders.add(`${group.label}/${folder}`);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
const allTables = new Set(schema.map((t) => t.name));
|
|
73
|
+
return { allFolders, allTables };
|
|
74
|
+
}
|