🐛 fix /edit context item and tiptap refresh bugs

This commit is contained in:
Nate Sesti 2023-12-27 16:04:35 -05:00
parent 99df92342a
commit 78b3663fa7
4 changed files with 130 additions and 106 deletions

View file

@ -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");

View file

@ -105,9 +105,9 @@ function getFileItems(
}
export function getMentionSuggestion(
availableContextProviders: IContextProvider[],
miniSearch: MiniSearch,
firstResults: any[],
availableContextProvidersRef: MutableRefObject<IContextProvider[]>,
miniSearchRef: MutableRefObject<MiniSearch>,
firstResultsRef: MutableRefObject<any[]>,
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;
};

View file

@ -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 }));
}

View file

@ -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;