Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update current note in context at change #906

Merged
merged 1 commit into from
Dec 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading