Commit 705cd1de
705cd1de995cMerged
Scott Nixon <[email protected]> ยท 9 months ago ยท 1 file ยท ~1 modified
Description
[attractor-nb1] Update Terminal component to accept folder and container_id props
- Add folder: String and container_id: String props to Terminal component
- Update JavaScript interop from call1 to call2 to pass both arguments
- Add on_cleanup hook to call window.disposeTerminal on unmount
- Enable multiple terminal instances with unique container IDs
- Implement proper clone strategy for closure lifetimes
- Ensure complete resource cleanup (no memory leaks)
Details
- Commit
- 705cd1de995c
- Parent
- 47b6948fc534
crates/attractor-web/src/components/terminal.rsmodifiedView file
@@ -5,9 +5,18 @@
55
/// Renders a container div and calls `window.initTerminal()` from66
/// the inline script in the shell HTML. The JS handles WebSocket77
/// connection to the PTY bridge at `/api/terminal/ws`.8+
///9+
/// ## Props10+
/// - `folder`: Absolute path to the project directory where the PTY should spawn11+
/// - `container_id`: Unique ID for this terminal instance (e.g., "terminal-1")812
#[component]9-
pub fn Terminal() -> impl IntoView {10-
let container_id = "terminal-container";13+
pub fn Terminal(14+
#[prop(into)] folder: String,15+
#[prop(into)] container_id: String,16+
) -> impl IntoView {17+
// Clone for use in multiple closures18+
let container_id_clone = container_id.clone();19+
let folder_clone = folder.clone();1120
1221
// Initialize xterm.js after the element is mounted1322
#[cfg(feature = "hydrate")]@@ -16,18 +25,37 @@
1625
use wasm_bindgen::prelude::*;1726
1827
let window = web_sys::window().unwrap();28+
let container_id_inner = container_id_clone.clone();29+
let folder_inner = folder_clone.clone();30+
1931
let cb = Closure::once(move || {2032
let window = web_sys::window().unwrap();2133
if let Ok(func) = js_sys::Reflect::get(&window, &JsValue::from_str("initTerminal"))2234
{2335
if func.is_function() {2436
let func: js_sys::Function = func.into();25-
let _ = func.call1(&JsValue::NULL, &JsValue::from_str(container_id));37+
let _ = func.call2(38+
&JsValue::NULL,39+
&JsValue::from_str(&container_id_inner),40+
&JsValue::from_str(&folder_inner),41+
);2642
}2743
}2844
});2945
let _ = window.request_animation_frame(cb.as_ref().unchecked_ref());3046
cb.forget();47+
48+
// Cleanup: dispose terminal when component unmounts49+
on_cleanup(move || {50+
let window = web_sys::window().unwrap();51+
if let Ok(func) = js_sys::Reflect::get(&window, &JsValue::from_str("disposeTerminal"))52+
{53+
if func.is_function() {54+
let func: js_sys::Function = func.into();55+
let _ = func.call1(&JsValue::NULL, &JsValue::from_str(&container_id_clone));56+
}57+
}58+
});3159
});3260
}3361