@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,244 @@
1
+ import { Input, Button, Spinner, Tooltip } from '@fluentui/react-components';
2
+ import {
3
+ ChevronRightRegular,
4
+ ChevronDownRegular,
5
+ SearchRegular,
6
+ DismissRegular,
7
+ } from '@fluentui/react-icons';
8
+ import React, { useMemo, useCallback, useState } from 'react';
9
+
10
+ import type { SchemaTable } from '../../services/schema';
11
+ import { useExplorerStyles } from '../ExplorerWorkspace/ExplorerWorkspace.styles';
12
+ import { SchemaTableItem } from './SchemaTableItem';
13
+ import { SchemaColumnItem } from './SchemaColumnItem';
14
+ import { FolderRow } from './FolderRow';
15
+ import { SchemaContextMenu } from './SchemaContextMenu';
16
+ import { groupSchemaByKindAndFolder, filterSchemaBySearch, computeExpandAllState } from './SchemaSidebar.logic';
17
+
18
+ const searchContainerStyle: React.CSSProperties = { display: 'flex', gap: '4px', alignItems: 'center' };
19
+ const clearButtonStyle: React.CSSProperties = { minWidth: 'auto', padding: 0 };
20
+ const expandButtonStyle: React.CSSProperties = { minWidth: 'auto' };
21
+
22
+ const searchIcon = <SearchRegular fontSize={12} />;
23
+ const dismissIcon = <DismissRegular fontSize={10} />;
24
+
25
+ interface SchemaSidebarProps {
26
+ schema: SchemaTable[];
27
+ insertText: (text: string) => void;
28
+ schemaContextRunQuery: (kql: string) => void;
29
+ loading?: boolean;
30
+ }
31
+
32
+ const loadingContainerStyle = { display: 'flex', alignItems: 'center', justifyContent: 'center', flex: 1 } as const;
33
+ const folderPaddingStyle = { paddingLeft: '12px' } as const;
34
+
35
+ export function SchemaSidebar({ schema, insertText, schemaContextRunQuery, loading }: SchemaSidebarProps) {
36
+ const styles = useExplorerStyles();
37
+ const [schemaSearch, setSchemaSearch] = useState('');
38
+ const [expandedFolders, setExpandedFolders] = useState<Set<string>>(new Set());
39
+ const [expandedTables, setExpandedTables] = useState<Set<string>>(new Set());
40
+ const [schemaContextMenu, setSchemaContextMenu] = useState<{ tableName: string; x: number; y: number } | null>(null);
41
+
42
+ const toggleFolder = useCallback((folder: string) => {
43
+ setExpandedFolders((prev) => {
44
+ const next = new Set(prev);
45
+ if (next.has(folder)) {
46
+ next.delete(folder);
47
+ } else {
48
+ next.add(folder);
49
+ }
50
+ return next;
51
+ });
52
+ }, []);
53
+
54
+ const toggleTable = useCallback((tableName: string) => {
55
+ setExpandedTables((prev) => {
56
+ const next = new Set(prev);
57
+ if (next.has(tableName)) {
58
+ next.delete(tableName);
59
+ } else {
60
+ next.add(tableName);
61
+ }
62
+ return next;
63
+ });
64
+ }, []);
65
+
66
+ const groupedByKind = useMemo(() => groupSchemaByKindAndFolder(schema), [schema]);
67
+
68
+ const filteredSchema = useMemo(() => filterSchemaBySearch(schema, schemaSearch), [schema, schemaSearch]);
69
+
70
+ const handleSearchChange = useCallback((_: React.ChangeEvent<HTMLInputElement>, data: { value: string }) => {
71
+ setSchemaSearch(data.value);
72
+ }, []);
73
+
74
+ const handleClearSearch = useCallback(() => setSchemaSearch(''), []);
75
+
76
+ const handleToggleAll = useCallback(() => {
77
+ if (expandedTables.size > 0 || expandedFolders.size > 0) {
78
+ setExpandedFolders(new Set());
79
+ setExpandedTables(new Set());
80
+ } else {
81
+ const { allFolders, allTables } = computeExpandAllState(groupedByKind, schema);
82
+ setExpandedFolders(allFolders);
83
+ setExpandedTables(allTables);
84
+ }
85
+ }, [expandedTables, expandedFolders, groupedByKind, schema]);
86
+
87
+ const contentAfter = useMemo(() => {
88
+ if (!schemaSearch) {
89
+ return undefined;
90
+ }
91
+ return <Button appearance="transparent" icon={dismissIcon} size="small" onClick={handleClearSearch} style={clearButtonStyle} />;
92
+ }, [schemaSearch, handleClearSearch]);
93
+
94
+ const expandCollapseIcon = useMemo(
95
+ () => (expandedTables.size > 0 ? <ChevronDownRegular /> : <ChevronRightRegular />),
96
+ [expandedTables],
97
+ );
98
+
99
+ return (
100
+ <div className={styles.schemaList}>
101
+ <div className={styles.schemaSearch}>
102
+ <div style={searchContainerStyle}>
103
+ <Input className={styles.schemaSearchInput} size="small"
104
+ contentBefore={searchIcon}
105
+ placeholder="Search..."
106
+ value={schemaSearch}
107
+ onChange={handleSearchChange}
108
+ contentAfter={contentAfter}
109
+ />
110
+ <Tooltip content={expandedTables.size > 0 ? 'Collapse all' : 'Expand all'} relationship="label">
111
+ <Button appearance="subtle" size="small"
112
+ icon={expandCollapseIcon}
113
+ style={expandButtonStyle}
114
+ onClick={handleToggleAll} />
115
+ </Tooltip>
116
+ </div>
117
+ </div>
118
+ <div className={styles.schemaTree}>
119
+ {loading ? (
120
+ <div style={loadingContainerStyle}><Spinner size="small" label="Loading schema…" /></div>
121
+ ) : filteredSchema ? (
122
+ filteredSchema.map((t) => {
123
+ const matchingCols = schemaSearch ? t.columns.filter((c) => c.name.toLowerCase().includes(schemaSearch.toLowerCase())) : [];
124
+ return (
125
+ <div key={t.name}>
126
+ <SchemaTableItem
127
+ table={t}
128
+ isExpanded={expandedTables.has(t.name)}
129
+ toggleTable={toggleTable}
130
+ setSchemaContextMenu={setSchemaContextMenu}
131
+ schemaItemClassName={styles.schemaItem}
132
+ schemaColTypeClassName={styles.schemaColType}
133
+ />
134
+ {(expandedTables.has(t.name) || matchingCols.length > 0) && t.columns.map((c) => {
135
+ const isMatch = schemaSearch && c.name.toLowerCase().includes(schemaSearch.toLowerCase());
136
+ if (schemaSearch && !expandedTables.has(t.name) && !isMatch) {
137
+ return null;
138
+ }
139
+ return (
140
+ <SchemaColumnItem
141
+ key={c.name}
142
+ column={c}
143
+ insertText={insertText}
144
+ schemaColumnClassName={styles.schemaColumn}
145
+ schemaColTypeClassName={styles.schemaColType}
146
+ isMatch={!!isMatch}
147
+ />
148
+ );
149
+ })}
150
+ </div>
151
+ );
152
+ })
153
+ ) : (
154
+ groupedByKind.map((group) => (
155
+ <div key={group.label}>
156
+ <FolderRow
157
+ folder={group.label}
158
+ isExpanded={expandedFolders.has(group.label)}
159
+ toggleFolder={toggleFolder}
160
+ className={styles.folderRow}
161
+ />
162
+ {expandedFolders.has(group.label) && (
163
+ <>
164
+ {Object.keys(group.folders).sort().map((folder) => {
165
+ const items = group.folders[folder];
166
+ if (!folder) {
167
+ // No sub-folder — render tables directly
168
+ return items.map((t) => (
169
+ <div key={t.name}>
170
+ <SchemaTableItem
171
+ table={t}
172
+ isExpanded={expandedTables.has(t.name)}
173
+ toggleTable={toggleTable}
174
+ setSchemaContextMenu={setSchemaContextMenu}
175
+ schemaItemClassName={styles.schemaItem}
176
+ schemaColTypeClassName={styles.schemaColType}
177
+ />
178
+ {expandedTables.has(t.name) && t.columns.map((c) => (
179
+ <SchemaColumnItem
180
+ key={c.name}
181
+ column={c}
182
+ insertText={insertText}
183
+ schemaColumnClassName={styles.schemaColumn}
184
+ schemaColTypeClassName={styles.schemaColType}
185
+ />
186
+ ))}
187
+ </div>
188
+ ));
189
+ }
190
+ // Sub-folder within the kind
191
+ return (
192
+ <div key={folder} style={folderPaddingStyle}>
193
+ <FolderRow
194
+ folder={folder}
195
+ folderKey={`${group.label}/${folder}`}
196
+ isExpanded={expandedFolders.has(`${group.label}/${folder}`)}
197
+ toggleFolder={toggleFolder}
198
+ className={styles.folderRow}
199
+ />
200
+ {expandedFolders.has(`${group.label}/${folder}`) && items.map((t) => (
201
+ <div key={t.name}>
202
+ <SchemaTableItem
203
+ table={t}
204
+ isExpanded={expandedTables.has(t.name)}
205
+ toggleTable={toggleTable}
206
+ setSchemaContextMenu={setSchemaContextMenu}
207
+ schemaItemClassName={styles.schemaItem}
208
+ schemaColTypeClassName={styles.schemaColType}
209
+ />
210
+ {expandedTables.has(t.name) && t.columns.map((c) => (
211
+ <SchemaColumnItem
212
+ key={c.name}
213
+ column={c}
214
+ insertText={insertText}
215
+ schemaColumnClassName={styles.schemaColumn}
216
+ schemaColTypeClassName={styles.schemaColType}
217
+ />
218
+ ))}
219
+ </div>
220
+ ))}
221
+ </div>
222
+ );
223
+ })}
224
+ </>
225
+ )}
226
+ </div>
227
+ ))
228
+ )}
229
+ </div>
230
+ {schemaContextMenu && (
231
+ <SchemaContextMenu
232
+ tableName={schemaContextMenu.tableName}
233
+ x={schemaContextMenu.x}
234
+ y={schemaContextMenu.y}
235
+ insertText={insertText}
236
+ schemaContextRunQuery={schemaContextRunQuery}
237
+ setSchemaContextMenu={setSchemaContextMenu}
238
+ contextMenuClassName={styles.contextMenu}
239
+ contextMenuItemClassName={styles.contextMenuItem}
240
+ />
241
+ )}
242
+ </div>
243
+ );
244
+ }
@@ -0,0 +1,77 @@
1
+ import React, { useMemo, useCallback } from 'react';
2
+ import { ChevronDownRegular, ChevronRightRegular, TableSimpleRegular } from '@fluentui/react-icons';
3
+
4
+ import type { SchemaTable } from '../../services/schema';
5
+ import { useExplorerColors } from '../../context/ExplorerColorContext';
6
+
7
+ const badgeBaseStyle: React.CSSProperties = { fontSize: '9px', fontWeight: 700, width: '14px', textAlign: 'center', flexShrink: 0 };
8
+ const tableIconFlexStyle: React.CSSProperties = { flexShrink: 0 };
9
+
10
+ interface BadgeStyles {
11
+ functionBadge: React.CSSProperties;
12
+ mvBadge: React.CSSProperties;
13
+ lookupBadge: React.CSSProperties;
14
+ }
15
+
16
+ export function tableIcon(table: SchemaTable, badges: BadgeStyles): React.ReactNode {
17
+ if (table.kind === 'function') {
18
+ return <span title="Function" style={badges.functionBadge}>fn</span>;
19
+ }
20
+ if (table.kind === 'materializedView') {
21
+ return <span title="Materialized View" style={badges.mvBadge}>MV</span>;
22
+ }
23
+ if (table.folder === 'Lookup') {
24
+ return <span title="Lookup Table" style={badges.lookupBadge}>LU</span>;
25
+ }
26
+ return <TableSimpleRegular fontSize={12} style={tableIconFlexStyle} />;
27
+ }
28
+
29
+ export interface SchemaTableItemProps {
30
+ table: SchemaTable;
31
+ isExpanded: boolean;
32
+ toggleTable: (name: string) => void;
33
+ setSchemaContextMenu: (menu: { tableName: string; x: number; y: number }) => void;
34
+ schemaItemClassName: string;
35
+ schemaColTypeClassName: string;
36
+ }
37
+
38
+ export function SchemaTableItem({ table, isExpanded, toggleTable, setSchemaContextMenu, schemaItemClassName, schemaColTypeClassName }: SchemaTableItemProps) {
39
+ const { semantic } = useExplorerColors();
40
+ const badges = useMemo<BadgeStyles>(() => ({
41
+ functionBadge: { ...badgeBaseStyle, color: semantic.functionBadge },
42
+ mvBadge: { ...badgeBaseStyle, color: semantic.materializedViewBadge },
43
+ lookupBadge: { ...badgeBaseStyle, color: semantic.lookupBadge },
44
+ }), [semantic.functionBadge, semantic.materializedViewBadge, semantic.lookupBadge]);
45
+ const handleClick = useCallback(() => toggleTable(table.name), [toggleTable, table.name]);
46
+ const handleContextMenu = useCallback((e: React.MouseEvent) => {
47
+ e.preventDefault();
48
+ setSchemaContextMenu({ tableName: table.name, x: e.clientX, y: e.clientY });
49
+ }, [setSchemaContextMenu, table.name]);
50
+ const handleDragStart = useCallback((e: React.DragEvent) => {
51
+ e.dataTransfer.setData('text/plain', table.name);
52
+ e.dataTransfer.effectAllowed = 'copy';
53
+ }, [table.name]);
54
+ const handleKeyDown = useCallback((e: React.KeyboardEvent) => {
55
+ if (e.key === 'Enter' || e.key === ' ') {
56
+ e.preventDefault();
57
+ toggleTable(table.name);
58
+ }
59
+ }, [toggleTable, table.name]);
60
+
61
+ return (
62
+ <div className={schemaItemClassName}
63
+ role="treeitem"
64
+ data-context-menu
65
+ onClick={handleClick}
66
+ onContextMenu={handleContextMenu}
67
+ onKeyDown={handleKeyDown}
68
+ tabIndex={0}
69
+ draggable
70
+ onDragStart={handleDragStart}
71
+ title={table.description}>
72
+ {isExpanded ? <ChevronDownRegular fontSize={10} /> : <ChevronRightRegular fontSize={10} />}
73
+ {tableIcon(table, badges)}<span>{table.name}</span>
74
+ <span className={schemaColTypeClassName}>{table.columns.length}</span>
75
+ </div>
76
+ );
77
+ }
@@ -0,0 +1,127 @@
1
+ import { describe, it, expect, vi, beforeEach } from 'vitest';
2
+
3
+ import { shouldConfirmClose, shouldConfirmCloseOthers, getDropIndicatorIndex } from './TabBar.logic';
4
+ import type { QueryTab } from '../ExplorerWorkspace/ExplorerWorkspace.logic';
5
+
6
+ beforeEach(() => {
7
+ vi.spyOn(console, 'error').mockImplementation(() => {});
8
+ vi.spyOn(console, 'warn').mockImplementation(() => {});
9
+ });
10
+
11
+ function makeTab(id: string, kql = ''): QueryTab {
12
+ return { id, title: `Tab ${id}`, kql, connectionId: '' };
13
+ }
14
+
15
+ // ---------------------------------------------------------------------------
16
+ // shouldConfirmClose
17
+ // ---------------------------------------------------------------------------
18
+
19
+ describe('shouldConfirmClose', () => {
20
+ it('returns true when tab has non-empty kql content', () => {
21
+ const tabs = [makeTab('t1', 'StormEvents | take 10')];
22
+ expect(shouldConfirmClose(tabs, 't1')).toBe(true);
23
+ });
24
+
25
+ it('returns false when tab has empty kql', () => {
26
+ const tabs = [makeTab('t1', '')];
27
+ expect(shouldConfirmClose(tabs, 't1')).toBe(false);
28
+ });
29
+
30
+ it('returns false when tab has whitespace-only kql', () => {
31
+ const tabs = [makeTab('t1', ' ')];
32
+ expect(shouldConfirmClose(tabs, 't1')).toBe(false);
33
+ });
34
+
35
+ it('returns false when tabId does not exist', () => {
36
+ const tabs = [makeTab('t1', 'some kql')];
37
+ expect(shouldConfirmClose(tabs, 'nonexistent')).toBe(false);
38
+ });
39
+
40
+ it('returns false for empty tabs array', () => {
41
+ expect(shouldConfirmClose([], 't1')).toBe(false);
42
+ });
43
+
44
+ it('finds the correct tab among multiple tabs', () => {
45
+ const tabs = [makeTab('t1', ''), makeTab('t2', 'content'), makeTab('t3', '')];
46
+ expect(shouldConfirmClose(tabs, 't2')).toBe(true);
47
+ expect(shouldConfirmClose(tabs, 't1')).toBe(false);
48
+ });
49
+ });
50
+
51
+ // ---------------------------------------------------------------------------
52
+ // shouldConfirmCloseOthers
53
+ // ---------------------------------------------------------------------------
54
+
55
+ describe('shouldConfirmCloseOthers', () => {
56
+ it('returns true when other tabs have content', () => {
57
+ const tabs = [makeTab('t1', 'keep'), makeTab('t2', 'has content')];
58
+ expect(shouldConfirmCloseOthers(tabs, 't1')).toBe(true);
59
+ });
60
+
61
+ it('returns false when other tabs are empty', () => {
62
+ const tabs = [makeTab('t1', 'keep'), makeTab('t2', ''), makeTab('t3', ' ')];
63
+ expect(shouldConfirmCloseOthers(tabs, 't1')).toBe(false);
64
+ });
65
+
66
+ it('returns false when there is only one tab', () => {
67
+ const tabs = [makeTab('t1', 'content')];
68
+ expect(shouldConfirmCloseOthers(tabs, 't1')).toBe(false);
69
+ });
70
+
71
+ it('returns false for empty tabs array', () => {
72
+ expect(shouldConfirmCloseOthers([], 't1')).toBe(false);
73
+ });
74
+
75
+ it('checks all other tabs, not just the first', () => {
76
+ const tabs = [makeTab('t1', 'keep'), makeTab('t2', ''), makeTab('t3', 'content')];
77
+ expect(shouldConfirmCloseOthers(tabs, 't1')).toBe(true);
78
+ });
79
+ });
80
+
81
+ // ---------------------------------------------------------------------------
82
+ // getDropIndicatorIndex
83
+ // ---------------------------------------------------------------------------
84
+
85
+ describe('getDropIndicatorIndex', () => {
86
+ const rect = { left: 100, width: 200, top: 0, height: 40 } as DOMRect;
87
+
88
+ it('returns toIdx when cursor is before midpoint', () => {
89
+ // midpoint is 200 (100 + 200/2), clientX < 200 → insertIdx = toIdx
90
+ const result = getDropIndicatorIndex(0, 2, rect, 150);
91
+ expect(result).toBe(2);
92
+ });
93
+
94
+ it('returns toIdx + 1 when cursor is after midpoint', () => {
95
+ // clientX >= 200 → insertIdx = toIdx + 1
96
+ const result = getDropIndicatorIndex(0, 2, rect, 250);
97
+ expect(result).toBe(3);
98
+ });
99
+
100
+ it('returns null when drop position equals dragFromIdx', () => {
101
+ // insertIdx = 2 (before midpoint), dragFromIdx = 2 → null
102
+ const result = getDropIndicatorIndex(2, 2, rect, 150);
103
+ expect(result).toBeNull();
104
+ });
105
+
106
+ it('returns null when drop position equals dragFromIdx + 1', () => {
107
+ // insertIdx = 3 (after midpoint), dragFromIdx = 2 → 3 === 2+1 → null
108
+ const result = getDropIndicatorIndex(2, 2, rect, 250);
109
+ expect(result).toBeNull();
110
+ });
111
+
112
+ it('returns index for valid reorder from left to right', () => {
113
+ const result = getDropIndicatorIndex(0, 3, rect, 250);
114
+ expect(result).toBe(4);
115
+ });
116
+
117
+ it('returns index for valid reorder from right to left', () => {
118
+ const result = getDropIndicatorIndex(3, 0, rect, 150);
119
+ expect(result).toBe(0);
120
+ });
121
+
122
+ it('handles cursor exactly at midpoint (goes to after)', () => {
123
+ // clientX === midpoint (200), 200 < 200 is false → insertIdx = toIdx + 1
124
+ const result = getDropIndicatorIndex(0, 2, rect, 200);
125
+ expect(result).toBe(3);
126
+ });
127
+ });
@@ -0,0 +1,35 @@
1
+ /**
2
+ * Pure functions for TabBar — no React dependencies.
3
+ */
4
+
5
+ import type { QueryTab } from '../ExplorerWorkspace/ExplorerWorkspace.logic';
6
+
7
+ /**
8
+ * Returns true if the tab identified by tabId has non-empty KQL content,
9
+ * meaning the user should be prompted before closing.
10
+ */
11
+ export function shouldConfirmClose(tabs: QueryTab[], tabId: string): boolean {
12
+ const tab = tabs.find((t) => t.id === tabId);
13
+ return !!tab && !!tab.kql.trim();
14
+ }
15
+
16
+ /**
17
+ * Returns true if any tab OTHER than keepTabId has non-empty KQL content,
18
+ * meaning the user should be prompted before closing others.
19
+ */
20
+ export function shouldConfirmCloseOthers(tabs: QueryTab[], keepTabId: string): boolean {
21
+ return tabs.some((t) => t.id !== keepTabId && t.kql.trim().length > 0);
22
+ }
23
+
24
+ /**
25
+ * Computes where to show the drop indicator when reordering tabs.
26
+ * Returns an insertion index, or null if the tab would stay in place.
27
+ */
28
+ export function getDropIndicatorIndex(dragFromIdx: number, toIdx: number, rect: DOMRect, clientX: number): number | null {
29
+ const midX = rect.left + rect.width / 2;
30
+ const insertIdx = clientX < midX ? toIdx : toIdx + 1;
31
+ if (insertIdx !== dragFromIdx && insertIdx !== dragFromIdx + 1) {
32
+ return insertIdx;
33
+ }
34
+ return null;
35
+ }