| 1 | import { useContext, useEffect } from "react"; |
| 2 | import { useSelector } from "react-redux"; |
| 3 | import { IdeMessengerContext } from "../../../../context/IdeMessenger"; |
| 4 | import { useAppDispatch, useAppSelector } from "../../../../redux/hooks"; |
| 5 | import { |
| 6 | selectFirstPendingToolCall, |
| 7 | selectPendingToolCalls, |
| 8 | } from "../../../../redux/selectors/selectToolCalls"; |
| 9 | import { cancelToolCall } from "../../../../redux/slices/sessionSlice"; |
| 10 | import { callToolById } from "../../../../redux/thunks/callToolById"; |
| 11 | import { logToolUsage } from "../../../../redux/util"; |
| 12 | import { isJetBrains } from "../../../../util"; |
| 13 | import { BlockSettingsTopToolbar } from "./BlockSettingsTopToolbar"; |
| 14 | import { EditOutcomeToolbar } from "./EditOutcomeToolbar"; |
| 15 | import { EditToolbar } from "./EditToolbar"; |
| 16 | import { IsApplyingToolbar } from "./IsApplyingToolbar"; |
| 17 | import { PendingApplyStatesToolbar } from "./PendingApplyStatesToolbar"; |
| 18 | import { PendingToolCallToolbar } from "./PendingToolCallToolbar"; |
| 19 | import { StreamingToolbar } from "./StreamingToolbar"; |
| 20 | import { TtsActiveToolbar } from "./TtsActiveToolbar"; |
| 21 | |
| 22 | // Keyboard shortcut detection utilities |
| 23 | const isExecuteToolCallShortcut = (event: KeyboardEvent) => { |
| 24 | const metaKey = event.metaKey || event.ctrlKey; |
| 25 | return metaKey && event.key === "Enter"; |
| 26 | }; |
| 27 | |
| 28 | const isCancelToolCallShortcut = ( |
| 29 | event: KeyboardEvent, |
| 30 | isJetBrains: boolean, |
| 31 | ) => { |
| 32 | const metaKey = event.metaKey || event.ctrlKey; |
| 33 | const altKey = event.altKey; |
| 34 | const modifierKey = isJetBrains ? altKey : metaKey; |
| 35 | return modifierKey && event.key === "Backspace"; |
| 36 | }; |
| 37 | |
| 38 | export function LumpToolbar() { |
| 39 | const dispatch = useAppDispatch(); |
| 40 | const ideMessenger = useContext(IdeMessengerContext); |
| 41 | const ttsActive = useAppSelector((state) => state.ui.ttsActive); |
| 42 | const isStreaming = useAppSelector((state) => state.session.isStreaming); |
| 43 | const isInEdit = useAppSelector((state) => state.session.isInEdit); |
| 44 | const jetbrains = isJetBrains(); |
| 45 | const pendingToolCalls = useAppSelector(selectPendingToolCalls); |
| 46 | const firstPendingToolCall = useAppSelector(selectFirstPendingToolCall); |
| 47 | const editApplyState = useAppSelector( |
| 48 | (state) => state.editModeState.applyState, |
| 49 | ); |
| 50 | const applyStates = useAppSelector( |
| 51 | (state) => state.session.codeBlockApplyStates.states, |
| 52 | ); |
| 53 | const pendingApplyStates = applyStates.filter( |
| 54 | (state) => state.status === "done", |
| 55 | ); |
| 56 | const isApplying = applyStates.some((state) => state.status === "streaming"); |
| 57 | |
| 58 | useEffect(() => { |
| 59 | if (!firstPendingToolCall) { |
| 60 | return; |
| 61 | } |
| 62 | |
| 63 | const handleToolCallKeyboardShortcuts = (event: KeyboardEvent) => { |
| 64 | if (isExecuteToolCallShortcut(event)) { |
| 65 | event.preventDefault(); |
| 66 | event.stopPropagation(); |
| 67 | |
| 68 | void dispatch( |
| 69 | callToolById({ toolCallId: firstPendingToolCall.toolCallId }), |
| 70 | ); |
| 71 | } else if (isCancelToolCallShortcut(event, jetbrains)) { |
| 72 | event.preventDefault(); |
| 73 | event.stopPropagation(); |
| 74 | void dispatch( |
| 75 | cancelToolCall({ |
| 76 | toolCallId: firstPendingToolCall.toolCallId, |
| 77 | }), |
| 78 | ); |
| 79 | |
| 80 | logToolUsage(firstPendingToolCall, false, true, ideMessenger); |
| 81 | } |
| 82 | }; |
| 83 | |
| 84 | document.addEventListener("keydown", handleToolCallKeyboardShortcuts); |
| 85 | return () => { |
| 86 | document.removeEventListener("keydown", handleToolCallKeyboardShortcuts); |
| 87 | }; |
| 88 | }, [firstPendingToolCall]); |
| 89 | |
| 90 | if (isApplying) { |
| 91 | return <IsApplyingToolbar />; |
| 92 | } |
| 93 | |
| 94 | if (isInEdit) { |
| 95 | if (editApplyState.status === "done") { |
| 96 | return <EditOutcomeToolbar />; |
| 97 | } |
| 98 | |
| 99 | return <EditToolbar />; |
| 100 | } |
| 101 | |
| 102 | if (ttsActive) { |
| 103 | return <TtsActiveToolbar />; |
| 104 | } |
| 105 | |
| 106 | if (isStreaming) { |
| 107 | return <StreamingToolbar />; |
| 108 | } |
| 109 | |
| 110 | if (pendingToolCalls.length > 0) { |
| 111 | return <PendingToolCallToolbar />; |
| 112 | } |
| 113 | |
| 114 | if (pendingApplyStates.length > 0) { |
| 115 | return ( |
| 116 | <PendingApplyStatesToolbar pendingApplyStates={pendingApplyStates} /> |
| 117 | ); |
| 118 | } |
| 119 | |
| 120 | return <BlockSettingsTopToolbar />; |
| 121 | } |