Skip to content

Commit

Permalink
Update current note in context at change (#906)
Browse files Browse the repository at this point in the history
  • Loading branch information
logancyang authored Dec 7, 2024
1 parent c70a039 commit f1f3a19
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 5 deletions.
3 changes: 2 additions & 1 deletion src/components/chat-components/ChatControls.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ interface ChatControlsProps {
setContextNotes: React.Dispatch<React.SetStateAction<TFile[]>>;
includeActiveNote: boolean;
setIncludeActiveNote: React.Dispatch<React.SetStateAction<boolean>>;
activeNote: TFile | null;
contextUrls: string[];
onRemoveUrl: (url: string) => void;
chatHistory: ChatMessage[];
Expand All @@ -40,13 +41,13 @@ const ChatControls: React.FC<ChatControlsProps> = ({
setContextNotes,
includeActiveNote,
setIncludeActiveNote,
activeNote,
contextUrls,
onRemoveUrl,
chatHistory,
}) => {
const [selectedChain, setSelectedChain] = useChainType();
const [isIndexLoaded, setIsIndexLoaded] = useState(false);
const activeNote = app.workspace.getActiveFile();

useEffect(() => {
isIndexLoadedPromise.then((loaded) => {
Expand Down
33 changes: 29 additions & 4 deletions src/components/chat-components/ChatInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ const ChatInput = forwardRef<{ focus: () => void }, ChatInputProps>(
const containerRef = useRef<HTMLDivElement>(null);
const [currentModelKey, setCurrentModelKey] = useModelKey();
const [currentChain] = useChainType();
const [currentActiveNote, setCurrentActiveNote] = useState<TFile | null>(
app.workspace.getActiveFile()
);
const settings = useSettingsValue();

useImperativeHandle(ref, () => ({
Expand Down Expand Up @@ -324,8 +327,6 @@ const ChatInput = forwardRef<{ focus: () => void }, ChatInputProps>(
const currentTitles = new Set(extractNoteTitles(inputMessage));
// Get all URLs mentioned in the input
const currentUrls = mention.extractAllUrls(inputMessage);
// Get the currently open note in the editor
const activeNote = app.workspace.getActiveFile();

setContextNotes((prev) =>
prev.filter((note) => {
Expand All @@ -339,7 +340,7 @@ const ChatInput = forwardRef<{ focus: () => void }, ChatInputProps>(
const wasAddedViaReference = (note as any).wasAddedViaReference === true;

// Special handling for the active note
if (note.path === activeNote?.path) {
if (note.path === currentActiveNote?.path) {
if (wasAddedViaReference) {
// Case 1: Active note was added by typing [[note]]
// Keep it only if its title is still in the input
Expand Down Expand Up @@ -369,7 +370,30 @@ const ChatInput = forwardRef<{ focus: () => void }, ChatInputProps>(

// Remove any URLs that are no longer present in the input
setContextUrls((prev) => prev.filter((url) => currentUrls.includes(url)));
}, [inputMessage, includeActiveNote]);
}, [inputMessage, includeActiveNote, currentActiveNote]);

// Update the current active note whenever it changes
useEffect(() => {
let timeoutId: ReturnType<typeof setTimeout>;

const handleActiveLeafChange = () => {
// Clear any existing timeout
clearTimeout(timeoutId);

// Set new timeout
timeoutId = setTimeout(() => {
const activeNote = app.workspace.getActiveFile();
setCurrentActiveNote(activeNote);
}, 100); // Wait 100ms after the last event because it fires multiple times
};

const eventRef = app.workspace.on("active-leaf-change", handleActiveLeafChange);

return () => {
clearTimeout(timeoutId); // Clean up any pending timeout
app.workspace.offref(eventRef); // unregister
};
}, [app.workspace]);

return (
<div className="chat-input-container" ref={containerRef}>
Expand All @@ -383,6 +407,7 @@ const ChatInput = forwardRef<{ focus: () => void }, ChatInputProps>(
setContextNotes={setContextNotes}
includeActiveNote={includeActiveNote}
setIncludeActiveNote={setIncludeActiveNote}
activeNote={currentActiveNote}
contextUrls={contextUrls}
onRemoveUrl={(url: string) => setContextUrls((prev) => prev.filter((u) => u !== url))}
chatHistory={chatHistory}
Expand Down

0 comments on commit f1f3a19

Please sign in to comment.