@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.
Files changed (90) hide show
  1. package/LICENSE +21 -0
  2. package/package.json +46 -0
  3. package/src/bootstrap.spec.tsx +290 -0
  4. package/src/bootstrap.tsx +195 -0
  5. package/src/colors.ts +23 -0
  6. package/src/components/ChartPanel/ChartPanel.logic.spec.ts +358 -0
  7. package/src/components/ChartPanel/ChartPanel.logic.ts +318 -0
  8. package/src/components/ChartPanel/ChartPanel.spec.tsx +331 -0
  9. package/src/components/ChartPanel/ChartPanel.styles.ts +14 -0
  10. package/src/components/ChartPanel/ChartPanel.tsx +151 -0
  11. package/src/components/ConnectionDialog/ConnectionDialog.spec.tsx +767 -0
  12. package/src/components/ConnectionDialog/ConnectionDialog.tsx +652 -0
  13. package/src/components/ConnectionDialog/ConnectionRow.spec.tsx +104 -0
  14. package/src/components/ConnectionDialog/ConnectionRow.tsx +177 -0
  15. package/src/components/EditorToolbar/EditorToolbar.spec.tsx +599 -0
  16. package/src/components/EditorToolbar/EditorToolbar.tsx +259 -0
  17. package/src/components/Explorer/Explorer.spec.tsx +615 -0
  18. package/src/components/Explorer/Explorer.tsx +49 -0
  19. package/src/components/ExplorerWorkspace/ExplorerWorkspace.logic.spec.ts +715 -0
  20. package/src/components/ExplorerWorkspace/ExplorerWorkspace.logic.ts +324 -0
  21. package/src/components/ExplorerWorkspace/ExplorerWorkspace.spec.tsx +450 -0
  22. package/src/components/ExplorerWorkspace/ExplorerWorkspace.styles.ts +478 -0
  23. package/src/components/ExplorerWorkspace/ExplorerWorkspace.tsx +648 -0
  24. package/src/components/ExplorerWorkspace/kqlLanguage.spec.ts +321 -0
  25. package/src/components/ExplorerWorkspace/kqlLanguage.ts +155 -0
  26. package/src/components/ResultsPanel/ElapsedTimer.spec.tsx +51 -0
  27. package/src/components/ResultsPanel/ElapsedTimer.tsx +15 -0
  28. package/src/components/ResultsPanel/HistoryItem.spec.tsx +123 -0
  29. package/src/components/ResultsPanel/HistoryItem.tsx +56 -0
  30. package/src/components/ResultsPanel/ResultSetTab.tsx +18 -0
  31. package/src/components/ResultsPanel/ResultsPanel.spec.tsx +1194 -0
  32. package/src/components/ResultsPanel/ResultsPanel.tsx +321 -0
  33. package/src/components/ResultsPanel/TabButton.tsx +11 -0
  34. package/src/components/ResultsTable/ColumnHeader.tsx +42 -0
  35. package/src/components/ResultsTable/ColumnPickerItem.tsx +16 -0
  36. package/src/components/ResultsTable/FilterCell.tsx +24 -0
  37. package/src/components/ResultsTable/ResultsTable.logic.spec.ts +655 -0
  38. package/src/components/ResultsTable/ResultsTable.logic.ts +264 -0
  39. package/src/components/ResultsTable/ResultsTable.spec.tsx +930 -0
  40. package/src/components/ResultsTable/ResultsTable.styles.ts +108 -0
  41. package/src/components/ResultsTable/ResultsTable.tsx +501 -0
  42. package/src/components/ResultsTable/VirtualCell.spec.tsx +55 -0
  43. package/src/components/ResultsTable/VirtualCell.tsx +32 -0
  44. package/src/components/ResultsTable/VirtualRow.tsx +55 -0
  45. package/src/components/SchemaSidebar/FolderRow.tsx +35 -0
  46. package/src/components/SchemaSidebar/SchemaColumnItem.tsx +41 -0
  47. package/src/components/SchemaSidebar/SchemaContextMenu.spec.tsx +148 -0
  48. package/src/components/SchemaSidebar/SchemaContextMenu.tsx +49 -0
  49. package/src/components/SchemaSidebar/SchemaSidebar.logic.spec.ts +230 -0
  50. package/src/components/SchemaSidebar/SchemaSidebar.logic.ts +74 -0
  51. package/src/components/SchemaSidebar/SchemaSidebar.spec.tsx +1111 -0
  52. package/src/components/SchemaSidebar/SchemaSidebar.tsx +244 -0
  53. package/src/components/SchemaSidebar/SchemaTableItem.tsx +77 -0
  54. package/src/components/TabBar/TabBar.logic.spec.ts +127 -0
  55. package/src/components/TabBar/TabBar.logic.ts +35 -0
  56. package/src/components/TabBar/TabBar.spec.tsx +929 -0
  57. package/src/components/TabBar/TabBar.tsx +284 -0
  58. package/src/components/TabBar/TabItem.tsx +166 -0
  59. package/src/components/VegaChart/VegaChart.spec.tsx +126 -0
  60. package/src/components/VegaChart/VegaChart.tsx +224 -0
  61. package/src/config.spec.ts +44 -0
  62. package/src/config.ts +47 -0
  63. package/src/context/ExplorerColorContext.ts +14 -0
  64. package/src/context/ExplorerStateContext.ts +49 -0
  65. package/src/context/KustoClientContext.ts +12 -0
  66. package/src/context/context.spec.tsx +183 -0
  67. package/src/index.ts +14 -0
  68. package/src/services/cache.spec.ts +31 -0
  69. package/src/services/cache.ts +16 -0
  70. package/src/services/concurrency.spec.ts +139 -0
  71. package/src/services/concurrency.ts +46 -0
  72. package/src/services/kusto.spec.ts +919 -0
  73. package/src/services/kusto.ts +472 -0
  74. package/src/services/queryHistory.spec.ts +287 -0
  75. package/src/services/queryHistory.ts +146 -0
  76. package/src/services/schema.spec.ts +618 -0
  77. package/src/services/schema.ts +294 -0
  78. package/src/services/state-service.spec.ts +670 -0
  79. package/src/services/state-service.ts +602 -0
  80. package/src/services/useKustoQuery.spec.ts +234 -0
  81. package/src/services/useKustoQuery.ts +124 -0
  82. package/src/state/actions.ts +50 -0
  83. package/src/state/index.ts +3 -0
  84. package/src/state/reducer.spec.ts +644 -0
  85. package/src/state/reducer.ts +207 -0
  86. package/src/state/types.ts +54 -0
  87. package/tsconfig.json +7 -0
  88. package/tsconfig.lint.json +4 -0
  89. package/tsconfig.spec.json +7 -0
  90. package/vitest.config.ts +34 -0
