Skip to main content

dioxus_leaflet/components/
polyline.rs

1use std::rc::Rc;
2
3use dioxus::{
4    core::{spawn_forever, use_drop},
5    prelude::*,
6};
7use dioxus_logger::tracing::error;
8
9use crate::{LatLng, PathOptions, interop, types::Id};
10
11#[component]
12pub fn Polyline(
13    coordinates: ReadSignal<Vec<Vec<LatLng>>>,
14    #[props(default = PathOptions::default())] options: ReadSignal<PathOptions>,
15    children: Element,
16) -> Element {
17    let id: Rc<Id> = use_context();
18    let id = use_context_provider(|| Rc::new(Id::polyline(&id, dioxus_core::current_scope_id().0)));
19
20    let id2 = id.clone();
21    use_effect(move || {
22        let id = id2.clone();
23        let coords = coordinates();
24        let opts = options();
25        spawn(async move {
26            if let Err(e) = interop::update_polyline(&id, &coords, &opts).await {
27                error!("{e}");
28            }
29        });
30    });
31
32    let id2 = id.clone();
33    use_drop(move || {
34        let id = id2.clone();
35        spawn_forever(async move {
36            if let Err(e) = interop::delete_polyline(&id).await {
37                error!("{e}");
38            }
39        });
40    });
41
42    rsx!(
43        {children}
44
45    )
46}