| 1 | import { useContext, useEffect, useMemo } from "react"; |
| 2 | import { useSelector } from "react-redux"; |
| 3 | import styled from "styled-components"; |
| 4 | import { AnimatedEllipsis } from "../.."; |
| 5 | import { IdeMessengerContext } from "../../../context/IdeMessenger"; |
| 6 | import { useAppDispatch, useAppSelector } from "../../../redux/hooks"; |
| 7 | import { selectCurrentToolCall } from "../../../redux/selectors/selectCurrentToolCall"; |
| 8 | import { callCurrentTool } from "../../../redux/thunks/callCurrentTool"; |
| 9 | import { cancelCurrentToolCall } from "../../../redux/thunks/cancelCurrentToolCall"; |
| 10 | import { cancelStream } from "../../../redux/thunks/cancelStream"; |
| 11 | import { |
| 12 | getAltKeyLabel, |
| 13 | getFontSize, |
| 14 | getMetaKeyLabel, |
| 15 | isJetBrains, |
| 16 | } from "../../../util"; |
| 17 | import { EnterButton } from "../InputToolbar/EnterButton"; |
| 18 | import { BlockSettingsTopToolbar } from "./BlockSettingsTopToolbar"; |
| 19 | |
| 20 | const Container = styled.div` |
| 21 | display: flex; |
| 22 | justify-content: space-between; |
| 23 | align-items: center; |
| 24 | width: 100%; |
| 25 | `; |
| 26 | |
| 27 | const StopButton = styled.div` |
| 28 | font-size: ${getFontSize() - 3}px; |
| 29 | padding: 2px; |
| 30 | padding-right: 4px; |
| 31 | cursor: pointer; |
| 32 | `; |
| 33 | |
| 34 | function GeneratingIndicator() { |
| 35 | return ( |
| 36 | <div className="text-xs text-gray-400"> |
| 37 | <span>Generating</span> |
| 38 | <AnimatedEllipsis /> |
| 39 | </div> |
| 40 | ); |
| 41 | } |
| 42 | |
| 43 | function ApplyingIndicator() { |
| 44 | return ( |
| 45 | <div className="text-xs text-gray-400"> |
| 46 | <span>Applying</span> |
| 47 | <AnimatedEllipsis /> |
| 48 | </div> |
| 49 | ); |
| 50 | } |
| 51 | |
| 52 | export function LumpToolbar() { |
| 53 | const dispatch = useAppDispatch(); |
| 54 | const ideMessenger = useContext(IdeMessengerContext); |
| 55 | const ttsActive = useAppSelector((state) => state.ui.ttsActive); |
| 56 | const isStreaming = useAppSelector((state) => state.session.isStreaming); |
| 57 | const jetbrains = isJetBrains(); |
| 58 | const applyStates = useAppSelector( |
| 59 | (store) => store.session.codeBlockApplyStates.states, |
| 60 | ); |
| 61 | const isApplying = useMemo(() => { |
| 62 | return applyStates.find((state) => state.status === "streaming"); |
| 63 | }, [applyStates]); |
| 64 | |
| 65 | const toolCallState = useSelector(selectCurrentToolCall); |
| 66 | |
| 67 | const handleKeyDown = (event: KeyboardEvent) => { |
| 68 | if (toolCallState?.status === "generated") { |
| 69 | const metaKey = event.metaKey || event.ctrlKey; |
| 70 | const altKey = event.altKey; |
| 71 | |
| 72 | if (metaKey && event.key === "Enter") { |
| 73 | event.preventDefault(); |
| 74 | event.stopPropagation(); |
| 75 | dispatch(callCurrentTool()); |
| 76 | } else if ((jetbrains ? altKey : metaKey) && event.key === "Backspace") { |
| 77 | event.preventDefault(); |
| 78 | event.stopPropagation(); |
| 79 | dispatch(cancelCurrentToolCall()); |
| 80 | } |
| 81 | } |
| 82 | }; |
| 83 | |
| 84 | useEffect(() => { |
| 85 | document.addEventListener("keydown", handleKeyDown); |
| 86 | return () => { |
| 87 | document.removeEventListener("keydown", handleKeyDown); |
| 88 | }; |
| 89 | }, [toolCallState]); |
| 90 | |
| 91 | if (ttsActive) { |
| 92 | return ( |
| 93 | <Container> |
| 94 | <GeneratingIndicator /> |
| 95 | <StopButton |
| 96 | className="text-gray-400" |
| 97 | onClick={() => { |
| 98 | ideMessenger.post("tts/kill", undefined); |
| 99 | }} |
| 100 | > |
| 101 | ■ Stop TTS |
| 102 | </StopButton> |
| 103 | </Container> |
| 104 | ); |
| 105 | } |
| 106 | |
| 107 | if (isApplying) { |
| 108 | return ( |
| 109 | <Container> |
| 110 | <ApplyingIndicator /> |
| 111 | <StopButton |
| 112 | className="text-gray-400" |
| 113 | onClick={() => { |
| 114 | // Note that this will NOT stop generation but once apply is cancelled will show the Generating/cancel option |
| 115 | // Apply is prioritized because it can be more catastrophic |
| 116 | // Intentional to be more WYSIWYG for now |
| 117 | ideMessenger.post("cancelApply", undefined); |
| 118 | }} |
| 119 | > |
| 120 | {/* JetBrains overrides cmd+backspace, so we have to use another shortcut */} |
| 121 | {jetbrains ? getAltKeyLabel() : getMetaKeyLabel()} ⌫ Cancel |
| 122 | </StopButton> |
| 123 | </Container> |
| 124 | ); |
| 125 | } |
| 126 | |
| 127 | if (isStreaming) { |
| 128 | return ( |
| 129 | <Container> |
| 130 | <GeneratingIndicator /> |
| 131 | <StopButton |
| 132 | className="text-gray-400" |
| 133 | onClick={() => { |
| 134 | dispatch(cancelStream()); |
| 135 | }} |
| 136 | > |
| 137 | {/* JetBrains overrides cmd+backspace, so we have to use another shortcut */} |
| 138 | {jetbrains ? getAltKeyLabel() : getMetaKeyLabel()} ⌫ Cancel |
| 139 | </StopButton> |
| 140 | </Container> |
| 141 | ); |
| 142 | } |
| 143 | |
| 144 | if (toolCallState?.status === "generated") { |
| 145 | return ( |
| 146 | <Container> |
| 147 | <div className="flex flex-row items-center pb-0.5 pr-1 text-xs text-gray-400"> |
| 148 | <span className="hidden sm:flex">Pending tool call</span> |
| 149 | </div> |
| 150 | |
| 151 | <div className="flex gap-2 pb-0.5"> |
| 152 | <StopButton |
| 153 | className="text-gray-400" |
| 154 | onClick={() => dispatch(cancelCurrentToolCall())} |
| 155 | data-testid="reject-tool-call-button" |
| 156 | > |
| 157 | {/* JetBrains overrides cmd+backspace, so we have to use another shortcut */} |
| 158 | {jetbrains ? getAltKeyLabel() : getMetaKeyLabel()} ⌫ Cancel |
| 159 | </StopButton> |
| 160 | <EnterButton |
| 161 | isPrimary={true} |
| 162 | className="text-gray-400" |
| 163 | onClick={() => dispatch(callCurrentTool())} |
| 164 | data-testid="accept-tool-call-button" |
| 165 | > |
| 166 | {getMetaKeyLabel()} ⏎ Continue |
| 167 | </EnterButton> |
| 168 | </div> |
| 169 | </Container> |
| 170 | ); |
| 171 | } |
| 172 | |
| 173 | return <BlockSettingsTopToolbar />; |
| 174 | } |