@grackle-ai/web-components 0.107.2
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/.rush/temp/3ae72563f781afd72723475938136f113846603e.untar.log +10 -0
- package/.rush/temp/bc1d5bf9201ce71abeaeaddd096deb9b0805d703.untar.log +10 -0
- package/.rush/temp/operation/_phase_build/all.log +18 -0
- package/.rush/temp/operation/_phase_build/log-chunks.jsonl +18 -0
- package/.rush/temp/operation/_phase_build/state.json +3 -0
- package/.rush/temp/operation/_phase_test/all.log +121 -0
- package/.rush/temp/operation/_phase_test/log-chunks.jsonl +121 -0
- package/.rush/temp/operation/_phase_test/state.json +3 -0
- package/.rush/temp/shrinkwrap-deps.json +938 -0
- package/.storybook/main.ts +22 -0
- package/.storybook/preview.tsx +30 -0
- package/config/rig.json +4 -0
- package/config/rush-project.json +12 -0
- package/dist/index.css +1 -0
- package/dist/index.js +39221 -0
- package/eslint.config.cjs +5 -0
- package/package.json +83 -0
- package/rush-logs/web-components._phase_build.cache.log +4 -0
- package/rush-logs/web-components._phase_test.cache.log +4 -0
- package/src/components/chat/ChatInput.module.scss +81 -0
- package/src/components/chat/ChatInput.stories.tsx +91 -0
- package/src/components/chat/ChatInput.tsx +168 -0
- package/src/components/chat/index.ts +6 -0
- package/src/components/dag/DagView.module.scss +149 -0
- package/src/components/dag/DagView.stories.tsx +125 -0
- package/src/components/dag/DagView.tsx +109 -0
- package/src/components/dag/TaskNode.stories.tsx +133 -0
- package/src/components/dag/TaskNode.tsx +40 -0
- package/src/components/dag/useDagLayout.ts +139 -0
- package/src/components/display/Breadcrumbs.module.scss +71 -0
- package/src/components/display/Breadcrumbs.stories.tsx +80 -0
- package/src/components/display/Breadcrumbs.tsx +46 -0
- package/src/components/display/Button.module.scss +110 -0
- package/src/components/display/Button.stories.tsx +88 -0
- package/src/components/display/Button.tsx +40 -0
- package/src/components/display/ConfirmDialog.module.scss +67 -0
- package/src/components/display/ConfirmDialog.stories.tsx +81 -0
- package/src/components/display/ConfirmDialog.tsx +88 -0
- package/src/components/display/CopyButton.module.scss +41 -0
- package/src/components/display/CopyButton.stories.tsx +78 -0
- package/src/components/display/CopyButton.tsx +64 -0
- package/src/components/display/DemoBanner.module.scss +37 -0
- package/src/components/display/DemoBanner.stories.tsx +40 -0
- package/src/components/display/DemoBanner.tsx +23 -0
- package/src/components/display/EventHoverRow.module.scss +102 -0
- package/src/components/display/EventHoverRow.stories.tsx +99 -0
- package/src/components/display/EventHoverRow.tsx +154 -0
- package/src/components/display/EventRenderer.module.scss +272 -0
- package/src/components/display/EventRenderer.stories.tsx +186 -0
- package/src/components/display/EventRenderer.tsx +271 -0
- package/src/components/display/EventStream.module.scss +93 -0
- package/src/components/display/EventStream.stories.tsx +249 -0
- package/src/components/display/EventStream.tsx +369 -0
- package/src/components/display/FloatingActionBar.module.scss +107 -0
- package/src/components/display/FloatingActionBar.stories.tsx +122 -0
- package/src/components/display/FloatingActionBar.tsx +119 -0
- package/src/components/display/SessionAttemptSelector.module.scss +50 -0
- package/src/components/display/SessionAttemptSelector.stories.tsx +78 -0
- package/src/components/display/SessionAttemptSelector.tsx +49 -0
- package/src/components/display/SessionPicker.module.scss +200 -0
- package/src/components/display/SessionPicker.stories.tsx +169 -0
- package/src/components/display/SessionPicker.tsx +214 -0
- package/src/components/display/Skeleton.module.scss +58 -0
- package/src/components/display/Skeleton.stories.tsx +94 -0
- package/src/components/display/Skeleton.tsx +127 -0
- package/src/components/display/Spinner.module.scss +41 -0
- package/src/components/display/Spinner.stories.tsx +66 -0
- package/src/components/display/Spinner.tsx +32 -0
- package/src/components/display/SplashScreen.module.scss +20 -0
- package/src/components/display/SplashScreen.stories.tsx +26 -0
- package/src/components/display/SplashScreen.tsx +16 -0
- package/src/components/display/SplitButton.module.scss +166 -0
- package/src/components/display/SplitButton.stories.tsx +95 -0
- package/src/components/display/SplitButton.tsx +128 -0
- package/src/components/display/Tooltip.module.scss +84 -0
- package/src/components/display/Tooltip.stories.tsx +240 -0
- package/src/components/display/Tooltip.tsx +184 -0
- package/src/components/display/extractText.test.tsx +48 -0
- package/src/components/display/index.ts +20 -0
- package/src/components/editable/EditableCheckbox.stories.tsx +54 -0
- package/src/components/editable/EditableCheckbox.tsx +39 -0
- package/src/components/editable/EditableField.module.scss +135 -0
- package/src/components/editable/EditableSelect.tsx +164 -0
- package/src/components/editable/EditableTextArea.stories.tsx +50 -0
- package/src/components/editable/EditableTextArea.tsx +148 -0
- package/src/components/editable/EditableTextField.stories.tsx +62 -0
- package/src/components/editable/EditableTextField.tsx +153 -0
- package/src/components/editable/EnvironmentSelect.module.scss +17 -0
- package/src/components/editable/EnvironmentSelect.stories.tsx +61 -0
- package/src/components/editable/EnvironmentSelect.tsx +87 -0
- package/src/components/editable/index.ts +13 -0
- package/src/components/editable/useEditableField.test.tsx +233 -0
- package/src/components/editable/useEditableField.ts +173 -0
- package/src/components/index.ts +20 -0
- package/src/components/knowledge/KnowledgeDetailPanel.module.scss +162 -0
- package/src/components/knowledge/KnowledgeDetailPanel.stories.tsx +208 -0
- package/src/components/knowledge/KnowledgeDetailPanel.tsx +122 -0
- package/src/components/knowledge/KnowledgeGraph.module.scss +110 -0
- package/src/components/knowledge/KnowledgeGraph.stories.tsx +180 -0
- package/src/components/knowledge/KnowledgeGraph.tsx +455 -0
- package/src/components/knowledge/KnowledgeNav.module.scss +130 -0
- package/src/components/knowledge/KnowledgeNav.stories.tsx +108 -0
- package/src/components/knowledge/KnowledgeNav.tsx +138 -0
- package/src/components/knowledge/index.ts +3 -0
- package/src/components/layout/AppNav.module.scss +82 -0
- package/src/components/layout/AppNav.stories.tsx +115 -0
- package/src/components/layout/AppNav.tsx +133 -0
- package/src/components/layout/BottomStatusBar.module.scss +58 -0
- package/src/components/layout/BottomStatusBar.stories.tsx +35 -0
- package/src/components/layout/BottomStatusBar.tsx +206 -0
- package/src/components/layout/Sidebar.module.scss +60 -0
- package/src/components/layout/Sidebar.stories.tsx +46 -0
- package/src/components/layout/Sidebar.tsx +84 -0
- package/src/components/layout/StatusBar.module.scss +108 -0
- package/src/components/layout/StatusBar.stories.tsx +119 -0
- package/src/components/layout/StatusBar.tsx +70 -0
- package/src/components/layout/index.ts +9 -0
- package/src/components/lists/EnvironmentNav.module.scss +118 -0
- package/src/components/lists/EnvironmentNav.stories.tsx +121 -0
- package/src/components/lists/EnvironmentNav.tsx +133 -0
- package/src/components/lists/FindingsNav.module.scss +126 -0
- package/src/components/lists/FindingsNav.tsx +146 -0
- package/src/components/lists/TaskList.module.scss +206 -0
- package/src/components/lists/TaskList.stories.tsx +401 -0
- package/src/components/lists/TaskList.tsx +509 -0
- package/src/components/lists/index.ts +6 -0
- package/src/components/lists/listHelpers.tsx +130 -0
- package/src/components/notifications/Callout.module.scss +83 -0
- package/src/components/notifications/Callout.stories.tsx +81 -0
- package/src/components/notifications/Callout.tsx +64 -0
- package/src/components/notifications/Toast.module.scss +86 -0
- package/src/components/notifications/Toast.stories.tsx +71 -0
- package/src/components/notifications/Toast.tsx +51 -0
- package/src/components/notifications/ToastContainer.module.scss +23 -0
- package/src/components/notifications/ToastContainer.stories.tsx +66 -0
- package/src/components/notifications/ToastContainer.tsx +29 -0
- package/src/components/notifications/UpdateBanner.stories.tsx +77 -0
- package/src/components/notifications/UpdateBanner.test.tsx +64 -0
- package/src/components/notifications/UpdateBanner.tsx +44 -0
- package/src/components/notifications/index.ts +8 -0
- package/src/components/panels/AboutPanel.stories.tsx +70 -0
- package/src/components/panels/AboutPanel.tsx +66 -0
- package/src/components/panels/AppearancePanel.stories.tsx +45 -0
- package/src/components/panels/AppearancePanel.tsx +97 -0
- package/src/components/panels/CredentialProvidersPanel.stories.tsx +62 -0
- package/src/components/panels/CredentialProvidersPanel.tsx +111 -0
- package/src/components/panels/EnvironmentEditPanel.module.scss +170 -0
- package/src/components/panels/EnvironmentEditPanel.stories.tsx +206 -0
- package/src/components/panels/EnvironmentEditPanel.tsx +785 -0
- package/src/components/panels/FindingsPanel.module.scss +94 -0
- package/src/components/panels/FindingsPanel.stories.tsx +109 -0
- package/src/components/panels/FindingsPanel.tsx +76 -0
- package/src/components/panels/KeyboardShortcutsPanel.module.scss +65 -0
- package/src/components/panels/KeyboardShortcutsPanel.stories.tsx +40 -0
- package/src/components/panels/KeyboardShortcutsPanel.tsx +104 -0
- package/src/components/panels/PluginsPanel.tsx +77 -0
- package/src/components/panels/SettingsPanel.module.scss +336 -0
- package/src/components/panels/TaskActionButtons.module.scss +22 -0
- package/src/components/panels/TaskActionButtons.stories.tsx +125 -0
- package/src/components/panels/TaskActionButtons.tsx +87 -0
- package/src/components/panels/TaskEditPanel.module.scss +202 -0
- package/src/components/panels/TaskEditPanel.stories.tsx +75 -0
- package/src/components/panels/TaskEditPanel.tsx +328 -0
- package/src/components/panels/TaskOverviewPanel.module.scss +236 -0
- package/src/components/panels/TaskOverviewPanel.stories.tsx +219 -0
- package/src/components/panels/TaskOverviewPanel.tsx +270 -0
- package/src/components/panels/TokensPanel.stories.tsx +131 -0
- package/src/components/panels/TokensPanel.tsx +143 -0
- package/src/components/panels/WorkpadPanel.module.scss +39 -0
- package/src/components/panels/WorkpadPanel.stories.tsx +56 -0
- package/src/components/panels/WorkpadPanel.tsx +63 -0
- package/src/components/panels/index.ts +13 -0
- package/src/components/personas/McpToolSelector.module.scss +109 -0
- package/src/components/personas/McpToolSelector.stories.tsx +129 -0
- package/src/components/personas/McpToolSelector.tsx +180 -0
- package/src/components/personas/PersonaManager.module.scss +233 -0
- package/src/components/personas/PersonaManager.stories.tsx +139 -0
- package/src/components/personas/PersonaManager.tsx +122 -0
- package/src/components/schedules/ScheduleManager.module.scss +98 -0
- package/src/components/schedules/ScheduleManager.stories.tsx +78 -0
- package/src/components/schedules/ScheduleManager.tsx +160 -0
- package/src/components/settings/SettingsNav.module.scss +82 -0
- package/src/components/settings/SettingsNav.stories.tsx +83 -0
- package/src/components/settings/SettingsNav.tsx +104 -0
- package/src/components/streams/StreamDetailPanel.module.scss +206 -0
- package/src/components/streams/StreamDetailPanel.stories.tsx +132 -0
- package/src/components/streams/StreamDetailPanel.tsx +119 -0
- package/src/components/streams/StreamList.module.scss +92 -0
- package/src/components/streams/StreamList.stories.tsx +99 -0
- package/src/components/streams/StreamList.tsx +114 -0
- package/src/components/streams/index.ts +10 -0
- package/src/components/tools/AgentToolCard.module.scss +118 -0
- package/src/components/tools/AgentToolCard.stories.tsx +304 -0
- package/src/components/tools/AgentToolCard.tsx +247 -0
- package/src/components/tools/FileEditCard.stories.tsx +138 -0
- package/src/components/tools/FileEditCard.tsx +160 -0
- package/src/components/tools/FileReadCard.stories.tsx +120 -0
- package/src/components/tools/FileReadCard.tsx +106 -0
- package/src/components/tools/FindingCard.stories.tsx +124 -0
- package/src/components/tools/FindingCard.tsx +178 -0
- package/src/components/tools/GenericToolCard.stories.tsx +80 -0
- package/src/components/tools/GenericToolCard.tsx +111 -0
- package/src/components/tools/IpcCard.stories.tsx +129 -0
- package/src/components/tools/IpcCard.tsx +178 -0
- package/src/components/tools/KnowledgeCard.stories.tsx +112 -0
- package/src/components/tools/KnowledgeCard.tsx +165 -0
- package/src/components/tools/MetadataCard.stories.tsx +32 -0
- package/src/components/tools/MetadataCard.tsx +39 -0
- package/src/components/tools/SearchCard.stories.tsx +74 -0
- package/src/components/tools/SearchCard.tsx +86 -0
- package/src/components/tools/ShellCard.stories.tsx +112 -0
- package/src/components/tools/ShellCard.tsx +106 -0
- package/src/components/tools/TaskCard.stories.tsx +123 -0
- package/src/components/tools/TaskCard.tsx +203 -0
- package/src/components/tools/TodoCard.module.scss +131 -0
- package/src/components/tools/TodoCard.stories.tsx +202 -0
- package/src/components/tools/TodoCard.tsx +200 -0
- package/src/components/tools/ToolCard.stories.tsx +177 -0
- package/src/components/tools/ToolCard.tsx +60 -0
- package/src/components/tools/ToolCardProps.ts +20 -0
- package/src/components/tools/ToolSearchCard.stories.tsx +81 -0
- package/src/components/tools/ToolSearchCard.tsx +86 -0
- package/src/components/tools/WorkpadCard.stories.tsx +106 -0
- package/src/components/tools/WorkpadCard.tsx +125 -0
- package/src/components/tools/classifyTool.test.ts +44 -0
- package/src/components/tools/classifyTool.ts +134 -0
- package/src/components/tools/parseDiff.ts +95 -0
- package/src/components/tools/parseShellOutput.ts +28 -0
- package/src/components/tools/toolCardHelpers.test.ts +53 -0
- package/src/components/tools/toolCards.module.scss +234 -0
- package/src/components/workspace/WorkspaceBoard.module.scss +238 -0
- package/src/components/workspace/WorkspaceBoard.stories.tsx +240 -0
- package/src/components/workspace/WorkspaceBoard.tsx +232 -0
- package/src/components/workspace/WorkspaceFormFields.module.scss +79 -0
- package/src/components/workspace/WorkspaceFormFields.stories.tsx +133 -0
- package/src/components/workspace/WorkspaceFormFields.tsx +185 -0
- package/src/context/GrackleContext.ts +28 -0
- package/src/context/GrackleContextTypes.ts +64 -0
- package/src/context/SidebarContext.tsx +53 -0
- package/src/context/ThemeContext.tsx +21 -0
- package/src/context/ToastContext.tsx +56 -0
- package/src/hooks/types.ts +864 -0
- package/src/hooks/useEventSelection.test.ts +204 -0
- package/src/hooks/useEventSelection.ts +158 -0
- package/src/hooks/useSmartScroll.ts +151 -0
- package/src/hooks/useTheme.ts +228 -0
- package/src/index.ts +210 -0
- package/src/mocks/MockGrackleProvider.tsx +1397 -0
- package/src/mocks/mockData.ts +1966 -0
- package/src/mocks/mockKnowledgeData.ts +294 -0
- package/src/scss.d.ts +12 -0
- package/src/styles/global.scss +244 -0
- package/src/styles/mixins.scss +278 -0
- package/src/styles/prism-theme.scss +148 -0
- package/src/styles/theme.scss +1102 -0
- package/src/test-utils/storybook-decorators.tsx +50 -0
- package/src/test-utils/storybook-helpers.ts +262 -0
- package/src/themes.ts +142 -0
- package/src/utils/boardColumns.ts +141 -0
- package/src/utils/breadcrumbs.test.ts +285 -0
- package/src/utils/breadcrumbs.ts +222 -0
- package/src/utils/dashboard.test.ts +156 -0
- package/src/utils/dashboard.ts +195 -0
- package/src/utils/eventContent.test.ts +353 -0
- package/src/utils/eventContent.ts +209 -0
- package/src/utils/findingCategory.ts +33 -0
- package/src/utils/format.ts +27 -0
- package/src/utils/iconSize.ts +18 -0
- package/src/utils/navigation.ts +205 -0
- package/src/utils/route-config.test.ts +128 -0
- package/src/utils/scrollUtils.test.ts +65 -0
- package/src/utils/scrollUtils.ts +49 -0
- package/src/utils/sessionEvents.test.ts +302 -0
- package/src/utils/sessionEvents.ts +233 -0
- package/src/utils/taskStatus.tsx +137 -0
- package/src/utils/time.ts +92 -0
- package/tsconfig.json +8 -0
- package/vite.config.ts +20 -0
- package/vitest.config.ts +10 -0
|
@@ -0,0 +1,204 @@
|
|
|
1
|
+
// @vitest-environment jsdom
|
|
2
|
+
import { describe, it, expect, vi, beforeEach } from "vitest";
|
|
3
|
+
import { renderHook, act } from "@testing-library/react";
|
|
4
|
+
import { useEventSelection } from "./useEventSelection.js";
|
|
5
|
+
import type { DisplayEvent } from "../utils/sessionEvents.js";
|
|
6
|
+
|
|
7
|
+
/** Build a minimal DisplayEvent. */
|
|
8
|
+
function makeEvent(eventType: string, content: string = ""): DisplayEvent {
|
|
9
|
+
return {
|
|
10
|
+
sessionId: "sess-1",
|
|
11
|
+
eventType,
|
|
12
|
+
timestamp: "2026-01-15T14:34:00Z",
|
|
13
|
+
content,
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/** Sample events: 5 content-bearing + 1 status (non-content). */
|
|
18
|
+
const SAMPLE_EVENTS: DisplayEvent[] = [
|
|
19
|
+
makeEvent("user_input", "Hello"), // 0
|
|
20
|
+
makeEvent("text", "Response 1"), // 1
|
|
21
|
+
makeEvent("status", "running"), // 2 (non-content)
|
|
22
|
+
makeEvent("tool_result", "file contents"), // 3
|
|
23
|
+
makeEvent("text", "Response 2"), // 4
|
|
24
|
+
makeEvent("error", "Something broke"), // 5
|
|
25
|
+
];
|
|
26
|
+
|
|
27
|
+
const mockFormat = vi.fn((events: DisplayEvent[]) =>
|
|
28
|
+
events.map((e) => e.content).join("\n"),
|
|
29
|
+
);
|
|
30
|
+
|
|
31
|
+
function renderSelection(events: DisplayEvent[] = SAMPLE_EVENTS): ReturnType<typeof renderHook<ReturnType<typeof useEventSelection>, unknown>> {
|
|
32
|
+
return renderHook(() =>
|
|
33
|
+
useEventSelection({ events, formatForClipboard: mockFormat }),
|
|
34
|
+
);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
describe("useEventSelection", () => {
|
|
38
|
+
beforeEach(() => {
|
|
39
|
+
mockFormat.mockClear();
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
it("starts in non-selecting mode with empty selection", () => {
|
|
43
|
+
const { result } = renderSelection();
|
|
44
|
+
expect(result.current.isSelecting).toBe(false);
|
|
45
|
+
expect(result.current.selectedCount).toBe(0);
|
|
46
|
+
expect(result.current.selectedIndices.size).toBe(0);
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
it("enters selection mode with an initial index", () => {
|
|
50
|
+
const { result } = renderSelection();
|
|
51
|
+
act(() => { result.current.enterSelectionMode(1); });
|
|
52
|
+
expect(result.current.isSelecting).toBe(true);
|
|
53
|
+
expect(result.current.selectedCount).toBe(1);
|
|
54
|
+
expect(result.current.selectedIndices.has(1)).toBe(true);
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
it("enters selection mode without an initial index", () => {
|
|
58
|
+
const { result } = renderSelection();
|
|
59
|
+
act(() => { result.current.enterSelectionMode(); });
|
|
60
|
+
expect(result.current.isSelecting).toBe(true);
|
|
61
|
+
expect(result.current.selectedCount).toBe(0);
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
it("cancels selection mode and clears selection", () => {
|
|
65
|
+
const { result } = renderSelection();
|
|
66
|
+
act(() => { result.current.enterSelectionMode(0); });
|
|
67
|
+
act(() => { result.current.toggleEvent(1); });
|
|
68
|
+
expect(result.current.selectedCount).toBe(2);
|
|
69
|
+
|
|
70
|
+
act(() => { result.current.cancelSelection(); });
|
|
71
|
+
expect(result.current.isSelecting).toBe(false);
|
|
72
|
+
expect(result.current.selectedCount).toBe(0);
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
it("toggles individual events on and off", () => {
|
|
76
|
+
const { result } = renderSelection();
|
|
77
|
+
act(() => { result.current.enterSelectionMode(0); });
|
|
78
|
+
|
|
79
|
+
// Toggle on index 3
|
|
80
|
+
act(() => { result.current.toggleEvent(3); });
|
|
81
|
+
expect(result.current.selectedIndices.has(0)).toBe(true);
|
|
82
|
+
expect(result.current.selectedIndices.has(3)).toBe(true);
|
|
83
|
+
expect(result.current.selectedCount).toBe(2);
|
|
84
|
+
|
|
85
|
+
// Toggle off index 0
|
|
86
|
+
act(() => { result.current.toggleEvent(0); });
|
|
87
|
+
expect(result.current.selectedIndices.has(0)).toBe(false);
|
|
88
|
+
expect(result.current.selectedCount).toBe(1);
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
it("shift-click selects a range of content-bearing events", () => {
|
|
92
|
+
const { result } = renderSelection();
|
|
93
|
+
act(() => { result.current.enterSelectionMode(0); });
|
|
94
|
+
|
|
95
|
+
// Shift-click index 4 should select 0, 1, 3, 4 (skip 2 which is status)
|
|
96
|
+
act(() => { result.current.toggleEvent(4, true); });
|
|
97
|
+
expect(result.current.selectedIndices.has(0)).toBe(true);
|
|
98
|
+
expect(result.current.selectedIndices.has(1)).toBe(true);
|
|
99
|
+
expect(result.current.selectedIndices.has(2)).toBe(false); // status event skipped
|
|
100
|
+
expect(result.current.selectedIndices.has(3)).toBe(true);
|
|
101
|
+
expect(result.current.selectedIndices.has(4)).toBe(true);
|
|
102
|
+
expect(result.current.selectedCount).toBe(4);
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
it("shift-click works in reverse direction", () => {
|
|
106
|
+
const { result } = renderSelection();
|
|
107
|
+
act(() => { result.current.enterSelectionMode(4); });
|
|
108
|
+
|
|
109
|
+
// Shift-click index 1 should select 1, 3, 4 (skip 2)
|
|
110
|
+
act(() => { result.current.toggleEvent(1, true); });
|
|
111
|
+
expect(result.current.selectedIndices.has(1)).toBe(true);
|
|
112
|
+
expect(result.current.selectedIndices.has(2)).toBe(false);
|
|
113
|
+
expect(result.current.selectedIndices.has(3)).toBe(true);
|
|
114
|
+
expect(result.current.selectedIndices.has(4)).toBe(true);
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
it("selectAll selects only content-bearing events", () => {
|
|
118
|
+
const { result } = renderSelection();
|
|
119
|
+
act(() => { result.current.enterSelectionMode(); });
|
|
120
|
+
act(() => { result.current.selectAll(); });
|
|
121
|
+
|
|
122
|
+
// Indices 0, 1, 3, 4, 5 are content-bearing; 2 is status
|
|
123
|
+
expect(result.current.selectedCount).toBe(5);
|
|
124
|
+
expect(result.current.selectedIndices.has(0)).toBe(true);
|
|
125
|
+
expect(result.current.selectedIndices.has(1)).toBe(true);
|
|
126
|
+
expect(result.current.selectedIndices.has(2)).toBe(false);
|
|
127
|
+
expect(result.current.selectedIndices.has(3)).toBe(true);
|
|
128
|
+
expect(result.current.selectedIndices.has(4)).toBe(true);
|
|
129
|
+
expect(result.current.selectedIndices.has(5)).toBe(true);
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
it("exits selection mode when last event is deselected", () => {
|
|
133
|
+
const { result } = renderSelection();
|
|
134
|
+
act(() => { result.current.enterSelectionMode(0); });
|
|
135
|
+
expect(result.current.isSelecting).toBe(true);
|
|
136
|
+
expect(result.current.selectedCount).toBe(1);
|
|
137
|
+
|
|
138
|
+
// Deselect the only selected event
|
|
139
|
+
act(() => { result.current.toggleEvent(0); });
|
|
140
|
+
expect(result.current.isSelecting).toBe(false);
|
|
141
|
+
expect(result.current.selectedCount).toBe(0);
|
|
142
|
+
});
|
|
143
|
+
|
|
144
|
+
it("deselectAll clears selection but stays in selection mode", () => {
|
|
145
|
+
const { result } = renderSelection();
|
|
146
|
+
act(() => { result.current.enterSelectionMode(0); });
|
|
147
|
+
act(() => { result.current.toggleEvent(1); });
|
|
148
|
+
expect(result.current.selectedCount).toBe(2);
|
|
149
|
+
|
|
150
|
+
act(() => { result.current.deselectAll(); });
|
|
151
|
+
expect(result.current.isSelecting).toBe(true);
|
|
152
|
+
expect(result.current.selectedCount).toBe(0);
|
|
153
|
+
});
|
|
154
|
+
|
|
155
|
+
it("copySelected calls formatForClipboard with selected events in order", async () => {
|
|
156
|
+
// Mock clipboard
|
|
157
|
+
Object.assign(navigator, {
|
|
158
|
+
clipboard: { writeText: vi.fn().mockResolvedValue(undefined) },
|
|
159
|
+
});
|
|
160
|
+
|
|
161
|
+
const { result } = renderSelection();
|
|
162
|
+
act(() => { result.current.enterSelectionMode(4); });
|
|
163
|
+
act(() => { result.current.toggleEvent(0); });
|
|
164
|
+
|
|
165
|
+
let success = false;
|
|
166
|
+
await act(async () => {
|
|
167
|
+
success = await result.current.copySelected();
|
|
168
|
+
});
|
|
169
|
+
|
|
170
|
+
expect(success).toBe(true);
|
|
171
|
+
expect(mockFormat).toHaveBeenCalledTimes(1);
|
|
172
|
+
// Events should be in index order (0 before 4), not selection order
|
|
173
|
+
const calledWith = mockFormat.mock.calls[0][0] as DisplayEvent[];
|
|
174
|
+
expect(calledWith[0].content).toBe("Hello"); // index 0
|
|
175
|
+
expect(calledWith[1].content).toBe("Response 2"); // index 4
|
|
176
|
+
});
|
|
177
|
+
|
|
178
|
+
it("copySelected returns false when clipboard fails", async () => {
|
|
179
|
+
Object.assign(navigator, {
|
|
180
|
+
clipboard: { writeText: vi.fn().mockRejectedValue(new Error("denied")) },
|
|
181
|
+
});
|
|
182
|
+
|
|
183
|
+
const { result } = renderSelection();
|
|
184
|
+
act(() => { result.current.enterSelectionMode(0); });
|
|
185
|
+
|
|
186
|
+
let success = true;
|
|
187
|
+
await act(async () => {
|
|
188
|
+
success = await result.current.copySelected();
|
|
189
|
+
});
|
|
190
|
+
expect(success).toBe(false);
|
|
191
|
+
});
|
|
192
|
+
|
|
193
|
+
it("copySelected returns false when nothing is selected", async () => {
|
|
194
|
+
const { result } = renderSelection();
|
|
195
|
+
act(() => { result.current.enterSelectionMode(); });
|
|
196
|
+
|
|
197
|
+
let success = true;
|
|
198
|
+
await act(async () => {
|
|
199
|
+
success = await result.current.copySelected();
|
|
200
|
+
});
|
|
201
|
+
expect(success).toBe(false);
|
|
202
|
+
expect(mockFormat).not.toHaveBeenCalled();
|
|
203
|
+
});
|
|
204
|
+
});
|
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Selection state management for the multi-select feature in EventStream.
|
|
3
|
+
*
|
|
4
|
+
* Manages entering/exiting selection mode, toggling individual events,
|
|
5
|
+
* shift-click range selection, select all, and clipboard copy.
|
|
6
|
+
*
|
|
7
|
+
* @module
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import { useState, useRef, useCallback, useMemo } from "react";
|
|
11
|
+
import type { DisplayEvent } from "../utils/sessionEvents.js";
|
|
12
|
+
import { isContentBearingEvent } from "../utils/eventContent.js";
|
|
13
|
+
|
|
14
|
+
/** Options for the useEventSelection hook. */
|
|
15
|
+
export interface UseEventSelectionOptions {
|
|
16
|
+
/** All events currently in the stream. */
|
|
17
|
+
events: DisplayEvent[];
|
|
18
|
+
/** Formats events as text for the clipboard. */
|
|
19
|
+
formatForClipboard: (events: DisplayEvent[]) => string;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/** Return value of the useEventSelection hook. */
|
|
23
|
+
export interface UseEventSelectionReturn {
|
|
24
|
+
/** Whether selection mode is active. */
|
|
25
|
+
isSelecting: boolean;
|
|
26
|
+
/** Set of selected event indices (from the events array). */
|
|
27
|
+
selectedIndices: ReadonlySet<number>;
|
|
28
|
+
/** Number of selected events. */
|
|
29
|
+
selectedCount: number;
|
|
30
|
+
/** Enter selection mode, optionally selecting an initial event. */
|
|
31
|
+
enterSelectionMode: (initialIndex?: number) => void;
|
|
32
|
+
/** Exit selection mode and clear all selections. */
|
|
33
|
+
cancelSelection: () => void;
|
|
34
|
+
/** Toggle an event at the given index. When shiftKey is true, selects the range from the last-toggled anchor. */
|
|
35
|
+
toggleEvent: (index: number, shiftKey?: boolean) => void;
|
|
36
|
+
/** Select all content-bearing events. */
|
|
37
|
+
selectAll: () => void;
|
|
38
|
+
/** Deselect all events but stay in selection mode. */
|
|
39
|
+
deselectAll: () => void;
|
|
40
|
+
/** Copy selected events to clipboard. Returns true on success. */
|
|
41
|
+
copySelected: () => Promise<boolean>;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Hook that manages event selection state for the EventStream multi-select feature.
|
|
46
|
+
*
|
|
47
|
+
* The caller is responsible for rendering checkboxes, highlights, and action bars
|
|
48
|
+
* based on the returned state. The `copySelected` method uses `navigator.clipboard`
|
|
49
|
+
* and will return `false` in environments where the Clipboard API is unavailable.
|
|
50
|
+
*/
|
|
51
|
+
export function useEventSelection({
|
|
52
|
+
events,
|
|
53
|
+
formatForClipboard,
|
|
54
|
+
}: UseEventSelectionOptions): UseEventSelectionReturn {
|
|
55
|
+
const [isSelecting, setIsSelecting] = useState(false);
|
|
56
|
+
const [selectedIndices, setSelectedIndices] = useState<ReadonlySet<number>>(new Set());
|
|
57
|
+
/** Anchor index for shift-click range selection. */
|
|
58
|
+
const anchorRef = useRef<number | undefined>(undefined);
|
|
59
|
+
|
|
60
|
+
const selectedCount = useMemo(() => selectedIndices.size, [selectedIndices]);
|
|
61
|
+
|
|
62
|
+
const enterSelectionMode = useCallback((initialIndex?: number) => {
|
|
63
|
+
setIsSelecting(true);
|
|
64
|
+
if (initialIndex !== undefined) {
|
|
65
|
+
setSelectedIndices(new Set([initialIndex]));
|
|
66
|
+
anchorRef.current = initialIndex;
|
|
67
|
+
} else {
|
|
68
|
+
setSelectedIndices(new Set());
|
|
69
|
+
anchorRef.current = undefined;
|
|
70
|
+
}
|
|
71
|
+
}, []);
|
|
72
|
+
|
|
73
|
+
const cancelSelection = useCallback(() => {
|
|
74
|
+
setIsSelecting(false);
|
|
75
|
+
setSelectedIndices(new Set());
|
|
76
|
+
anchorRef.current = undefined;
|
|
77
|
+
}, []);
|
|
78
|
+
|
|
79
|
+
const toggleEvent = useCallback(
|
|
80
|
+
(index: number, shiftKey?: boolean) => {
|
|
81
|
+
if (shiftKey && anchorRef.current !== undefined) {
|
|
82
|
+
// Range selection: select all content-bearing events between anchor and index
|
|
83
|
+
const start = Math.min(anchorRef.current, index);
|
|
84
|
+
const end = Math.max(anchorRef.current, index);
|
|
85
|
+
setSelectedIndices((prev) => {
|
|
86
|
+
const next = new Set(prev);
|
|
87
|
+
for (let i = start; i <= end; i++) {
|
|
88
|
+
if (i < events.length && isContentBearingEvent(events[i])) {
|
|
89
|
+
next.add(i);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
return next;
|
|
93
|
+
});
|
|
94
|
+
} else {
|
|
95
|
+
// Single toggle — exit selection mode if deselecting the last item
|
|
96
|
+
setSelectedIndices((prev) => {
|
|
97
|
+
const next = new Set(prev);
|
|
98
|
+
if (next.has(index)) {
|
|
99
|
+
next.delete(index);
|
|
100
|
+
} else {
|
|
101
|
+
next.add(index);
|
|
102
|
+
}
|
|
103
|
+
if (next.size === 0) {
|
|
104
|
+
setIsSelecting(false);
|
|
105
|
+
anchorRef.current = undefined;
|
|
106
|
+
} else {
|
|
107
|
+
anchorRef.current = index;
|
|
108
|
+
}
|
|
109
|
+
return next;
|
|
110
|
+
});
|
|
111
|
+
}
|
|
112
|
+
},
|
|
113
|
+
[events],
|
|
114
|
+
);
|
|
115
|
+
|
|
116
|
+
const selectAll = useCallback(() => {
|
|
117
|
+
const all = new Set<number>();
|
|
118
|
+
for (let i = 0; i < events.length; i++) {
|
|
119
|
+
if (isContentBearingEvent(events[i])) {
|
|
120
|
+
all.add(i);
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
setSelectedIndices(all);
|
|
124
|
+
}, [events]);
|
|
125
|
+
|
|
126
|
+
const deselectAll = useCallback(() => {
|
|
127
|
+
setSelectedIndices(new Set());
|
|
128
|
+
}, []);
|
|
129
|
+
|
|
130
|
+
const copySelected = useCallback(async (): Promise<boolean> => {
|
|
131
|
+
const sorted = [...selectedIndices].sort((a, b) => a - b);
|
|
132
|
+
const selectedEvents = sorted
|
|
133
|
+
.filter((i) => i < events.length)
|
|
134
|
+
.map((i) => events[i]);
|
|
135
|
+
if (selectedEvents.length === 0) {
|
|
136
|
+
return false;
|
|
137
|
+
}
|
|
138
|
+
const text = formatForClipboard(selectedEvents);
|
|
139
|
+
try {
|
|
140
|
+
await navigator.clipboard.writeText(text);
|
|
141
|
+
return true;
|
|
142
|
+
} catch {
|
|
143
|
+
return false;
|
|
144
|
+
}
|
|
145
|
+
}, [selectedIndices, events, formatForClipboard]);
|
|
146
|
+
|
|
147
|
+
return {
|
|
148
|
+
isSelecting,
|
|
149
|
+
selectedIndices,
|
|
150
|
+
selectedCount,
|
|
151
|
+
enterSelectionMode,
|
|
152
|
+
cancelSelection,
|
|
153
|
+
toggleEvent,
|
|
154
|
+
selectAll,
|
|
155
|
+
deselectAll,
|
|
156
|
+
copySelected,
|
|
157
|
+
};
|
|
158
|
+
}
|
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
import { useCallback, useEffect, useLayoutEffect, useRef, useState, type RefObject } from "react";
|
|
2
|
+
import { isNearAnchor, computeScrollCompensation, SCROLL_ANCHOR_THRESHOLD_PX } from "../utils/scrollUtils.js";
|
|
3
|
+
|
|
4
|
+
/** Options for the useSmartScroll hook. */
|
|
5
|
+
interface UseSmartScrollOptions {
|
|
6
|
+
/** Ref to the scrollable container element. */
|
|
7
|
+
// eslint-disable-next-line @rushstack/no-new-null
|
|
8
|
+
scrollRef: RefObject<HTMLDivElement | null>;
|
|
9
|
+
/** Length of the content list — triggers scroll checks on change. */
|
|
10
|
+
contentLength: number;
|
|
11
|
+
/** Whether newest-at-top mode is active (anchor is top instead of bottom). */
|
|
12
|
+
isReversed: boolean;
|
|
13
|
+
/** When true, auto-scroll is suppressed (e.g. during multi-select mode). */
|
|
14
|
+
paused?: boolean;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/** Return value from the useSmartScroll hook. */
|
|
18
|
+
interface UseSmartScrollReturn {
|
|
19
|
+
/** Whether the user is currently at the anchor position (bottom or top). */
|
|
20
|
+
isAtAnchor: boolean;
|
|
21
|
+
/** Smooth-scrolls to the anchor and re-enables auto-scroll. */
|
|
22
|
+
scrollToAnchor: () => void;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Smart auto-scroll hook that respects user reading position.
|
|
27
|
+
*
|
|
28
|
+
* - Auto-scrolls to anchor (bottom or top) when new content arrives and user is at anchor.
|
|
29
|
+
* - Detects when user scrolls away from anchor and disables auto-scroll.
|
|
30
|
+
* - Provides a callback to manually scroll back to anchor.
|
|
31
|
+
* - In reverse mode, compensates scrollTop to prevent viewport shift from prepended content.
|
|
32
|
+
*/
|
|
33
|
+
export function useSmartScroll({
|
|
34
|
+
scrollRef,
|
|
35
|
+
contentLength,
|
|
36
|
+
isReversed,
|
|
37
|
+
paused,
|
|
38
|
+
}: UseSmartScrollOptions): UseSmartScrollReturn {
|
|
39
|
+
const [isAtAnchor, setIsAtAnchor] = useState(true);
|
|
40
|
+
const prevScrollHeightRef = useRef<number>(0);
|
|
41
|
+
const mountedRef = useRef(false);
|
|
42
|
+
const rafIdRef = useRef<number>(0);
|
|
43
|
+
|
|
44
|
+
// Throttled scroll listener — uses rAF to avoid excessive React work during fast scrolling.
|
|
45
|
+
// Only calls setState when the boolean actually changes.
|
|
46
|
+
useEffect(() => {
|
|
47
|
+
const element = scrollRef.current;
|
|
48
|
+
if (!element) {
|
|
49
|
+
return;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
let lastKnownValue: boolean = true;
|
|
53
|
+
|
|
54
|
+
const handleScroll = (): void => {
|
|
55
|
+
if (rafIdRef.current) {
|
|
56
|
+
return;
|
|
57
|
+
}
|
|
58
|
+
rafIdRef.current = requestAnimationFrame(() => {
|
|
59
|
+
rafIdRef.current = 0;
|
|
60
|
+
const near = isNearAnchor(
|
|
61
|
+
element.scrollTop,
|
|
62
|
+
element.scrollHeight,
|
|
63
|
+
element.clientHeight,
|
|
64
|
+
isReversed,
|
|
65
|
+
SCROLL_ANCHOR_THRESHOLD_PX,
|
|
66
|
+
);
|
|
67
|
+
if (near !== lastKnownValue) {
|
|
68
|
+
lastKnownValue = near;
|
|
69
|
+
setIsAtAnchor(near);
|
|
70
|
+
}
|
|
71
|
+
});
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
element.addEventListener("scroll", handleScroll, { passive: true });
|
|
75
|
+
return () => {
|
|
76
|
+
element.removeEventListener("scroll", handleScroll);
|
|
77
|
+
if (rafIdRef.current) {
|
|
78
|
+
cancelAnimationFrame(rafIdRef.current);
|
|
79
|
+
rafIdRef.current = 0;
|
|
80
|
+
}
|
|
81
|
+
};
|
|
82
|
+
}, [scrollRef, isReversed]);
|
|
83
|
+
|
|
84
|
+
// Initial scroll — useLayoutEffect to avoid flash before paint
|
|
85
|
+
useLayoutEffect(() => {
|
|
86
|
+
const element = scrollRef.current;
|
|
87
|
+
if (!element || mountedRef.current) {
|
|
88
|
+
return;
|
|
89
|
+
}
|
|
90
|
+
mountedRef.current = true;
|
|
91
|
+
if (isReversed) {
|
|
92
|
+
element.scrollTop = 0;
|
|
93
|
+
} else {
|
|
94
|
+
element.scrollTop = element.scrollHeight;
|
|
95
|
+
}
|
|
96
|
+
setIsAtAnchor(true);
|
|
97
|
+
}, [scrollRef, isReversed]);
|
|
98
|
+
|
|
99
|
+
// Auto-scroll on new content + reverse-mode compensation.
|
|
100
|
+
// prevScrollHeightRef is captured at the END of this effect so the NEXT
|
|
101
|
+
// invocation sees the prior commit's scrollHeight (not the current one).
|
|
102
|
+
useLayoutEffect(() => {
|
|
103
|
+
const element = scrollRef.current;
|
|
104
|
+
if (!element) {
|
|
105
|
+
return;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
// Skip auto-scroll while paused (e.g. during multi-select mode) to avoid
|
|
109
|
+
// disrupting the user's selection. Still update prevScrollHeight so scroll
|
|
110
|
+
// compensation is correct when unpaused.
|
|
111
|
+
if (paused) {
|
|
112
|
+
prevScrollHeightRef.current = element.scrollHeight;
|
|
113
|
+
return;
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
if (isReversed && !isAtAnchor) {
|
|
117
|
+
// Compensate scrollTop to prevent viewport shift from prepended content
|
|
118
|
+
const compensation = computeScrollCompensation(
|
|
119
|
+
prevScrollHeightRef.current,
|
|
120
|
+
element.scrollHeight,
|
|
121
|
+
);
|
|
122
|
+
if (compensation > 0) {
|
|
123
|
+
element.scrollTop += compensation;
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
if (isAtAnchor) {
|
|
128
|
+
if (isReversed) {
|
|
129
|
+
element.scrollTo({ top: 0, behavior: "smooth" });
|
|
130
|
+
} else {
|
|
131
|
+
element.scrollTo({ top: element.scrollHeight, behavior: "smooth" });
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
// Update prevScrollHeight AFTER applying compensation/auto-scroll
|
|
136
|
+
// so the next render can compute the delta correctly.
|
|
137
|
+
prevScrollHeightRef.current = element.scrollHeight;
|
|
138
|
+
}, [contentLength, isAtAnchor, isReversed, paused, scrollRef]);
|
|
139
|
+
|
|
140
|
+
const scrollToAnchor = useCallback((): void => {
|
|
141
|
+
const element = scrollRef.current;
|
|
142
|
+
if (!element) {
|
|
143
|
+
return;
|
|
144
|
+
}
|
|
145
|
+
const target = isReversed ? 0 : element.scrollHeight;
|
|
146
|
+
element.scrollTo({ top: target, behavior: "smooth" });
|
|
147
|
+
setIsAtAnchor(true);
|
|
148
|
+
}, [scrollRef, isReversed]);
|
|
149
|
+
|
|
150
|
+
return { isAtAnchor, scrollToAnchor };
|
|
151
|
+
}
|