@@ -0,0 +1,234 @@
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 type { KustoClient } from './kusto';
7
+
8
+ function createMockClient(result?: { rows: unknown[]; columns: unknown[] }): KustoClient {
9
+ return {
10
+ queryKusto: vi.fn().mockResolvedValue(result ?? { rows: [{ id: 1 }], columns: [{ ColumnName: 'id' }], resultSets: [] }),
11
+ queryKustoMgmt: vi.fn().mockResolvedValue(result ?? { rows: [{ id: 1 }], columns: [{ ColumnName: 'id' }], resultSets: [] }),
12
+ clearQueryCache: vi.fn(),
13
+ getQueryCacheSize: vi.fn().mockReturnValue(0),
14
+ };
15
+ }
16
+
17
+ function renderHookAsync<T>(
18
+ hook: () => T,
19
+ ): { getResult: () => T; container: HTMLDivElement; root: ReturnType<typeof createRoot> } {
20
+ let result: T;
21
+ function TestComponent() {
22
+ result = hook();
23
+ return null;
24
+ }
25
+ const container = document.createElement('div');
26
+ document.body.appendChild(container);
27
+ const root = createRoot(container);
28
+ act(() => {
29
+ root.render(createElement(TestComponent));
30
+ });
31
+ return {
32
+ getResult: () => result!,
33
+ container,
34
+ root,
35
+ };
36
+ }
37
+
38
+ beforeEach(() => {
39
+ vi.spyOn(console, 'error').mockImplementation(() => {});
40
+ vi.spyOn(console, 'warn').mockImplementation(() => {});
41
+ });
42
+
43
+ afterEach(() => {
44
+ vi.restoreAllMocks();
45
+ });
46
+
47
+ describe('useKustoQuery', () => {
48
+ // Dynamic import to avoid hoisting issues
49
+ it('returns loading state initially then data', async () => {
50
+ const { useKustoQuery } = await import('./useKustoQuery');
51
+ const client = createMockClient({ rows: [{ name: 'test' }], columns: [{ ColumnName: 'name' }] });
52
+ const { getResult, container, root } = renderHookAsync(() => useKustoQuery('MyTable | take 1', { client }));
53
+
54
+ // Initially loading
55
+ expect(getResult().loading).toBe(true);
56
+ expect(getResult().kql).toBe('MyTable | take 1');
57
+
58
+ // Wait for async resolution
59
+ await act(async () => {
60
+ await vi.dynamicImportSettled();
61
+ });
62
+
63
+ // After resolution
64
+ expect(getResult().loading).toBe(false);
65
+ expect(getResult().data).toEqual([{ name: 'test' }]);
66
+ expect(getResult().error).toBeNull();
67
+
68
+ act(() => { root.unmount(); });
69
+ container.remove();
70
+ });
71
+
72
+ it('returns null data for empty kql', async () => {
73
+ const { useKustoQuery } = await import('./useKustoQuery');
74
+ const client = createMockClient();
75
+ const { getResult, container, root } = renderHookAsync(() => useKustoQuery('', { client }));
76
+
77
+ expect(getResult().loading).toBe(false);
78
+ expect(getResult().data).toBeNull();
79
+
80
+ act(() => { root.unmount(); });
81
+ container.remove();
82
+ });
83
+
84
+ it('handles errors', async () => {
85
+ const { useKustoQuery } = await import('./useKustoQuery');
86
+ const client = createMockClient();
87
+ vi.mocked(client.queryKusto).mockRejectedValue(new Error('Query failed'));
88
+
89
+ const { getResult, container, root } = renderHookAsync(() => useKustoQuery('bad query', { client }));
90
+
91
+ await act(async () => {
92
+ await new Promise((r) => setTimeout(r, 10));
93
+ });
94
+
95
+ expect(getResult().loading).toBe(false);
96
+ expect(getResult().error).toBe('Query failed');
97
+ expect(getResult().data).toBeNull();
98
+
99
+ act(() => { root.unmount(); });
100
+ container.remove();
101
+ });
102
+ });
103
+
104
+ describe('useKustoMgmtQuery', () => {
105
+ it('fetches management query results', async () => {
106
+ const { useKustoMgmtQuery } = await import('./useKustoQuery');
107
+ const client = createMockClient({ rows: [{ TableName: 'T1' }], columns: [{ ColumnName: 'TableName' }] });
108
+ const { getResult, container, root } = renderHookAsync(() => useKustoMgmtQuery('.show tables', { client }));
109
+
110
+ await act(async () => {
111
+ await new Promise((r) => setTimeout(r, 10));
112
+ });
113
+
114
+ expect(getResult().loading).toBe(false);
115
+ expect(getResult().data).toEqual([{ TableName: 'T1' }]);
116
+
117
+ act(() => { root.unmount(); });
118
+ container.remove();
119
+ });
120
+
121
+ it('handles management query errors', async () => {
122
+ const { useKustoMgmtQuery } = await import('./useKustoQuery');
123
+ const client = createMockClient();
124
+ vi.mocked(client.queryKustoMgmt).mockRejectedValue(new Error('mgmt failed'));
125
+ const { getResult, container, root } = renderHookAsync(() => useKustoMgmtQuery('.bad command', { client }));
126
+
127
+ await act(async () => {
128
+ await new Promise((r) => setTimeout(r, 10));
129
+ });
130
+
131
+ expect(getResult().loading).toBe(false);
132
+ expect(getResult().error).toBe('mgmt failed');
133
+
134
+ act(() => { root.unmount(); });
135
+ container.remove();
136
+ });
137
+ });
138
+
139
+ describe('useDeferredKustoQuery', () => {
140
+ it('does not query until element is visible', async () => {
141
+ const { useDeferredKustoQuery } = await import('./useKustoQuery');
142
+ const client = createMockClient();
143
+ const { getResult, container, root } = renderHookAsync(() => useDeferredKustoQuery('MyTable | take 1', { client }));
144
+
145
+ // Initially not visible — should not query
146
+ expect(getResult().loading).toBe(false);
147
+ expect(getResult().data).toBeNull();
148
+ expect(getResult().containerRef).toBeDefined();
149
+
150
+ act(() => { root.unmount(); });
151
+ container.remove();
152
+ });
153
+
154
+ it('queries after IntersectionObserver fires', async () => {
155
+ // Mock IntersectionObserver
156
+ let observerCallback: (entries: { isIntersecting: boolean }[]) => void = () => {};
157
+ const mockObserve = vi.fn();
158
+ const mockDisconnect = vi.fn();
159
+ vi.stubGlobal('IntersectionObserver', class {
160
+ constructor(cb: (entries: { isIntersecting: boolean }[]) => void) { observerCallback = cb; }
161
+ observe = mockObserve;
162
+ disconnect = mockDisconnect;
163
+ });
164
+
165
+ const { useDeferredKustoQuery } = await import('./useKustoQuery');
166
+ const client = createMockClient({ rows: [{ a: 1 }], columns: [{ ColumnName: 'a' }] });
167
+ let refValue: HTMLDivElement | null = null;
168
+
169
+ function TestComp() {
170
+ const result = useDeferredKustoQuery('Q | take 1', { client });
171
+ // Simulate attaching ref
172
+ if (result.containerRef && !refValue) {
173
+ refValue = document.createElement('div');
174
+ (result.containerRef as React.MutableRefObject<HTMLDivElement | null>).current = refValue;
175
+ }
176
+ return null;
177
+ }
178
+
179
+ const testContainer = document.createElement('div');
180
+ document.body.appendChild(testContainer);
181
+ const testRoot = createRoot(testContainer);
182
+ act(() => { testRoot.render(createElement(TestComp)); });
183
+
184
+ // Trigger intersection
185
+ act(() => { observerCallback([{ isIntersecting: true }]); });
186
+
187
+ await act(async () => {
188
+ await new Promise((r) => setTimeout(r, 10));
189
+ });
190
+
191
+ expect(client.queryKusto).toHaveBeenCalled();
192
+
193
+ act(() => { testRoot.unmount(); });
194
+ testContainer.remove();
195
+ vi.unstubAllGlobals();
196
+ });
197
+ });
198
+
199
+ describe('useKustoQuery refresh', () => {
200
+ it('re-executes query on refresh', async () => {
201
+ const { useKustoQuery } = await import('./useKustoQuery');
202
+ const client = createMockClient({ rows: [{ a: 1 }], columns: [{ ColumnName: 'a' }] });
203
+ let refreshFn: (() => void) | undefined;
204
+
205
+ function TestComp() {
206
+ const result = useKustoQuery('Q | take 1', { client });
207
+ refreshFn = result.refresh;
208
+ return null;
209
+ }
210
+
211
+ const testContainer = document.createElement('div');
212
+ document.body.appendChild(testContainer);
213
+ const testRoot = createRoot(testContainer);
214
+ act(() => { testRoot.render(createElement(TestComp)); });
215
+
216
+ await act(async () => {
217
+ await new Promise((r) => setTimeout(r, 10));
218
+ });
219
+
220
+ expect(client.queryKusto).toHaveBeenCalledTimes(1);
221
+
222
+ // Trigger refresh
223
+ act(() => { refreshFn?.(); });
224
+
225
+ await act(async () => {
226
+ await new Promise((r) => setTimeout(r, 10));
227
+ });
228
+
229
+ expect(client.queryKusto).toHaveBeenCalledTimes(2);
230
+
231
+ act(() => { testRoot.unmount(); });
232
+ testContainer.remove();
233
+ });
234
+ });
@@ -0,0 +1,124 @@
1
+ import { useState, useEffect, useCallback, useRef } from 'react';
2
+
3
+ import type { KustoClient, KustoResult, QueryPriority } from './kusto';
4
+
5
+ interface UseKustoQueryOptions {
6
+ priority?: QueryPriority;
7
+ client: KustoClient;
8
+ }
9
+
10
+ interface UseKustoQueryResult<T> {
11
+ data: T[] | null;
12
+ loading: boolean;
13
+ error: string | null;
14
+ refresh: () => void;
15
+ kql: string;
16
+ }
17
+
18
+ export function useKustoQuery<T = Record<string, unknown>>(kql: string, options: UseKustoQueryOptions): UseKustoQueryResult<T> {
19
+ const { client, priority } = options;
20
+ const [data, setData] = useState<T[] | null>(null);
21
+ const [loading, setLoading] = useState(true);
22
+ const [error, setError] = useState<string | null>(null);
23
+ const [refreshKey, setRefreshKey] = useState(0);
24
+
25
+ const refresh = useCallback(() => setRefreshKey((k) => k + 1), []);
26
+
27
+ useEffect(() => {
28
+ if (!kql || !kql.trim()) {
29
+ setData(null);
30
+ setLoading(false);
31
+ return;
32
+ }
33
+
34
+ let cancelled = false;
35
+ setLoading(true);
36
+ setError(null);
37
+
38
+ client.queryKusto<T>(kql, undefined, priority)
39
+ .then((result: KustoResult<T>) => {
40
+ if (!cancelled) {
41
+ setData(result.rows);
42
+ setLoading(false);
43
+ }
44
+ })
45
+ .catch((err: Error) => {
46
+ if (!cancelled) {
47
+ setError(err.message);
48
+ setLoading(false);
49
+ }
50
+ });
51
+
52
+ return () => {
53
+ cancelled = true;
54
+ };
55
+ }, [kql, refreshKey, priority, client]);
56
+
57
+ return { data, loading, error, refresh, kql };
58
+ }
59
+
60
+ export function useDeferredKustoQuery<T = Record<string, unknown>>(
61
+ kql: string,
62
+ options: UseKustoQueryOptions,
63
+ ): UseKustoQueryResult<T> & { containerRef: React.RefObject<HTMLDivElement | null> } {
64
+ const [visible, setVisible] = useState(false);
65
+ const containerRef = useRef<HTMLDivElement | null>(null);
66
+
67
+ useEffect(() => {
68
+ const el = containerRef.current;
69
+ if (!el) {
70
+ return;
71
+ }
72
+
73
+ const observer = new IntersectionObserver(
74
+ ([entry]) => {
75
+ if (entry.isIntersecting) {
76
+ setVisible(true);
77
+ observer.disconnect();
78
+ }
79
+ },
80
+ { rootMargin: '400px' },
81
+ );
82
+ observer.observe(el);
83
+ return () => observer.disconnect();
84
+ }, []);
85
+
86
+ const result = useKustoQuery<T>(visible ? kql : '', options);
87
+ return { ...result, containerRef };
88
+ }
89
+
90
+ export function useKustoMgmtQuery<T = Record<string, unknown>>(kql: string, options: { client: KustoClient }): UseKustoQueryResult<T> {
91
+ const { client } = options;
92
+ const [data, setData] = useState<T[] | null>(null);
93
+ const [loading, setLoading] = useState(true);
94
+ const [error, setError] = useState<string | null>(null);
95
+ const [refreshKey, setRefreshKey] = useState(0);
96
+
97
+ const refresh = useCallback(() => setRefreshKey((k) => k + 1), []);
98
+
99
+ useEffect(() => {
100
+ let cancelled = false;
101
+ setLoading(true);
102
+ setError(null);
103
+
104
+ client.queryKustoMgmt<T>(kql)
105
+ .then((result: KustoResult<T>) => {
106
+ if (!cancelled) {
107
+ setData(result.rows);
108
+ setLoading(false);
109
+ }
110
+ })
111
+ .catch((err: Error) => {
112
+ if (!cancelled) {
113
+ setError(err.message);
114
+ setLoading(false);
115
+ }
116
+ });
117
+
118
+ return () => {
119
+ cancelled = true;
120
+ };
121
+ }, [kql, refreshKey, client]);
122
+
123
+ return { data, loading, error, refresh, kql };
124
+ }
@@ -0,0 +1,50 @@
1
+ import type { QueryStats, KustoResultSet } from '../services/kusto';
2
+ import type { QueryHistoryEntry } from '../services/queryHistory';
3
+ import type { SchemaTable } from '../services/schema';
4
+ import type { KustoConnection } from '../components/ExplorerWorkspace/ExplorerWorkspace.logic';
5
+ import type { ExplorerState } from './types';
6
+
7
+ export type ExplorerAction =
8
+ // Connections
9
+ | { type: 'SET_CONNECTIONS'; connections: KustoConnection[] }
10
+ | { type: 'SET_TAB_CONNECTION'; tabId: string; connectionId: string }
11
+
12
+ // Schema
13
+ | { type: 'SET_SCHEMA'; schema: SchemaTable[] }
14
+ | { type: 'SET_SCHEMA_SEARCH'; search: string }
15
+ | { type: 'TOGGLE_FOLDER'; folder: string }
16
+ | { type: 'TOGGLE_TABLE'; table: string }
17
+ | { type: 'EXPAND_ALL_FOLDERS'; folders: string[] }
18
+ | { type: 'COLLAPSE_ALL_FOLDERS' }
19
+ | { type: 'SET_SCHEMA_CONTEXT_MENU'; menu: ExplorerState['schemaContextMenu'] }
20
+
21
+ // Tabs
22
+ | { type: 'ADD_TAB' }
23
+ | { type: 'CLOSE_TAB'; tabId: string }
24
+ | { type: 'CLOSE_OTHER_TABS'; tabId: string }
25
+ | { type: 'CLOSE_ALL_TABS' }
26
+ | { type: 'DUPLICATE_TAB'; tabId: string }
27
+ | { type: 'SWITCH_TAB'; tabId: string }
28
+ | { type: 'UPDATE_TAB_KQL'; tabId: string; kql: string }
29
+ | { type: 'RENAME_TAB'; tabId: string; title: string }
30
+ | { type: 'REORDER_TABS'; fromIdx: number; toIdx: number }
31
+ | { type: 'TOGGLE_SPLIT'; direction: 'vertical' | 'horizontal'; splitTabId?: string }
32
+ | { type: 'CLOSE_SPLIT' }
33
+ | { type: 'SET_FOCUSED_PANE'; pane: 'primary' | 'secondary' }
34
+
35
+ // Query execution
36
+ | { type: 'QUERY_START'; queryKey: string }
37
+ | { type: 'QUERY_SUCCESS'; rows: Record<string, unknown>[]; columns: string[]; columnTypes: Record<string, string>; elapsed: number; stats: QueryStats | null; resultSets: KustoResultSet[] }
38
+ | { type: 'QUERY_ERROR'; error: string }
39
+ | { type: 'QUERY_CANCEL' }
40
+
41
+ // Results
42
+ | { type: 'SET_RESULTS_TAB'; tab: ExplorerState['resultsTab'] }
43
+ | { type: 'SET_ACTIVE_RESULT_SET'; index: number }
44
+ | { type: 'SET_HISTORY'; history: QueryHistoryEntry[] }
45
+ | { type: 'LOAD_HISTORY_ENTRY'; rows: Record<string, unknown>[]; columns: string[]; columnTypes: Record<string, string>; kql: string }
46
+
47
+ // UI
48
+ | { type: 'SET_EDITOR_HEIGHT'; height: number }
49
+ | { type: 'SET_HAS_SELECTION'; hasSelection: boolean }
50
+ | { type: 'SET_CAN_RECALL'; canRecall: boolean };
@@ -0,0 +1,3 @@
1
+ export type { ExplorerState, InitialStateArgs } from './types';
2
+ export type { ExplorerAction } from './actions';
3
+ export { explorerReducer, createInitialState } from './reducer';