Skip to content

Commit 21ce60d

Browse files
committed
fix: cherry-pick upstream improvements
- PR winfunc#407: Use ES module import for Tauri event listeners - Fixes event listeners not receiving events in ClaudeCodeSession - Uses runtime check instead of module load time detection - PR winfunc#344: Handle missing ~/.claude directory gracefully - Check if directory exists before canonicalizing - Fall back to original path if canonicalize fails Note: PR winfunc#278 (IME fix) and PR winfunc#373 (installation_type fix) were already implemented
1 parent ca386d2 commit 21ce60d

File tree

2 files changed

+31
-19
lines changed

2 files changed

+31
-19
lines changed

src-tauri/src/commands/claude.rs

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -203,11 +203,24 @@ fn get_shell_config_sync(app_handle: &AppHandle) -> ShellConfig {
203203

204204
/// Gets the path to the ~/.claude directory
205205
fn get_claude_dir() -> Result<PathBuf> {
206-
dirs::home_dir()
206+
let claude_path = dirs::home_dir()
207207
.context("Could not find home directory")?
208-
.join(".claude")
209-
.canonicalize()
210-
.context("Could not find ~/.claude directory")
208+
.join(".claude");
209+
210+
// First check if the directory exists
211+
if !claude_path.exists() {
212+
return Err(anyhow::anyhow!("~/.claude directory does not exist"));
213+
}
214+
215+
// Try to canonicalize, but fall back to the original path if it fails
216+
match claude_path.canonicalize() {
217+
Ok(canonical_path) => Ok(canonical_path),
218+
Err(_) => {
219+
// If canonicalize fails but the directory exists, use the original path
220+
log::warn!("Could not canonicalize ~/.claude path, using original path");
221+
Ok(claude_path)
222+
}
223+
}
211224
}
212225

213226
/// Gets the actual project path by reading the cwd from the JSONL entries

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)