diff --git a/surfsense_desktop/src/modules/quick-ask.ts b/surfsense_desktop/src/modules/quick-ask.ts index 8eb094812..45bfe7c04 100644 --- a/surfsense_desktop/src/modules/quick-ask.ts +++ b/surfsense_desktop/src/modules/quick-ask.ts @@ -1,10 +1,11 @@ -import { BrowserWindow, clipboard, globalShortcut, screen } from 'electron'; +import { BrowserWindow, clipboard, globalShortcut, ipcMain, screen } from 'electron'; import path from 'path'; import { IPC_CHANNELS } from '../ipc/channels'; import { getServerPort } from './server'; const SHORTCUT = 'CommandOrControl+Option+S'; let quickAskWindow: BrowserWindow | null = null; +let pendingText = ''; function hideQuickAsk(): void { if (quickAskWindow && !quickAskWindow.isDestroyed()) { @@ -62,22 +63,20 @@ export function registerQuickAsk(): void { const text = clipboard.readText().trim(); if (!text) return; - const isExisting = quickAskWindow && !quickAskWindow.isDestroyed(); + pendingText = text; const cursor = screen.getCursorScreenPoint(); - const win = createQuickAskWindow(cursor.x, cursor.y); - - if (isExisting) { - win.webContents.send(IPC_CHANNELS.QUICK_ASK_TEXT, text); - } else { - win.webContents.once('did-finish-load', () => { - win.webContents.send(IPC_CHANNELS.QUICK_ASK_TEXT, text); - }); - } + createQuickAskWindow(cursor.x, cursor.y); }); if (!ok) { console.log(`Quick-ask: failed to register ${SHORTCUT}`); } + + ipcMain.handle(IPC_CHANNELS.QUICK_ASK_TEXT, () => { + const text = pendingText; + pendingText = ''; + return text; + }); } export function unregisterQuickAsk(): void { diff --git a/surfsense_desktop/src/preload.ts b/surfsense_desktop/src/preload.ts index ca894d6b3..9c857de1b 100644 --- a/surfsense_desktop/src/preload.ts +++ b/surfsense_desktop/src/preload.ts @@ -17,11 +17,5 @@ contextBridge.exposeInMainWorld('electronAPI', { ipcRenderer.removeListener(IPC_CHANNELS.DEEP_LINK, listener); }; }, - onQuickAskText: (callback: (text: string) => void) => { - const listener = (_event: unknown, text: string) => callback(text); - ipcRenderer.on(IPC_CHANNELS.QUICK_ASK_TEXT, listener); - return () => { - ipcRenderer.removeListener(IPC_CHANNELS.QUICK_ASK_TEXT, listener); - }; - }, + getQuickAskText: () => ipcRenderer.invoke(IPC_CHANNELS.QUICK_ASK_TEXT), }); diff --git a/surfsense_web/components/assistant-ui/inline-mention-editor.tsx b/surfsense_web/components/assistant-ui/inline-mention-editor.tsx index ae490cdd0..40bd16f8d 100644 --- a/surfsense_web/components/assistant-ui/inline-mention-editor.tsx +++ b/surfsense_web/components/assistant-ui/inline-mention-editor.tsx @@ -119,7 +119,9 @@ export const InlineMentionEditor = forwardRef { if (!initialText || !editorRef.current) return; - editorRef.current.textContent = initialText + "\n"; + editorRef.current.innerText = initialText; + editorRef.current.appendChild(document.createElement("br")); + editorRef.current.appendChild(document.createElement("br")); setIsEmpty(false); onChange?.(initialText, Array.from(mentionedDocs.values())); editorRef.current.focus(); diff --git a/surfsense_web/components/assistant-ui/thread.tsx b/surfsense_web/components/assistant-ui/thread.tsx index eb98fd025..64ec79ef2 100644 --- a/surfsense_web/components/assistant-ui/thread.tsx +++ b/surfsense_web/components/assistant-ui/thread.tsx @@ -332,7 +332,7 @@ const Composer: FC = () => { const [quickAskText, setQuickAskText] = useState(); useEffect(() => { - return window.electronAPI?.onQuickAskText((text) => { + window.electronAPI?.getQuickAskText().then((text) => { if (text) setQuickAskText(text); }); }, []); diff --git a/surfsense_web/types/window.d.ts b/surfsense_web/types/window.d.ts index 6c7e192db..c8b4c004a 100644 --- a/surfsense_web/types/window.d.ts +++ b/surfsense_web/types/window.d.ts @@ -10,7 +10,7 @@ interface ElectronAPI { openExternal: (url: string) => void; getAppVersion: () => Promise; onDeepLink: (callback: (url: string) => void) => () => void; - onQuickAskText: (callback: (text: string) => void) => () => void; + getQuickAskText: () => Promise; } declare global {