Skip to content

Commit

Permalink
feat: Allow for verifying a service instance (#550)
Browse files Browse the repository at this point in the history
  • Loading branch information
hrzlgnm authored Nov 25, 2024
1 parent 254b8f0 commit 3f3c0a7
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 27 deletions.
12 changes: 11 additions & 1 deletion src-tauri/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
use clap::builder::TypedValueParser as _;
#[cfg(desktop)]
use clap::Parser;
use mdns_sd::{ServiceDaemon, ServiceEvent, ServiceInfo};
use mdns_sd::{ServiceDaemon, ServiceEvent, ServiceInfo, VERIFY_TIMEOUT_DEFAULT};
use models::*;
#[cfg(all(desktop, not(debug_assertions)))]
use shared_constants::SPLASH_SCREEN_DURATION;
Expand Down Expand Up @@ -102,6 +102,14 @@ fn stop_browse(service_type: String, state: State<ManagedState>) {
}
}

#[tauri::command]
fn verify(instance_fullname: String, state: State<ManagedState>) {
if let Ok(mdns) = state.daemon.lock() {
mdns.verify(instance_fullname, VERIFY_TIMEOUT_DEFAULT)
.expect("To verify an instance");
}
}

fn from_service_info(info: &ServiceInfo) -> ResolvedService {
let mut sorted_addresses: Vec<IpAddr> = info.get_addresses().clone().drain().collect();
sorted_addresses.sort();
Expand Down Expand Up @@ -476,6 +484,7 @@ pub fn run() {
open,
send_metrics,
stop_browse,
verify,
version,
])
.run(tauri::generate_context!())
Expand All @@ -497,6 +506,7 @@ pub fn run_mobile() {
open,
send_metrics,
stop_browse,
verify,
])
.run(tauri::generate_context!())
.expect("error while running tauri application");
Expand Down
81 changes: 55 additions & 26 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,22 @@ async fn stop_browse(service_type: String) {
.await;
}

#[derive(Serialize, Deserialize)]
#[allow(non_snake_case)]
struct VerifyArgs<'a> {
instanceFullname: &'a str,
}

async fn verify_instance(instance_fullname: String) {
let _ = invoke::<()>(
"verify",
&VerifyArgs {
instanceFullname: &instance_fullname,
},
)
.await;
}

/// Component to render a string vector into a table
#[component]
fn ValuesTable(values: Vec<String>, #[prop(into)] title: String) -> impl IntoView {
Expand Down Expand Up @@ -294,21 +310,6 @@ fn AutoCompleteServiceType(
}
}

#[derive(Serialize, Deserialize)]
struct CopyToClipboardArgs<'a> {
contents: &'a str,
}

async fn copy_to_clipboard(contents: String) {
let _ = invoke::<()>(
"copy_to_clipboard",
&CopyToClipboardArgs {
contents: &contents,
},
)
.await;
}

/// Component that allows to copy the shown text to the clipboard
#[component]
fn ToClipBoardCopyable(
Expand Down Expand Up @@ -339,10 +340,38 @@ fn ToClipBoardCopyable(
}
}

#[derive(Serialize, Deserialize)]
struct CopyToClipboardArgs<'a> {
contents: &'a str,
}

async fn copy_to_clipboard(contents: String) {
let _ = invoke::<()>(
"copy_to_clipboard",
&CopyToClipboardArgs {
contents: &contents,
},
)
.await;
}

/// Component that shows a service as a card
#[component]
fn ResolvedServiceGridItem(resolved_service: ResolvedService) -> impl IntoView {
log::debug!("ResolvedServiceGridItem");

let instance_fullname = create_rw_signal(format!(
"{}.{}",
resolved_service.instance_name, resolved_service.hostname
));
let verify_action = create_action(|instance_fullname: &String| {
let instance_fullname = instance_fullname.clone();
async move { verify_instance(instance_fullname.clone()).await }
});
let on_verify_click = move |_| {
verify_action.dispatch(instance_fullname.get_untracked());
};

let mut hostname = resolved_service.hostname;
hostname.pop(); // remove the trailing dot
let updated_at = DateTime::from_timestamp_millis(resolved_service.updated_at_ms as i64)
Expand All @@ -362,20 +391,14 @@ fn ResolvedServiceGridItem(resolved_service: ResolvedService) -> impl IntoView {
None => vec![],
Some(s) => vec![s],
};

let card_title = get_instance_name(resolved_service.instance_name.as_str());
let details_title = card_title.clone();
let show_details = create_rw_signal(false);
let hostname_variant = match resolved_service.dead {
true => TagVariant::Default,
false => TagVariant::Success,
};
let port_variant = match resolved_service.dead {
true => TagVariant::Default,
false => TagVariant::Warning,
};
let addrs_footer = match resolved_service.dead {
true => vec![],
false => addrs.clone(),
let (hostname_variant, port_variant, addrs_footer) = if resolved_service.dead {
(TagVariant::Default, TagVariant::Default, vec![])
} else {
(TagVariant::Success, TagVariant::Warning, addrs.clone())
};
view! {
<GridItem>
Expand All @@ -384,6 +407,12 @@ fn ResolvedServiceGridItem(resolved_service: ResolvedService) -> impl IntoView {
{as_local_datetime.format("%Y-%m-%d %H:%M:%S").to_string()}
</CardHeaderExtra>
<Space align=SpaceAlign::Center>
<Button
size=ButtonSize::Tiny
on_click=on_verify_click
disabled=resolved_service.dead
icon=icondata::MdiCheckAll
/>
<Tag variant=hostname_variant>{hostname}</Tag>
<Tag variant=port_variant>{resolved_service.port}</Tag>
<Button
Expand Down

0 comments on commit 3f3c0a7

Please sign in to comment.