2024-01-03 18:30:52 +00:00
|
|
|
|
use crate::{channel_view::ChannelView, is_channels_feature_enabled, ChatPanelSettings};
|
2023-09-08 01:06:05 +00:00
|
|
|
|
use anyhow::Result;
|
2023-09-15 01:05:44 +00:00
|
|
|
|
use call::ActiveCall;
|
2023-09-14 23:22:21 +00:00
|
|
|
|
use channel::{ChannelChat, ChannelChatEvent, ChannelMessageId, ChannelStore};
|
2023-10-04 21:29:08 +00:00
|
|
|
|
use client::Client;
|
2023-10-05 00:47:30 +00:00
|
|
|
|
use collections::HashMap;
|
2023-09-08 01:06:05 +00:00
|
|
|
|
use db::kvp::KEY_VALUE_STORE;
|
2023-09-07 18:16:51 +00:00
|
|
|
|
use editor::Editor;
|
|
|
|
|
use gpui::{
|
2024-01-03 18:30:52 +00:00
|
|
|
|
actions, div, list, prelude::*, px, serde_json, AnyElement, AppContext, AsyncWindowContext,
|
|
|
|
|
ClickEvent, ElementId, EventEmitter, FocusableView, ListOffset, ListScrollEvent, ListState,
|
|
|
|
|
Model, Render, Subscription, Task, View, ViewContext, VisualContext, WeakView,
|
2023-09-07 18:16:51 +00:00
|
|
|
|
};
|
2023-10-18 00:59:42 +00:00
|
|
|
|
use language::LanguageRegistry;
|
2023-09-07 18:16:51 +00:00
|
|
|
|
use menu::Confirm;
|
2023-10-18 00:59:42 +00:00
|
|
|
|
use message_editor::MessageEditor;
|
2023-09-08 01:06:05 +00:00
|
|
|
|
use project::Fs;
|
2023-10-05 21:19:13 +00:00
|
|
|
|
use rich_text::RichText;
|
2023-09-08 01:06:05 +00:00
|
|
|
|
use serde::{Deserialize, Serialize};
|
2024-01-03 18:30:52 +00:00
|
|
|
|
use settings::{Settings, SettingsStore};
|
2023-09-07 18:16:51 +00:00
|
|
|
|
use std::sync::Arc;
|
2024-01-03 18:30:52 +00:00
|
|
|
|
use theme::ActiveTheme as _;
|
2023-09-07 18:16:51 +00:00
|
|
|
|
use time::{OffsetDateTime, UtcOffset};
|
2024-01-03 18:30:52 +00:00
|
|
|
|
use ui::{prelude::*, Avatar, Button, Icon, IconButton, Label, TabBar, Tooltip};
|
2023-09-07 18:16:51 +00:00
|
|
|
|
use util::{ResultExt, TryFutureExt};
|
2023-09-08 01:06:05 +00:00
|
|
|
|
use workspace::{
|
2024-01-03 18:30:52 +00:00
|
|
|
|
dock::{DockPosition, Panel, PanelEvent},
|
2023-09-08 01:06:05 +00:00
|
|
|
|
Workspace,
|
|
|
|
|
};
|
2023-09-07 18:16:51 +00:00
|
|
|
|
|
2023-10-18 00:59:42 +00:00
|
|
|
|
mod message_editor;
|
|
|
|
|
|
2023-09-07 18:16:51 +00:00
|
|
|
|
const MESSAGE_LOADING_THRESHOLD: usize = 50;
|
2023-09-08 01:06:05 +00:00
|
|
|
|
const CHAT_PANEL_KEY: &'static str = "ChatPanel";
|
2023-09-07 18:16:51 +00:00
|
|
|
|
|
2024-01-03 18:30:52 +00:00
|
|
|
|
pub fn init(cx: &mut AppContext) {
|
|
|
|
|
cx.observe_new_views(|workspace: &mut Workspace, _| {
|
|
|
|
|
workspace.register_action(|workspace, _: &ToggleFocus, cx| {
|
|
|
|
|
workspace.toggle_panel_focus::<ChatPanel>(cx);
|
|
|
|
|
});
|
|
|
|
|
})
|
|
|
|
|
.detach();
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-07 18:16:51 +00:00
|
|
|
|
pub struct ChatPanel {
|
|
|
|
|
client: Arc<Client>,
|
2024-01-03 18:30:52 +00:00
|
|
|
|
channel_store: Model<ChannelStore>,
|
2023-10-05 00:47:30 +00:00
|
|
|
|
languages: Arc<LanguageRegistry>,
|
2024-01-03 18:30:52 +00:00
|
|
|
|
message_list: ListState,
|
|
|
|
|
active_chat: Option<(Model<ChannelChat>, Subscription)>,
|
|
|
|
|
input_editor: View<MessageEditor>,
|
2023-09-07 18:16:51 +00:00
|
|
|
|
local_timezone: UtcOffset,
|
2023-09-08 01:06:05 +00:00
|
|
|
|
fs: Arc<dyn Fs>,
|
2024-01-03 18:30:52 +00:00
|
|
|
|
width: Option<Pixels>,
|
2023-10-04 00:40:10 +00:00
|
|
|
|
active: bool,
|
2023-09-08 01:06:05 +00:00
|
|
|
|
pending_serialization: Task<Option<()>>,
|
2023-09-08 20:28:19 +00:00
|
|
|
|
subscriptions: Vec<gpui::Subscription>,
|
2024-01-03 18:30:52 +00:00
|
|
|
|
workspace: WeakView<Workspace>,
|
2023-10-23 11:48:49 +00:00
|
|
|
|
is_scrolled_to_bottom: bool,
|
2023-10-05 21:19:13 +00:00
|
|
|
|
markdown_data: HashMap<ChannelMessageId, RichText>,
|
2023-09-08 01:06:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Serialize, Deserialize)]
|
|
|
|
|
struct SerializedChatPanel {
|
2024-01-03 18:30:52 +00:00
|
|
|
|
width: Option<Pixels>,
|
2023-09-07 18:16:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-09-08 01:06:05 +00:00
|
|
|
|
#[derive(Debug)]
|
|
|
|
|
pub enum Event {
|
|
|
|
|
DockPositionChanged,
|
|
|
|
|
Focus,
|
|
|
|
|
Dismissed,
|
|
|
|
|
}
|
2023-09-07 18:16:51 +00:00
|
|
|
|
|
2024-01-03 18:30:52 +00:00
|
|
|
|
actions!(chat_panel, [ToggleFocus]);
|
2023-09-07 18:16:51 +00:00
|
|
|
|
|
|
|
|
|
impl ChatPanel {
|
2024-01-03 18:30:52 +00:00
|
|
|
|
pub fn new(workspace: &mut Workspace, cx: &mut ViewContext<Workspace>) -> View<Self> {
|
2023-09-08 01:06:05 +00:00
|
|
|
|
let fs = workspace.app_state().fs.clone();
|
|
|
|
|
let client = workspace.app_state().client.clone();
|
2023-10-06 21:16:08 +00:00
|
|
|
|
let channel_store = ChannelStore::global(cx);
|
2023-10-05 00:47:30 +00:00
|
|
|
|
let languages = workspace.app_state().languages.clone();
|
2023-09-08 01:06:05 +00:00
|
|
|
|
|
2024-01-03 18:30:52 +00:00
|
|
|
|
let input_editor = cx.new_view(|cx| {
|
2023-10-18 00:59:42 +00:00
|
|
|
|
MessageEditor::new(
|
|
|
|
|
languages.clone(),
|
|
|
|
|
channel_store.clone(),
|
2024-01-03 18:30:52 +00:00
|
|
|
|
cx.new_view(|cx| Editor::auto_height(4, cx)),
|
2023-09-07 18:16:51 +00:00
|
|
|
|
cx,
|
2023-10-18 00:59:42 +00:00
|
|
|
|
)
|
2023-09-07 18:16:51 +00:00
|
|
|
|
});
|
2023-09-08 01:06:05 +00:00
|
|
|
|
|
2023-09-15 01:05:44 +00:00
|
|
|
|
let workspace_handle = workspace.weak_handle();
|
|
|
|
|
|
2024-01-03 18:30:52 +00:00
|
|
|
|
cx.new_view(|cx: &mut ViewContext<Self>| {
|
|
|
|
|
let view = cx.view().downgrade();
|
|
|
|
|
let message_list =
|
|
|
|
|
ListState::new(0, gpui::ListAlignment::Bottom, px(1000.), move |ix, cx| {
|
|
|
|
|
if let Some(view) = view.upgrade() {
|
|
|
|
|
view.update(cx, |view, cx| {
|
|
|
|
|
view.render_message(ix, cx).into_any_element()
|
|
|
|
|
})
|
|
|
|
|
} else {
|
|
|
|
|
div().into_any()
|
|
|
|
|
}
|
|
|
|
|
});
|
2023-09-07 18:16:51 +00:00
|
|
|
|
|
2024-01-03 18:30:52 +00:00
|
|
|
|
message_list.set_scroll_handler(cx.listener(|this, event: &ListScrollEvent, cx| {
|
|
|
|
|
if event.visible_range.start < MESSAGE_LOADING_THRESHOLD {
|
|
|
|
|
this.load_more_messages(cx);
|
|
|
|
|
}
|
|
|
|
|
this.is_scrolled_to_bottom = event.visible_range.end == event.count;
|
|
|
|
|
}));
|
2023-09-07 18:16:51 +00:00
|
|
|
|
|
2023-09-08 01:06:05 +00:00
|
|
|
|
let mut this = Self {
|
|
|
|
|
fs,
|
|
|
|
|
client,
|
|
|
|
|
channel_store,
|
2023-10-05 00:47:30 +00:00
|
|
|
|
languages,
|
2024-01-03 18:30:52 +00:00
|
|
|
|
message_list,
|
2023-09-11 20:44:41 +00:00
|
|
|
|
active_chat: Default::default(),
|
2023-09-08 01:06:05 +00:00
|
|
|
|
pending_serialization: Task::ready(None),
|
|
|
|
|
input_editor,
|
2024-01-03 18:30:52 +00:00
|
|
|
|
local_timezone: cx.local_timezone(),
|
2023-09-08 20:28:19 +00:00
|
|
|
|
subscriptions: Vec::new(),
|
2023-09-15 01:38:37 +00:00
|
|
|
|
workspace: workspace_handle,
|
2023-10-23 11:48:49 +00:00
|
|
|
|
is_scrolled_to_bottom: true,
|
2023-10-04 00:40:10 +00:00
|
|
|
|
active: false,
|
2023-09-08 01:06:05 +00:00
|
|
|
|
width: None,
|
2023-10-05 00:47:30 +00:00
|
|
|
|
markdown_data: Default::default(),
|
2023-09-08 01:06:05 +00:00
|
|
|
|
};
|
2023-09-07 18:16:51 +00:00
|
|
|
|
|
2023-09-08 20:28:19 +00:00
|
|
|
|
let mut old_dock_position = this.position(cx);
|
2024-01-03 18:30:52 +00:00
|
|
|
|
this.subscriptions.push(cx.observe_global::<SettingsStore>(
|
|
|
|
|
move |this: &mut Self, cx| {
|
|
|
|
|
let new_dock_position = this.position(cx);
|
|
|
|
|
if new_dock_position != old_dock_position {
|
|
|
|
|
old_dock_position = new_dock_position;
|
|
|
|
|
cx.emit(Event::DockPositionChanged);
|
|
|
|
|
}
|
|
|
|
|
cx.notify();
|
|
|
|
|
},
|
|
|
|
|
));
|
2023-09-08 01:06:05 +00:00
|
|
|
|
|
|
|
|
|
this
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-23 11:48:49 +00:00
|
|
|
|
pub fn is_scrolled_to_bottom(&self) -> bool {
|
|
|
|
|
self.is_scrolled_to_bottom
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-03 18:30:52 +00:00
|
|
|
|
pub fn active_chat(&self) -> Option<Model<ChannelChat>> {
|
2023-10-04 03:47:54 +00:00
|
|
|
|
self.active_chat.as_ref().map(|(chat, _)| chat.clone())
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-08 01:06:05 +00:00
|
|
|
|
pub fn load(
|
2024-01-03 18:30:52 +00:00
|
|
|
|
workspace: WeakView<Workspace>,
|
|
|
|
|
cx: AsyncWindowContext,
|
|
|
|
|
) -> Task<Result<View<Self>>> {
|
2023-09-08 01:06:05 +00:00
|
|
|
|
cx.spawn(|mut cx| async move {
|
|
|
|
|
let serialized_panel = if let Some(panel) = cx
|
2024-01-03 18:30:52 +00:00
|
|
|
|
.background_executor()
|
2023-09-08 01:06:05 +00:00
|
|
|
|
.spawn(async move { KEY_VALUE_STORE.read_kvp(CHAT_PANEL_KEY) })
|
|
|
|
|
.await
|
|
|
|
|
.log_err()
|
|
|
|
|
.flatten()
|
|
|
|
|
{
|
|
|
|
|
Some(serde_json::from_str::<SerializedChatPanel>(&panel)?)
|
|
|
|
|
} else {
|
|
|
|
|
None
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
workspace.update(&mut cx, |workspace, cx| {
|
|
|
|
|
let panel = Self::new(workspace, cx);
|
|
|
|
|
if let Some(serialized_panel) = serialized_panel {
|
|
|
|
|
panel.update(cx, |panel, cx| {
|
|
|
|
|
panel.width = serialized_panel.width;
|
|
|
|
|
cx.notify();
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
panel
|
|
|
|
|
})
|
2023-09-07 18:16:51 +00:00
|
|
|
|
})
|
2023-09-08 01:06:05 +00:00
|
|
|
|
}
|
2023-09-07 18:16:51 +00:00
|
|
|
|
|
2023-09-08 01:06:05 +00:00
|
|
|
|
fn serialize(&mut self, cx: &mut ViewContext<Self>) {
|
|
|
|
|
let width = self.width;
|
2024-01-03 18:30:52 +00:00
|
|
|
|
self.pending_serialization = cx.background_executor().spawn(
|
2023-09-08 01:06:05 +00:00
|
|
|
|
async move {
|
|
|
|
|
KEY_VALUE_STORE
|
|
|
|
|
.write_kvp(
|
|
|
|
|
CHAT_PANEL_KEY.into(),
|
|
|
|
|
serde_json::to_string(&SerializedChatPanel { width })?,
|
|
|
|
|
)
|
|
|
|
|
.await?;
|
|
|
|
|
anyhow::Ok(())
|
|
|
|
|
}
|
|
|
|
|
.log_err(),
|
|
|
|
|
);
|
2023-09-07 18:16:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-01-03 18:30:52 +00:00
|
|
|
|
fn set_active_chat(&mut self, chat: Model<ChannelChat>, cx: &mut ViewContext<Self>) {
|
2023-09-11 20:44:41 +00:00
|
|
|
|
if self.active_chat.as_ref().map(|e| &e.0) != Some(&chat) {
|
2023-10-23 15:47:21 +00:00
|
|
|
|
let channel_id = chat.read(cx).channel_id;
|
2023-09-07 18:16:51 +00:00
|
|
|
|
{
|
2023-10-23 15:47:21 +00:00
|
|
|
|
self.markdown_data.clear();
|
2023-09-09 00:06:39 +00:00
|
|
|
|
let chat = chat.read(cx);
|
|
|
|
|
self.message_list.reset(chat.message_count());
|
2023-10-23 15:47:21 +00:00
|
|
|
|
|
|
|
|
|
let channel_name = chat.channel(cx).map(|channel| channel.name.clone());
|
2023-10-18 00:59:42 +00:00
|
|
|
|
self.input_editor.update(cx, |editor, cx| {
|
2023-10-23 15:47:21 +00:00
|
|
|
|
editor.set_channel(channel_id, channel_name, cx);
|
2023-09-07 18:16:51 +00:00
|
|
|
|
});
|
2023-10-18 00:59:42 +00:00
|
|
|
|
};
|
2023-09-09 00:06:39 +00:00
|
|
|
|
let subscription = cx.subscribe(&chat, Self::channel_did_change);
|
2023-09-11 20:44:41 +00:00
|
|
|
|
self.active_chat = Some((chat, subscription));
|
2023-10-04 00:40:10 +00:00
|
|
|
|
self.acknowledge_last_message(cx);
|
2023-09-09 00:29:16 +00:00
|
|
|
|
cx.notify();
|
2023-09-07 18:16:51 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn channel_did_change(
|
|
|
|
|
&mut self,
|
2024-01-03 18:30:52 +00:00
|
|
|
|
_: Model<ChannelChat>,
|
2023-09-07 18:16:51 +00:00
|
|
|
|
event: &ChannelChatEvent,
|
|
|
|
|
cx: &mut ViewContext<Self>,
|
|
|
|
|
) {
|
|
|
|
|
match event {
|
|
|
|
|
ChannelChatEvent::MessagesUpdated {
|
|
|
|
|
old_range,
|
|
|
|
|
new_count,
|
|
|
|
|
} => {
|
|
|
|
|
self.message_list.splice(old_range.clone(), *new_count);
|
2023-10-04 17:13:02 +00:00
|
|
|
|
if self.active {
|
|
|
|
|
self.acknowledge_last_message(cx);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
ChannelChatEvent::NewMessage {
|
|
|
|
|
channel_id,
|
|
|
|
|
message_id,
|
|
|
|
|
} => {
|
|
|
|
|
if !self.active {
|
|
|
|
|
self.channel_store.update(cx, |store, cx| {
|
|
|
|
|
store.new_message(*channel_id, *message_id, cx)
|
|
|
|
|
})
|
|
|
|
|
}
|
2023-09-07 18:16:51 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
cx.notify();
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-03 18:30:52 +00:00
|
|
|
|
fn acknowledge_last_message(&mut self, cx: &mut ViewContext<Self>) {
|
2023-10-23 11:48:49 +00:00
|
|
|
|
if self.active && self.is_scrolled_to_bottom {
|
2023-10-04 00:40:10 +00:00
|
|
|
|
if let Some((chat, _)) = &self.active_chat {
|
|
|
|
|
chat.update(cx, |chat, cx| {
|
|
|
|
|
chat.acknowledge_last_message(cx);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-03 18:30:52 +00:00
|
|
|
|
fn render_channel(&self, cx: &mut ViewContext<Self>) -> AnyElement {
|
|
|
|
|
v_stack()
|
|
|
|
|
.full()
|
|
|
|
|
.on_action(cx.listener(Self::send))
|
|
|
|
|
.child(
|
|
|
|
|
h_stack().z_index(1).child(
|
|
|
|
|
TabBar::new("chat_header")
|
|
|
|
|
.child(
|
|
|
|
|
h_stack()
|
|
|
|
|
.w_full()
|
|
|
|
|
.h(rems(ui::Tab::HEIGHT_IN_REMS))
|
|
|
|
|
.px_2()
|
|
|
|
|
.child(Label::new(
|
|
|
|
|
self.active_chat
|
|
|
|
|
.as_ref()
|
|
|
|
|
.and_then(|c| {
|
|
|
|
|
Some(format!("#{}", c.0.read(cx).channel(cx)?.name))
|
|
|
|
|
})
|
|
|
|
|
.unwrap_or_default(),
|
|
|
|
|
)),
|
|
|
|
|
)
|
|
|
|
|
.end_child(
|
|
|
|
|
IconButton::new("notes", Icon::File)
|
|
|
|
|
.on_click(cx.listener(Self::open_notes))
|
|
|
|
|
.tooltip(|cx| Tooltip::text("Open notes", cx)),
|
|
|
|
|
)
|
|
|
|
|
.end_child(
|
|
|
|
|
IconButton::new("call", Icon::AudioOn)
|
|
|
|
|
.on_click(cx.listener(Self::join_call))
|
|
|
|
|
.tooltip(|cx| Tooltip::text("Join call", cx)),
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
)
|
|
|
|
|
.child(div().flex_grow().px_2().py_1().map(|this| {
|
|
|
|
|
if self.active_chat.is_some() {
|
|
|
|
|
this.child(list(self.message_list.clone()).full())
|
|
|
|
|
} else {
|
|
|
|
|
this
|
|
|
|
|
}
|
|
|
|
|
}))
|
|
|
|
|
.child(
|
|
|
|
|
div()
|
|
|
|
|
.z_index(1)
|
|
|
|
|
.p_2()
|
|
|
|
|
.bg(cx.theme().colors().background)
|
|
|
|
|
.child(self.input_editor.clone()),
|
2023-09-07 18:16:51 +00:00
|
|
|
|
)
|
|
|
|
|
.into_any()
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-03 18:30:52 +00:00
|
|
|
|
fn render_message(&mut self, ix: usize, cx: &mut ViewContext<Self>) -> impl IntoElement {
|
|
|
|
|
let active_chat = &self.active_chat.as_ref().unwrap().0;
|
|
|
|
|
let (message, is_continuation_from_previous, is_continuation_to_next, is_admin) =
|
|
|
|
|
active_chat.update(cx, |active_chat, cx| {
|
2023-10-22 16:00:02 +00:00
|
|
|
|
let is_admin = self
|
|
|
|
|
.channel_store
|
|
|
|
|
.read(cx)
|
2023-10-23 15:47:21 +00:00
|
|
|
|
.is_channel_admin(active_chat.channel_id);
|
|
|
|
|
|
2023-10-22 16:00:02 +00:00
|
|
|
|
let last_message = active_chat.message(ix.saturating_sub(1));
|
|
|
|
|
let this_message = active_chat.message(ix).clone();
|
2024-01-03 18:30:52 +00:00
|
|
|
|
let next_message =
|
|
|
|
|
active_chat.message(ix.saturating_add(1).min(active_chat.message_count() - 1));
|
|
|
|
|
|
|
|
|
|
let is_continuation_from_previous = last_message.id != this_message.id
|
|
|
|
|
&& last_message.sender.id == this_message.sender.id;
|
|
|
|
|
let is_continuation_to_next = this_message.id != next_message.id
|
|
|
|
|
&& this_message.sender.id == next_message.sender.id;
|
2023-10-22 16:00:02 +00:00
|
|
|
|
|
|
|
|
|
if let ChannelMessageId::Saved(id) = this_message.id {
|
|
|
|
|
if this_message
|
|
|
|
|
.mentions
|
|
|
|
|
.iter()
|
|
|
|
|
.any(|(_, user_id)| Some(*user_id) == self.client.user_id())
|
|
|
|
|
{
|
|
|
|
|
active_chat.acknowledge_message(id);
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-10-21 15:18:27 +00:00
|
|
|
|
|
2023-10-22 16:00:02 +00:00
|
|
|
|
(
|
|
|
|
|
this_message,
|
2024-01-03 18:30:52 +00:00
|
|
|
|
is_continuation_from_previous,
|
|
|
|
|
is_continuation_to_next,
|
2023-10-22 16:00:02 +00:00
|
|
|
|
is_admin,
|
|
|
|
|
)
|
|
|
|
|
});
|
2023-09-14 23:22:21 +00:00
|
|
|
|
|
2024-01-03 18:30:52 +00:00
|
|
|
|
let _is_pending = message.is_pending();
|
2023-10-20 13:15:39 +00:00
|
|
|
|
let text = self.markdown_data.entry(message.id).or_insert_with(|| {
|
2023-10-23 10:50:57 +00:00
|
|
|
|
Self::render_markdown_with_mentions(&self.languages, self.client.id(), &message)
|
2023-10-20 13:15:39 +00:00
|
|
|
|
});
|
2023-10-05 00:47:30 +00:00
|
|
|
|
|
2023-09-07 18:16:51 +00:00
|
|
|
|
let now = OffsetDateTime::now_utc();
|
2023-10-20 13:15:39 +00:00
|
|
|
|
|
2023-09-14 23:22:21 +00:00
|
|
|
|
let belongs_to_user = Some(message.sender.id) == self.client.user_id();
|
2023-10-17 09:16:17 +00:00
|
|
|
|
let message_id_to_remove = if let (ChannelMessageId::Saved(id), true) =
|
|
|
|
|
(message.id, belongs_to_user || is_admin)
|
|
|
|
|
{
|
|
|
|
|
Some(id)
|
|
|
|
|
} else {
|
|
|
|
|
None
|
|
|
|
|
};
|
2023-09-14 23:22:21 +00:00
|
|
|
|
|
2024-01-03 18:30:52 +00:00
|
|
|
|
let element_id: ElementId = match message.id {
|
|
|
|
|
ChannelMessageId::Saved(id) => ("saved-message", id).into(),
|
|
|
|
|
ChannelMessageId::Pending(id) => ("pending-message", id).into(),
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
v_stack()
|
|
|
|
|
.w_full()
|
|
|
|
|
.id(element_id)
|
|
|
|
|
.relative()
|
|
|
|
|
.overflow_hidden()
|
|
|
|
|
.group("")
|
|
|
|
|
.when(!is_continuation_from_previous, |this| {
|
|
|
|
|
this.child(
|
|
|
|
|
h_stack()
|
|
|
|
|
.gap_2()
|
|
|
|
|
.child(Avatar::new(message.sender.avatar_uri.clone()))
|
|
|
|
|
.child(Label::new(message.sender.github_login.clone()))
|
|
|
|
|
.child(
|
|
|
|
|
Label::new(format_timestamp(
|
|
|
|
|
message.timestamp,
|
|
|
|
|
now,
|
|
|
|
|
self.local_timezone,
|
|
|
|
|
))
|
|
|
|
|
.color(Color::Muted),
|
|
|
|
|
),
|
|
|
|
|
)
|
|
|
|
|
})
|
|
|
|
|
.when(!is_continuation_to_next, |this|
|
|
|
|
|
// HACK: This should really be a margin, but margins seem to get collapsed.
|
|
|
|
|
this.pb_2())
|
|
|
|
|
.child(text.element("body".into(), cx))
|
|
|
|
|
.child(
|
|
|
|
|
div()
|
|
|
|
|
.absolute()
|
|
|
|
|
.top_1()
|
|
|
|
|
.right_2()
|
|
|
|
|
.w_8()
|
|
|
|
|
.visible_on_hover("")
|
|
|
|
|
.children(message_id_to_remove.map(|message_id| {
|
|
|
|
|
IconButton::new(("remove", message_id), Icon::XCircle).on_click(
|
|
|
|
|
cx.listener(move |this, _, cx| {
|
|
|
|
|
this.remove_message(message_id, cx);
|
|
|
|
|
}),
|
2023-10-05 18:58:41 +00:00
|
|
|
|
)
|
2024-01-03 18:30:52 +00:00
|
|
|
|
})),
|
|
|
|
|
)
|
2023-09-07 18:16:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-10-23 10:50:57 +00:00
|
|
|
|
fn render_markdown_with_mentions(
|
|
|
|
|
language_registry: &Arc<LanguageRegistry>,
|
|
|
|
|
current_user_id: u64,
|
|
|
|
|
message: &channel::ChannelMessage,
|
|
|
|
|
) -> RichText {
|
|
|
|
|
let mentions = message
|
|
|
|
|
.mentions
|
|
|
|
|
.iter()
|
|
|
|
|
.map(|(range, user_id)| rich_text::Mention {
|
|
|
|
|
range: range.clone(),
|
|
|
|
|
is_self_mention: *user_id == current_user_id,
|
|
|
|
|
})
|
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
|
|
|
|
|
|
rich_text::render_markdown(message.body.clone(), &mentions, language_registry, None)
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-03 18:30:52 +00:00
|
|
|
|
fn render_sign_in_prompt(&self, cx: &mut ViewContext<Self>) -> AnyElement {
|
|
|
|
|
Button::new("sign-in", "Sign in to use chat")
|
|
|
|
|
.on_click(cx.listener(move |this, _, cx| {
|
|
|
|
|
let client = this.client.clone();
|
|
|
|
|
cx.spawn(|this, mut cx| async move {
|
|
|
|
|
if client
|
|
|
|
|
.authenticate_and_connect(true, &cx)
|
|
|
|
|
.log_err()
|
|
|
|
|
.await
|
|
|
|
|
.is_some()
|
|
|
|
|
{
|
|
|
|
|
this.update(&mut cx, |_, cx| {
|
|
|
|
|
cx.focus_self();
|
|
|
|
|
})
|
|
|
|
|
.ok();
|
2023-09-15 01:05:44 +00:00
|
|
|
|
}
|
|
|
|
|
})
|
2024-01-03 18:30:52 +00:00
|
|
|
|
.detach();
|
|
|
|
|
}))
|
|
|
|
|
.into_any_element()
|
2023-09-07 18:16:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn send(&mut self, _: &Confirm, cx: &mut ViewContext<Self>) {
|
2023-09-11 20:44:41 +00:00
|
|
|
|
if let Some((chat, _)) = self.active_chat.as_ref() {
|
2023-10-18 00:59:42 +00:00
|
|
|
|
let message = self
|
|
|
|
|
.input_editor
|
|
|
|
|
.update(cx, |editor, cx| editor.take_message(cx));
|
2023-09-07 18:16:51 +00:00
|
|
|
|
|
2023-09-11 20:44:41 +00:00
|
|
|
|
if let Some(task) = chat
|
2023-10-18 23:56:03 +00:00
|
|
|
|
.update(cx, |chat, cx| chat.send_message(message, cx))
|
2023-09-07 18:16:51 +00:00
|
|
|
|
.log_err()
|
|
|
|
|
{
|
|
|
|
|
task.detach();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-14 23:22:21 +00:00
|
|
|
|
fn remove_message(&mut self, id: u64, cx: &mut ViewContext<Self>) {
|
|
|
|
|
if let Some((chat, _)) = self.active_chat.as_ref() {
|
|
|
|
|
chat.update(cx, |chat, cx| chat.remove_message(id, cx).detach())
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-03 18:30:52 +00:00
|
|
|
|
fn load_more_messages(&mut self, cx: &mut ViewContext<Self>) {
|
2023-09-11 20:44:41 +00:00
|
|
|
|
if let Some((chat, _)) = self.active_chat.as_ref() {
|
|
|
|
|
chat.update(cx, |channel, cx| {
|
2023-10-19 19:31:45 +00:00
|
|
|
|
if let Some(task) = channel.load_more_messages(cx) {
|
|
|
|
|
task.detach();
|
|
|
|
|
}
|
2023-09-07 18:16:51 +00:00
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-09-09 00:06:39 +00:00
|
|
|
|
|
|
|
|
|
pub fn select_channel(
|
|
|
|
|
&mut self,
|
|
|
|
|
selected_channel_id: u64,
|
2023-10-19 19:31:45 +00:00
|
|
|
|
scroll_to_message_id: Option<u64>,
|
2023-09-09 00:06:39 +00:00
|
|
|
|
cx: &mut ViewContext<ChatPanel>,
|
|
|
|
|
) -> Task<Result<()>> {
|
2023-10-19 21:04:34 +00:00
|
|
|
|
let open_chat = self
|
|
|
|
|
.active_chat
|
|
|
|
|
.as_ref()
|
|
|
|
|
.and_then(|(chat, _)| {
|
2023-10-23 15:47:21 +00:00
|
|
|
|
(chat.read(cx).channel_id == selected_channel_id)
|
2023-10-19 21:04:34 +00:00
|
|
|
|
.then(|| Task::ready(anyhow::Ok(chat.clone())))
|
|
|
|
|
})
|
|
|
|
|
.unwrap_or_else(|| {
|
|
|
|
|
self.channel_store.update(cx, |store, cx| {
|
|
|
|
|
store.open_channel_chat(selected_channel_id, cx)
|
|
|
|
|
})
|
|
|
|
|
});
|
2023-09-11 20:44:41 +00:00
|
|
|
|
|
2023-09-09 00:06:39 +00:00
|
|
|
|
cx.spawn(|this, mut cx| async move {
|
|
|
|
|
let chat = open_chat.await?;
|
|
|
|
|
this.update(&mut cx, |this, cx| {
|
2023-10-19 19:31:45 +00:00
|
|
|
|
this.set_active_chat(chat.clone(), cx);
|
|
|
|
|
})?;
|
|
|
|
|
|
|
|
|
|
if let Some(message_id) = scroll_to_message_id {
|
|
|
|
|
if let Some(item_ix) =
|
2024-01-03 18:30:52 +00:00
|
|
|
|
ChannelChat::load_history_since_message(chat.clone(), message_id, (*cx).clone())
|
2023-10-19 21:04:34 +00:00
|
|
|
|
.await
|
2023-10-19 19:31:45 +00:00
|
|
|
|
{
|
2023-10-19 21:04:34 +00:00
|
|
|
|
this.update(&mut cx, |this, cx| {
|
|
|
|
|
if this.active_chat.as_ref().map_or(false, |(c, _)| *c == chat) {
|
|
|
|
|
this.message_list.scroll_to(ListOffset {
|
|
|
|
|
item_ix,
|
2024-01-03 18:30:52 +00:00
|
|
|
|
offset_in_item: px(0.0),
|
2023-10-19 21:04:34 +00:00
|
|
|
|
});
|
|
|
|
|
cx.notify();
|
|
|
|
|
}
|
2023-10-19 19:31:45 +00:00
|
|
|
|
})?;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Ok(())
|
2023-09-09 00:06:39 +00:00
|
|
|
|
})
|
|
|
|
|
}
|
2023-09-15 01:38:37 +00:00
|
|
|
|
|
2024-01-03 18:30:52 +00:00
|
|
|
|
fn open_notes(&mut self, _: &ClickEvent, cx: &mut ViewContext<Self>) {
|
2023-09-15 01:38:37 +00:00
|
|
|
|
if let Some((chat, _)) = &self.active_chat {
|
2023-10-19 19:03:44 +00:00
|
|
|
|
let channel_id = chat.read(cx).channel_id;
|
2024-01-03 18:30:52 +00:00
|
|
|
|
if let Some(workspace) = self.workspace.upgrade() {
|
2023-09-21 23:13:12 +00:00
|
|
|
|
ChannelView::open(channel_id, workspace, cx).detach();
|
2023-09-15 01:38:37 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-03 18:30:52 +00:00
|
|
|
|
fn join_call(&mut self, _: &ClickEvent, cx: &mut ViewContext<Self>) {
|
2023-09-15 01:38:37 +00:00
|
|
|
|
if let Some((chat, _)) = &self.active_chat {
|
2023-10-19 19:03:44 +00:00
|
|
|
|
let channel_id = chat.read(cx).channel_id;
|
2023-09-15 01:38:37 +00:00
|
|
|
|
ActiveCall::global(cx)
|
|
|
|
|
.update(cx, |call, cx| call.join_channel(channel_id, cx))
|
|
|
|
|
.detach_and_log_err(cx);
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-09-07 18:16:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-01-03 18:30:52 +00:00
|
|
|
|
impl EventEmitter<Event> for ChatPanel {}
|
2023-09-07 18:16:51 +00:00
|
|
|
|
|
2024-01-03 18:30:52 +00:00
|
|
|
|
impl Render for ChatPanel {
|
|
|
|
|
fn render(&mut self, cx: &mut ViewContext<Self>) -> impl IntoElement {
|
|
|
|
|
div()
|
|
|
|
|
.full()
|
|
|
|
|
.child(if self.client.user_id().is_some() {
|
|
|
|
|
self.render_channel(cx)
|
|
|
|
|
} else {
|
|
|
|
|
self.render_sign_in_prompt(cx)
|
|
|
|
|
})
|
|
|
|
|
.min_w(px(150.))
|
2023-09-07 18:16:51 +00:00
|
|
|
|
}
|
2024-01-03 18:30:52 +00:00
|
|
|
|
}
|
2023-09-15 01:05:44 +00:00
|
|
|
|
|
2024-01-03 18:30:52 +00:00
|
|
|
|
impl FocusableView for ChatPanel {
|
|
|
|
|
fn focus_handle(&self, cx: &AppContext) -> gpui::FocusHandle {
|
|
|
|
|
self.input_editor.read(cx).focus_handle(cx)
|
2023-09-15 01:05:44 +00:00
|
|
|
|
}
|
2023-09-07 18:16:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-09-08 01:06:05 +00:00
|
|
|
|
impl Panel for ChatPanel {
|
|
|
|
|
fn position(&self, cx: &gpui::WindowContext) -> DockPosition {
|
2024-01-03 18:30:52 +00:00
|
|
|
|
ChatPanelSettings::get_global(cx).dock
|
2023-09-08 01:06:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn position_is_valid(&self, position: DockPosition) -> bool {
|
|
|
|
|
matches!(position, DockPosition::Left | DockPosition::Right)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn set_position(&mut self, position: DockPosition, cx: &mut ViewContext<Self>) {
|
2023-09-08 20:28:19 +00:00
|
|
|
|
settings::update_settings_file::<ChatPanelSettings>(self.fs.clone(), cx, move |settings| {
|
|
|
|
|
settings.dock = Some(position)
|
|
|
|
|
});
|
2023-09-08 01:06:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-01-03 18:30:52 +00:00
|
|
|
|
fn size(&self, cx: &gpui::WindowContext) -> Pixels {
|
2023-09-08 01:06:05 +00:00
|
|
|
|
self.width
|
2024-01-03 18:30:52 +00:00
|
|
|
|
.unwrap_or_else(|| ChatPanelSettings::get_global(cx).default_width)
|
2023-09-08 01:06:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-01-03 18:30:52 +00:00
|
|
|
|
fn set_size(&mut self, size: Option<Pixels>, cx: &mut ViewContext<Self>) {
|
2023-09-08 01:06:05 +00:00
|
|
|
|
self.width = size;
|
|
|
|
|
self.serialize(cx);
|
|
|
|
|
cx.notify();
|
|
|
|
|
}
|
|
|
|
|
|
2023-09-15 17:12:28 +00:00
|
|
|
|
fn set_active(&mut self, active: bool, cx: &mut ViewContext<Self>) {
|
2023-10-04 00:40:10 +00:00
|
|
|
|
self.active = active;
|
|
|
|
|
if active {
|
|
|
|
|
self.acknowledge_last_message(cx);
|
2023-10-06 19:56:18 +00:00
|
|
|
|
if !is_channels_feature_enabled(cx) {
|
2023-10-04 00:40:10 +00:00
|
|
|
|
cx.emit(Event::Dismissed);
|
|
|
|
|
}
|
2023-09-15 17:12:28 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-03 18:30:52 +00:00
|
|
|
|
fn persistent_name() -> &'static str {
|
|
|
|
|
"ChatPanel"
|
2023-09-08 01:06:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-01-03 18:30:52 +00:00
|
|
|
|
fn icon(&self, _cx: &WindowContext) -> Option<ui::Icon> {
|
|
|
|
|
Some(ui::Icon::MessageBubbles)
|
2023-09-15 17:12:28 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-01-03 18:30:52 +00:00
|
|
|
|
fn icon_tooltip(&self, _cx: &WindowContext) -> Option<&'static str> {
|
|
|
|
|
Some("Chat Panel")
|
2023-09-08 01:06:05 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-01-03 18:30:52 +00:00
|
|
|
|
fn toggle_action(&self) -> Box<dyn gpui::Action> {
|
|
|
|
|
Box::new(ToggleFocus)
|
2023-09-08 01:06:05 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-03 18:30:52 +00:00
|
|
|
|
impl EventEmitter<PanelEvent> for ChatPanel {}
|
|
|
|
|
|
2023-09-07 18:16:51 +00:00
|
|
|
|
fn format_timestamp(
|
|
|
|
|
mut timestamp: OffsetDateTime,
|
|
|
|
|
mut now: OffsetDateTime,
|
|
|
|
|
local_timezone: UtcOffset,
|
|
|
|
|
) -> String {
|
|
|
|
|
timestamp = timestamp.to_offset(local_timezone);
|
|
|
|
|
now = now.to_offset(local_timezone);
|
|
|
|
|
|
|
|
|
|
let today = now.date();
|
|
|
|
|
let date = timestamp.date();
|
|
|
|
|
let mut hour = timestamp.hour();
|
|
|
|
|
let mut part = "am";
|
|
|
|
|
if hour > 12 {
|
|
|
|
|
hour -= 12;
|
|
|
|
|
part = "pm";
|
|
|
|
|
}
|
|
|
|
|
if date == today {
|
|
|
|
|
format!("{:02}:{:02}{}", hour, timestamp.minute(), part)
|
|
|
|
|
} else if date.next_day() == Some(today) {
|
|
|
|
|
format!("yesterday at {:02}:{:02}{}", hour, timestamp.minute(), part)
|
|
|
|
|
} else {
|
|
|
|
|
format!("{:02}/{}/{}", date.month() as u32, date.day(), date.year())
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-09-14 23:22:21 +00:00
|
|
|
|
|
2023-10-23 10:50:57 +00:00
|
|
|
|
#[cfg(test)]
|
|
|
|
|
mod tests {
|
|
|
|
|
use super::*;
|
2024-01-03 18:30:52 +00:00
|
|
|
|
use gpui::HighlightStyle;
|
2023-10-23 10:50:57 +00:00
|
|
|
|
use pretty_assertions::assert_eq;
|
2024-01-03 18:30:52 +00:00
|
|
|
|
use rich_text::Highlight;
|
2023-10-23 10:50:57 +00:00
|
|
|
|
use util::test::marked_text_ranges;
|
|
|
|
|
|
|
|
|
|
#[gpui::test]
|
|
|
|
|
fn test_render_markdown_with_mentions() {
|
|
|
|
|
let language_registry = Arc::new(LanguageRegistry::test());
|
|
|
|
|
let (body, ranges) = marked_text_ranges("*hi*, «@abc», let's **call** «@fgh»", false);
|
|
|
|
|
let message = channel::ChannelMessage {
|
|
|
|
|
id: ChannelMessageId::Saved(0),
|
|
|
|
|
body,
|
|
|
|
|
timestamp: OffsetDateTime::now_utc(),
|
|
|
|
|
sender: Arc::new(client::User {
|
|
|
|
|
github_login: "fgh".into(),
|
2024-01-03 18:30:52 +00:00
|
|
|
|
avatar_uri: "avatar_fgh".into(),
|
2023-10-23 10:50:57 +00:00
|
|
|
|
id: 103,
|
|
|
|
|
}),
|
|
|
|
|
nonce: 5,
|
|
|
|
|
mentions: vec![(ranges[0].clone(), 101), (ranges[1].clone(), 102)],
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let message = ChatPanel::render_markdown_with_mentions(&language_registry, 102, &message);
|
|
|
|
|
|
|
|
|
|
// Note that the "'" was replaced with ’ due to smart punctuation.
|
|
|
|
|
let (body, ranges) = marked_text_ranges("«hi», «@abc», let’s «call» «@fgh»", false);
|
|
|
|
|
assert_eq!(message.text, body);
|
|
|
|
|
assert_eq!(
|
|
|
|
|
message.highlights,
|
|
|
|
|
vec![
|
|
|
|
|
(
|
|
|
|
|
ranges[0].clone(),
|
|
|
|
|
HighlightStyle {
|
2024-01-03 18:30:52 +00:00
|
|
|
|
font_style: Some(gpui::FontStyle::Italic),
|
2023-10-23 10:50:57 +00:00
|
|
|
|
..Default::default()
|
|
|
|
|
}
|
|
|
|
|
.into()
|
|
|
|
|
),
|
|
|
|
|
(ranges[1].clone(), Highlight::Mention),
|
|
|
|
|
(
|
|
|
|
|
ranges[2].clone(),
|
|
|
|
|
HighlightStyle {
|
2024-01-03 18:30:52 +00:00
|
|
|
|
font_weight: Some(gpui::FontWeight::BOLD),
|
2023-10-23 10:50:57 +00:00
|
|
|
|
..Default::default()
|
|
|
|
|
}
|
|
|
|
|
.into()
|
|
|
|
|
),
|
|
|
|
|
(ranges[3].clone(), Highlight::SelfMention)
|
|
|
|
|
]
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|