Skip to content

Commit bf20e84

Browse files
committed
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)
1 parent 70c16d8 commit bf20e84

File tree

1 file changed

+14
-15
lines changed

1 file changed

+14
-15
lines changed

src/components/ClaudeCodeSession.tsx

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,38 +15,37 @@ import { Label } from "@/components/ui/label";
1515
import { Popover } from "@/components/ui/popover";
1616
import { api, type Session } from "@/lib/api";
1717
import { cn } from "@/lib/utils";
18+
import { listen as tauriListen } from "@tauri-apps/api/event";
1819

19-
// Conditional imports for Tauri APIs
20-
let tauriListen: any;
2120
type UnlistenFn = () => void;
2221

23-
try {
24-
if (typeof window !== 'undefined' && window.__TAURI__) {
25-
tauriListen = require("@tauri-apps/api/event").listen;
26-
}
27-
} catch (e) {
28-
console.log('[ClaudeCodeSession] Tauri APIs not available, using web mode');
29-
}
22+
// Runtime check for Tauri environment (must be function to check at call time)
23+
const isTauriEnv = () => typeof window !== 'undefined' && !!(window.__TAURI__ || window.__TAURI_INTERNALS__);
3024

31-
// Web-compatible replacements
32-
const listen = tauriListen || ((eventName: string, callback: (event: any) => void) => {
25+
// Web-compatible fallback for non-Tauri environments
26+
const webListen = (eventName: string, callback: (event: any) => void) => {
3327
console.log('[ClaudeCodeSession] Setting up DOM event listener for:', eventName);
3428

35-
// In web mode, listen for DOM events
3629
const domEventHandler = (event: any) => {
3730
console.log('[ClaudeCodeSession] DOM event received:', eventName, event.detail);
38-
// Simulate Tauri event structure
3931
callback({ payload: event.detail });
4032
};
4133

4234
window.addEventListener(eventName, domEventHandler);
4335

44-
// Return unlisten function
4536
return Promise.resolve(() => {
4637
console.log('[ClaudeCodeSession] Removing DOM event listener for:', eventName);
4738
window.removeEventListener(eventName, domEventHandler);
4839
});
49-
});
40+
};
41+
42+
// Dynamic listen function that checks environment at runtime
43+
const listen = (eventName: string, callback: (event: any) => void) => {
44+
if (isTauriEnv()) {
45+
return tauriListen(eventName, callback);
46+
}
47+
return webListen(eventName, callback);
48+
};
5049
import { StreamMessage } from "./StreamMessage";
5150
import { FloatingPromptInput, type FloatingPromptInputRef } from "./FloatingPromptInput";
5251
import { ErrorBoundary } from "./ErrorBoundary";

0 commit comments

Comments
 (0)