use gloo::net::http::Request; use wasm_bindgen_futures::spawn_local; use yew::prelude::*; use crate::{api::APIEndpoints, components::refresh_button::RefreshButton}; use base64::{Engine as _, engine::general_purpose}; fn string_to_base64_data_url(data: &str, content_type: &str) -> String { let mut buf = String::new(); general_purpose::STANDARD.encode_string(data, &mut buf); format!("data:{};base64,{}", content_type, buf) } #[derive(Properties, PartialEq)] pub struct ConfigProps { pub dir: AttrValue, pub file_name: AttrValue, #[prop_or("".into())] pub contents: AttrValue, } #[function_component(Config)] pub fn config(props: &ConfigProps) -> Html { let dir_name = props.dir.clone(); let file_name = props.file_name.clone(); let contents = use_state(|| props.contents.to_string()); let get_contents = { let contents = contents.clone(); Callback::from(move |_| { let url = APIEndpoints::get_contents(dir_name.clone().as_str(), file_name.clone().as_str()); let contents = contents.clone(); spawn_local(async move { match Request::get(&url).send().await { Ok(response) if response.ok() => { if let Ok(text) = response.text().await { contents.set(text); } } _ => contents.set("...Ошибка загрузки содержимого...".into()), } }) }) }; { let get_contents_clone = get_contents.clone(); let is_contents_empty = props.contents.is_empty(); use_effect_with((), move |_| { if is_contents_empty { get_contents_clone.emit(()); } }); } html! {
} }