From 78b3663fa7f74b0cf742f1abb5ddf080f78dc46f Mon Sep 17 00:00:00 2001 From: Nate Sesti Date: Wed, 27 Dec 2023 16:04:35 -0500 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20fix=20/edit=20context=20item=20a?= =?UTF-8?q?nd=20tiptap=20refresh=20bugs?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- gui/src/components/mainInput/TipTapEditor.tsx | 202 +++++++++--------- gui/src/components/mainInput/getSuggestion.ts | 20 +- gui/src/hooks/useChatHandler.ts | 1 - gui/src/hooks/useUpdatingRef.tsx | 13 ++ 4 files changed, 130 insertions(+), 106 deletions(-) create mode 100644 gui/src/hooks/useUpdatingRef.tsx diff --git a/gui/src/components/mainInput/TipTapEditor.tsx b/gui/src/components/mainInput/TipTapEditor.tsx index e288407f5..1bcd0ff0e 100644 --- a/gui/src/components/mainInput/TipTapEditor.tsx +++ b/gui/src/components/mainInput/TipTapEditor.tsx @@ -17,6 +17,7 @@ import { } from ".."; import { SearchContext } from "../../App"; import useHistory from "../../hooks/useHistory"; +import useUpdatingRef from "../../hooks/useUpdatingRef"; import { RootStore } from "../../redux/store"; import { isMetaEquivalentKeyPressed } from "../../util"; import { isJetBrains, postToIde } from "../../util/ide"; @@ -79,8 +80,8 @@ function TipTapEditor(props: TipTapEditorProps) { const { saveSession } = useHistory(dispatch); - const inSubmenu = useRef(false); - const inDropdown = useRef(false); + const inSubmenuRef = useRef(false); + const inDropdownRef = useRef(false); const enterSubmenu = async (editor: Editor) => { const contents = editor.getText(); @@ -93,7 +94,7 @@ function TipTapEditor(props: TipTapEditorProps) { from: indexOfAt + 2, to: contents.length + 1, }); - inSubmenu.current = true; + inSubmenuRef.current = true; // to trigger refresh of suggestions editor.commands.insertContent(" "); @@ -104,106 +105,109 @@ function TipTapEditor(props: TipTapEditorProps) { }; const onClose = () => { - inSubmenu.current = false; - inDropdown.current = false; + inSubmenuRef.current = false; + inDropdownRef.current = false; }; const onOpen = () => { - inDropdown.current = true; + inDropdownRef.current = true; }; - const editor = useEditor( - { - extensions: [ - Document, - History, - Placeholder.configure({ - placeholder: - historyLength === 0 - ? "Ask a question, '/' for slash commands, '@' to add context" - : "Ask a follow-up", - }), - Paragraph.extend({ - addKeyboardShortcuts() { - return { - Enter: () => { - if (inDropdown.current) { - return false; - } - - props.onEnter(this.editor.getJSON()); - return true; - }, - - "Cmd-Enter": () => { - props.onEnter(this.editor.getJSON()); - return true; - }, - - "Shift-Enter": () => - this.editor.commands.first(({ commands }) => [ - () => commands.newlineInCode(), - () => commands.createParagraphNear(), - () => commands.liftEmptyBlock(), - () => commands.splitBlock(), - ]), - }; - }, - }).configure({ - HTMLAttributes: { - class: "my-1", - }, - }), - Text, - Mention.configure({ - HTMLAttributes: { - class: "mention", - }, - suggestion: getMentionSuggestion( - props.availableContextProviders, - miniSearch, - firstResults, - enterSubmenu, - onClose, - onOpen, - inSubmenu - ), - renderText: (props) => { - return `@${props.node.attrs.label || props.node.attrs.id}`; - }, - }), - SlashCommand.configure({ - HTMLAttributes: { - class: "mention", - }, - suggestion: getCommandSuggestion( - props.availableSlashCommands, - onClose, - onOpen - ), - renderText: (props) => { - return props.node.attrs.label; - }, - }), - CodeBlockExtension, - ], - editorProps: { - attributes: { - class: "outline-none -mt-1 overflow-hidden", - style: "font-size: 14px;", - }, - }, - content: props.editorState || props.content, - }, - [ - props.availableContextProviders, - historyLength, - miniSearch, - firstResults, - inDropdown, - ] + const contextItems = useSelector( + (store: RootStore) => store.state.contextItems ); + const miniSearchRef = useUpdatingRef(miniSearch); + const availableContextProvidersRef = useUpdatingRef( + props.availableContextProviders + ); + const firstResultsRef = useUpdatingRef(firstResults); + const historyLengthRef = useUpdatingRef(historyLength); + const onEnterRef = useUpdatingRef(props.onEnter); + + const editor = useEditor({ + extensions: [ + Document, + History, + Placeholder.configure({ + placeholder: () => + historyLengthRef.current === 0 + ? "Ask a question, '/' for slash commands, '@' to add context" + : "Ask a follow-up", + }), + Paragraph.extend({ + addKeyboardShortcuts() { + return { + Enter: () => { + if (inDropdownRef.current) { + return false; + } + + onEnterRef.current(this.editor.getJSON()); + return true; + }, + + "Cmd-Enter": () => { + onEnterRef.current(this.editor.getJSON()); + return true; + }, + + "Shift-Enter": () => + this.editor.commands.first(({ commands }) => [ + () => commands.newlineInCode(), + () => commands.createParagraphNear(), + () => commands.liftEmptyBlock(), + () => commands.splitBlock(), + ]), + }; + }, + }).configure({ + HTMLAttributes: { + class: "my-1", + }, + }), + Text, + Mention.configure({ + HTMLAttributes: { + class: "mention", + }, + suggestion: getMentionSuggestion( + availableContextProvidersRef, + miniSearchRef, + firstResultsRef, + enterSubmenu, + onClose, + onOpen, + inSubmenuRef + ), + renderText: (props) => { + return `@${props.node.attrs.label || props.node.attrs.id}`; + }, + }), + SlashCommand.configure({ + HTMLAttributes: { + class: "mention", + }, + suggestion: getCommandSuggestion( + props.availableSlashCommands, + onClose, + onOpen + ), + renderText: (props) => { + return props.node.attrs.label; + }, + }), + CodeBlockExtension, + ], + editorProps: { + attributes: { + class: "outline-none -mt-1 overflow-hidden", + style: "font-size: 14px;", + }, + }, + content: props.editorState || props.content || "", + }); + // This is a mechanism for overriding the IDE keyboard shortcut when inside of the webview const [ignoreHighlightedCode, setIgnoreHighlightedCode] = useState(false); @@ -240,7 +244,7 @@ function TipTapEditor(props: TipTapEditorProps) { if (event.data.type === "userInput") { const input = event.data.input; editor.commands.insertContent(input); - props.onEnter(editor.getJSON()); + onEnterRef.current(editor.getJSON()); } else if (event.data.type === "focusContinueInput") { if (historyLength > 0) { saveSession(); @@ -263,7 +267,7 @@ function TipTapEditor(props: TipTapEditorProps) { })`, description: rif.filepath, id: { - providerTitle: "file", + providerTitle: "code", itemId: rif.filepath, }, }; @@ -321,7 +325,7 @@ function TipTapEditor(props: TipTapEditorProps) { } }} onEnter={() => { - props.onEnter(editor.getJSON()); + onEnterRef.current(editor.getJSON()); }} onClick={() => { editor.commands.focus("end"); diff --git a/gui/src/components/mainInput/getSuggestion.ts b/gui/src/components/mainInput/getSuggestion.ts index 8407b4800..f17f9db05 100644 --- a/gui/src/components/mainInput/getSuggestion.ts +++ b/gui/src/components/mainInput/getSuggestion.ts @@ -105,9 +105,9 @@ function getFileItems( } export function getMentionSuggestion( - availableContextProviders: IContextProvider[], - miniSearch: MiniSearch, - firstResults: any[], + availableContextProvidersRef: MutableRefObject, + miniSearchRef: MutableRefObject, + firstResultsRef: MutableRefObject, enterSubmenu: (editor: Editor) => void, onClose: () => void, onOpen: () => void, @@ -115,11 +115,15 @@ export function getMentionSuggestion( ) { const items = ({ query }) => { if (inSubmenu.current) { - return getFileItems(query, miniSearch, firstResults); + return getFileItems( + query, + miniSearchRef.current, + firstResultsRef.current + ); } const mainResults = - availableContextProviders + availableContextProvidersRef.current ?.filter( (provider) => provider.description.title @@ -141,7 +145,11 @@ export function getMentionSuggestion( .sort((c, _) => (c.id === "file" ? -1 : 1)) || []; if (mainResults.length === 0) { - return getFileItems(query, miniSearch, firstResults); + return getFileItems( + query, + miniSearchRef.current, + firstResultsRef.current + ); } return mainResults; }; diff --git a/gui/src/hooks/useChatHandler.ts b/gui/src/hooks/useChatHandler.ts index 8e909be8f..1d24bff17 100644 --- a/gui/src/hooks/useChatHandler.ts +++ b/gui/src/hooks/useChatHandler.ts @@ -122,7 +122,6 @@ function useChatHandler(dispatch: Dispatch) { dispatch(resubmitAtIndex({ index, content, editorState })); } else { newHistory = [...history, historyItem]; - console.log("Submitting message"); dispatch(submitMessage({ message, editorState })); } diff --git a/gui/src/hooks/useUpdatingRef.tsx b/gui/src/hooks/useUpdatingRef.tsx new file mode 100644 index 000000000..4034839dc --- /dev/null +++ b/gui/src/hooks/useUpdatingRef.tsx @@ -0,0 +1,13 @@ +import { useEffect, useRef } from "react"; + +function useUpdatingRef(value: any, deps: any[] = []) { + const ref = useRef(value); + + useEffect(() => { + ref.current = value; + }, [value, ...deps]); + + return ref; +} + +export default useUpdatingRef;