@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,224 @@
|
|
|
1
|
+
import { useEffect, useMemo, useRef, useState } from 'react';
|
|
2
|
+
import type { CSSProperties } from 'react';
|
|
3
|
+
import type { View as VegaView } from 'vega';
|
|
4
|
+
import type { TopLevelSpec } from 'vega-lite';
|
|
5
|
+
|
|
6
|
+
// Use TopLevelSpec from vega-lite instead of VisualizationSpec from vega-embed
|
|
7
|
+
type VisualizationSpec = TopLevelSpec;
|
|
8
|
+
|
|
9
|
+
const DEFAULT_PALETTE = [
|
|
10
|
+
'#6a8799', '#909d63', '#ebc17a', '#bc5653', '#b06698',
|
|
11
|
+
'#c9dfff', '#7eaac7', '#acbbd0', '#636363', '#d9d9d9',
|
|
12
|
+
];
|
|
13
|
+
|
|
14
|
+
function getVegaThemeColors(isDark: boolean) {
|
|
15
|
+
return {
|
|
16
|
+
text: isDark ? '#c0c0c0' : '#333',
|
|
17
|
+
grid: isDark ? '#3a3a3a' : '#e0e0e0',
|
|
18
|
+
domain: isDark ? '#4a4a4a' : '#ccc',
|
|
19
|
+
arcStroke: isDark ? '#2a2a2a' : '#ffffff',
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export interface VegaChartProps {
|
|
24
|
+
spec: VisualizationSpec;
|
|
25
|
+
data: { values: unknown[] };
|
|
26
|
+
isDark: boolean;
|
|
27
|
+
onClick?: (datum: Record<string, unknown>) => void;
|
|
28
|
+
height?: number;
|
|
29
|
+
palette?: string[];
|
|
30
|
+
rawVegaSpec?: Record<string, unknown>;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export type { VisualizationSpec };
|
|
34
|
+
|
|
35
|
+
export function VegaChart({ spec, data, isDark, onClick, height, palette, rawVegaSpec }: VegaChartProps) {
|
|
36
|
+
const containerRef = useRef<HTMLDivElement>(null);
|
|
37
|
+
const viewRef = useRef<VegaView | null>(null);
|
|
38
|
+
const [width, setWidth] = useState(0);
|
|
39
|
+
const [renderError, setRenderError] = useState<string | null>(null);
|
|
40
|
+
|
|
41
|
+
useEffect(() => {
|
|
42
|
+
if (!containerRef.current) {
|
|
43
|
+
return;
|
|
44
|
+
}
|
|
45
|
+
const observer = new ResizeObserver((entries) => {
|
|
46
|
+
const entry = entries[0];
|
|
47
|
+
if (entry) {
|
|
48
|
+
setWidth(entry.contentRect.width);
|
|
49
|
+
}
|
|
50
|
+
});
|
|
51
|
+
observer.observe(containerRef.current);
|
|
52
|
+
return () => observer.disconnect();
|
|
53
|
+
}, []);
|
|
54
|
+
|
|
55
|
+
const config = useMemo(() => {
|
|
56
|
+
const colors = getVegaThemeColors(isDark);
|
|
57
|
+
const chartPalette = palette ?? DEFAULT_PALETTE;
|
|
58
|
+
return {
|
|
59
|
+
background: 'transparent',
|
|
60
|
+
axis: {
|
|
61
|
+
labelColor: colors.text,
|
|
62
|
+
titleColor: colors.text,
|
|
63
|
+
gridColor: colors.grid,
|
|
64
|
+
domainColor: colors.domain,
|
|
65
|
+
tickColor: colors.domain,
|
|
66
|
+
},
|
|
67
|
+
axisY: {
|
|
68
|
+
titleAngle: -90,
|
|
69
|
+
titleAnchor: 'middle',
|
|
70
|
+
titlePadding: 20,
|
|
71
|
+
},
|
|
72
|
+
axisX: {
|
|
73
|
+
titleAlign: 'center',
|
|
74
|
+
titlePadding: 8,
|
|
75
|
+
},
|
|
76
|
+
legend: {
|
|
77
|
+
labelColor: colors.text,
|
|
78
|
+
titleColor: colors.text,
|
|
79
|
+
},
|
|
80
|
+
title: {
|
|
81
|
+
color: colors.text,
|
|
82
|
+
},
|
|
83
|
+
header: {
|
|
84
|
+
labelColor: colors.text,
|
|
85
|
+
titleColor: colors.text,
|
|
86
|
+
},
|
|
87
|
+
view: {
|
|
88
|
+
stroke: 'transparent',
|
|
89
|
+
},
|
|
90
|
+
range: {
|
|
91
|
+
category: chartPalette,
|
|
92
|
+
},
|
|
93
|
+
arc: {
|
|
94
|
+
stroke: colors.arcStroke,
|
|
95
|
+
},
|
|
96
|
+
bar: {
|
|
97
|
+
discreteBandSize: { band: 0.75 },
|
|
98
|
+
continuousBandSize: 15,
|
|
99
|
+
},
|
|
100
|
+
scale: {
|
|
101
|
+
bandPaddingInner: 0.25,
|
|
102
|
+
},
|
|
103
|
+
};
|
|
104
|
+
}, [isDark, palette]);
|
|
105
|
+
|
|
106
|
+
const chartHeight = height ?? 250;
|
|
107
|
+
|
|
108
|
+
const fullSpec = useMemo(() => {
|
|
109
|
+
const specObj = spec as unknown as Record<string, unknown>;
|
|
110
|
+
const isStepHeight = typeof specObj['height'] === 'object';
|
|
111
|
+
const isStepWidth = typeof specObj['width'] === 'object';
|
|
112
|
+
|
|
113
|
+
// For step-based width, drop the step and use container width with autosize: 'fit'
|
|
114
|
+
// Vega-Lite distributes bars evenly across the available space
|
|
115
|
+
let resolvedSpec = { ...specObj };
|
|
116
|
+
if (isStepWidth) {
|
|
117
|
+
const { width: _stepWidth, ...rest } = resolvedSpec;
|
|
118
|
+
resolvedSpec = rest;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
return {
|
|
122
|
+
$schema: 'https://vega.github.io/schema/vega-lite/v6.json' as const,
|
|
123
|
+
...resolvedSpec,
|
|
124
|
+
...(!resolvedSpec['width'] ? { width: width > 0 ? width - 40 : undefined } : {}),
|
|
125
|
+
...(!resolvedSpec['height'] ? { height: chartHeight } : {}),
|
|
126
|
+
...(isStepHeight ? { autosize: { type: 'none' as const, contains: 'padding' as const } } : { autosize: { type: 'fit' as const, contains: 'padding' as const } }),
|
|
127
|
+
config: {
|
|
128
|
+
...config,
|
|
129
|
+
axisX: {
|
|
130
|
+
...config.axisX,
|
|
131
|
+
titleX: (width - 40) / 2,
|
|
132
|
+
},
|
|
133
|
+
axisY: {
|
|
134
|
+
...config.axisY,
|
|
135
|
+
titleY: chartHeight / 2,
|
|
136
|
+
},
|
|
137
|
+
},
|
|
138
|
+
} as unknown as VisualizationSpec;
|
|
139
|
+
}, [spec, width, chartHeight, config]);
|
|
140
|
+
|
|
141
|
+
const embedSpec = useMemo(() => {
|
|
142
|
+
const s = { ...fullSpec } as Record<string, unknown>;
|
|
143
|
+
s['data'] = { values: data.values };
|
|
144
|
+
return s as unknown as VisualizationSpec;
|
|
145
|
+
}, [fullSpec, data.values]);
|
|
146
|
+
|
|
147
|
+
// Render chart using vega + vega-lite directly (no vega-embed dependency)
|
|
148
|
+
const chartRef = useRef<HTMLDivElement>(null);
|
|
149
|
+
useEffect(() => {
|
|
150
|
+
if (!chartRef.current || width <= 0) {
|
|
151
|
+
return;
|
|
152
|
+
}
|
|
153
|
+
let cancelled = false;
|
|
154
|
+
let view: VegaView | null = null;
|
|
155
|
+
setRenderError(null);
|
|
156
|
+
|
|
157
|
+
void (async () => {
|
|
158
|
+
try {
|
|
159
|
+
const [{ compile }, { parse, View }, { default: createTooltipHandler }] = await Promise.all([
|
|
160
|
+
import('vega-lite'),
|
|
161
|
+
import('vega'),
|
|
162
|
+
import('vega-tooltip'),
|
|
163
|
+
]);
|
|
164
|
+
if (cancelled || !chartRef.current) {
|
|
165
|
+
return;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
const vegaSpec = rawVegaSpec
|
|
169
|
+
? rawVegaSpec
|
|
170
|
+
: compile(embedSpec).spec;
|
|
171
|
+
const runtime = parse(vegaSpec);
|
|
172
|
+
view = new View(runtime, {
|
|
173
|
+
renderer: 'canvas',
|
|
174
|
+
container: chartRef.current,
|
|
175
|
+
hover: true,
|
|
176
|
+
});
|
|
177
|
+
|
|
178
|
+
const handler = createTooltipHandler(view, { theme: isDark ? 'dark' : 'light' });
|
|
179
|
+
view.tooltip(handler.call.bind(handler));
|
|
180
|
+
|
|
181
|
+
await view.runAsync();
|
|
182
|
+
|
|
183
|
+
viewRef.current = view;
|
|
184
|
+
if (onClick) {
|
|
185
|
+
view.addEventListener('click', (_event: unknown, item: unknown) => {
|
|
186
|
+
const typedItem = item as { datum?: Record<string, unknown> } | undefined;
|
|
187
|
+
if (typedItem?.datum) {
|
|
188
|
+
onClick(typedItem.datum);
|
|
189
|
+
}
|
|
190
|
+
});
|
|
191
|
+
}
|
|
192
|
+
} catch (err) {
|
|
193
|
+
if (!cancelled) {
|
|
194
|
+
const message = err instanceof Error ? err.message : 'Chart render failed';
|
|
195
|
+
setRenderError(message);
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
})();
|
|
199
|
+
|
|
200
|
+
return () => {
|
|
201
|
+
cancelled = true;
|
|
202
|
+
if (view) {
|
|
203
|
+
view.finalize();
|
|
204
|
+
}
|
|
205
|
+
};
|
|
206
|
+
}, [embedSpec, rawVegaSpec, onClick, isDark, width]);
|
|
207
|
+
|
|
208
|
+
const containerStyle = useMemo((): CSSProperties => ({ width: '100%', minHeight: height ?? 250 }), [height]);
|
|
209
|
+
const errorStyle = useMemo((): CSSProperties => ({
|
|
210
|
+
display: 'flex', alignItems: 'center', justifyContent: 'center',
|
|
211
|
+
height: height ?? 250, color: '#636363', fontSize: '12px',
|
|
212
|
+
fontStyle: 'italic', padding: '16px', textAlign: 'center',
|
|
213
|
+
}), [height]);
|
|
214
|
+
|
|
215
|
+
return (
|
|
216
|
+
<div ref={containerRef} style={containerStyle}>
|
|
217
|
+
{renderError ? (
|
|
218
|
+
<div style={errorStyle}>
|
|
219
|
+
Chart unavailable: {renderError}
|
|
220
|
+
</div>
|
|
221
|
+
) : width > 0 ? <div ref={chartRef} /> : null}
|
|
222
|
+
</div>
|
|
223
|
+
);
|
|
224
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { describe, it, expect } from 'vitest';
|
|
2
|
+
|
|
3
|
+
import { CONNECTION_COLORS, DEFAULT_CONNECTION, DEFAULT_QUERY, KEYBOARD_SHORTCUTS, buildWellKnownClusters } from './config';
|
|
4
|
+
import type { WellKnownCluster } from './config';
|
|
5
|
+
|
|
6
|
+
describe('config', () => {
|
|
7
|
+
it('CONNECTION_COLORS has 5 entries', () => {
|
|
8
|
+
expect(CONNECTION_COLORS).toHaveLength(5);
|
|
9
|
+
});
|
|
10
|
+
|
|
11
|
+
it('DEFAULT_CONNECTION has required fields', () => {
|
|
12
|
+
expect(DEFAULT_CONNECTION.id).toBeTruthy();
|
|
13
|
+
expect(DEFAULT_CONNECTION.clusterUrl).toContain('kusto.windows.net');
|
|
14
|
+
expect(DEFAULT_CONNECTION.database).toBeTruthy();
|
|
15
|
+
expect(DEFAULT_CONNECTION.color).toBeTruthy();
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
it('DEFAULT_QUERY is non-empty', () => {
|
|
19
|
+
expect(DEFAULT_QUERY.length).toBeGreaterThan(0);
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
it('KEYBOARD_SHORTCUTS has entries', () => {
|
|
23
|
+
expect(KEYBOARD_SHORTCUTS.length).toBeGreaterThan(0);
|
|
24
|
+
expect(KEYBOARD_SHORTCUTS[0].keys).toBeTruthy();
|
|
25
|
+
expect(KEYBOARD_SHORTCUTS[0].action).toBeTruthy();
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
it('buildWellKnownClusters assigns colors cyclically', () => {
|
|
29
|
+
const clusters: WellKnownCluster[] = [
|
|
30
|
+
{ id: 'a', name: 'A', clusterUrl: 'https://a.kusto.windows.net', database: 'DB' },
|
|
31
|
+
{ id: 'b', name: 'B', clusterUrl: 'https://b.kusto.windows.net', database: 'DB' },
|
|
32
|
+
{ id: 'c', name: 'C', clusterUrl: 'https://c.kusto.windows.net', database: 'DB' },
|
|
33
|
+
{ id: 'd', name: 'D', clusterUrl: 'https://d.kusto.windows.net', database: 'DB' },
|
|
34
|
+
{ id: 'e', name: 'E', clusterUrl: 'https://e.kusto.windows.net', database: 'DB' },
|
|
35
|
+
{ id: 'f', name: 'F', clusterUrl: 'https://f.kusto.windows.net', database: 'DB' },
|
|
36
|
+
];
|
|
37
|
+
const result = buildWellKnownClusters(clusters);
|
|
38
|
+
expect(result).toHaveLength(6);
|
|
39
|
+
expect(result[0].color).toBe(CONNECTION_COLORS[0]);
|
|
40
|
+
expect(result[5].color).toBe(CONNECTION_COLORS[0]); // Wraps around
|
|
41
|
+
expect(result[0].id).toBe('a');
|
|
42
|
+
expect(result[0].clusterUrl).toBe('https://a.kusto.windows.net');
|
|
43
|
+
});
|
|
44
|
+
});
|
package/src/config.ts
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Explorer configuration — types, defaults, and constants.
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
export interface KustoConnection {
|
|
6
|
+
id: string;
|
|
7
|
+
name: string;
|
|
8
|
+
clusterUrl: string;
|
|
9
|
+
database: string;
|
|
10
|
+
color: string;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export const CONNECTION_COLORS = ['#909d63', '#6a8799', '#ebc17a', '#bc5653', '#b06698'];
|
|
14
|
+
|
|
15
|
+
export const DEFAULT_CONNECTION: KustoConnection = {
|
|
16
|
+
id: 'kusto-help',
|
|
17
|
+
name: 'help',
|
|
18
|
+
clusterUrl: 'https://help.kusto.windows.net',
|
|
19
|
+
database: 'Samples',
|
|
20
|
+
color: CONNECTION_COLORS[0],
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
export type WellKnownCluster = { id: string; name: string; clusterUrl: string; database: string };
|
|
24
|
+
|
|
25
|
+
export function buildWellKnownClusters(clusters: readonly WellKnownCluster[]): KustoConnection[] {
|
|
26
|
+
return clusters.map((c, i) => ({
|
|
27
|
+
...c,
|
|
28
|
+
color: CONNECTION_COLORS[i % CONNECTION_COLORS.length],
|
|
29
|
+
}));
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export const DEFAULT_QUERY = `StormEvents
|
|
33
|
+
| summarize count() by State
|
|
34
|
+
| top 10 by count_
|
|
35
|
+
| render barchart`;
|
|
36
|
+
|
|
37
|
+
export const KEYBOARD_SHORTCUTS = [
|
|
38
|
+
{ keys: 'Ctrl+Shift+Enter', action: 'Run query / selection' },
|
|
39
|
+
{ keys: 'Ctrl+Shift+R', action: 'Recall cached results' },
|
|
40
|
+
{ keys: 'Ctrl+Shift+F', action: 'Format query' },
|
|
41
|
+
{ keys: 'Ctrl+/', action: 'Toggle comment' },
|
|
42
|
+
{ keys: 'Ctrl+D', action: 'Duplicate line' },
|
|
43
|
+
{ keys: 'Ctrl+Shift+K', action: 'Delete line' },
|
|
44
|
+
{ keys: 'Alt+↑/↓', action: 'Move line up/down' },
|
|
45
|
+
{ keys: 'Ctrl+Space', action: 'Trigger autocomplete' },
|
|
46
|
+
{ keys: 'Ctrl+Z / Ctrl+Y', action: 'Undo / Redo' },
|
|
47
|
+
];
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { createContext, useContext } from 'react';
|
|
2
|
+
import type { ExplorerColorConfig } from '../colors';
|
|
3
|
+
|
|
4
|
+
const ExplorerColorContext = createContext<ExplorerColorConfig | null>(null);
|
|
5
|
+
|
|
6
|
+
export const ExplorerColorProvider = ExplorerColorContext.Provider;
|
|
7
|
+
|
|
8
|
+
export function useExplorerColors(): ExplorerColorConfig {
|
|
9
|
+
const config = useContext(ExplorerColorContext);
|
|
10
|
+
if (!config) {
|
|
11
|
+
throw new Error('ExplorerColorProvider not provided');
|
|
12
|
+
}
|
|
13
|
+
return config;
|
|
14
|
+
}
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import { createContext, useContext, useMemo } from 'react';
|
|
2
|
+
import type { Dispatch } from 'react';
|
|
3
|
+
|
|
4
|
+
import type { ExplorerState, ExplorerAction } from '../state';
|
|
5
|
+
import type { KustoConnection } from '../components/ExplorerWorkspace/ExplorerWorkspace.logic';
|
|
6
|
+
import { DEFAULT_CONNECTION } from '../components/ExplorerWorkspace/ExplorerWorkspace.logic';
|
|
7
|
+
|
|
8
|
+
export const ExplorerStateContext = createContext<ExplorerState | null>(null);
|
|
9
|
+
export const ExplorerDispatchContext = createContext<Dispatch<ExplorerAction> | null>(null);
|
|
10
|
+
|
|
11
|
+
export function useExplorerState(): ExplorerState {
|
|
12
|
+
const ctx = useContext(ExplorerStateContext);
|
|
13
|
+
if (!ctx) {
|
|
14
|
+
throw new Error('useExplorerState must be used within ExplorerProvider');
|
|
15
|
+
}
|
|
16
|
+
return ctx;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export function useExplorerDispatch(): Dispatch<ExplorerAction> {
|
|
20
|
+
const ctx = useContext(ExplorerDispatchContext);
|
|
21
|
+
if (!ctx) {
|
|
22
|
+
throw new Error('useExplorerDispatch must be used within ExplorerProvider');
|
|
23
|
+
}
|
|
24
|
+
return ctx;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export function useActiveTabs() {
|
|
28
|
+
const { tabs, activeTabId, splitEnabled, splitTabId } = useExplorerState();
|
|
29
|
+
const activeTab = useMemo(() => tabs.find((t) => t.id === activeTabId) ?? tabs[0], [tabs, activeTabId]);
|
|
30
|
+
const splitTab = useMemo(() => splitEnabled ? (tabs.find((t) => t.id === splitTabId) ?? null) : null, [tabs, splitEnabled, splitTabId]);
|
|
31
|
+
return { activeTab, splitTab };
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export function useConnection(connectionId: string): KustoConnection {
|
|
35
|
+
const { connections } = useExplorerState();
|
|
36
|
+
return useMemo(
|
|
37
|
+
() => connections.find((c) => c.id === connectionId) ?? connections[0] ?? DEFAULT_CONNECTION,
|
|
38
|
+
[connections, connectionId],
|
|
39
|
+
);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export function useFocusedConnection(): KustoConnection {
|
|
43
|
+
const { tabs, activeTabId, splitTabId, focusedPane, connections } = useExplorerState();
|
|
44
|
+
return useMemo(() => {
|
|
45
|
+
const tabId = focusedPane === 'primary' ? activeTabId : splitTabId;
|
|
46
|
+
const tab = tabs.find((t) => t.id === tabId) ?? tabs[0];
|
|
47
|
+
return connections.find((c) => c.id === tab?.connectionId) ?? connections[0] ?? DEFAULT_CONNECTION;
|
|
48
|
+
}, [tabs, activeTabId, splitTabId, focusedPane, connections]);
|
|
49
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { createContext, useContext } from 'react';
|
|
2
|
+
import type { KustoClient } from '../services/kusto';
|
|
3
|
+
|
|
4
|
+
export const KustoClientContext = createContext<KustoClient | null>(null);
|
|
5
|
+
|
|
6
|
+
export function useKustoClient(): KustoClient {
|
|
7
|
+
const client = useContext(KustoClientContext);
|
|
8
|
+
if (!client) {
|
|
9
|
+
throw new Error('KustoClientContext not provided');
|
|
10
|
+
}
|
|
11
|
+
return client;
|
|
12
|
+
}
|
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
|
|
2
|
+
import React, { createElement } from 'react';
|
|
3
|
+
import { createRoot } from 'react-dom/client';
|
|
4
|
+
import { act } from 'react';
|
|
5
|
+
|
|
6
|
+
import { KustoClientContext, useKustoClient } from './KustoClientContext';
|
|
7
|
+
import { ExplorerColorProvider, useExplorerColors } from './ExplorerColorContext';
|
|
8
|
+
import { ExplorerStateContext, ExplorerDispatchContext, useExplorerState, useExplorerDispatch, useActiveTabs, useConnection, useFocusedConnection } from './ExplorerStateContext';
|
|
9
|
+
import type { ExplorerColorConfig } from '../colors';
|
|
10
|
+
import { DEFAULT_CONNECTION } from '../components/ExplorerWorkspace/ExplorerWorkspace.logic';
|
|
11
|
+
|
|
12
|
+
beforeEach(() => {
|
|
13
|
+
vi.spyOn(console, 'error').mockImplementation(() => {});
|
|
14
|
+
vi.spyOn(console, 'warn').mockImplementation(() => {});
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
afterEach(() => {
|
|
18
|
+
vi.restoreAllMocks();
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
function renderHook<T>(hook: () => T, wrapper?: React.FC<{ children: React.ReactNode }>): T {
|
|
22
|
+
let result: T;
|
|
23
|
+
function TestComponent() {
|
|
24
|
+
result = hook();
|
|
25
|
+
return null;
|
|
26
|
+
}
|
|
27
|
+
const container = document.createElement('div');
|
|
28
|
+
document.body.appendChild(container);
|
|
29
|
+
const root = createRoot(container);
|
|
30
|
+
const el = createElement(TestComponent);
|
|
31
|
+
act(() => {
|
|
32
|
+
root.render(wrapper ? createElement(wrapper, null, el) : el);
|
|
33
|
+
});
|
|
34
|
+
act(() => {
|
|
35
|
+
root.unmount();
|
|
36
|
+
});
|
|
37
|
+
container.remove();
|
|
38
|
+
return result!;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
describe('KustoClientContext', () => {
|
|
42
|
+
it('throws when used outside provider', () => {
|
|
43
|
+
expect(() => renderHook(() => useKustoClient())).toThrow('KustoClientContext not provided');
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
it('returns client when provided', () => {
|
|
47
|
+
const mockClient = { queryKusto: () => {}, queryKustoMgmt: () => {}, clearQueryCache: () => {}, getQueryCacheSize: () => 0 };
|
|
48
|
+
const client = renderHook(
|
|
49
|
+
() => useKustoClient(),
|
|
50
|
+
({ children }) => createElement(KustoClientContext.Provider, { value: mockClient as never }, children),
|
|
51
|
+
);
|
|
52
|
+
expect(client).toBe(mockClient);
|
|
53
|
+
});
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
describe('ExplorerColorContext', () => {
|
|
57
|
+
it('throws when used outside provider', () => {
|
|
58
|
+
expect(() => renderHook(() => useExplorerColors())).toThrow('ExplorerColorProvider not provided');
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
it('returns colors when provided', () => {
|
|
62
|
+
const mockColors: ExplorerColorConfig = {
|
|
63
|
+
semantic: {
|
|
64
|
+
backdrop: 'a', functionBadge: 'b', highlightHoverBg: 'c', lookupBadge: 'd',
|
|
65
|
+
materializedViewBadge: 'e', scrollThumb: 'f', scrollThumbHover: 'g',
|
|
66
|
+
selectionBg: 'h', selectionSubtle: 'i', shadowLight: 'j', shadowMedium: 'k',
|
|
67
|
+
},
|
|
68
|
+
chart: { palette: ['#1'] },
|
|
69
|
+
};
|
|
70
|
+
const colors = renderHook(
|
|
71
|
+
() => useExplorerColors(),
|
|
72
|
+
({ children }) => createElement(ExplorerColorProvider, { value: mockColors }, children),
|
|
73
|
+
);
|
|
74
|
+
expect(colors).toBe(mockColors);
|
|
75
|
+
});
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
describe('ExplorerStateContext', () => {
|
|
79
|
+
it('useExplorerState throws when used outside provider', () => {
|
|
80
|
+
expect(() => renderHook(() => useExplorerState())).toThrow('useExplorerState must be used within ExplorerProvider');
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
it('useExplorerDispatch throws when used outside provider', () => {
|
|
84
|
+
expect(() => renderHook(() => useExplorerDispatch())).toThrow('useExplorerDispatch must be used within ExplorerProvider');
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
it('returns state when provided', () => {
|
|
88
|
+
const mockState = { tabs: [], activeTabId: '1', connections: [], splitEnabled: false, splitTabId: null, focusedPane: 'primary' as const };
|
|
89
|
+
const state = renderHook(
|
|
90
|
+
() => useExplorerState(),
|
|
91
|
+
({ children }) => createElement(ExplorerStateContext.Provider, { value: mockState as never }, children),
|
|
92
|
+
);
|
|
93
|
+
expect(state).toBe(mockState);
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
it('returns dispatch when provided', () => {
|
|
97
|
+
const mockDispatch = () => {};
|
|
98
|
+
const dispatch = renderHook(
|
|
99
|
+
() => useExplorerDispatch(),
|
|
100
|
+
({ children }) => createElement(ExplorerDispatchContext.Provider, { value: mockDispatch as never }, children),
|
|
101
|
+
);
|
|
102
|
+
expect(dispatch).toBe(mockDispatch);
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
it('useActiveTabs returns active and split tabs', () => {
|
|
106
|
+
const tabs = [
|
|
107
|
+
{ id: 't1', name: 'Tab 1', query: '', connectionId: 'c1' },
|
|
108
|
+
{ id: 't2', name: 'Tab 2', query: '', connectionId: 'c1' },
|
|
109
|
+
];
|
|
110
|
+
const mockState = {
|
|
111
|
+
tabs, activeTabId: 't1', splitEnabled: true, splitTabId: 't2',
|
|
112
|
+
connections: [], focusedPane: 'primary' as const,
|
|
113
|
+
};
|
|
114
|
+
const result = renderHook(
|
|
115
|
+
() => useActiveTabs(),
|
|
116
|
+
({ children }) => createElement(ExplorerStateContext.Provider, { value: mockState as never }, children),
|
|
117
|
+
);
|
|
118
|
+
expect(result.activeTab.id).toBe('t1');
|
|
119
|
+
expect(result.splitTab?.id).toBe('t2');
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
it('useActiveTabs returns null splitTab when split disabled', () => {
|
|
123
|
+
const tabs = [{ id: 't1', name: 'Tab 1', query: '', connectionId: 'c1' }];
|
|
124
|
+
const mockState = {
|
|
125
|
+
tabs, activeTabId: 't1', splitEnabled: false, splitTabId: null,
|
|
126
|
+
connections: [], focusedPane: 'primary' as const,
|
|
127
|
+
};
|
|
128
|
+
const result = renderHook(
|
|
129
|
+
() => useActiveTabs(),
|
|
130
|
+
({ children }) => createElement(ExplorerStateContext.Provider, { value: mockState as never }, children),
|
|
131
|
+
);
|
|
132
|
+
expect(result.activeTab.id).toBe('t1');
|
|
133
|
+
expect(result.splitTab).toBeNull();
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
it('useConnection returns connection by id', () => {
|
|
137
|
+
const connections = [
|
|
138
|
+
{ id: 'c1', name: 'Cluster 1', clusterUrl: 'https://c1.kusto.windows.net', database: 'DB1', color: '#red' },
|
|
139
|
+
{ id: 'c2', name: 'Cluster 2', clusterUrl: 'https://c2.kusto.windows.net', database: 'DB2', color: '#blue' },
|
|
140
|
+
];
|
|
141
|
+
const mockState = {
|
|
142
|
+
tabs: [], activeTabId: '', splitEnabled: false, splitTabId: null,
|
|
143
|
+
connections, focusedPane: 'primary' as const,
|
|
144
|
+
};
|
|
145
|
+
const conn = renderHook(
|
|
146
|
+
() => useConnection('c2'),
|
|
147
|
+
({ children }) => createElement(ExplorerStateContext.Provider, { value: mockState as never }, children),
|
|
148
|
+
);
|
|
149
|
+
expect(conn.id).toBe('c2');
|
|
150
|
+
});
|
|
151
|
+
|
|
152
|
+
it('useConnection falls back to DEFAULT_CONNECTION', () => {
|
|
153
|
+
const mockState = {
|
|
154
|
+
tabs: [], activeTabId: '', splitEnabled: false, splitTabId: null,
|
|
155
|
+
connections: [], focusedPane: 'primary' as const,
|
|
156
|
+
};
|
|
157
|
+
const conn = renderHook(
|
|
158
|
+
() => useConnection('nonexistent'),
|
|
159
|
+
({ children }) => createElement(ExplorerStateContext.Provider, { value: mockState as never }, children),
|
|
160
|
+
);
|
|
161
|
+
expect(conn.id).toBe(DEFAULT_CONNECTION.id);
|
|
162
|
+
});
|
|
163
|
+
|
|
164
|
+
it('useFocusedConnection returns focused pane connection', () => {
|
|
165
|
+
const connections = [
|
|
166
|
+
{ id: 'c1', name: 'C1', clusterUrl: 'u1', database: 'd1', color: '#1' },
|
|
167
|
+
{ id: 'c2', name: 'C2', clusterUrl: 'u2', database: 'd2', color: '#2' },
|
|
168
|
+
];
|
|
169
|
+
const tabs = [
|
|
170
|
+
{ id: 't1', name: 'Tab 1', query: '', connectionId: 'c1' },
|
|
171
|
+
{ id: 't2', name: 'Tab 2', query: '', connectionId: 'c2' },
|
|
172
|
+
];
|
|
173
|
+
const mockState = {
|
|
174
|
+
tabs, activeTabId: 't1', splitEnabled: true, splitTabId: 't2',
|
|
175
|
+
connections, focusedPane: 'split' as const,
|
|
176
|
+
};
|
|
177
|
+
const conn = renderHook(
|
|
178
|
+
() => useFocusedConnection(),
|
|
179
|
+
({ children }) => createElement(ExplorerStateContext.Provider, { value: mockState as never }, children),
|
|
180
|
+
);
|
|
181
|
+
expect(conn.id).toBe('c2');
|
|
182
|
+
});
|
|
183
|
+
});
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export { Explorer } from './components/Explorer/Explorer';
|
|
2
|
+
export type { ExplorerProps } from './components/Explorer/Explorer';
|
|
3
|
+
export type { WellKnownCluster, KustoConnection } from './config';
|
|
4
|
+
export { DEFAULT_QUERY, DEFAULT_CONNECTION, CONNECTION_COLORS, KEYBOARD_SHORTCUTS, buildWellKnownClusters } from './config';
|
|
5
|
+
export { KustoClientContext, useKustoClient } from './context/KustoClientContext';
|
|
6
|
+
export { StateService, stateService, STORE_NAMES } from './services/state-service';
|
|
7
|
+
export type { StoreName, StoreEntry, StoreEntryInfo, StoreStats } from './services/state-service';
|
|
8
|
+
export type { ExplorerColorConfig, ExplorerSemanticColors, ExplorerChartColors } from './colors';
|
|
9
|
+
export { ExplorerColorProvider, useExplorerColors } from './context/ExplorerColorContext';
|
|
10
|
+
export { createKustoClient } from './services/kusto';
|
|
11
|
+
export type { KustoClient, KustoClientConfig, KustoTarget, KustoStateService, KustoResult, QueryStats, KustoResultSet, KustoVisualization, QueryPriority, QueryKustoOptions } from './services/kusto';
|
|
12
|
+
export { useKustoQuery, useKustoMgmtQuery } from './services/useKustoQuery';
|
|
13
|
+
export { bootstrapExplorer, useExplorerTheme } from './bootstrap';
|
|
14
|
+
export type { BootstrapExplorerOptions } from './bootstrap';
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { describe, it, expect } from 'vitest';
|
|
2
|
+
import { computeCacheKey } from './cache';
|
|
3
|
+
|
|
4
|
+
describe('computeCacheKey', () => {
|
|
5
|
+
it('returns a deterministic key for the same input', () => {
|
|
6
|
+
const key1 = computeCacheKey('hello', 'pfx_');
|
|
7
|
+
const key2 = computeCacheKey('hello', 'pfx_');
|
|
8
|
+
expect(key1).toBe(key2);
|
|
9
|
+
});
|
|
10
|
+
|
|
11
|
+
it('produces different keys for different texts', () => {
|
|
12
|
+
const key1 = computeCacheKey('hello', 'pfx_');
|
|
13
|
+
const key2 = computeCacheKey('world', 'pfx_');
|
|
14
|
+
expect(key1).not.toBe(key2);
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
it('prepends the prefix', () => {
|
|
18
|
+
const key = computeCacheKey('test', 'myprefix_');
|
|
19
|
+
expect(key.startsWith('myprefix_')).toBe(true);
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
it('includes the text length in the key', () => {
|
|
23
|
+
const key = computeCacheKey('abc', 'p_');
|
|
24
|
+
expect(key).toContain('_3');
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
it('handles empty string', () => {
|
|
28
|
+
const key = computeCacheKey('', 'pfx_');
|
|
29
|
+
expect(key).toBe('pfx_0_0');
|
|
30
|
+
});
|
|
31
|
+
});
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Cache key utility. Used by kusto.ts to generate deterministic
|
|
3
|
+
* cache keys for StateService.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Compute a cache key from a query string.
|
|
8
|
+
* Uses djb2 hash + string length to reduce collision risk.
|
|
9
|
+
*/
|
|
10
|
+
export function computeCacheKey(text: string, prefix: string): string {
|
|
11
|
+
let hash = 0;
|
|
12
|
+
for (let i = 0; i < text.length; i++) {
|
|
13
|
+
hash = ((hash << 5) - hash + text.charCodeAt(i)) | 0;
|
|
14
|
+
}
|
|
15
|
+
return `${prefix}${hash}_${text.length}`;
|
|
16
|
+
}
|