simple chat window
This commit is contained in:
+40
-4
@@ -26,10 +26,17 @@ pub struct UserState {
|
||||
pub channel: ChannelId,
|
||||
}
|
||||
|
||||
pub struct Chat {
|
||||
pub text: String,
|
||||
pub dangerous_html: String,
|
||||
pub sender: Option<UserId>,
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct ServerState {
|
||||
pub channels: HashMap<ChannelId, ChannelState>,
|
||||
pub users: HashMap<UserId, UserState>,
|
||||
pub chat: Vec<Chat>,
|
||||
}
|
||||
|
||||
pub struct State {
|
||||
@@ -72,14 +79,43 @@ pub fn Channel(id: ChannelId) -> Element {
|
||||
)
|
||||
}
|
||||
|
||||
#[component]
|
||||
pub fn ChatHistory() -> Element {
|
||||
let server = STATE.server.read();
|
||||
rsx!(
|
||||
div {
|
||||
style: "margin: 16px; padding: 16px; border: solid black 1px;",
|
||||
for chat in server.chat.iter() {
|
||||
if let Some(sender) = chat.sender.and_then(|u| server.users.get(&u)) {
|
||||
span {
|
||||
style: "border: solid black 1px; border-radius: 4px;",
|
||||
"{sender.name}"
|
||||
}
|
||||
"\u{8194}"
|
||||
}
|
||||
span {
|
||||
dangerous_inner_html: "{chat.dangerous_html}",
|
||||
}
|
||||
hr {}
|
||||
}
|
||||
}
|
||||
)
|
||||
}
|
||||
|
||||
#[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 }
|
||||
rsx!(
|
||||
div {
|
||||
style: "margin: 16px; padding: 16px; border: solid black 1px;",
|
||||
for (id, state) in server.channels.iter() {
|
||||
if state.parent.is_none() {
|
||||
Channel { id: *id }
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
ChatHistory {}
|
||||
)
|
||||
}
|
||||
|
||||
pub fn app() -> Element {
|
||||
|
||||
+16
@@ -1,6 +1,7 @@
|
||||
pub mod app;
|
||||
|
||||
use app::ChannelState;
|
||||
use app::Chat;
|
||||
use app::UserState;
|
||||
use dioxus::prelude::*;
|
||||
|
||||
@@ -656,6 +657,21 @@ fn accept_packet(
|
||||
}
|
||||
}
|
||||
}
|
||||
ControlPacket::TextMessage(u) => {
|
||||
let mut server = STATE.server.write();
|
||||
if u.has_message() {
|
||||
let text = u.get_message().to_string();
|
||||
server.chat.push(Chat {
|
||||
sender: if u.has_actor() {
|
||||
Some(u.get_actor())
|
||||
} else {
|
||||
None
|
||||
},
|
||||
dangerous_html: html_purifier::purifier(&text, Default::default()),
|
||||
text: text,
|
||||
});
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user