diff --git a/src/renderer/App.tsx b/src/renderer/App.tsx index 57d3473..9ac7290 100644 --- a/src/renderer/App.tsx +++ b/src/renderer/App.tsx @@ -1,4 +1,4 @@ -import { useEffect, useState } from 'react' +import { useEffect, useRef, useState } from 'react' import { ExportDialog } from './components/Export/ExportDialog' import type { ExportOptions } from './components/Export/ExportDialog' import { FileTree } from './components/FileTree/FileTree' @@ -58,6 +58,7 @@ export default function App(): JSX.Element { const [isFirstRun, setIsFirstRun] = useState(false) const [focusPeek, setFocusPeek] = useState(false) const [projectTitle, setProjectTitle] = useState('') + const menuHandlerRef = useRef<((action: string) => Promise) | null>(null) useEffect(() => { if (!focusMode) { @@ -99,49 +100,51 @@ export default function App(): JSX.Element { }, []) // Handle menu actions sent from the main process - useEffect(() => { - return window.api.onMenuAction(async (action) => { - if (action === 'save') { - if (activeFilePath && isDirty) { - await window.api.writeFile(activeFilePath, activeFileContent) - await window.api.saveRevision(activeFilePath, activeFileContent) - markSaved() - } - } else if (action === 'toggleSidebar') { - setSidebarOpen((v) => { const n = !v; localStorage.setItem('sidebarOpen', String(n)); return n }) - } else if (action === 'toggleChat') { - setChatOpen((v) => { const n = !v; localStorage.setItem('chatOpen', String(n)); return n }) - } else if (action === 'toggleRevisions') { - toggleRevisionPanel() - } else if (action === 'toggleTheme') { - toggleTheme() - } else if (action === 'fontIncrease') { - setFontSize(fontSize + 1) - } else if (action === 'fontDecrease') { - setFontSize(fontSize - 1) - } else if (action === 'fontReset') { - setFontSize(15) - } else if (action === 'projectSearch') { - openProjectSearch() - } else if (action === 'openSettings') { - setSettingsOpen(true) - } else if (action === 'toggleFocusMode') { - toggleFocusMode() - } else if (action === 'openProject') { - const picked = await window.api.pickProjectFolder() - if (picked) await switchProject(picked) - } else if (action.startsWith('openRecent:')) { - await switchProject(action.slice('openRecent:'.length)) - } else if (action === 'exportPDF') { - if (activeFilePath && activeFileContent) { - const fileName = activeFilePath.split('/').pop()?.replace(/\.md$/, '') ?? 'document' - await window.api.exportPDF(activeFileContent, fileName) - } - } else if (action === 'exportProjectPDF') { - setExportOpen(true) + menuHandlerRef.current = async (action: string): Promise => { + if (action === 'save') { + if (activeFilePath && isDirty) { + await window.api.writeFile(activeFilePath, activeFileContent) + await window.api.saveRevision(activeFilePath, activeFileContent) + markSaved() } - }) - }, [activeFilePath, isDirty, activeFileContent, fontSize]) + } else if (action === 'toggleSidebar') { + setSidebarOpen((v) => { const n = !v; localStorage.setItem('sidebarOpen', String(n)); return n }) + } else if (action === 'toggleChat') { + setChatOpen((v) => { const n = !v; localStorage.setItem('chatOpen', String(n)); return n }) + } else if (action === 'toggleRevisions') { + toggleRevisionPanel() + } else if (action === 'toggleTheme') { + toggleTheme() + } else if (action === 'fontIncrease') { + setFontSize(fontSize + 1) + } else if (action === 'fontDecrease') { + setFontSize(fontSize - 1) + } else if (action === 'fontReset') { + setFontSize(15) + } else if (action === 'projectSearch') { + openProjectSearch() + } else if (action === 'openSettings') { + setSettingsOpen(true) + } else if (action === 'toggleFocusMode') { + toggleFocusMode() + } else if (action === 'openProject') { + const picked = await window.api.pickProjectFolder() + if (picked) await switchProject(picked) + } else if (action.startsWith('openRecent:')) { + await switchProject(action.slice('openRecent:'.length)) + } else if (action === 'exportPDF') { + if (activeFilePath && activeFileContent) { + const fileName = activeFilePath.split('/').pop()?.replace(/\.md$/, '') ?? 'document' + await window.api.exportPDF(activeFileContent, fileName) + } + } else if (action === 'exportProjectPDF') { + setExportOpen(true) + } + } + + useEffect(() => { + return window.api.onMenuAction((action) => menuHandlerRef.current?.(action)) + }, []) // Handle Cmd+S / Ctrl+S and Cmd+Shift+F / Ctrl+Shift+F useEffect(() => {