@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,284 @@
1
+ import { Button, Tooltip, Menu, MenuList, MenuItem, MenuPopover, MenuDivider, Dialog, DialogSurface, DialogBody, DialogTitle, DialogContent, DialogActions, DialogTrigger } from '@fluentui/react-components';
2
+ import { AddRegular, SplitVerticalRegular, SplitHorizontalRegular, RenameRegular, CopyRegular, DismissRegular, DismissCircleRegular } from '@fluentui/react-icons';
3
+ import React, { useState, useCallback, useRef, useMemo } from 'react';
4
+
5
+ import { useExplorerState, useExplorerDispatch } from '../../context/ExplorerStateContext';
6
+ import { useExplorerStyles } from '../ExplorerWorkspace/ExplorerWorkspace.styles';
7
+ import { TabItem } from './TabItem';
8
+ import { shouldConfirmClose, shouldConfirmCloseOthers, getDropIndicatorIndex } from './TabBar.logic';
9
+
10
+ const addIcon = <AddRegular />;
11
+ const splitVerticalIcon = <SplitVerticalRegular />;
12
+ const splitHorizontalIcon = <SplitHorizontalRegular />;
13
+ const renameIcon = <RenameRegular />;
14
+ const copyIcon = <CopyRegular />;
15
+ const dismissMenuIcon = <DismissRegular />;
16
+ const dismissCircleIcon = <DismissCircleRegular />;
17
+ const minWidthAutoStyle = { minWidth: 'auto' } as const;
18
+ const addTabButtonStyle = { minWidth: 'auto', height: '28px', marginLeft: '4px', marginRight: '4px' } as const;
19
+ export interface TabBarProps {
20
+ dragTabIdRef: React.MutableRefObject<string | null>;
21
+ onSetEditorDropTarget: (target: 'left' | 'right' | 'top' | 'bottom' | null) => void;
22
+ }
23
+
24
+ export function TabBar({ dragTabIdRef, onSetEditorDropTarget }: TabBarProps) {
25
+ const styles = useExplorerStyles();
26
+ const { tabs, activeTabId, splitTabId, splitEnabled, splitDirection, focusedPane, connections } = useExplorerState();
27
+ const dispatch = useExplorerDispatch();
28
+
29
+ const [renamingTabId, setRenamingTabId] = useState<string | null>(null);
30
+ const [renameValue, setRenameValue] = useState('');
31
+ const [contextMenu, setContextMenu] = useState<{ tabId: string; x: number; y: number } | null>(null);
32
+ const [pendingCloseTabId, setPendingCloseTabId] = useState<string | null>(null);
33
+ const [pendingCloseOthersTabId, setPendingCloseOthersTabId] = useState<string | null>(null);
34
+ const dragTabRef = useRef<number | null>(null);
35
+
36
+ const startRename = useCallback((tabId: string, currentTitle: string) => {
37
+ setRenamingTabId(tabId);
38
+ setRenameValue(currentTitle);
39
+ setContextMenu(null);
40
+ }, []);
41
+
42
+ const commitRename = useCallback(() => {
43
+ if (renamingTabId && renameValue.trim()) {
44
+ dispatch({ type: "RENAME_TAB", tabId: renamingTabId, title: renameValue.trim() });
45
+ }
46
+ setRenamingTabId(null);
47
+ }, [renamingTabId, renameValue, dispatch]);
48
+
49
+ const cancelRename = useCallback(() => setRenamingTabId(null), []);
50
+
51
+ const [dropIndicatorIdx, setDropIndicatorIdx] = useState<number | null>(null);
52
+
53
+ const handleSwitch = useCallback((tabId: string) => dispatch({ type: "SWITCH_TAB", tabId }), [dispatch]);
54
+ const handleContextMenuOpen = useCallback((tabId: string, x: number, y: number) => {
55
+ setContextMenu({ tabId, x, y });
56
+ }, []);
57
+ const handleClose = useCallback((tabId: string) => {
58
+ if (shouldConfirmClose(tabs, tabId)) {
59
+ setPendingCloseTabId(tabId);
60
+ } else {
61
+ dispatch({ type: 'CLOSE_TAB', tabId });
62
+ }
63
+ }, [tabs, dispatch]);
64
+ const confirmClose = useCallback(() => {
65
+ if (pendingCloseTabId) {
66
+ dispatch({ type: 'CLOSE_TAB', tabId: pendingCloseTabId });
67
+ setPendingCloseTabId(null);
68
+ }
69
+ }, [pendingCloseTabId, dispatch]);
70
+ const cancelClose = useCallback(() => setPendingCloseTabId(null), []);
71
+ const confirmCloseOthers = useCallback(() => {
72
+ if (pendingCloseOthersTabId) {
73
+ dispatch({ type: 'CLOSE_OTHER_TABS', tabId: pendingCloseOthersTabId });
74
+ setPendingCloseOthersTabId(null);
75
+ }
76
+ }, [pendingCloseOthersTabId, dispatch]);
77
+ const cancelCloseOthers = useCallback(() => setPendingCloseOthersTabId(null), []);
78
+ const handleDragStart = useCallback((tabIdx: number, tabId: string) => {
79
+ dragTabRef.current = tabIdx;
80
+ dragTabIdRef.current = tabId;
81
+ }, [dragTabIdRef]);
82
+ const handleDragOverTab = useCallback((e: React.DragEvent, toIdx: number) => {
83
+ if (dragTabRef.current === null) {
84
+ return;
85
+ }
86
+ e.preventDefault();
87
+ e.dataTransfer.dropEffect = 'move';
88
+ const rect = e.currentTarget.getBoundingClientRect();
89
+ setDropIndicatorIdx(getDropIndicatorIndex(dragTabRef.current, toIdx, rect, e.clientX));
90
+ }, []);
91
+ const handleDragDrop = useCallback((fromRef: React.MutableRefObject<number | null>, toIdx: number) => {
92
+ if (fromRef.current !== null && fromRef.current !== toIdx) {
93
+ dispatch({ type: "REORDER_TABS", fromIdx: fromRef.current, toIdx });
94
+ }
95
+ fromRef.current = null;
96
+ dragTabIdRef.current = null;
97
+ setDropIndicatorIdx(null);
98
+ }, [dispatch, dragTabIdRef]);
99
+ const handleDragEnd = useCallback(() => {
100
+ dragTabRef.current = null;
101
+ dragTabIdRef.current = null;
102
+ onSetEditorDropTarget(null);
103
+ setDropIndicatorIdx(null);
104
+ }, [dragTabIdRef, onSetEditorDropTarget]);
105
+ const handleAddTab = useCallback(() => dispatch({ type: "ADD_TAB" }), [dispatch]);
106
+ const handleSplitVertical = useCallback(() => dispatch({ type: 'TOGGLE_SPLIT', direction: 'vertical' }), [dispatch]);
107
+ const handleSplitHorizontal = useCallback(() => dispatch({ type: 'TOGGLE_SPLIT', direction: 'horizontal' }), [dispatch]);
108
+
109
+ const handleContextRename = useCallback(() => {
110
+ const tab = tabs.find((t) => t.id === contextMenu?.tabId);
111
+ if (tab) {
112
+ startRename(tab.id, tab.title);
113
+ }
114
+ setContextMenu(null);
115
+ }, [tabs, contextMenu?.tabId, startRename]);
116
+
117
+ const handleContextDuplicate = useCallback(() => {
118
+ if (contextMenu) {
119
+ dispatch({ type: 'DUPLICATE_TAB', tabId: contextMenu.tabId });
120
+ }
121
+ setContextMenu(null);
122
+ }, [contextMenu, dispatch]);
123
+
124
+ const handleContextClose = useCallback(() => {
125
+ const tabId = contextMenu?.tabId;
126
+ setContextMenu(null);
127
+ if (tabId) {
128
+ handleClose(tabId);
129
+ }
130
+ }, [contextMenu?.tabId, handleClose]);
131
+
132
+ const handleContextCloseOthers = useCallback(() => {
133
+ const tabId = contextMenu?.tabId;
134
+ setContextMenu(null);
135
+ if (!tabId) {
136
+ return;
137
+ }
138
+ if (shouldConfirmCloseOthers(tabs, tabId)) {
139
+ setPendingCloseOthersTabId(tabId);
140
+ } else {
141
+ dispatch({ type: 'CLOSE_OTHER_TABS', tabId });
142
+ }
143
+ }, [contextMenu?.tabId, tabs, dispatch]);
144
+
145
+ const handleMenuOpenChange = useCallback((_e: unknown, data: { open: boolean }) => {
146
+ if (!data.open) {
147
+ setContextMenu(null);
148
+ }
149
+ }, []);
150
+
151
+ const handleCloseDialogOpenChange = useCallback((_e: unknown, data: { open: boolean }) => {
152
+ if (!data.open) {
153
+ cancelClose();
154
+ }
155
+ }, [cancelClose]);
156
+
157
+ const handleCloseOthersDialogOpenChange = useCallback((_e: unknown, data: { open: boolean }) => {
158
+ if (!data.open) {
159
+ cancelCloseOthers();
160
+ }
161
+ }, [cancelCloseOthers]);
162
+
163
+ const contextMenuPositioning = useMemo(() => {
164
+ if (!contextMenu) {
165
+ return undefined;
166
+ }
167
+ return { target: { getBoundingClientRect: () => ({ x: contextMenu.x, y: contextMenu.y, top: contextMenu.y, left: contextMenu.x, bottom: contextMenu.y, right: contextMenu.x, width: 0, height: 0, toJSON: () => ({}) }) } };
168
+ }, [contextMenu]);
169
+
170
+ const tabListRef = useRef<HTMLDivElement>(null);
171
+ const handleTabListWheel = useCallback((e: React.WheelEvent) => {
172
+ if (tabListRef.current && e.deltaY !== 0) {
173
+ tabListRef.current.scrollLeft += e.deltaY;
174
+ e.preventDefault();
175
+ }
176
+ }, []);
177
+
178
+ return (
179
+ <>
180
+ <div className={styles.queryTabBar}>
181
+ <div className={styles.queryTabList} ref={tabListRef} onWheel={handleTabListWheel}>
182
+ {tabs.map((t, idx) => {
183
+ const isPrimary = t.id === activeTabId;
184
+ const isSecondary = splitEnabled && t.id === splitTabId;
185
+ const isActive = (focusedPane === 'primary' && isPrimary) || (focusedPane === 'secondary' && isSecondary);
186
+ const connColor = connections.find((c) => c.id === t.connectionId)?.color ?? connections[0]?.color ?? '#636363';
187
+ return (
188
+ <React.Fragment key={t.id}>
189
+ {dropIndicatorIdx === idx && <div className={styles.tabDropIndicator} />}
190
+ <TabItem
191
+ tabId={t.id} title={t.title} idx={idx}
192
+ isActive={isActive} isPrimary={isPrimary} isSecondary={isSecondary}
193
+ isRenaming={renamingTabId === t.id} renameValue={renameValue}
194
+ tabCount={tabs.length} splitEnabled={splitEnabled}
195
+ connectionColor={connColor}
196
+ activeClassName={styles.queryTabActive} inactiveClassName={styles.queryTab}
197
+ closeClassName={styles.queryTabClose}
198
+ splitBadgeClassName={styles.splitBadge}
199
+ splitDirection={splitDirection}
200
+ onSwitch={handleSwitch} onStartRename={startRename}
201
+ onContextMenu={handleContextMenuOpen}
202
+ onRenameChange={setRenameValue} onCommitRename={commitRename}
203
+ onCancelRename={cancelRename} onClose={handleClose}
204
+ onDragStart={handleDragStart} onDragOverTab={handleDragOverTab}
205
+ onDragDrop={handleDragDrop}
206
+ onDragEnd={handleDragEnd} dragTabRef={dragTabRef}
207
+ />
208
+ </React.Fragment>
209
+ );
210
+ })}
211
+ {dropIndicatorIdx === tabs.length && <div className={styles.tabDropIndicator} />}
212
+ </div>
213
+ <Tooltip content="New tab" relationship="label" positioning="below">
214
+ <Button appearance="subtle" icon={addIcon} size="small"
215
+ onClick={handleAddTab} style={addTabButtonStyle} />
216
+ </Tooltip>
217
+ <div className={styles.queryTabActions}>
218
+ <Tooltip content="Split vertical" relationship="label" positioning="below">
219
+ <Button appearance="subtle" icon={splitVerticalIcon}
220
+ className={splitEnabled && splitDirection === 'vertical' ? styles.activeFilter : undefined}
221
+ onClick={handleSplitVertical} style={minWidthAutoStyle} />
222
+ </Tooltip>
223
+ <Tooltip content="Split horizontal" relationship="label" positioning="below">
224
+ <Button appearance="subtle" icon={splitHorizontalIcon}
225
+ className={splitEnabled && splitDirection === 'horizontal' ? styles.activeFilter : undefined}
226
+ onClick={handleSplitHorizontal} style={minWidthAutoStyle} />
227
+ </Tooltip>
228
+ </div>
229
+ </div>
230
+
231
+ {contextMenu && (
232
+ <Menu open positioning={contextMenuPositioning} onOpenChange={handleMenuOpenChange}>
233
+ <MenuPopover>
234
+ <MenuList>
235
+ <MenuItem icon={renameIcon} onClick={handleContextRename}>Rename</MenuItem>
236
+ <MenuItem icon={copyIcon} onClick={handleContextDuplicate}>Duplicate</MenuItem>
237
+ {tabs.length > 1 && <MenuDivider />}
238
+ {tabs.length > 1 && (
239
+ <MenuItem icon={dismissMenuIcon} onClick={handleContextClose}>Close</MenuItem>
240
+ )}
241
+ {tabs.length > 1 && (
242
+ <MenuItem icon={dismissCircleIcon} onClick={handleContextCloseOthers}>Close Others</MenuItem>
243
+ )}
244
+ </MenuList>
245
+ </MenuPopover>
246
+ </Menu>
247
+ )}
248
+
249
+ <Dialog open={pendingCloseTabId !== null} onOpenChange={handleCloseDialogOpenChange}>
250
+ <DialogSurface>
251
+ <DialogBody>
252
+ <DialogTitle>Close tab?</DialogTitle>
253
+ <DialogContent>
254
+ This tab has unsaved content that will be lost. Are you sure you want to close it?
255
+ </DialogContent>
256
+ <DialogActions>
257
+ <DialogTrigger disableButtonEnhancement>
258
+ <Button appearance="secondary" onClick={cancelClose}>Cancel</Button>
259
+ </DialogTrigger>
260
+ <Button appearance="primary" onClick={confirmClose}>Close</Button>
261
+ </DialogActions>
262
+ </DialogBody>
263
+ </DialogSurface>
264
+ </Dialog>
265
+
266
+ <Dialog open={pendingCloseOthersTabId !== null} onOpenChange={handleCloseOthersDialogOpenChange}>
267
+ <DialogSurface>
268
+ <DialogBody>
269
+ <DialogTitle>Close other tabs?</DialogTitle>
270
+ <DialogContent>
271
+ Some tabs have unsaved content that will be lost. Are you sure you want to close them?
272
+ </DialogContent>
273
+ <DialogActions>
274
+ <DialogTrigger disableButtonEnhancement>
275
+ <Button appearance="secondary" onClick={cancelCloseOthers}>Cancel</Button>
276
+ </DialogTrigger>
277
+ <Button appearance="primary" onClick={confirmCloseOthers}>Close Others</Button>
278
+ </DialogActions>
279
+ </DialogBody>
280
+ </DialogSurface>
281
+ </Dialog>
282
+ </>
283
+ );
284
+ }
@@ -0,0 +1,166 @@
1
+ import { useCallback, useEffect, useRef } from 'react';
2
+ import { DismissRegular } from '@fluentui/react-icons';
3
+ import { tokens } from '@fluentui/react-components';
4
+
5
+ const connectionDotStyle = (color: string) => ({ display: 'inline-block', width: '8px', height: '8px', borderRadius: '50%', backgroundColor: color, flexShrink: 0, marginRight: '4px' }) as const;
6
+ const splitBadgePrimaryStyle = { backgroundColor: tokens.colorBrandBackground, color: '#fff' } as const;
7
+ const splitBadgeSecondaryStyle = { backgroundColor: tokens.colorPaletteGreenBackground3, color: '#fff' } as const;
8
+
9
+ const renameInputStyle = {
10
+ fontSize: '12px',
11
+ border: 'none',
12
+ background: 'transparent',
13
+ color: 'inherit',
14
+ outline: `1px solid ${tokens.colorBrandStroke1}`,
15
+ borderRadius: '2px',
16
+ padding: '0 2px',
17
+ fontFamily: 'inherit',
18
+ lineHeight: 'inherit',
19
+ minWidth: '40px',
20
+ } as const;
21
+
22
+ export interface TabItemProps {
23
+ tabId: string;
24
+ title: string;
25
+ isActive: boolean;
26
+ isPrimary: boolean;
27
+ isSecondary: boolean;
28
+ isRenaming: boolean;
29
+ renameValue: string;
30
+ tabCount: number;
31
+ idx: number;
32
+ connectionColor: string;
33
+ activeClassName: string;
34
+ inactiveClassName: string;
35
+ closeClassName: string;
36
+ splitBadgeClassName: string;
37
+ splitDirection: 'vertical' | 'horizontal';
38
+ onSwitch: (tabId: string) => void;
39
+ onStartRename: (tabId: string, title: string) => void;
40
+ onContextMenu: (tabId: string, x: number, y: number) => void;
41
+ onRenameChange: (value: string) => void;
42
+ onCommitRename: () => void;
43
+ onCancelRename: () => void;
44
+ onClose: (tabId: string) => void;
45
+ onDragStart: (idx: number, tabId: string) => void;
46
+ onDragOverTab: (e: React.DragEvent, toIdx: number) => void;
47
+ onDragDrop: (fromRef: React.MutableRefObject<number | null>, toIdx: number) => void;
48
+ onDragEnd: () => void;
49
+ dragTabRef: React.MutableRefObject<number | null>;
50
+ splitEnabled: boolean;
51
+ }
52
+
53
+ export function TabItem({
54
+ tabId, title, isActive, isPrimary, isSecondary, isRenaming, renameValue,
55
+ tabCount, idx, connectionColor, activeClassName, inactiveClassName, closeClassName,
56
+ splitBadgeClassName, splitDirection,
57
+ onSwitch, onStartRename, onContextMenu, onRenameChange, onCommitRename, onCancelRename,
58
+ onClose, onDragStart, onDragOverTab, onDragDrop, onDragEnd, dragTabRef, splitEnabled,
59
+ }: TabItemProps) {
60
+ const handleClick = useCallback(() => onSwitch(tabId), [onSwitch, tabId]);
61
+ const handleDoubleClick = useCallback(() => onStartRename(tabId, title), [onStartRename, tabId, title]);
62
+ const handleMouseDown = useCallback((e: React.MouseEvent) => {
63
+ if (e.button === 1 && tabCount > 1) {
64
+ e.preventDefault();
65
+ onClose(tabId);
66
+ }
67
+ }, [onClose, tabId, tabCount]);
68
+ const handleContextMenu = useCallback((e: React.MouseEvent) => {
69
+ e.preventDefault();
70
+ onContextMenu(tabId, e.clientX, e.clientY);
71
+ }, [onContextMenu, tabId]);
72
+ const handleDragStartEvt = useCallback((e: React.DragEvent) => {
73
+ e.dataTransfer.setData('text/plain', tabId);
74
+ e.dataTransfer.effectAllowed = 'move';
75
+ onDragStart(idx, tabId);
76
+ }, [onDragStart, idx, tabId]);
77
+ const handleDragOver = useCallback((e: React.DragEvent) => {
78
+ onDragOverTab(e, idx);
79
+ }, [onDragOverTab, idx]);
80
+ const handleDrop = useCallback(() => {
81
+ onDragDrop(dragTabRef, idx);
82
+ }, [onDragDrop, dragTabRef, idx]);
83
+ const handleInputChange = useCallback((e: React.ChangeEvent<HTMLInputElement>) => onRenameChange(e.target.value), [onRenameChange]);
84
+ const handleInputKeyDown = useCallback((e: React.KeyboardEvent) => {
85
+ if (e.key === 'Enter') {
86
+ onCommitRename();
87
+ }
88
+ if (e.key === 'Escape') {
89
+ onCancelRename();
90
+ }
91
+ }, [onCommitRename, onCancelRename]);
92
+ const handleInputClick = useCallback((e: React.MouseEvent) => e.stopPropagation(), []);
93
+ const handleCloseClick = useCallback((e: React.MouseEvent) => {
94
+ e.stopPropagation();
95
+ onClose(tabId);
96
+ }, [onClose, tabId]);
97
+ const handleKeyDown = useCallback((e: React.KeyboardEvent) => {
98
+ if (e.key === 'Enter' || e.key === ' ') {
99
+ handleClick();
100
+ }
101
+ }, [handleClick]);
102
+ const handleCloseKeyDown = useCallback((e: React.KeyboardEvent) => {
103
+ if (e.key === 'Enter' || e.key === ' ') {
104
+ e.stopPropagation();
105
+ onClose(tabId);
106
+ }
107
+ }, [onClose, tabId]);
108
+
109
+ const inputRef = useRef<HTMLInputElement>(null);
110
+ useEffect(() => {
111
+ if (isRenaming && inputRef.current) {
112
+ inputRef.current.focus();
113
+ inputRef.current.select();
114
+ }
115
+ }, [isRenaming]);
116
+
117
+ return (
118
+ <div
119
+ className={isActive ? activeClassName : inactiveClassName}
120
+ role="tab" aria-selected={isActive}
121
+ data-context-menu
122
+ draggable={!isRenaming}
123
+ tabIndex={0}
124
+ onClick={handleClick}
125
+ onKeyDown={handleKeyDown}
126
+ onMouseDown={handleMouseDown}
127
+ onDoubleClick={handleDoubleClick}
128
+ onContextMenu={handleContextMenu}
129
+ onDragStart={handleDragStartEvt}
130
+ onDragOver={handleDragOver}
131
+ onDrop={handleDrop}
132
+ onDragEnd={onDragEnd}>
133
+ {isRenaming ? (
134
+ <input
135
+ ref={inputRef}
136
+ value={renameValue}
137
+ size={Math.max(1, renameValue.length)}
138
+ onChange={handleInputChange}
139
+ onBlur={onCommitRename}
140
+ onKeyDown={handleInputKeyDown}
141
+ onClick={handleInputClick}
142
+ style={renameInputStyle}
143
+ />
144
+ ) : (
145
+ <>
146
+ <span><span style={connectionDotStyle(connectionColor)} />{title}</span>
147
+ {isPrimary && splitEnabled && (
148
+ <span className={splitBadgeClassName} style={splitBadgePrimaryStyle}>
149
+ {splitDirection === 'vertical' ? 'L' : 'T'}
150
+ </span>
151
+ )}
152
+ {isSecondary && (
153
+ <span className={splitBadgeClassName} style={splitBadgeSecondaryStyle}>
154
+ {splitDirection === 'vertical' ? 'R' : 'B'}
155
+ </span>
156
+ )}
157
+ </>
158
+ )}
159
+ {!isRenaming && tabCount > 1 && (
160
+ <span className={`${closeClassName} tab-close`} role="menuitem" tabIndex={0} onClick={handleCloseClick} onKeyDown={handleCloseKeyDown}>
161
+ <DismissRegular fontSize={10} />
162
+ </span>
163
+ )}
164
+ </div>
165
+ );
166
+ }
@@ -0,0 +1,126 @@
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
+
6
+ import { VegaChart } from './VegaChart';
7
+ import type { VisualizationSpec } from './VegaChart';
8
+
9
+ // Polyfill ResizeObserver for jsdom — fires callback immediately with mock dimensions
10
+ let resizeCallback: ((entries: { contentRect: { width: number } }[]) => void) | null = null;
11
+ class MockResizeObserver {
12
+ constructor(cb: (entries: { contentRect: { width: number } }[]) => void) {
13
+ resizeCallback = cb;
14
+ }
15
+ observe = vi.fn().mockImplementation(() => {
16
+ // Fire with a realistic width
17
+ resizeCallback?.([{ contentRect: { width: 600 } }]);
18
+ });
19
+ unobserve = vi.fn();
20
+ disconnect = vi.fn();
21
+ }
22
+ vi.stubGlobal('ResizeObserver', MockResizeObserver);
23
+
24
+ let container: HTMLDivElement;
25
+ let root: ReturnType<typeof createRoot>;
26
+
27
+ beforeEach(() => {
28
+ vi.spyOn(console, 'error').mockImplementation(() => {});
29
+ vi.spyOn(console, 'warn').mockImplementation(() => {});
30
+ container = document.createElement('div');
31
+ document.body.appendChild(container);
32
+ root = createRoot(container);
33
+ });
34
+
35
+ afterEach(() => {
36
+ act(() => { root.unmount(); });
37
+ container.remove();
38
+ vi.restoreAllMocks();
39
+ });
40
+
41
+ const baseSpec: VisualizationSpec = {
42
+ mark: 'bar',
43
+ encoding: {
44
+ x: { field: 'category', type: 'nominal' },
45
+ y: { field: 'value', type: 'quantitative' },
46
+ },
47
+ } as VisualizationSpec;
48
+
49
+ const baseData = { values: [{ category: 'A', value: 10 }, { category: 'B', value: 20 }] };
50
+
51
+ describe('VegaChart', () => {
52
+ it('renders a container div', () => {
53
+ act(() => {
54
+ root.render(React.createElement(VegaChart, {
55
+ spec: baseSpec, data: baseData, isDark: false,
56
+ }));
57
+ });
58
+ expect(container.querySelector('div')).toBeTruthy();
59
+ });
60
+
61
+ it('renders with isDark=true', () => {
62
+ act(() => {
63
+ root.render(React.createElement(VegaChart, {
64
+ spec: baseSpec, data: baseData, isDark: true,
65
+ }));
66
+ });
67
+ expect(container.querySelector('div')).toBeTruthy();
68
+ });
69
+
70
+ it('renders with custom height', () => {
71
+ act(() => {
72
+ root.render(React.createElement(VegaChart, {
73
+ spec: baseSpec, data: baseData, isDark: false, height: 400,
74
+ }));
75
+ });
76
+ const div = container.querySelector('div');
77
+ expect(div?.style.minHeight).toBe('400px');
78
+ });
79
+
80
+ it('renders with custom palette', () => {
81
+ act(() => {
82
+ root.render(React.createElement(VegaChart, {
83
+ spec: baseSpec, data: baseData, isDark: false,
84
+ palette: ['#ff0000', '#00ff00', '#0000ff'],
85
+ }));
86
+ });
87
+ expect(container.querySelector('div')).toBeTruthy();
88
+ });
89
+
90
+ it('renders with onClick handler', () => {
91
+ const onClick = vi.fn();
92
+ act(() => {
93
+ root.render(React.createElement(VegaChart, {
94
+ spec: baseSpec, data: baseData, isDark: false, onClick,
95
+ }));
96
+ });
97
+ expect(container.querySelector('div')).toBeTruthy();
98
+ });
99
+
100
+ it('shows error message on render failure', async () => {
101
+ // Pass an invalid spec to trigger a render error
102
+ const badSpec = { mark: 'invalid_mark_type' } as unknown as VisualizationSpec;
103
+ act(() => {
104
+ root.render(React.createElement(VegaChart, {
105
+ spec: badSpec, data: baseData, isDark: false,
106
+ }));
107
+ });
108
+
109
+ // The async vega import + compile may fail — wait for error state
110
+ await act(async () => {
111
+ await new Promise((r) => setTimeout(r, 100));
112
+ });
113
+
114
+ // Either renders a chart div or shows error message
115
+ expect(container.querySelector('div')).toBeTruthy();
116
+ });
117
+
118
+ it('renders with empty data', () => {
119
+ act(() => {
120
+ root.render(React.createElement(VegaChart, {
121
+ spec: baseSpec, data: { values: [] }, isDark: false,
122
+ }));
123
+ });
124
+ expect(container.querySelector('div')).toBeTruthy();
125
+ });
126
+ });