Skip to content

Commit

Permalink
optimize and cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
miralandlabs committed Jun 21, 2024
1 parent 1801016 commit 3715408
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 13 deletions.
15 changes: 13 additions & 2 deletions src/components/code_block.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
use dioxus::prelude::*;
#[cfg(feature = "desktop")]
use dioxus_std::clipboard::use_clipboard;

use crate::components::CopyIcon;
#[cfg(feature = "web")]
use crate::hooks::use_clipboard;

#[component]
pub fn CodeBlock(text: String) -> Element {
let mut clipboard = use_clipboard();
let clipboard = use_clipboard();
let mut solid = use_signal(|| false);
use_future(move || async move {
if *solid.read() {
Expand All @@ -28,7 +30,16 @@ pub fn CodeBlock(text: String) -> Element {
button {
class: "flex shrink-0 px-2 py-1 mb-auto rounded hover-100 active-200 transition-colors",
onclick: move |_e| {
// clipboard.set(text.clone()).ok();

#[cfg(feature = "web")]
if let Some(cb) = clipboard.clone() {
let _ = cb.write_text(&text);
}

#[cfg(feature = "desktop")]
clipboard.set(text.clone()).ok();

solid.set(true);
},
CopyIcon {
Expand Down
20 changes: 19 additions & 1 deletion src/components/copyable.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use dioxus::prelude::*;
#[cfg(feature = "desktop")]
use dioxus_std::clipboard::use_clipboard;

use crate::components::CopyIcon;
#[cfg(feature = "web")]
use crate::hooks::use_clipboard;

#[component]
pub fn Copyable(
Expand All @@ -26,7 +28,15 @@ pub fn Copyable(
button {
class: "flex flex-row gap-2 shrink-0 p-2 mx-auto rounded transition-colors hover-100 active-200 font-semibold",
onclick: move |_e| {
// clipboard.set(value.clone()).ok();
#[cfg(feature = "web")]
if let Some(cb) = clipboard.clone() {
let _ = cb.write_text(&value);
}

#[cfg(feature = "desktop")]
clipboard.set(value.clone()).ok();

solid.set(true);
},
CopyIcon {
Expand All @@ -45,7 +55,15 @@ pub fn Copyable(
button {
class: "flex shrink-0 p-2 rounded transition-colors hover-100 active-200",
onclick: move |_e| {
// clipboard.set(value.clone()).ok();
#[cfg(feature = "web")]
if let Some(cb) = clipboard.clone() {
let _ = cb.write_text(&value);
}

#[cfg(feature = "desktop")]
clipboard.set(value.clone()).ok();

solid.set(true);
},
CopyIcon {
Expand Down
8 changes: 8 additions & 0 deletions src/components/import_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ pub enum ImportKeyStep {
Import,
}

// MI
#[component]
pub fn ImportKey() -> Element {
let mut step = use_signal(|| ImportKeyStep::Loading);
let sol_balance = use_sol_balance();
Expand Down Expand Up @@ -59,6 +61,8 @@ pub fn ImportKey() -> Element {
e
}

// MI
#[component]
fn ImportKeyLoading() -> Element {
rsx! {
div {
Expand Down Expand Up @@ -88,6 +92,8 @@ fn ImportKeyWarning(step: Signal<ImportKeyStep>, balance: u64) -> Element {
}
}

// MI
#[component]
fn ImportKeyHeader() -> Element {
rsx! {
div {
Expand All @@ -109,6 +115,8 @@ fn ImportKeyHeader() -> Element {

const KEY_LENGTH: usize = 64;

// MI
#[component]
fn ImportKeyImport() -> Element {
let mut sol_balance = use_signal::<Option<u64>>(|| None);
let mut keypair_persistent = use_keypair_persistent();
Expand Down
4 changes: 2 additions & 2 deletions src/components/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ mod backup_keypair_warning;
mod balance;
// mod banner;
mod claim;
// mod code_block;
mod code_block;
mod copyable;
// mod download;
mod export_key;
Expand Down Expand Up @@ -37,7 +37,7 @@ pub use backup_keypair_warning::*;
pub use balance::*;
// pub use banner::*;
pub use claim::*;
// pub use code_block::*;
pub use code_block::*;
pub use copyable::*;
// pub use download::*;
pub use export_key::*;
Expand Down
4 changes: 4 additions & 0 deletions src/hooks/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
mod use_appearance;
#[cfg(feature = "web")]
mod use_clipboard;
mod use_date;
mod use_explorer;
mod use_gateway;
Expand All @@ -19,6 +21,8 @@ mod use_transfers;
mod use_treasury;

pub use use_appearance::*;
#[cfg(feature = "web")]
pub use use_clipboard::*;
pub use use_date::*;
pub use use_explorer::*;
pub use use_gateway::*;
Expand Down
9 changes: 9 additions & 0 deletions src/hooks/use_clipboard.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// use dioxus::prelude::*;
use web_sys::{window, Clipboard};

pub fn use_clipboard() -> Option<Clipboard> {
window()
.expect("Failed to get window")
.navigator()
.clipboard()
}
18 changes: 10 additions & 8 deletions src/miner/web_worker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ pub struct WebWorkerRequest {
}

/// Mining response for web workers
#[derive(Debug, Clone, Serialize, Deserialize)]
#[derive(Debug, Default, Clone, Serialize, Deserialize)]
pub struct WebWorkerResponse {
pub hash: KeccakHash,
pub nonce: u64,
Expand All @@ -35,6 +35,7 @@ pub fn start_worker() {

scope.set_onmessage(Some(&js_sys::Function::unchecked_from_js(
Closure::<dyn Fn(MessageEvent)>::new(move |e: MessageEvent| {
// if let Err(_) = from_value::<WebWorkerRequest>(e.data()) { return; }; // MI
let req: WebWorkerRequest = from_value(e.data()).unwrap();
let res = find_next_hash(req.hash, req.difficulty, req.nonce, req.pubkey);
scope_.post_message(&to_value(&res).unwrap()).unwrap();
Expand All @@ -56,6 +57,7 @@ pub fn create_web_worker(cx: UseChannel<WebWorkerResponse>) -> Worker {
// On message
worker.set_onmessage(Some(&js_sys::Function::unchecked_from_js(
Closure::<dyn Fn(MessageEvent)>::new(move |e: MessageEvent| {
// if let Err(_) = from_value::<WebWorkerResponse>(e.data()) { return; }; // MI
let res: WebWorkerResponse = from_value(e.data()).unwrap();
async_std::task::block_on({
let cx = cx.clone();
Expand All @@ -67,13 +69,13 @@ pub fn create_web_worker(cx: UseChannel<WebWorkerResponse>) -> Worker {
.into_js_value(),
)));

// On error
worker.set_onerror(Some(&js_sys::Function::unchecked_from_js(
Closure::<dyn Fn(MessageEvent)>::new(move |e: MessageEvent| {
log::info!("Error from worker: {:?}", e.data());
})
.into_js_value(),
)));
// // On error
// worker.set_onerror(Some(&js_sys::Function::unchecked_from_js(
// Closure::<dyn Fn(MessageEvent)>::new(move |e: MessageEvent| {
// log::info!("Error from worker: {:?}", e.data());
// })
// .into_js_value(),
// )));

worker
}
Expand Down

0 comments on commit 3715408

Please sign in to comment.