fix: guard native file drops

This commit is contained in:
lucaronin
2026-04-25 10:07:43 +02:00
parent 151e993ab7
commit c427cd2492
6 changed files with 223 additions and 43 deletions

View File

@@ -14,6 +14,8 @@ import {
const NOTE_LIST_SEARCH_MENU_ID = 'edit-toggle-note-list-search'
type NativeUnlisten = () => void | Promise<void>
export interface MenuEventHandlers extends AppCommandHandlers {
activeTabPath: string | null
modifiedCount?: number
@@ -57,12 +59,18 @@ function syncNativeMenuState(state: MenuStatePayload): void {
.catch((err) => console.warn('[menu] Failed to sync native menu state:', err))
}
function cleanupNativeMenuListener(unlisten: NativeUnlisten): void {
void Promise.resolve()
.then(unlisten)
.catch(() => {})
}
function useNativeMenuEventListener(handlersRef: { current: MenuEventHandlers }) {
useEffect(() => {
if (!isTauri()) return
let disposed = false
let unlisten: (() => void) | null = null
let unlisten: NativeUnlisten | null = null
import('@tauri-apps/api/event')
.then(async ({ listen }) => {
@@ -71,7 +79,7 @@ function useNativeMenuEventListener(handlersRef: { current: MenuEventHandlers })
})
if (disposed) {
teardown()
cleanupNativeMenuListener(teardown)
return
}
@@ -83,7 +91,7 @@ function useNativeMenuEventListener(handlersRef: { current: MenuEventHandlers })
return () => {
disposed = true
unlisten?.()
if (unlisten) cleanupNativeMenuListener(unlisten)
}
}, [handlersRef])
}