use crate::app_types::{AppPublicSiteInfo, AppSiteConnection};
use bounce::*;
use dialtone_reqwest::api_v1::sites::get_site_info::get_site_info;
use dialtone_reqwest::site_connection::SiteConnection;
use gloo::console::log;
use yew::prelude::*;
use yew_router::prelude::*;
use crate::main_router::*;
pub mod app_types;
pub mod components;
pub mod main_router;
pub mod utils;
const IPV4_LOCAL_ADDR: &str = "127.0.0.1";
const IPV6_LOCAL_ADDR: &str = "::1";
const LOCALHOST: &str = "localhost";
#[function_component(App)]
fn app() -> Html {
log!("Starting DialTone Yew App");
let (site_connection, host_name) = get_host_info();
log!(
"sites backend =",
&site_connection.host_addr,
";secure =",
&site_connection.secure.to_string(),
";host name =",
&host_name
);
let sc_host_addr = site_connection.host_addr;
html! {
<BounceRoot>
<AppSiteInfoSetter host={sc_host_addr} site_name={host_name} />
<BrowserRouter>
<Switch<MainRoute> render={Switch::render(main_route_switch)} />
</BrowserRouter>
</BounceRoot>
}
}
pub fn get_host_info() -> (SiteConnection, String) {
let location = gloo::utils::window().location();
let location_host = location.host().unwrap();
let location_host_name = location.hostname().unwrap();
let (sc_host_addr, sc_host_name) = match location_host_name.as_str() {
IPV4_LOCAL_ADDR => (format!("{}:3000", location_host_name), LOCALHOST),
IPV6_LOCAL_ADDR => (format!("{}:3000", location_host_name), LOCALHOST),
_ => (location_host, location_host_name.as_str()),
};
let protocol = location.protocol().unwrap();
let secure = protocol != "http:";
(
SiteConnection {
host_addr: sc_host_addr,
secure,
auth_data: None,
host_name: Some(location_host_name.clone()),
user_name: None,
},
sc_host_name.to_string(),
)
}
#[derive(Properties, Debug, PartialEq)]
pub struct AppSiteInfoSetterProps {
host: String,
site_name: String,
}
#[function_component(AppSiteInfoSetter)]
fn app_site_info_setter(props: &AppSiteInfoSetterProps) -> Html {
let site_connection = SiteConnection::new_site(&props.host, &props.site_name);
let atom = use_atom_setter::<AppPublicSiteInfo>();
{
use_effect_with_deps(
move |_| {
wasm_bindgen_futures::spawn_local(async move {
let public_site_info = get_site_info(&site_connection).await.unwrap();
let app_public_site_info = AppPublicSiteInfo(Some(public_site_info));
let _ = &app_public_site_info.save();
atom(app_public_site_info);
});
|| ()
},
(),
)
}
let h = html! {
<></>
};
h
}
fn main() {
yew::start_app::<App>();
}