Skip to content

Commit ebfd6d8

Browse files
author
Marlon Costa
committed
Merge PR winfunc#407: ES Module Imports
Add web-compatible fallback for non-Tauri environments using dynamic listen function that checks environment at runtime.
2 parents 07759cd + bf20e84 commit ebfd6d8

1 file changed

Lines changed: 33 additions & 3 deletions

File tree

src/components/ClaudeCodeSession.tsx

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import React, { useState, useEffect, useRef, useMemo } from "react";
22
import { motion, AnimatePresence } from "framer-motion";
3-
import {
3+
import {
44
Copy,
55
ChevronDown,
66
GitBranch,
@@ -15,8 +15,7 @@ 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-
19-
import { listen, type UnlistenFn } from "@tauri-apps/api/event";
18+
import { listen as tauriListen } from "@tauri-apps/api/event";
2019
import { StreamMessage } from "./StreamMessage";
2120
import {
2221
FloatingPromptInput,
@@ -46,6 +45,37 @@ import {
4645
} from "@/hooks";
4746
import { SessionPersistenceService } from "@/services/sessionPersistence";
4847

48+
type UnlistenFn = () => void;
49+
50+
// Runtime check for Tauri environment (must be function to check at call time)
51+
const isTauriEnv = () => typeof window !== 'undefined' && !!(window.__TAURI__ || window.__TAURI_INTERNALS__);
52+
53+
// Web-compatible fallback for non-Tauri environments
54+
const webListen = (eventName: string, callback: (event: { payload: unknown }) => void): Promise<UnlistenFn> => {
55+
console.log('[ClaudeCodeSession] Setting up DOM event listener for:', eventName);
56+
57+
const domEventHandler = (event: Event) => {
58+
const customEvent = event as CustomEvent;
59+
console.log('[ClaudeCodeSession] DOM event received:', eventName, customEvent.detail);
60+
callback({ payload: customEvent.detail });
61+
};
62+
63+
window.addEventListener(eventName, domEventHandler);
64+
65+
return Promise.resolve(() => {
66+
console.log('[ClaudeCodeSession] Removing DOM event listener for:', eventName);
67+
window.removeEventListener(eventName, domEventHandler);
68+
});
69+
};
70+
71+
// Dynamic listen function that checks environment at runtime
72+
const listen = (eventName: string, callback: (event: { payload: unknown }) => void): Promise<UnlistenFn> => {
73+
if (isTauriEnv()) {
74+
return tauriListen(eventName, callback);
75+
}
76+
return webListen(eventName, callback);
77+
};
78+
4979
interface ClaudeCodeSessionProps {
5080
/**
5181
* Optional session to resume (when clicking from SessionList)

0 commit comments

Comments
 (0)