use crate::Result;
use futures_util::SinkExt;
use std::net::SocketAddr;
use tokio_tungstenite::tungstenite::Message;
use crate::view::{component, view};
pub async fn notify_ready(addr: Option<SocketAddr>) {
let Ok(base) = std::env::var("TOPCOAT_DEV_URL") else {
return;
};
let ws_url = http_to_ws(&base) + "/ws";
let Ok((mut ws, _)) = tokio_tungstenite::connect_async(&ws_url).await else {
eprintln!("topcoat dev: failed to connect to {ws_url}");
return;
};
let text = match addr {
Some(addr) => format!("ready {addr}"),
None => "ready".to_owned(),
};
let _ = ws.send(Message::Text(text.into())).await;
let _ = ws.close(None).await;
}
fn http_to_ws(url: &str) -> String {
if let Some(rest) = url.strip_prefix("http://") {
format!("ws://{rest}")
} else if let Some(rest) = url.strip_prefix("https://") {
format!("wss://{rest}")
} else {
url.to_string()
}
}
#[component]
pub async fn script(#[default(true)] status_indicator: bool) -> Result {
let Ok(base) = std::env::var("TOPCOAT_DEV_URL") else {
return view! {};
};
let src = format!("{base}/dev.js");
let indicator_off = (!status_indicator).then_some("false");
view! { <script src=(src) data-status-indicator=(indicator_off)></script> }
}