Elffuss BraidHub

kikocis/continue public

f433400a74a4 · 3.9 KiBRawPermalink
1import { useContext, useEffect } from "react";
2import { useSelector } from "react-redux";
3import { IdeMessengerContext } from "../../../../context/IdeMessenger";
4import { useAppDispatch, useAppSelector } from "../../../../redux/hooks";
5import {
6 selectFirstPendingToolCall,
7 selectPendingToolCalls,
8} from "../../../../redux/selectors/selectToolCalls";
9import { cancelToolCall } from "../../../../redux/slices/sessionSlice";
10import { callToolById } from "../../../../redux/thunks/callToolById";
11import { logToolUsage } from "../../../../redux/util";
12import { isJetBrains } from "../../../../util";
13import { BlockSettingsTopToolbar } from "./BlockSettingsTopToolbar";
14import { EditOutcomeToolbar } from "./EditOutcomeToolbar";
15import { EditToolbar } from "./EditToolbar";
16import { IsApplyingToolbar } from "./IsApplyingToolbar";
17import { PendingApplyStatesToolbar } from "./PendingApplyStatesToolbar";
18import { PendingToolCallToolbar } from "./PendingToolCallToolbar";
19import { StreamingToolbar } from "./StreamingToolbar";
20import { TtsActiveToolbar } from "./TtsActiveToolbar";
21
22// Keyboard shortcut detection utilities
23const isExecuteToolCallShortcut = (event: KeyboardEvent) => {
24 const metaKey = event.metaKey || event.ctrlKey;
25 return metaKey && event.key === "Enter";
26};
27
28const 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
38export 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}