use crate::{
Context, Entity, IntoElement, ParentElement, Render, SharedString, Styled, StyledExt, Window,
div, v_flex,
};
#[derive(Debug, Clone)]
pub enum MessageType {
Text(String),
CodeBlock {
language: String,
code: String,
},
}
#[derive(Debug, Clone)]
pub struct Message {
pub content: MessageType,
pub id: String,
}
impl Message {
pub fn text(content: impl Into<String>) -> Self {
Self {
content: MessageType::Text(content.into()),
id: uuid::Uuid::new_v4().to_string(),
}
}
pub fn code_block(language: impl Into<String>, code: impl Into<String>) -> Self {
Self {
content: MessageType::CodeBlock {
language: language.into(),
code: code.into(),
},
id: uuid::Uuid::new_v4().to_string(),
}
}
}
#[derive(Debug, Clone)]
pub struct MessageGroup {
pub sender: SharedString,
pub messages: Vec<Message>,
}
#[derive(Default)]
pub struct ChatState {
pub groups: Vec<MessageGroup>,
pub input_text: String,
}
impl ChatState {
pub fn add_group(&mut self, group: MessageGroup) {
self.groups.push(group);
}
pub fn clear(&mut self) {
self.groups.clear();
}
}
pub struct ChatView {
state: Entity<ChatState>,
}
impl ChatView {
pub fn new(state: Entity<ChatState>) -> Self {
Self { state }
}
}
impl Render for ChatView {
fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
let state = self.state.read(cx);
v_flex()
.w_full()
.h_full()
.children(state.groups.iter().map(|group| {
v_flex()
.w_full()
.gap_1()
.py_2()
.child(
div()
.text_sm()
.font_semibold()
.text_color(crate::gray_300())
.child(group.sender.clone()),
)
.children(group.messages.iter().map(|msg| {
match &msg.content {
MessageType::Text(text) => div()
.text_sm()
.text_color(crate::gray_100())
.child(text.clone()),
MessageType::CodeBlock { language, code } => v_flex()
.w_full()
.bg(crate::gray_800())
.rounded_md()
.overflow_hidden()
.child(
div()
.px_3()
.py_1()
.bg(crate::gray_700())
.text_xs()
.text_color(crate::gray_400())
.child(language.clone()),
)
.child(div().px_3().py_2().text_sm().child(code.clone())),
}
}))
}))
}
}
pub mod actions;
pub mod attachments;
pub mod bubble;
pub mod marker;
pub mod prompts;
pub mod scroller;
pub mod sender;
pub mod sources;
pub mod suggestion;
pub mod thought_chain;
pub use actions::*;
pub use attachments::*;
pub use bubble::*;
pub use marker::*;
pub use prompts::*;
pub use scroller::*;
pub use sender::*;
pub use sources::*;
pub use suggestion::*;
pub use thought_chain::*;