simple channel & user tree
This commit is contained in:
+70
@@ -1,19 +1,87 @@
|
||||
#![allow(non_snake_case)]
|
||||
|
||||
use std::collections::{BTreeMap, BTreeSet, HashMap};
|
||||
|
||||
use dioxus::prelude::*;
|
||||
use ordermap::OrderSet;
|
||||
|
||||
use super::Command;
|
||||
use super::Command::*;
|
||||
use super::ConnectionState::{self, *};
|
||||
|
||||
pub type ChannelId = u32;
|
||||
pub type UserId = u32;
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct ChannelState {
|
||||
pub name: String,
|
||||
pub children: OrderSet<ChannelId>,
|
||||
pub users: OrderSet<UserId>,
|
||||
pub parent: Option<ChannelId>,
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct UserState {
|
||||
pub name: String,
|
||||
pub channel: ChannelId,
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct ServerState {
|
||||
pub channels: HashMap<ChannelId, ChannelState>,
|
||||
pub users: HashMap<UserId, UserState>,
|
||||
}
|
||||
|
||||
pub struct State {
|
||||
pub status: GlobalSignal<ConnectionState>,
|
||||
pub server: GlobalSignal<ServerState>,
|
||||
}
|
||||
|
||||
pub static STATE: State = State {
|
||||
status: Signal::global(|| Disconnected),
|
||||
server: Signal::global(|| Default::default()),
|
||||
};
|
||||
|
||||
#[component]
|
||||
pub fn User(id: UserId) -> Element {
|
||||
let server = STATE.server.read();
|
||||
let state = server.users.get(&id)?;
|
||||
rsx!(
|
||||
span {
|
||||
style: "border: solid black 1px; border-radius: 4px;",
|
||||
"{state.name}"
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
#[component]
|
||||
pub fn Channel(id: ChannelId) -> Element {
|
||||
let server = STATE.server.read();
|
||||
let state = server.channels.get(&id)?;
|
||||
rsx!(
|
||||
"{state.name}"
|
||||
div {
|
||||
style: "border-left: solid black 1px; padding-left: 8px;",
|
||||
for child in state.children.iter() {
|
||||
Channel { id: *child }
|
||||
}
|
||||
for id in state.users.iter() {
|
||||
User { id: *id }
|
||||
}
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
#[component]
|
||||
pub fn ServerView() -> Element {
|
||||
let server = STATE.server.read();
|
||||
rsx!(for (id, state) in server.channels.iter() {
|
||||
if state.parent.is_none() {
|
||||
Channel { id: *id }
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
pub fn app() -> Element {
|
||||
let net = use_coroutine(|rx: UnboundedReceiver<Command>| super::network_entrypoint(rx));
|
||||
let mut username = use_signal(|| "".to_string());
|
||||
@@ -52,6 +120,8 @@ pub fn app() -> Element {
|
||||
onclick: move |event| net.send(Disconnect),
|
||||
"Disconnect"
|
||||
}
|
||||
br {}
|
||||
ServerView {}
|
||||
}
|
||||
}
|
||||
br {}
|
||||
|
||||
Reference in New Issue
Block a user