From 2b04af0d1d434cf7fc677512667ffdb58b3098c3 Mon Sep 17 00:00:00 2001 From: Dilawar Singh Date: Sun, 23 Mar 2025 11:52:48 +0530 Subject: [PATCH 1/9] Update README.md Adds list of projects --- README.md | 3 +++ 1 file changed, 3 insertions(+) diff --git a/README.md b/README.md index 7a3d2e8..6b8ee22 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,5 @@ # rust-projects + Some simple rust projects for learning + +- [Leptos UI](./leptos) a simple SPA using leptos. From 07c2f2613e1c9665a3c957a124bb1c13cff342bf Mon Sep 17 00:00:00 2001 From: Dilawar Singh Date: Sun, 23 Mar 2025 11:54:03 +0530 Subject: [PATCH 2/9] Updates README --- README.md | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 6b8ee22..e359911 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,6 @@ # rust-projects -Some simple rust projects for learning +This is a playground for quick-and-dirty projects in Rust for learning. Once a +project matures, I usually moves it to its own repository. -- [Leptos UI](./leptos) a simple SPA using leptos. +1. [Leptos UI](./leptos) a simple SPA using leptos. From ee07f7d697fa0fd07a62f93c7ed1b0a030e01753 Mon Sep 17 00:00:00 2001 From: Dilawar Singh Date: Sun, 23 Mar 2025 11:55:09 +0530 Subject: [PATCH 3/9] Adds README.md --- leptos/README.md | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 leptos/README.md diff --git a/leptos/README.md b/leptos/README.md new file mode 100644 index 0000000..2030146 --- /dev/null +++ b/leptos/README.md @@ -0,0 +1,6 @@ +# Leptos UI + +A simple SPA using leptos. + +- A QR Scanner +- A Audio Recorder From 7c7497f38c4095650afbcb6570358b90f3454761 Mon Sep 17 00:00:00 2001 From: Dilawar Singh Date: Sun, 23 Mar 2025 11:58:29 +0530 Subject: [PATCH 4/9] dummy component to stream audio --- leptos/src/app.rs | 2 ++ leptos/src/components/audio.rs | 21 +++++++++++++++++++++ leptos/src/components/mod.rs | 3 +++ 3 files changed, 26 insertions(+) create mode 100644 leptos/src/components/audio.rs diff --git a/leptos/src/app.rs b/leptos/src/app.rs index 582168b..e8546d5 100644 --- a/leptos/src/app.rs +++ b/leptos/src/app.rs @@ -26,6 +26,7 @@ pub fn App() -> impl IntoView { "Home" "Form" "QR Scanner" + "Audio Rec" @@ -34,6 +35,7 @@ pub fn App() -> impl IntoView { +
diff --git a/leptos/src/components/audio.rs b/leptos/src/components/audio.rs new file mode 100644 index 0000000..7c6cdbb --- /dev/null +++ b/leptos/src/components/audio.rs @@ -0,0 +1,21 @@ +//! Audio Streamer component + +use leptos::prelude::*; +use leptos_qr_scanner::Scan; +use reactive_stores::Store; +use thaw::*; + +use crate::css::styles; +use crate::storage::{GlobalState, GlobalStateStoreFields}; + +#[component] +pub fn AudioStream() -> impl IntoView { + let stream = RwSignal::new(true); + + let result = RwSignal::new(vec![]); + + view! { + + + } +} diff --git a/leptos/src/components/mod.rs b/leptos/src/components/mod.rs index 48cd666..8ef4491 100644 --- a/leptos/src/components/mod.rs +++ b/leptos/src/components/mod.rs @@ -8,3 +8,6 @@ pub use login::*; pub(crate) mod qr; pub use qr::*; + +pub(crate) mod audio; +pub use audio::*; From 2819d1df194c808ffa78b80c5640c4fe33a067cc Mon Sep 17 00:00:00 2001 From: Dilawar Singh Date: Sun, 23 Mar 2025 17:09:47 +0530 Subject: [PATCH 5/9] Recording can be started/stopped easily --- leptos/Cargo.toml | 6 +++- leptos/src/components/audio.rs | 50 +++++++++++++++++++++++++++++----- 2 files changed, 48 insertions(+), 8 deletions(-) diff --git a/leptos/Cargo.toml b/leptos/Cargo.toml index 44fa2c1..79b0da7 100644 --- a/leptos/Cargo.toml +++ b/leptos/Cargo.toml @@ -8,7 +8,11 @@ codee = { version = "0.3.0", features = ["json_serde"] } console_error_panic_hook = "0.1.7" leptos = { version = "0.7.7", features = ["csr", "nightly", "tracing"] } leptos-qr-scanner = { git = "https://github.com/dilawar/leptos-qr-scanner" } -leptos-use = { version = "0.15.7", features = ["storage", "docs" ] } +leptos-use = { version = "0.15.7", features = [ + "storage", + "docs", + "use_user_media", +] } leptos_meta = "0.7.7" leptos_router = { version = "0.7.7", features = ["tracing"] } reactive_stores = "0.1.8" diff --git a/leptos/src/components/audio.rs b/leptos/src/components/audio.rs index 7c6cdbb..f1da74e 100644 --- a/leptos/src/components/audio.rs +++ b/leptos/src/components/audio.rs @@ -1,21 +1,57 @@ //! Audio Streamer component use leptos::prelude::*; -use leptos_qr_scanner::Scan; -use reactive_stores::Store; +use leptos_use::{use_user_media_with_options, UseUserMediaOptions, UseUserMediaReturn}; use thaw::*; use crate::css::styles; -use crate::storage::{GlobalState, GlobalStateStoreFields}; #[component] pub fn AudioStream() -> impl IntoView { - let stream = RwSignal::new(true); + let node = NodeRef::::new(); - let result = RwSignal::new(vec![]); + let start_rec = RwSignal::new(true); + let result = RwSignal::new_local("".to_string()); - view! { + let options = UseUserMediaOptions::default().video(false).audio(true); + + let UseUserMediaReturn { + stream, + start, + stop, + .. + } = use_user_media_with_options(options); + + start(); - + let effect = Effect::watch( + move || start_rec.get(), + move |val, _prev, _| { + if !val { + tracing::info!("Stopping recording."); + stop(); + } else { + tracing::info!("Starting recording."); + start(); + } + }, + false, + ); + + Effect::new(move |_| { + tracing::info!("State of the recording: {}.", start_rec.get_untracked()); + node.get().map(|v| match stream.get() { + Some(Ok(s)) => v.set_src_object(Some(&s)), + Some(Err(e)) => tracing::error!("Failed to get media stream: {e:?}"), + None => tracing::debug!("No stream yet"), + }); + }); + + view! { + + } } From 72437f1cb9f7c2712ebbed910c502693da720411 Mon Sep 17 00:00:00 2001 From: Dilawar Singh Date: Sun, 23 Mar 2025 21:33:16 +0530 Subject: [PATCH 6/9] temp: --- leptos/src/components/audio.rs | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/leptos/src/components/audio.rs b/leptos/src/components/audio.rs index f1da74e..09c2cbc 100644 --- a/leptos/src/components/audio.rs +++ b/leptos/src/components/audio.rs @@ -13,6 +13,7 @@ pub fn AudioStream() -> impl IntoView { let start_rec = RwSignal::new(true); let result = RwSignal::new_local("".to_string()); + // Create options to fetch only audio stream. let options = UseUserMediaOptions::default().video(false).audio(true); let UseUserMediaReturn { @@ -22,9 +23,10 @@ pub fn AudioStream() -> impl IntoView { .. } = use_user_media_with_options(options); - start(); + stream.add_event_listener_with_callback - let effect = Effect::watch( + // start/stop recording + let _effect = Effect::watch( move || start_rec.get(), move |val, _prev, _| { if !val { @@ -35,13 +37,16 @@ pub fn AudioStream() -> impl IntoView { start(); } }, - false, + true, /* Trigger it as soon as possible */ ); Effect::new(move |_| { - tracing::info!("State of the recording: {}.", start_rec.get_untracked()); + tracing::debug!("State of the recording: {}.", start_rec.get_untracked()); node.get().map(|v| match stream.get() { - Some(Ok(s)) => v.set_src_object(Some(&s)), + Some(Ok(s)) => { + tracing::debug!("Setting stream {s:?} to src..."); + v.set_src_object(Some(&s)); + }, Some(Err(e)) => tracing::error!("Failed to get media stream: {e:?}"), None => tracing::debug!("No stream yet"), }); @@ -49,9 +54,10 @@ pub fn AudioStream() -> impl IntoView { view! { + // Eventually I was to draw something related to audio stream here. + } } From 1717422ec3d702fdd3d3a68d83aeb3d1caa2fd0b Mon Sep 17 00:00:00 2001 From: Dilawar Singh Date: Mon, 24 Mar 2025 11:57:51 +0530 Subject: [PATCH 7/9] remove nightly --- leptos/Cargo.toml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/leptos/Cargo.toml b/leptos/Cargo.toml index 79b0da7..e3455a4 100644 --- a/leptos/Cargo.toml +++ b/leptos/Cargo.toml @@ -6,7 +6,7 @@ edition = "2024" [dependencies] codee = { version = "0.3.0", features = ["json_serde"] } console_error_panic_hook = "0.1.7" -leptos = { version = "0.7.7", features = ["csr", "nightly", "tracing"] } +leptos = { version = "0.7.7", features = ["csr", "tracing"] } leptos-qr-scanner = { git = "https://github.com/dilawar/leptos-qr-scanner" } leptos-use = { version = "0.15.7", features = [ "storage", @@ -18,7 +18,7 @@ leptos_router = { version = "0.7.7", features = ["tracing"] } reactive_stores = "0.1.8" serde = { workspace = true, features = ["derive"] } stylance = "0.5.5" -thaw = { version = "0.4.4", features = ["csr", "nightly"] } +thaw = { version = "0.4.4", features = ["csr"] } tracing.workspace = true tracing-subscriber.workspace = true tracing-subscriber-wasm = "0.1.0" From 11b1d51d8d1259bbac793fa8d08b268ec0677f54 Mon Sep 17 00:00:00 2001 From: Dilawar Singh Date: Mon, 24 Mar 2025 11:58:07 +0530 Subject: [PATCH 8/9] Remove nightly from toolchain file as well --- leptos/rust-toolchain.toml | 1 - 1 file changed, 1 deletion(-) diff --git a/leptos/rust-toolchain.toml b/leptos/rust-toolchain.toml index 962e35d..e7b48e6 100644 --- a/leptos/rust-toolchain.toml +++ b/leptos/rust-toolchain.toml @@ -1,3 +1,2 @@ [toolchain] -channel = "nightly" targets = ["wasm32-unknwon-unknown"] From e73339898498b3d4e61d5891ea26ca4c576406c5 Mon Sep 17 00:00:00 2001 From: Dilawar Singh Date: Mon, 24 Mar 2025 12:00:05 +0530 Subject: [PATCH 9/9] removes stray code --- leptos/src/components/audio.rs | 2 -- 1 file changed, 2 deletions(-) diff --git a/leptos/src/components/audio.rs b/leptos/src/components/audio.rs index 09c2cbc..18aa5d8 100644 --- a/leptos/src/components/audio.rs +++ b/leptos/src/components/audio.rs @@ -23,8 +23,6 @@ pub fn AudioStream() -> impl IntoView { .. } = use_user_media_with_options(options); - stream.add_event_listener_with_callback - // start/stop recording let _effect = Effect::watch( move || start_rec.get(),