-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.rs
More file actions
61 lines (54 loc) · 2.35 KB
/
Copy pathmain.rs
File metadata and controls
61 lines (54 loc) · 2.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
// Import necessary Yew functionalities
use yew::prelude::*;
use yew_router::prelude::*;
use yew::Renderer;
use stylist::yew::Global; // For applying global styles
use web_sys::console; // For logging to the browser's console
use wasm_bindgen::prelude::*;
use js_sys::JsString; // For handling JavaScript strings in Rust
// Define your app's global style, route switch, and contexts here.
use yew_javascript_interop_example::global_style::global_style;
// use yew_javascript_interop_example::route::{switch, Route};
// use yew_javascript_interop_example::contexts::text_provider::TextProvider;
// Inline JavaScript function example embedded directly in Rust.
// It gets the 'session_id' from the URL's query parameters or returns 'ID_NOT_FOUND' if not present.
#[wasm_bindgen(inline_js = "
export function getSessionId1() {
const params = new URLSearchParams(window.location.search);
return params.get('session_id') || 'ID_NOT_FOUND';
}
")]
extern "C" {
fn getSessionId1() -> JsString;
}
// Imported JavaScript function example defined in a separate JavaScript file, located in your project's directory.
// Note: The file will be moved to the 'snippets' folder during the build process if using `cargo build`.
#[wasm_bindgen(module = "/session2.js")]
extern "C" {
fn getSessionId2() -> JsString;
}
// The main App component
#[function_component(App)]
pub fn app() -> Html {
// Call the inline JavaScript function and log the result to the console
let session_id_inline = getSessionId1().as_string().unwrap_or_else(|| "No Session ID".to_string());
console::log_1(&JsValue::from_str(&format!("Session ID Inline: {}", session_id_inline)));
// Call the imported JavaScript function and log the result to the console
let session_id_module = getSessionId2().as_string().unwrap_or_else(|| "No Session ID".to_string());
console::log_1(&JsValue::from_str(&format!("Session ID Module: {}", session_id_module)));
html! {
<>
<BrowserRouter>
<Global css={ global_style() } />
<Switch<Route> render={switch} />
</BrowserRouter>
</>
}
}
// Main function for initializing the Yew application
fn main() {
// Initialize the logger for WebAssembly
wasm_logger::init(wasm_logger::Config::default());
// Render the App component
Renderer::<App>::new().render();
}