1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
use dioxus::prelude::*;
use dioxus_native_core::NodeId;
use dioxus_router::prelude::use_navigator;
use freya_components::*;
use fermi::*;
use freya_hooks::{ScrollController, use_scroll_controller};

use crate::{node::NodeElement, NodeIdSerializer, Route, TreeNode};

static SCROLL: Atom<i32> = Atom(|_| 0);

#[allow(non_snake_case)]
#[inline_props]
pub fn NodesTree<'a>(
    cx: Scope<'a>,
    height: &'a str,
    selected_node_id: Option<NodeId>,
    onselected: EventHandler<'a, &'a TreeNode>,
) -> Element<'a> {
    let scroll_state = use_atom_state(cx, &SCROLL);
    let scroll_controller =  use_scroll_controller(cx, || ScrollController::new_vertical(scroll_state.into()));
    let router = use_navigator(cx);
    let nodes = use_shared_state::<Vec<TreeNode>>(cx).unwrap();

    render!(VirtualScrollView {
        width: "100%",
        height: "{height}",
        padding: "15",
        show_scrollbar: true,
        length: nodes.read().len(),
        item_size: 27.0,
        scroll_controller: scroll_controller,
        builder_values: (nodes, selected_node_id, onselected, router),
        builder: Box::new(move |(_k, i, _, values)| {
            let (nodes, selected_node_id, onselected, router) = values.unwrap();
            let nodes = nodes.read();
            let node = nodes.get(i).cloned().unwrap();
            rsx! {
                NodeElement {
                    key: "{node.id:?}",
                    is_selected: Some(node.id) == *selected_node_id,
                    onselected: |node: &TreeNode| {
                        onselected.call(node);
                        router.replace(Route::TreeStyleTab { node_id: node.id.serialize() });
                    },
                    node: node
                }
            }
        })
    })
}