| 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 { selectCurrentToolCall } from "../../../../redux/selectors/selectCurrentToolCall"; |
| 6 | import { cancelToolCall } from "../../../../redux/slices/sessionSlice"; |
| 7 | import { callCurrentTool } from "../../../../redux/thunks/callCurrentTool"; |
| 8 | import { logToolUsage } from "../../../../redux/util"; |
| 9 | import { isJetBrains } from "../../../../util"; |
| 10 | import { BlockSettingsTopToolbar } from "./BlockSettingsTopToolbar"; |
| 11 | import { EditOutcomeToolbar } from "./EditOutcomeToolbar"; |
| 12 | import { EditToolbar } from "./EditToolbar"; |
| 13 | import { IsApplyingToolbar } from "./IsApplyingToolbar"; |
| 14 | import { PendingApplyStatesToolbar } from "./PendingApplyStatesToolbar"; |
| 15 | import { PendingToolCallToolbar } from "./PendingToolCallToolbar"; |
| 16 | import { StreamingToolbar } from "./StreamingToolbar"; |
| 17 | import { TtsActiveToolbar } from "./TtsActiveToolbar"; |
| 18 | |
| 19 | export function LumpToolbar() { |
| 20 | const dispatch = useAppDispatch(); |
| 21 | const ttsActive = useAppSelector((state) => state.ui.ttsActive); |
| 22 | const isStreaming = useAppSelector((state) => state.session.isStreaming); |
| 23 | const isInEdit = useAppSelector((state) => state.session.isInEdit); |
| 24 | const jetbrains = isJetBrains(); |
| 25 | const toolCallState = useSelector(selectCurrentToolCall); |
| 26 | const editApplyState = useAppSelector( |
| 27 | (state) => state.editModeState.applyState, |
| 28 | ); |
| 29 | const applyStates = useAppSelector( |
| 30 | (state) => state.session.codeBlockApplyStates.states, |
| 31 | ); |
| 32 | |
| 33 | const pendingApplyStates = applyStates.filter( |
| 34 | (state) => state.status === "done", |
| 35 | ); |
| 36 | |
| 37 | const isApplying = applyStates.some((state) => state.status === "streaming"); |
| 38 | |
| 39 | useEffect(() => { |
| 40 | if (toolCallState?.status !== "generated") { |
| 41 | return; |
| 42 | } |
| 43 | |
| 44 | const handleKeyDown = (event: KeyboardEvent) => { |
| 45 | const metaKey = event.metaKey || event.ctrlKey; |
| 46 | const altKey = event.altKey; |
| 47 | |
| 48 | if (metaKey && event.key === "Enter") { |
| 49 | event.preventDefault(); |
| 50 | event.stopPropagation(); |
| 51 | void dispatch(callCurrentTool()); |
| 52 | } else if ((jetbrains ? altKey : metaKey) && event.key === "Backspace") { |
| 53 | event.preventDefault(); |
| 54 | event.stopPropagation(); |
| 55 | const ideMessenger = useContext(IdeMessengerContext); |
| 56 | logToolUsage(toolCallState, false, true, ideMessenger); |
| 57 | void dispatch( |
| 58 | cancelToolCall({ |
| 59 | toolCallId: toolCallState.toolCallId, |
| 60 | }), |
| 61 | ); |
| 62 | } |
| 63 | }; |
| 64 | |
| 65 | document.addEventListener("keydown", handleKeyDown); |
| 66 | return () => { |
| 67 | document.removeEventListener("keydown", handleKeyDown); |
| 68 | }; |
| 69 | }, [toolCallState]); |
| 70 | |
| 71 | if (isApplying) { |
| 72 | return <IsApplyingToolbar />; |
| 73 | } |
| 74 | |
| 75 | if (isInEdit) { |
| 76 | if (editApplyState.status === "done") { |
| 77 | return <EditOutcomeToolbar />; |
| 78 | } |
| 79 | |
| 80 | return <EditToolbar />; |
| 81 | } |
| 82 | |
| 83 | if (ttsActive) { |
| 84 | return <TtsActiveToolbar />; |
| 85 | } |
| 86 | |
| 87 | if (isStreaming) { |
| 88 | return <StreamingToolbar />; |
| 89 | } |
| 90 | |
| 91 | if (toolCallState?.status === "generated") { |
| 92 | return <PendingToolCallToolbar />; |
| 93 | } |
| 94 | |
| 95 | if (pendingApplyStates.length > 0) { |
| 96 | return ( |
| 97 | <PendingApplyStatesToolbar pendingApplyStates={pendingApplyStates} /> |
| 98 | ); |
| 99 | } |
| 100 | |
| 101 | return <BlockSettingsTopToolbar />; |
| 102 | } |