From bf20e849bbd8c2a4f0d840a18ac601e4ace1f4bd Mon Sep 17 00:00:00 2001 From: Felipe Rosa Date: Thu, 4 Dec 2025 19:59:16 -0300 Subject: [PATCH] fix: use ES module import for Tauri event listeners The event listeners were silently falling back to DOM events instead of Tauri events because require() fails in ES module builds and the environment check was evaluated at module load time. - Use ES module import instead of require() - Check environment at runtime (function) instead of module load (static) --- src/components/ClaudeCodeSession.tsx | 29 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 15 deletions(-) diff --git a/src/components/ClaudeCodeSession.tsx b/src/components/ClaudeCodeSession.tsx index f0f164f2..e4ad38c4 100644 --- a/src/components/ClaudeCodeSession.tsx +++ b/src/components/ClaudeCodeSession.tsx @@ -15,38 +15,37 @@ import { Label } from "@/components/ui/label"; import { Popover } from "@/components/ui/popover"; import { api, type Session } from "@/lib/api"; import { cn } from "@/lib/utils"; +import { listen as tauriListen } from "@tauri-apps/api/event"; -// Conditional imports for Tauri APIs -let tauriListen: any; type UnlistenFn = () => void; -try { - if (typeof window !== 'undefined' && window.__TAURI__) { - tauriListen = require("@tauri-apps/api/event").listen; - } -} catch (e) { - console.log('[ClaudeCodeSession] Tauri APIs not available, using web mode'); -} +// Runtime check for Tauri environment (must be function to check at call time) +const isTauriEnv = () => typeof window !== 'undefined' && !!(window.__TAURI__ || window.__TAURI_INTERNALS__); -// Web-compatible replacements -const listen = tauriListen || ((eventName: string, callback: (event: any) => void) => { +// Web-compatible fallback for non-Tauri environments +const webListen = (eventName: string, callback: (event: any) => void) => { console.log('[ClaudeCodeSession] Setting up DOM event listener for:', eventName); - // In web mode, listen for DOM events const domEventHandler = (event: any) => { console.log('[ClaudeCodeSession] DOM event received:', eventName, event.detail); - // Simulate Tauri event structure callback({ payload: event.detail }); }; window.addEventListener(eventName, domEventHandler); - // Return unlisten function return Promise.resolve(() => { console.log('[ClaudeCodeSession] Removing DOM event listener for:', eventName); window.removeEventListener(eventName, domEventHandler); }); -}); +}; + +// Dynamic listen function that checks environment at runtime +const listen = (eventName: string, callback: (event: any) => void) => { + if (isTauriEnv()) { + return tauriListen(eventName, callback); + } + return webListen(eventName, callback); +}; import { StreamMessage } from "./StreamMessage"; import { FloatingPromptInput, type FloatingPromptInputRef } from "./FloatingPromptInput"; import { ErrorBoundary } from "./ErrorBoundary";