| 1 | use std::cell::{Cell, RefCell}; |
| 2 | use std::collections::{HashMap, HashSet, VecDeque}; |
| 3 | use std::path::{Path, PathBuf}; |
| 4 | use std::rc::Rc; |
| 5 | use std::time::{Duration, Instant}; |
| 6 | |
| 7 | use chrono::{DateTime, Local, Utc}; |
| 8 | use gpui_kit::assets::IconName; |
| 9 | use gpui_kit::component::{ |
| 10 | button::{Button, ButtonVariants as _}, |
| 11 | h_flex, |
| 12 | input::{Escape, InputState}, |
| 13 | message_scroller::{MessageScroller, MessageScrollerState}, |
| 14 | tooltip::Tooltip, |
| 15 | v_flex, |
| 16 | }; |
| 17 | use gpui_kit::prelude::FluentBuilder as _; |
| 18 | use gpui_kit::*; |
| 19 | use store::{ChannelTabRecord, MessageRecord, OutboxState, OutboxTarget, is_bot_id}; |
| 20 | use teams_core::{ |
| 21 | ExecuteAction, FileCard, JumpPlan, LinkPreview, WindowDirection, is_notes_chat, without_islands, |
| 22 | }; |
| 23 | |
| 24 | mod channel_details; |
| 25 | mod chat_header_actions; |
| 26 | mod copilot; |
| 27 | pub use channel_details::{ |
| 28 | CHANNEL_DETAILS_BUTTON, CHANNEL_DETAILS_PINNED_TAG, CHANNEL_DETAILS_SHARED_TAG, |
| 29 | CHANNEL_DETAILS_TAG, |
| 30 | }; |
| 31 | mod deletes; |
| 32 | mod detached; |
| 33 | mod feed; |
| 34 | mod forward_select; |
| 35 | #[cfg(test)] |
| 36 | pub use forward_select::{ |
| 37 | FORWARD_FOOTER_BAR_TAG, FORWARD_FOOTER_COUNT_TAG, FORWARD_FOOTER_NEXT_TAG, FORWARD_FOOTER_TAG, |
| 38 | }; |
| 39 | mod absence; |
| 40 | mod appear_tracker; |
| 41 | mod jump; |
| 42 | mod notes_space; |
| 43 | mod pins; |
| 44 | mod proposal; |
| 45 | mod recap; |
| 46 | mod receipt_fade; |
| 47 | mod render_env; |
| 48 | mod scheduled; |
| 49 | mod shared; |
| 50 | |
| 51 | use super::add_people_dialog::open_add_people; |
| 52 | use super::attachments::FileActions; |
| 53 | use super::avatar::{ |
| 54 | bot_tile, member_stack, person_avatar, spec_avatar, team_avatar, with_presence, |
| 55 | }; |
| 56 | use super::call_controls::header_call_controls; |
| 57 | use super::channel_tabs::{ChannelPane, HEADER_TITLE_MAX_WIDTH}; |
| 58 | use super::chat_members::members_popover; |
| 59 | use super::chat_pop_out; |
| 60 | use super::composer::{Composer, ComposerEvent, EditPreview, Outgoing, ReplyPreview}; |
| 61 | use super::find_panel::{FIND_PANEL_WIDTH, FindPanel, FindPanelEvent}; |
| 62 | use super::group_name::{GroupNameSlot, group_name_button}; |
| 63 | use super::message_actions::{Action, DelegateEntry, MessageMenu}; |
| 64 | use super::message_context_menu::OpenContextMenu; |
| 65 | use super::message_row::{ |
| 66 | AVATAR_GAP, AVATAR_SIZE, JUMP_HIGHLIGHT_PERIOD, render_message_row, render_skeleton_row, |
| 67 | }; |
| 68 | use super::new_chat::{NewChatDraft, NewChatEvent, Pick, composer_placeholder}; |
| 69 | use super::pin_banner::{pin_banner_inset, pin_banner_overlay}; |
| 70 | use super::post_card::render_post; |
| 71 | use super::profile_card::opens_profile; |
| 72 | use super::reaction_picker::{PickHandler, ReactionPicker}; |
| 73 | use super::reaction_pills::{ReactionControls, ReactionPopover}; |
| 74 | use super::scroll_edge::edge_divider; |
| 75 | use super::toolbar_placement::{TimelineBounds, record_bounds}; |
| 76 | use super::widgets::{icon, symbol}; |
| 77 | use crate::agents::{AgentRun, StartRequest}; |
| 78 | use crate::app_state::{AppEvent, AppState, Selection, selection_title}; |
| 79 | use crate::backend::Engine; |
| 80 | use crate::card_state::CardScope; |
| 81 | use crate::channel_files::DownloadSource; |
| 82 | use crate::chat_settings_actions::shown_receipt; |
| 83 | use crate::data::{existing_one_on_one, is_one_on_one, one_on_one_bot, others}; |
| 84 | use crate::demo_library::DemoLibrary; |
| 85 | use crate::downloads::{self, ClickAction, DownloadKey, Downloads, PartFile, RevealTarget}; |
| 86 | use crate::emoji; |
| 87 | use crate::emoticons; |
| 88 | use crate::i18n::tr; |
| 89 | use crate::image_view_gallery::{ViewerRequest, collect_gallery, position_of}; |
| 90 | use crate::notice::short_error; |
| 91 | use crate::outbox::{Delivered, deliver, new_outbox_id, outbox_record, outgoing_of}; |
| 92 | use crate::pending_rows::pending_row; |
| 93 | use crate::people::{local_names, resolve_names}; |
| 94 | use crate::reaction_model::UNKNOWN_REACTOR; |
| 95 | use crate::read_state::{ReadTrigger, missed_before_fetch, plan_read}; |
| 96 | use crate::rows::{ |
| 97 | Delivery, LocalImage, MessageRow, PendingReactions, Receipt, Row, RowContext, StartInfo, |
| 98 | api_reaction, apply_pending_reactions, assign_series, changed_indices, diff_keys, flat_rows, |
| 99 | forward_sender_ids, has_own_reaction, message_draft, message_text, own_reaction_glyph, |
| 100 | place_local_rows, placeholder_rows, reaction_glyph, reply_excerpt, set_own_reaction, |
| 101 | thread_list_rows, thread_rows, |
| 102 | }; |
| 103 | use crate::runtime; |
| 104 | use crate::scheduled_rows::SCHEDULED_KEY_PREFIX; |
| 105 | use crate::sidebar_model::{AvatarSpec, Face, channel_unread}; |
| 106 | use crate::theme; |
| 107 | use crate::translation::TranslationLine; |
| 108 | use crate::typing::typing_tooltip; |
| 109 | use app_ui_kit::layout::{COMFY_RUNWAY_WIDTH, ISLAND_GAP, RAIL_WIDTH, SIDEBAR_WIDTH}; |
| 110 | use detached::DetachedWindow; |
| 111 | use feed::{FeedStash, subject_input}; |
| 112 | use mcp::ReactionProposal; |
| 113 | use render_env::RenderEnv; |
| 114 | use shared::SharedState; |
| 115 | |
| 116 | pub const MEMBERS_BUTTON: &str = "shell/islands/main/members-button"; |
| 117 | pub const HEADER_DIVIDER: &str = "shell/islands/main/divider-header"; |
| 118 | pub const TIMELINE_TAG: &str = "shell/islands/main/timeline"; |
| 119 | pub const FEED_EDGE_TAG: &str = "shell/islands/main/timeline/divider-feed"; |
| 120 | const SENT_PREVIEWS_KEPT: usize = 16; |
| 121 | const HOVER_LEAVE_GRACE: Duration = Duration::from_millis(250); |
| 122 | const POSTED_WITHOUT_BANNER: &str = "Posted, but Teams did not take the banner."; |
| 123 | const QUIET_RETRY_DELAYS: [Duration; 3] = [ |
| 124 | Duration::from_secs(2), |
| 125 | Duration::from_secs(5), |
| 126 | Duration::from_secs(15), |
| 127 | ]; |
| 128 | const HEADER_HEIGHT: f32 = 60.; |
| 129 | const HEADER_PADDING: f32 = 20.; |
| 130 | const HEADER_GAP: f32 = 12.; |
| 131 | const HEADER_COMPACT_GAP: f32 = 4.; |
| 132 | const POP_IN_BUTTON_SIZE: f32 = 32.; |
| 133 | const HEADER_LEAD_SIZE: f32 = 36.; |
| 134 | const TABS_INLINE_MIN_WIDTH: f32 = 560.; |
| 135 | const FIND_MIN_TIMELINE_WIDTH: f32 = 360.; |
| 136 | const TABS_ROW_HEIGHT: f32 = 36.; |
| 137 | const CHAT_OPEN_LIMIT: usize = 60; |
| 138 | const CHANNEL_OPEN_LIMIT: usize = 300; |
| 139 | const GROWTH_HEADROOM: usize = 20; |
| 140 | const META_USER_ID: &str = "me_user_id"; |
| 141 | const PENDING_KEY_PREFIX: &str = "pending-"; |
| 142 | const DEMO_DOWNLOAD_STEPS: u8 = 10; |
| 143 | const DEMO_DOWNLOAD_STEP: Duration = Duration::from_millis(100); |
| 144 | const JUMP_SCAN_LIMIT: usize = 5000; |
| 145 | const JUMP_CONTEXT_MESSAGES: usize = 12; |
| 146 | const PROGRESS_DELAY: Duration = Duration::from_millis(400); |
| 147 | const SLOW_AFTER: Duration = Duration::from_secs(8); |
| 148 | const PROGRESS_MIN_VISIBLE: Duration = Duration::from_millis(500); |
| 149 | const PROGRESS_BAR_PERIOD: Duration = Duration::from_millis(1400); |
| 150 | const PROGRESS_BAR_WIDTH: f32 = 0.35; |
| 151 | const TYPING_BUBBLE_HEIGHT: f32 = 28.; |
| 152 | const TYPING_BUBBLE_PADDING: f32 = 12.; |
| 153 | const TYPING_SIDE_INSET: f32 = 20.; |
| 154 | const COMPOSER_TOP_GAP: f32 = 16.; |
| 155 | const TYPING_TOP_GAP: f32 = 16.; |
| 156 | const TYPING_BOTTOM_GAP: f32 = 8.; |
| 157 | pub const TYPING_TAG: &str = "shell/islands/main/typing"; |
| 158 | const TYPING_DOT_SIZE: f32 = 5.; |
| 159 | const TYPING_DOT_COUNT: usize = 3; |
| 160 | const TYPING_PULSE_PERIOD: Duration = Duration::from_millis(1200); |
| 161 | |
| 162 | #[derive(Clone, Copy)] |
| 163 | enum HoverSlot { |
| 164 | Row, |
| 165 | Toolbar, |
| 166 | } |
| 167 | |
| 168 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 169 | enum ViewMode { |
| 170 | Flat, |
| 171 | ThreadList, |
| 172 | Thread(String), |
| 173 | } |
| 174 | |
| 175 | #[derive(Default)] |
| 176 | struct SyncProgress { |
| 177 | messages: bool, |
| 178 | receipts: bool, |
| 179 | empty_cache: bool, |
| 180 | newest_cached: Option<DateTime<Utc>>, |
| 181 | started_at: DateTime<Utc>, |
| 182 | shown_at: Option<Instant>, |
| 183 | slow: bool, |
| 184 | failed: bool, |
| 185 | } |
| 186 | |
| 187 | impl SyncProgress { |
| 188 | fn running(&self) -> bool { |
| 189 | self.messages || self.receipts |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | struct Current { |
| 194 | selection: Selection, |
| 195 | title: String, |
| 196 | mode: ViewMode, |
| 197 | loaded_limit: usize, |
| 198 | loaded_count: usize, |
| 199 | has_older: bool, |
| 200 | loading_older: bool, |
| 201 | older_blocked: bool, |
| 202 | quiet_retries: usize, |
| 203 | fetched: bool, |
| 204 | first_unread: Option<String>, |
| 205 | posts_new_after: Option<DateTime<Utc>>, |
| 206 | sync: SyncProgress, |
| 207 | detached: Option<DetachedWindow>, |
| 208 | window_request: Option<String>, |
| 209 | } |
| 210 | |
| 211 | struct ReactionDetails { |
| 212 | message_key: String, |
| 213 | anchor_glyph: String, |
| 214 | tab: Option<String>, |
| 215 | } |
| 216 | |
| 217 | #[derive(Clone, Copy, PartialEq, Eq)] |
| 218 | enum ReactionAttempt { |
| 219 | Acted, |
| 220 | Started, |
| 221 | AlreadyThere, |
| 222 | MissingMessage, |
| 223 | Blocked, |
| 224 | } |
| 225 | |
| 226 | pub struct ConversationView { |
| 227 | app: Entity<AppState>, |
| 228 | pinned: bool, |
| 229 | popped_out: bool, |
| 230 | scroller: Entity<MessageScrollerState>, |
| 231 | composer: Entity<Composer>, |
| 232 | subject: Entity<InputState>, |
| 233 | new_post_open: bool, |
| 234 | reply_composer: Option<Entity<Composer>>, |
| 235 | reply_root: Option<String>, |
| 236 | reply_drafts: HashMap<String, Outgoing>, |
| 237 | feed_stash: Option<FeedStash>, |
| 238 | feed_rows_seen: Rc<Cell<usize>>, |
| 239 | feed_at_top: bool, |
| 240 | older_anchor: Option<String>, |
| 241 | draft: Entity<NewChatDraft>, |
| 242 | draft_active: bool, |
| 243 | draft_created: Option<(Vec<String>, String)>, |
| 244 | draft_error: Option<String>, |
| 245 | rows: Rc<RefCell<Vec<Row>>>, |
| 246 | current: Option<Current>, |
| 247 | pending: Vec<MessageRow>, |
| 248 | saving_edits: HashSet<String>, |
| 249 | pending_counter: usize, |
| 250 | pending_outgoing: HashMap<String, Outgoing>, |
| 251 | sent_previews: VecDeque<(String, Vec<LocalImage>)>, |
| 252 | pending_reactions: PendingReactions, |
| 253 | downloads: Downloads, |
| 254 | hovered_message: Option<String>, |
| 255 | hover_leave_serial: u64, |
| 256 | toolbar_hovered: Option<String>, |
| 257 | context_menu: Option<OpenContextMenu>, |
| 258 | context_serial: u64, |
| 259 | timeline_bounds: TimelineBounds, |
| 260 | header_width: Rc<Cell<f32>>, |
| 261 | toolbar_pinned: Option<String>, |
| 262 | toolbar_suppressed: Option<String>, |
| 263 | recent: Entity<emoji::Recent>, |
| 264 | picker: Entity<ReactionPicker>, |
| 265 | reaction_details: Option<ReactionDetails>, |
| 266 | highlighted_message: Option<String>, |
| 267 | fetched_jump: Option<String>, |
| 268 | loading_post: Option<String>, |
| 269 | pin_index: usize, |
| 270 | channel_details_open: bool, |
| 271 | copilot: copilot::CopilotPane, |
| 272 | pinned_posts_expanded: bool, |
| 273 | channel_details_scroll: ScrollHandle, |
| 274 | scroller_watch: Option<(EntityId, Subscription)>, |
| 275 | pin_previews: HashMap<String, pins::PinPreview>, |
| 276 | pin_fetches: HashSet<String>, |
| 277 | notice: Option<String>, |
| 278 | scheduled_failure: Option<scheduled::ScheduledFailure>, |
| 279 | scheduled_in_flight: HashSet<String>, |
| 280 | drag_file_count: usize, |
| 281 | sync_generation: u64, |
| 282 | rebuild_generation: u64, |
| 283 | window_active: bool, |
| 284 | pane: ChannelPane, |
| 285 | recap: recap::RecapState, |
| 286 | recap_search: Entity<InputState>, |
| 287 | channel_tabs: Vec<ChannelTabRecord>, |
| 288 | tab_links: HashMap<String, String>, |
| 289 | focus_handle: FocusHandle, |
| 290 | shared: SharedState, |
| 291 | demo_library: DemoLibrary, |
| 292 | new_folder_input: Option<Entity<InputState>>, |
| 293 | agent_layouts: HashMap<String, (usize, bool)>, |
| 294 | agent_runs: Rc<RefCell<HashMap<String, AgentRun>>>, |
| 295 | reaction_proposals: Rc<RefCell<HashMap<String, ReactionProposal>>>, |
| 296 | receipt_fade: receipt_fade::ReceiptFade, |
| 297 | appear_tracker: appear_tracker::AppearTracker, |
| 298 | find_panel: Option<Entity<FindPanel>>, |
| 299 | find_subscription: Option<Subscription>, |
| 300 | group_name: GroupNameSlot, |
| 301 | _subscriptions: Vec<Subscription>, |
| 302 | } |
| 303 | |
| 304 | struct RebuildPlan { |
| 305 | conversation_id: String, |
| 306 | is_chat: bool, |
| 307 | mode: ViewMode, |
| 308 | limit: usize, |
| 309 | loading_post: bool, |
| 310 | thread_root: Option<String>, |
| 311 | window: Option<(DateTime<Utc>, DateTime<Utc>, bool, bool)>, |
| 312 | syncing_messages: bool, |
| 313 | older_blocked: bool, |
| 314 | can_load_older: bool, |
| 315 | start: Option<StartInfo>, |
| 316 | } |
| 317 | |
| 318 | struct LoadedRecords { |
| 319 | records: Vec<MessageRecord>, |
| 320 | sync_has_more: bool, |
| 321 | } |
| 322 | |
| 323 | struct BuiltRows { |
| 324 | rows: Vec<Row>, |
| 325 | has_older: bool, |
| 326 | placeholders: bool, |
| 327 | } |
| 328 | |
| 329 | fn load_records(store: &store::Store, plan: &RebuildPlan) -> LoadedRecords { |
| 330 | let sync_state = store.sync_state(&plan.conversation_id).ok().flatten(); |
| 331 | let records = match (&plan.thread_root, plan.window) { |
| 332 | (Some(root_id), _) => store |
| 333 | .thread_messages(&plan.conversation_id, root_id) |
| 334 | .unwrap_or_default(), |
| 335 | (None, Some((oldest, newest, _, _))) => store |
| 336 | .messages_between(&plan.conversation_id, oldest, newest) |
| 337 | .unwrap_or_default(), |
| 338 | (None, None) if plan.is_chat => without_islands( |
| 339 | store |
| 340 | .messages(&plan.conversation_id, None, plan.limit) |
| 341 | .unwrap_or_default(), |
| 342 | sync_state.as_ref().and_then(|state| state.oldest_loaded), |
| 343 | ), |
| 344 | (None, None) => store |
| 345 | .messages(&plan.conversation_id, None, plan.limit) |
| 346 | .unwrap_or_default(), |
| 347 | }; |
| 348 | LoadedRecords { |
| 349 | records, |
| 350 | sync_has_more: sync_state.as_ref().is_none_or(|state| state.has_more), |
| 351 | } |
| 352 | } |
| 353 | |
| 354 | fn build_rows(plan: &RebuildPlan, loaded: &LoadedRecords, context: &RowContext) -> BuiltRows { |
| 355 | let records = &loaded.records; |
| 356 | let has_older = loaded.sync_has_more && !plan.older_blocked && plan.can_load_older; |
| 357 | let placeholders = records.is_empty() |
| 358 | && (plan.loading_post || plan.syncing_messages && plan.thread_root.is_none()); |
| 359 | if placeholders { |
| 360 | return BuiltRows { |
| 361 | rows: placeholder_rows(), |
| 362 | has_older: false, |
| 363 | placeholders, |
| 364 | }; |
| 365 | } |
| 366 | let (rows, has_older) = match &plan.mode { |
| 367 | ViewMode::Flat => match plan.window { |
| 368 | Some((_, _, has_older_window, has_newer_window)) => { |
| 369 | let mut rows = flat_rows(records, context, has_older_window); |
| 370 | if has_newer_window { |
| 371 | rows.push(Row::LoadNewer); |
| 372 | } |
| 373 | (rows, has_older_window) |
| 374 | } |
| 375 | None => { |
| 376 | let mut rows = flat_rows(records, context, has_older); |
| 377 | if !has_older && let Some(start) = plan.start.clone() { |
| 378 | rows.insert(0, Row::Start(start)); |
| 379 | } |
| 380 | (rows, has_older) |
| 381 | } |
| 382 | }, |
| 383 | ViewMode::ThreadList => (thread_list_rows(records, context, has_older), has_older), |
| 384 | ViewMode::Thread(root_id) => (thread_rows(records, root_id, context), false), |
| 385 | }; |
| 386 | BuiltRows { |
| 387 | rows, |
| 388 | has_older, |
| 389 | placeholders, |
| 390 | } |
| 391 | } |
| 392 | |
| 393 | fn open_limit(selection: &Selection) -> usize { |
| 394 | match selection { |
| 395 | Selection::Chat(_) => CHAT_OPEN_LIMIT, |
| 396 | Selection::Channel(_) => CHANNEL_OPEN_LIMIT, |
| 397 | } |
| 398 | } |
| 399 | |
| 400 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 401 | enum DropTarget { |
| 402 | Chat, |
| 403 | Channel, |
| 404 | Library, |
| 405 | NewChat, |
| 406 | } |
| 407 | |
| 408 | fn drop_overlay_text(title: &str, target: DropTarget, count: usize) -> (String, String) { |
| 409 | let files = if count == 1 { |
| 410 | "1 file".to_owned() |
| 411 | } else { |
| 412 | format!("{count} files") |
| 413 | }; |
| 414 | match target { |
| 415 | DropTarget::Chat => ( |
| 416 | format!("Drop to attach to {title}"), |
| 417 | format!("{files}. Images go into the message, other files to your OneDrive."), |
| 418 | ), |
| 419 | DropTarget::Channel => ( |
| 420 | format!("Drop to attach to {title}"), |
| 421 | format!("{files}. Images go into the message, other files to the channel's Files."), |
| 422 | ), |
| 423 | DropTarget::Library => ( |
| 424 | format!("Drop to upload to {title}"), |
| 425 | format!("{files}. Files go into the folder you have open."), |
| 426 | ), |
| 427 | DropTarget::NewChat => ( |
| 428 | "Drop to attach to the new chat".to_owned(), |
| 429 | format!( |
| 430 | "{files}. Images go into the message. Files can be added once the chat exists." |
| 431 | ), |
| 432 | ), |
| 433 | } |
| 434 | } |
| 435 | |
| 436 | fn shown_in_view(mode: &ViewMode, record: &store::OutboxRecord) -> bool { |
| 437 | match mode { |
| 438 | ViewMode::Flat => record.target == OutboxTarget::Flat, |
| 439 | ViewMode::ThreadList => matches!(record.target, OutboxTarget::Post | OutboxTarget::Thread), |
| 440 | ViewMode::Thread(root_id) => { |
| 441 | record.target == OutboxTarget::Thread |
| 442 | && record.thread_root_id.as_deref() == Some(root_id.as_str()) |
| 443 | } |
| 444 | } |
| 445 | } |
| 446 | |
| 447 | enum DownloadEvent { |
| 448 | Progress(u8), |
| 449 | Finished(Result<PathBuf, String>), |
| 450 | } |
| 451 | |
| 452 | async fn save_file( |
| 453 | engine: &Engine, |
| 454 | card: &FileCard, |
| 455 | source: &DownloadSource, |
| 456 | events: &tokio::sync::mpsc::UnboundedSender<DownloadEvent>, |
| 457 | ) -> Result<PathBuf, String> { |
| 458 | let mut part = PartFile::create(&downloads::downloads_directory(), &card.name) |
| 459 | .map_err(|error| short_error(&error))?; |
| 460 | let write = |bytes: &[u8]| part.write(bytes); |
| 461 | let progress = |percent| { |
| 462 | let _ = events.send(DownloadEvent::Progress(percent)); |
| 463 | }; |
| 464 | match source { |
| 465 | DownloadSource::Share => engine.download_file(&card.open_url, write, progress).await, |
| 466 | DownloadSource::Library(file) => engine.download_library_file(file, write, progress).await, |
| 467 | } |
| 468 | .map_err(|error| short_error(&error))?; |
| 469 | part.finish().map_err(|error| short_error(&error)) |
| 470 | } |
| 471 | |
| 472 | impl ConversationView { |
| 473 | fn file_actions( |
| 474 | &self, |
| 475 | message_key: &str, |
| 476 | files: &[FileCard], |
| 477 | view: WeakEntity<Self>, |
| 478 | ) -> FileActions { |
| 479 | let conversation_id = self.conversation_id().unwrap_or_default(); |
| 480 | let (key, cards) = (message_key.to_owned(), files.to_vec()); |
| 481 | let open_urls: Vec<&str> = files.iter().map(|card| card.open_url.as_str()).collect(); |
| 482 | FileActions { |
| 483 | states: self |
| 484 | .downloads |
| 485 | .states_for(&conversation_id, message_key, &open_urls), |
| 486 | activate: Rc::new(move |index, cx| { |
| 487 | let Some(card) = cards.get(index).cloned() else { |
| 488 | return; |
| 489 | }; |
| 490 | let key = key.clone(); |
| 491 | let conversation_id = conversation_id.clone(); |
| 492 | view.update(cx, |this, cx| { |
| 493 | this.activate_file(&conversation_id, &key, card, DownloadSource::Share, cx) |
| 494 | }) |
| 495 | .ok(); |
| 496 | }), |
| 497 | } |
| 498 | } |
| 499 | |
| 500 | fn open_image_viewer(&mut self, message_key: &str, url: &str, cx: &mut Context<Self>) { |
| 501 | let Some(current) = self.current.as_ref() else { |
| 502 | return; |
| 503 | }; |
| 504 | let now = Local::now(); |
| 505 | let items = collect_gallery( |
| 506 | &self.rows.borrow(), |
| 507 | now.date_naive(), |
| 508 | app_base::format::Zone::Local, |
| 509 | ); |
| 510 | let Some(index) = position_of(&items, message_key, url) else { |
| 511 | return; |
| 512 | }; |
| 513 | let request = ViewerRequest { |
| 514 | conversation_id: current.selection.conversation_id().to_owned(), |
| 515 | conversation_title: selection_title(&self.app.read(cx).sidebar, ¤t.selection), |
| 516 | items, |
| 517 | index, |
| 518 | }; |
| 519 | self.app |
| 520 | .update(cx, |state, cx| state.request_image_viewer(request, cx)); |
| 521 | } |
| 522 | |
| 523 | fn activate_file( |
| 524 | &mut self, |
| 525 | conversation_id: &str, |
| 526 | message_key: &str, |
| 527 | card: FileCard, |
| 528 | source: DownloadSource, |
| 529 | cx: &mut Context<Self>, |
| 530 | ) { |
| 531 | let key = DownloadKey::new(conversation_id, message_key, &card.open_url); |
| 532 | match downloads::click_action(self.downloads.state(&key)) { |
| 533 | ClickAction::Ignore => {} |
| 534 | ClickAction::Start => self.start_download(key, card, source, cx), |
| 535 | ClickAction::Reveal(path) => { |
| 536 | let folder = path |
| 537 | .parent() |
| 538 | .map(Path::to_path_buf) |
| 539 | .unwrap_or_else(downloads::downloads_directory); |
| 540 | match downloads::reveal_target(&path, path.exists(), folder) { |
| 541 | RevealTarget::File(path) => downloads::reveal_in_file_manager(&path), |
| 542 | RevealTarget::Folder(folder) => cx.open_with_system(&folder), |
| 543 | } |
| 544 | } |
| 545 | } |
| 546 | } |
| 547 | |
| 548 | fn start_download( |
| 549 | &mut self, |
| 550 | key: DownloadKey, |
| 551 | card: FileCard, |
| 552 | source: DownloadSource, |
| 553 | cx: &mut Context<Self>, |
| 554 | ) { |
| 555 | if !self.downloads.begin(&key) { |
| 556 | return; |
| 557 | } |
| 558 | cx.notify(); |
| 559 | let state = self.app.read(cx); |
| 560 | let (demo, engine) = (state.mode.demo, state.engine.clone()); |
| 561 | let (sender, mut receiver) = tokio::sync::mpsc::unbounded_channel::<DownloadEvent>(); |
| 562 | if demo { |
| 563 | cx.spawn(async move |this, cx| { |
| 564 | for step in 1..=DEMO_DOWNLOAD_STEPS { |
| 565 | cx.background_executor().timer(DEMO_DOWNLOAD_STEP).await; |
| 566 | let percent = (u32::from(step) * 100 / u32::from(DEMO_DOWNLOAD_STEPS)) as u8; |
| 567 | let progressed = this.update(cx, |this, cx| { |
| 568 | this.downloads.set_progress(&key, percent); |
| 569 | cx.notify(); |
| 570 | }); |
| 571 | if progressed.is_err() { |
| 572 | return; |
| 573 | } |
| 574 | } |
| 575 | let outcome = |
| 576 | downloads::write_demo_file(&card.name).map_err(|error| short_error(&error)); |
| 577 | this.update(cx, |this, cx| { |
| 578 | this.downloads.finish(&key, outcome); |
| 579 | cx.notify(); |
| 580 | }) |
| 581 | .ok(); |
| 582 | }) |
| 583 | .detach(); |
| 584 | return; |
| 585 | } |
| 586 | let Some(engine) = engine else { |
| 587 | self.downloads.finish(&key, Err("not connected".to_owned())); |
| 588 | return; |
| 589 | }; |
| 590 | drop(runtime::spawn(async move { |
| 591 | let outcome = save_file(&engine, &card, &source, &sender).await; |
| 592 | let _ = sender.send(DownloadEvent::Finished(outcome)); |
| 593 | })); |
| 594 | cx.spawn(async move |this, cx| { |
| 595 | while let Some(event) = receiver.recv().await { |
| 596 | let updated = this.update(cx, |this, cx| { |
| 597 | match event { |
| 598 | DownloadEvent::Progress(percent) => { |
| 599 | this.downloads.set_progress(&key, percent) |
| 600 | } |
| 601 | DownloadEvent::Finished(outcome) => this.downloads.finish(&key, outcome), |
| 602 | } |
| 603 | cx.notify(); |
| 604 | }); |
| 605 | if updated.is_err() { |
| 606 | break; |
| 607 | } |
| 608 | } |
| 609 | }) |
| 610 | .detach(); |
| 611 | } |
| 612 | |
| 613 | pub fn new(app: Entity<AppState>, window: &mut Window, cx: &mut Context<Self>) -> Self { |
| 614 | Self::build(app, None, window, cx) |
| 615 | } |
| 616 | |
| 617 | pub fn pinned_to( |
| 618 | app: Entity<AppState>, |
| 619 | selection: Selection, |
| 620 | window: &mut Window, |
| 621 | cx: &mut Context<Self>, |
| 622 | ) -> Self { |
| 623 | Self::build(app, Some(selection), window, cx) |
| 624 | } |
| 625 | |
| 626 | pub fn popped_out( |
| 627 | app: Entity<AppState>, |
| 628 | selection: Selection, |
| 629 | window: &mut Window, |
| 630 | cx: &mut Context<Self>, |
| 631 | ) -> Self { |
| 632 | let mut view = Self::build(app, Some(selection), window, cx); |
| 633 | view.popped_out = true; |
| 634 | view |
| 635 | } |
| 636 | |
| 637 | fn build( |
| 638 | app: Entity<AppState>, |
| 639 | pinned: Option<Selection>, |
| 640 | window: &mut Window, |
| 641 | cx: &mut Context<Self>, |
| 642 | ) -> Self { |
| 643 | let scroller = cx.new(|cx| MessageScrollerState::new(0, cx)); |
| 644 | let composer = cx.new(|cx| Composer::new(app.clone(), window, cx)); |
| 645 | let subject = subject_input(window, cx); |
| 646 | let recap_search = recap::search_input(window, cx); |
| 647 | let recent = cx.new(|cx| emoji::Recent::load(&app.read(cx).store)); |
| 648 | let picker = cx.new(|cx| ReactionPicker::new(app.clone(), recent.clone(), window, cx)); |
| 649 | let draft = cx.new(|cx| NewChatDraft::new(app.clone(), window, cx)); |
| 650 | let mut subscriptions = vec![ |
| 651 | cx.subscribe_in(&app, window, Self::on_app_event), |
| 652 | cx.observe(&app, |_, _, cx| cx.notify()), |
| 653 | cx.subscribe_in(&composer, window, Self::on_composer_event), |
| 654 | cx.subscribe_in(&draft, window, Self::on_draft_event), |
| 655 | cx.subscribe_in(&subject, window, Self::on_subject_event), |
| 656 | cx.subscribe_in(&recap_search, window, Self::on_recap_search_event), |
| 657 | cx.observe_window_activation(window, Self::on_window_activation), |
| 658 | cx.on_app_quit(|this, cx| { |
| 659 | this.composer |
| 660 | .update(cx, |composer, cx| composer.save_draft_now(cx)); |
| 661 | async {} |
| 662 | }), |
| 663 | cx.observe_keystrokes(|this, event, _, cx| { |
| 664 | if event.keystroke.key == "escape" { |
| 665 | this.close_reaction_details(cx); |
| 666 | this.close_context_menu(false, cx); |
| 667 | } |
| 668 | }), |
| 669 | ]; |
| 670 | if let Some(runs) = crate::agents::runs_handle(cx) { |
| 671 | subscriptions.push(cx.observe(&runs, |this, _, cx| this.sync_agent_chips(cx))); |
| 672 | } |
| 673 | if let Some(api) = crate::agent_api::handle(cx) { |
| 674 | subscriptions.push(cx.observe(&api, |this, _, cx| this.sync_reaction_pills(cx))); |
| 675 | } |
| 676 | let mut view = ConversationView { |
| 677 | group_name: GroupNameSlot::default(), |
| 678 | app, |
| 679 | pinned: pinned.is_some(), |
| 680 | popped_out: false, |
| 681 | scroller, |
| 682 | composer, |
| 683 | subject, |
| 684 | new_post_open: false, |
| 685 | reply_composer: None, |
| 686 | reply_root: None, |
| 687 | reply_drafts: HashMap::new(), |
| 688 | feed_stash: None, |
| 689 | feed_rows_seen: Rc::new(Cell::new(usize::MAX)), |
| 690 | feed_at_top: true, |
| 691 | older_anchor: None, |
| 692 | draft, |
| 693 | draft_active: false, |
| 694 | draft_created: None, |
| 695 | draft_error: None, |
| 696 | rows: Rc::new(RefCell::new(Vec::new())), |
| 697 | current: None, |
| 698 | pending: Vec::new(), |
| 699 | saving_edits: HashSet::new(), |
| 700 | pending_counter: 0, |
| 701 | pending_outgoing: HashMap::new(), |
| 702 | sent_previews: VecDeque::new(), |
| 703 | pending_reactions: HashMap::new(), |
| 704 | downloads: Downloads::default(), |
| 705 | hovered_message: None, |
| 706 | hover_leave_serial: 0, |
| 707 | toolbar_hovered: None, |
| 708 | context_menu: None, |
| 709 | context_serial: 0, |
| 710 | timeline_bounds: TimelineBounds::default(), |
| 711 | header_width: Rc::default(), |
| 712 | toolbar_pinned: None, |
| 713 | toolbar_suppressed: None, |
| 714 | recent, |
| 715 | picker, |
| 716 | reaction_details: None, |
| 717 | highlighted_message: None, |
| 718 | fetched_jump: None, |
| 719 | loading_post: None, |
| 720 | pin_index: 0, |
| 721 | channel_details_open: false, |
| 722 | copilot: copilot::CopilotPane::default(), |
| 723 | pinned_posts_expanded: false, |
| 724 | channel_details_scroll: ScrollHandle::new(), |
| 725 | scroller_watch: None, |
| 726 | pin_previews: HashMap::new(), |
| 727 | pin_fetches: HashSet::new(), |
| 728 | notice: None, |
| 729 | scheduled_failure: None, |
| 730 | scheduled_in_flight: HashSet::new(), |
| 731 | drag_file_count: 0, |
| 732 | sync_generation: 0, |
| 733 | rebuild_generation: 0, |
| 734 | window_active: window.is_window_active(), |
| 735 | pane: ChannelPane::Posts, |
| 736 | recap: recap::RecapState::default(), |
| 737 | recap_search, |
| 738 | channel_tabs: Vec::new(), |
| 739 | tab_links: HashMap::new(), |
| 740 | focus_handle: cx.focus_handle(), |
| 741 | shared: SharedState::default(), |
| 742 | demo_library: DemoLibrary::default(), |
| 743 | new_folder_input: None, |
| 744 | agent_layouts: HashMap::new(), |
| 745 | agent_runs: Rc::new(RefCell::new(HashMap::new())), |
| 746 | reaction_proposals: Rc::new(RefCell::new(HashMap::new())), |
| 747 | receipt_fade: receipt_fade::ReceiptFade::default(), |
| 748 | appear_tracker: appear_tracker::AppearTracker::default(), |
| 749 | find_panel: None, |
| 750 | find_subscription: None, |
| 751 | _subscriptions: subscriptions, |
| 752 | }; |
| 753 | if let Some(selection) = pinned.or_else(|| view.app.read(cx).selection.clone()) { |
| 754 | view.open(selection, window, cx); |
| 755 | } |
| 756 | view |
| 757 | } |
| 758 | |
| 759 | #[cfg_attr(not(windows), allow(dead_code))] |
| 760 | pub fn composer_is_empty(&self, cx: &App) -> bool { |
| 761 | self.composer.read(cx).is_empty(cx) |
| 762 | } |
| 763 | |
| 764 | #[cfg_attr(not(windows), allow(dead_code))] |
| 765 | pub fn opening_title(&self) -> Option<String> { |
| 766 | let loading = self |
| 767 | .rows |
| 768 | .borrow() |
| 769 | .iter() |
| 770 | .any(|row| matches!(row, Row::Skeleton(_))); |
| 771 | self.current |
| 772 | .as_ref() |
| 773 | .filter(|_| loading) |
| 774 | .map(|current| current.title.clone()) |
| 775 | } |
| 776 | |
| 777 | pub fn send_in_flight(&self) -> bool { |
| 778 | self.pending |
| 779 | .iter() |
| 780 | .any(|row| matches!(row.delivery, Delivery::Sending)) |
| 781 | } |
| 782 | |
| 783 | fn on_window_activation(&mut self, window: &mut Window, cx: &mut Context<Self>) { |
| 784 | self.window_active = window.is_window_active(); |
| 785 | self.mark_read(ReadTrigger::Activation, cx); |
| 786 | } |
| 787 | |
| 788 | fn on_app_event( |
| 789 | &mut self, |
| 790 | _: &Entity<AppState>, |
| 791 | event: &AppEvent, |
| 792 | window: &mut Window, |
| 793 | cx: &mut Context<Self>, |
| 794 | ) { |
| 795 | match event { |
| 796 | AppEvent::Selection if self.pinned => {} |
| 797 | AppEvent::Selection => { |
| 798 | self.reaction_details = None; |
| 799 | let (new_chat, selection) = { |
| 800 | let state = self.app.read(cx); |
| 801 | (state.new_chat, state.selection.clone()) |
| 802 | }; |
| 803 | if new_chat { |
| 804 | self.enter_draft(window, cx); |
| 805 | return; |
| 806 | } |
| 807 | if self.draft_active { |
| 808 | let created = self |
| 809 | .draft_created |
| 810 | .as_ref() |
| 811 | .map(|(_, chat_id)| chat_id.clone()); |
| 812 | if let Some(chat_id) = created { |
| 813 | self.composer |
| 814 | .update(cx, |composer, _| composer.carry_images_into(chat_id)); |
| 815 | } |
| 816 | self.leave_draft(window, cx); |
| 817 | } |
| 818 | match selection { |
| 819 | Some(selection) => self.open(selection, window, cx), |
| 820 | None => { |
| 821 | self.current = None; |
| 822 | self.rebuild(true, cx); |
| 823 | } |
| 824 | } |
| 825 | } |
| 826 | AppEvent::Messages(conversation_id) => { |
| 827 | let is_current = self |
| 828 | .current |
| 829 | .as_ref() |
| 830 | .is_some_and(|current| current.selection.conversation_id() == conversation_id); |
| 831 | if is_current { |
| 832 | if self.shared_active() || self.recap_active() || self.channel_details_open { |
| 833 | self.refresh_shared_items(cx); |
| 834 | } |
| 835 | self.rebuild_in_background(cx); |
| 836 | self.mark_read(ReadTrigger::Incoming, cx); |
| 837 | self.on_pins_changed(conversation_id, cx); |
| 838 | } |
| 839 | } |
| 840 | AppEvent::Images(keys) => self.remeasure_images(keys, cx), |
| 841 | AppEvent::Cards(conversation_id) => self.refresh_cards(conversation_id, cx), |
| 842 | AppEvent::Jump => self.apply_pending_jump(cx), |
| 843 | AppEvent::Status => { |
| 844 | if let Some(current) = self.current.as_mut() |
| 845 | && current.sync.failed |
| 846 | { |
| 847 | current.fetched = false; |
| 848 | } |
| 849 | self.start_fetch(cx) |
| 850 | } |
| 851 | AppEvent::Directory => { |
| 852 | self.refresh_reactor_names(cx); |
| 853 | cx.notify(); |
| 854 | } |
| 855 | AppEvent::ForwardSelection => self.on_forward_selection_changed(window, cx), |
| 856 | AppEvent::TaskDialog |
| 857 | | AppEvent::LocalPreviews |
| 858 | | AppEvent::Forward |
| 859 | | AppEvent::ImageViewer |
| 860 | | AppEvent::StatusMessage |
| 861 | | AppEvent::CalendarEventPeek |
| 862 | | AppEvent::Settings(_) |
| 863 | | AppEvent::Profile |
| 864 | | AppEvent::CallHistory |
| 865 | | AppEvent::Ring |
| 866 | | AppEvent::Activity |
| 867 | | AppEvent::MissedCall(_) |
| 868 | | AppEvent::MeetingReminder |
| 869 | | AppEvent::MeetingChime |
| 870 | | AppEvent::CallTone(_) |
| 871 | | AppEvent::SoundSettings |
| 872 | | AppEvent::FollowedStatus { .. } |
| 873 | | AppEvent::OrgMessage(_) |
| 874 | | AppEvent::Call => {} |
| 875 | AppEvent::LiveMeeting => cx.notify(), |
| 876 | AppEvent::Saved => cx.notify(), |
| 877 | AppEvent::Translation | AppEvent::ChatSettings => self.rebuild(false, cx), |
| 878 | AppEvent::Pins(conversation_id) => self.on_pins_changed(conversation_id, cx), |
| 879 | AppEvent::Typing => cx.notify(), |
| 880 | AppEvent::Scheduled => self.rebuild(false, cx), |
| 881 | AppEvent::Outbox(conversation_id) => { |
| 882 | let is_current = self |
| 883 | .current |
| 884 | .as_ref() |
| 885 | .is_some_and(|current| current.selection.conversation_id() == conversation_id); |
| 886 | if is_current { |
| 887 | self.reload_pending(cx); |
| 888 | self.rebuild(false, cx); |
| 889 | } |
| 890 | } |
| 891 | AppEvent::Sidebar => { |
| 892 | self.reload_channel_tabs(cx); |
| 893 | self.mark_read(ReadTrigger::Incoming, cx); |
| 894 | self.refresh_reactor_names(cx); |
| 895 | if self.draft_active { |
| 896 | self.on_draft_changed(window, cx); |
| 897 | } else if let Some(current) = self.current.as_mut() { |
| 898 | let title = selection_title(&self.app.read(cx).sidebar, ¤t.selection); |
| 899 | if current.title != title { |
| 900 | current.title = title; |
| 901 | cx.notify(); |
| 902 | } |
| 903 | } |
| 904 | } |
| 905 | } |
| 906 | } |
| 907 | |
| 908 | fn on_composer_event( |
| 909 | &mut self, |
| 910 | _: &Entity<Composer>, |
| 911 | event: &ComposerEvent, |
| 912 | window: &mut Window, |
| 913 | cx: &mut Context<Self>, |
| 914 | ) { |
| 915 | self.report_composer_activity(event, cx); |
| 916 | match event { |
| 917 | ComposerEvent::Submit(outgoing) => self.submit_main((**outgoing).clone(), window, cx), |
| 918 | ComposerEvent::Schedule { outgoing, send_at } => { |
| 919 | self.schedule((**outgoing).clone(), *send_at, window, cx) |
| 920 | } |
| 921 | ComposerEvent::EditLast => self.edit_last_own(window, cx), |
| 922 | ComposerEvent::FocusMessages => window.focus(&self.focus_handle, cx), |
| 923 | ComposerEvent::Typing(active) => self.send_typing(*active, cx), |
| 924 | ComposerEvent::Edited => {} |
| 925 | ComposerEvent::ClosePost => self.cancel_new_post(window, cx), |
| 926 | ComposerEvent::RestorePost(subject) => { |
| 927 | self.restore_new_post(subject.clone(), window, cx) |
| 928 | } |
| 929 | } |
| 930 | } |
| 931 | |
| 932 | fn submit_main(&mut self, mut outgoing: Outgoing, window: &mut Window, cx: &mut Context<Self>) { |
| 933 | if self.draft_active { |
| 934 | return self.send_new_chat(outgoing, window, cx); |
| 935 | } |
| 936 | let in_feed = self.in_feed(); |
| 937 | if in_feed && outgoing.edit.is_none() { |
| 938 | outgoing.subject = self.take_subject(window, cx); |
| 939 | } |
| 940 | if in_feed { |
| 941 | self.close_new_post(cx); |
| 942 | } |
| 943 | match &outgoing.edit { |
| 944 | Some(edit) if edit.scheduled.is_some() => { |
| 945 | self.send_scheduled_edit(outgoing, window, cx) |
| 946 | } |
| 947 | Some(_) => self.send_edit(outgoing, window, cx), |
| 948 | None => self.send(outgoing, window, cx), |
| 949 | } |
| 950 | } |
| 951 | |
| 952 | fn typing_root(&self) -> Option<String> { |
| 953 | match self.current.as_ref().map(|current| ¤t.mode) { |
| 954 | Some(ViewMode::Thread(root_id)) => Some(root_id.clone()), |
| 955 | _ => None, |
| 956 | } |
| 957 | } |
| 958 | |
| 959 | fn send_typing(&self, active: bool, cx: &App) { |
| 960 | self.send_typing_in(self.typing_root(), active, cx); |
| 961 | } |
| 962 | |
| 963 | fn send_typing_in(&self, thread_root_id: Option<String>, active: bool, cx: &App) { |
| 964 | let state = self.app.read(cx); |
| 965 | if state.mode.demo || state.mode.read_only || self.draft_active { |
| 966 | return; |
| 967 | } |
| 968 | let (Some(engine), Some(conversation_id)) = (state.engine.clone(), self.conversation_id()) |
| 969 | else { |
| 970 | return; |
| 971 | }; |
| 972 | drop(runtime::spawn(async move { |
| 973 | let sent = engine |
| 974 | .send_typing(&conversation_id, thread_root_id.as_deref(), active) |
| 975 | .await; |
| 976 | if let Err(error) = sent |
| 977 | && cfg!(debug_assertions) |
| 978 | { |
| 979 | crate::diagnostics::record(format!("typing indicator not sent: {error}")); |
| 980 | } |
| 981 | })); |
| 982 | } |
| 983 | |
| 984 | fn stop_typing(&mut self, cx: &mut Context<Self>) { |
| 985 | if self |
| 986 | .composer |
| 987 | .update(cx, |composer, _| composer.stop_typing()) |
| 988 | { |
| 989 | self.send_typing(false, cx); |
| 990 | } |
| 991 | } |
| 992 | |
| 993 | fn render_typing_line(&self, cx: &App) -> Option<AnyElement> { |
| 994 | if self.in_feed() { |
| 995 | return None; |
| 996 | } |
| 997 | let app = self.app.read(cx); |
| 998 | let faces = self |
| 999 | .current |
| 1000 | .as_ref() |
| 1001 | .filter(|_| !self.draft_active) |
| 1002 | .map(|current| app.typing.faces(current.selection.conversation_id())) |
| 1003 | .unwrap_or_default(); |
| 1004 | if faces.is_empty() { |
| 1005 | return None; |
| 1006 | } |
| 1007 | let tooltip_text = typing_tooltip( |
| 1008 | &faces |
| 1009 | .iter() |
| 1010 | .map(|face| face.name.clone()) |
| 1011 | .collect::<Vec<_>>(), |
| 1012 | ); |
| 1013 | let faces_element = match faces.as_slice() { |
| 1014 | [face] => person_avatar( |
| 1015 | &app.directory, |
| 1016 | face.user_id.as_deref(), |
| 1017 | &face.name, |
| 1018 | AVATAR_SIZE, |
| 1019 | ), |
| 1020 | _ => member_stack(&app.directory, &faces, faces.len(), true).into_any_element(), |
| 1021 | }; |
| 1022 | Some( |
| 1023 | h_flex() |
| 1024 | .debug_selector(|| TYPING_TAG.into()) |
| 1025 | .h(px(TYPING_BUBBLE_HEIGHT)) |
| 1026 | .px(px(TYPING_SIDE_INSET)) |
| 1027 | .gap(px(AVATAR_GAP)) |
| 1028 | .items_center() |
| 1029 | .child( |
| 1030 | div() |
| 1031 | .id("typing-faces") |
| 1032 | .flex_none() |
| 1033 | .tooltip(move |window, cx| { |
| 1034 | Tooltip::new(tooltip_text.clone()).build(window, cx) |
| 1035 | }) |
| 1036 | .child(faces_element), |
| 1037 | ) |
| 1038 | .child( |
| 1039 | h_flex() |
| 1040 | .h_full() |
| 1041 | .px(px(TYPING_BUBBLE_PADDING)) |
| 1042 | .items_center() |
| 1043 | .rounded_full() |
| 1044 | .bg(theme::surface_raised()) |
| 1045 | .child(typing_dots()), |
| 1046 | ) |
| 1047 | .into_any_element(), |
| 1048 | ) |
| 1049 | } |
| 1050 | |
| 1051 | fn open(&mut self, selection: Selection, window: &mut Window, cx: &mut Context<Self>) { |
| 1052 | if self |
| 1053 | .find_panel |
| 1054 | .as_ref() |
| 1055 | .is_some_and(|panel| panel.read(cx).selection() != &selection) |
| 1056 | { |
| 1057 | self.find_panel = None; |
| 1058 | self.find_subscription = None; |
| 1059 | } |
| 1060 | self.stop_typing(cx); |
| 1061 | self.stop_reply_typing(cx); |
| 1062 | let next_conversation_id = selection.conversation_id().to_owned(); |
| 1063 | self.composer.update(cx, |composer, cx| { |
| 1064 | composer.leave_conversation(&next_conversation_id, cx) |
| 1065 | }); |
| 1066 | self.close_new_post(cx); |
| 1067 | self.feed_stash = None; |
| 1068 | self.feed_at_top = true; |
| 1069 | self.restore_subject(None, window, cx); |
| 1070 | self.toolbar_hovered = None; |
| 1071 | self.reset_copilot(); |
| 1072 | crate::inline_video::stop(cx); |
| 1073 | self.toolbar_pinned = None; |
| 1074 | self.toolbar_suppressed = None; |
| 1075 | self.context_menu = None; |
| 1076 | self.agent_layouts.clear(); |
| 1077 | self.agent_runs.borrow_mut().clear(); |
| 1078 | self.reaction_proposals.borrow_mut().clear(); |
| 1079 | let title = selection_title(&self.app.read(cx).sidebar, &selection); |
| 1080 | let mode = match selection { |
| 1081 | Selection::Chat(_) => ViewMode::Flat, |
| 1082 | Selection::Channel(_) => ViewMode::ThreadList, |
| 1083 | }; |
| 1084 | let first_unread = self.first_unread_id(&selection, cx); |
| 1085 | let posts_new_after = self.channel_read_until(&selection, cx); |
| 1086 | self.current = Some(Current { |
| 1087 | loaded_limit: open_limit(&selection), |
| 1088 | selection, |
| 1089 | title, |
| 1090 | mode, |
| 1091 | loaded_count: 0, |
| 1092 | has_older: false, |
| 1093 | loading_older: false, |
| 1094 | older_blocked: false, |
| 1095 | fetched: false, |
| 1096 | quiet_retries: 0, |
| 1097 | first_unread, |
| 1098 | posts_new_after, |
| 1099 | sync: SyncProgress::default(), |
| 1100 | detached: None, |
| 1101 | window_request: None, |
| 1102 | }); |
| 1103 | self.refresh_agent_runs(cx); |
| 1104 | self.refresh_reaction_pills(cx); |
| 1105 | self.sync_generation += 1; |
| 1106 | self.reset_channel_tabs(cx); |
| 1107 | self.reset_recap(cx); |
| 1108 | self.reload_pending(cx); |
| 1109 | self.notice = None; |
| 1110 | self.scheduled_failure = None; |
| 1111 | self.app.update(cx, |state, cx| state.refresh_scheduled(cx)); |
| 1112 | self.rebuild(true, cx); |
| 1113 | self.start_fetch(cx); |
| 1114 | self.mark_read(ReadTrigger::Open, cx); |
| 1115 | self.refresh_pin_previews(cx); |
| 1116 | self.pinned_posts_expanded = false; |
| 1117 | if let Some(conversation_id) = self.pin_conversation_id() { |
| 1118 | self.app |
| 1119 | .update(cx, |state, cx| state.refresh_pins(&conversation_id, cx)); |
| 1120 | } |
| 1121 | let conversation_id = self |
| 1122 | .current |
| 1123 | .as_ref() |
| 1124 | .map(|current| current.selection.conversation_id().to_owned()) |
| 1125 | .unwrap_or_default(); |
| 1126 | self.composer.update(cx, |composer, cx| { |
| 1127 | composer.set_conversation(&conversation_id, window, cx); |
| 1128 | }); |
| 1129 | self.sync_reply_composer(window, cx); |
| 1130 | if self.in_feed() { |
| 1131 | window.focus(&self.focus_handle, cx); |
| 1132 | } else if !self.draft_active { |
| 1133 | self.composer |
| 1134 | .update(cx, |composer, cx| composer.focus(window, cx)); |
| 1135 | } |
| 1136 | self.hovered_message = None; |
| 1137 | self.apply_pending_jump(cx); |
| 1138 | } |
| 1139 | |
| 1140 | fn enter_draft(&mut self, window: &mut Window, cx: &mut Context<Self>) { |
| 1141 | if !self.draft_active { |
| 1142 | self.stop_typing(cx); |
| 1143 | self.draft_active = true; |
| 1144 | self.current = None; |
| 1145 | self.clear_pending(); |
| 1146 | self.notice = None; |
| 1147 | self.draft_created = None; |
| 1148 | self.draft_error = None; |
| 1149 | self.toolbar_hovered = None; |
| 1150 | self.toolbar_pinned = None; |
| 1151 | self.toolbar_suppressed = None; |
| 1152 | self.hovered_message = None; |
| 1153 | self.sync_generation += 1; |
| 1154 | self.draft.update(cx, |draft, cx| draft.reset(window, cx)); |
| 1155 | self.composer |
| 1156 | .update(cx, |composer, cx| composer.set_conversation("", window, cx)); |
| 1157 | self.on_draft_changed(window, cx); |
| 1158 | self.rebuild(true, cx); |
| 1159 | } |
| 1160 | let people = self |
| 1161 | .app |
| 1162 | .update(cx, |state, _| std::mem::take(&mut state.new_chat_people)); |
| 1163 | if !people.is_empty() { |
| 1164 | let chips = people |
| 1165 | .into_iter() |
| 1166 | .filter_map(|person| { |
| 1167 | Some(Pick { |
| 1168 | user_id: person.user_id?, |
| 1169 | name: person.display_name, |
| 1170 | mail: None, |
| 1171 | }) |
| 1172 | }) |
| 1173 | .collect(); |
| 1174 | self.draft |
| 1175 | .update(cx, |draft, cx| draft.set_chips(chips, cx)); |
| 1176 | } |
| 1177 | self.draft |
| 1178 | .update(cx, |draft, cx| draft.focus_query(window, cx)); |
| 1179 | cx.notify(); |
| 1180 | } |
| 1181 | |
| 1182 | fn leave_draft(&mut self, window: &mut Window, cx: &mut Context<Self>) { |
| 1183 | self.draft_active = false; |
| 1184 | self.draft_created = None; |
| 1185 | self.draft_error = None; |
| 1186 | self.clear_pending(); |
| 1187 | self.draft.update(cx, |draft, cx| draft.reset(window, cx)); |
| 1188 | } |
| 1189 | |
| 1190 | fn on_draft_event( |
| 1191 | &mut self, |
| 1192 | _: &Entity<NewChatDraft>, |
| 1193 | event: &NewChatEvent, |
| 1194 | window: &mut Window, |
| 1195 | cx: &mut Context<Self>, |
| 1196 | ) { |
| 1197 | match event { |
| 1198 | NewChatEvent::Changed => self.on_draft_changed(window, cx), |
| 1199 | NewChatEvent::FocusComposer => self |
| 1200 | .composer |
| 1201 | .update(cx, |composer, cx| composer.focus(window, cx)), |
| 1202 | NewChatEvent::Close => self.app.update(cx, |state, cx| state.close_new_chat(cx)), |
| 1203 | } |
| 1204 | } |
| 1205 | |
| 1206 | fn draft_target_chat(&self, cx: &App) -> Option<String> { |
| 1207 | let chips = self.draft.read(cx).chips(); |
| 1208 | let [chip] = chips else { |
| 1209 | return None; |
| 1210 | }; |
| 1211 | let state = self.app.read(cx); |
| 1212 | existing_one_on_one( |
| 1213 | &state.sidebar.chats, |
| 1214 | state.directory.me.as_ref(), |
| 1215 | &chip.user_id, |
| 1216 | ) |
| 1217 | .map(|chat| chat.id.clone()) |
| 1218 | } |
| 1219 | |
| 1220 | fn on_draft_changed(&mut self, window: &mut Window, cx: &mut Context<Self>) { |
| 1221 | if !self.draft_active { |
| 1222 | return; |
| 1223 | } |
| 1224 | let target = self.draft_target_chat(cx); |
| 1225 | if target != self.conversation_id() { |
| 1226 | match target { |
| 1227 | Some(chat_id) => { |
| 1228 | self.composer.update(cx, |composer, _| { |
| 1229 | composer.carry_images_into(chat_id.clone()) |
| 1230 | }); |
| 1231 | self.open(Selection::Chat(chat_id), window, cx) |
| 1232 | } |
| 1233 | None => { |
| 1234 | self.composer.update(cx, |composer, cx| { |
| 1235 | composer.carry_images_into(String::new()); |
| 1236 | composer.set_conversation("", window, cx); |
| 1237 | }); |
| 1238 | self.current = None; |
| 1239 | self.sync_generation += 1; |
| 1240 | self.rebuild(true, cx); |
| 1241 | } |
| 1242 | } |
| 1243 | } |
| 1244 | let placeholder = { |
| 1245 | let draft = self.draft.read(cx); |
| 1246 | composer_placeholder(draft.chips(), &draft.group_name(cx)) |
| 1247 | }; |
| 1248 | self.composer.update(cx, |composer, cx| { |
| 1249 | composer.set_placeholder(&placeholder, window, cx) |
| 1250 | }); |
| 1251 | cx.notify(); |
| 1252 | } |
| 1253 | |
| 1254 | fn remeasure_images(&mut self, keys: &[String], cx: &mut Context<Self>) { |
| 1255 | let indices: Vec<usize> = self |
| 1256 | .rows |
| 1257 | .borrow() |
| 1258 | .iter() |
| 1259 | .enumerate() |
| 1260 | .filter(|(_, row)| { |
| 1261 | row.messages().any(|message| { |
| 1262 | message.images.iter().any(|image| keys.contains(&image.url)) |
| 1263 | || message |
| 1264 | .link_preview |
| 1265 | .as_ref() |
| 1266 | .and_then(LinkPreview::image) |
| 1267 | .is_some_and(|image| keys.contains(&image.url)) |
| 1268 | }) |
| 1269 | }) |
| 1270 | .map(|(index, _)| index) |
| 1271 | .collect(); |
| 1272 | self.scroller.update(cx, |scroller, cx| { |
| 1273 | for index in indices { |
| 1274 | scroller.remeasure_items(index..index + 1, cx); |
| 1275 | } |
| 1276 | }); |
| 1277 | cx.notify(); |
| 1278 | } |
| 1279 | |
| 1280 | fn refresh_cards(&mut self, conversation_id: &str, cx: &mut Context<Self>) { |
| 1281 | let is_current = self |
| 1282 | .current |
| 1283 | .as_ref() |
| 1284 | .is_some_and(|current| current.selection.conversation_id() == conversation_id); |
| 1285 | if !is_current { |
| 1286 | return; |
| 1287 | } |
| 1288 | self.rebuild(false, cx); |
| 1289 | let indices: Vec<usize> = self |
| 1290 | .rows |
| 1291 | .borrow() |
| 1292 | .iter() |
| 1293 | .enumerate() |
| 1294 | .filter(|(_, row)| { |
| 1295 | row.messages() |
| 1296 | .any(|message| !message.adaptive_cards.is_empty()) |
| 1297 | }) |
| 1298 | .map(|(index, _)| index) |
| 1299 | .collect(); |
| 1300 | self.scroller.update(cx, |scroller, cx| { |
| 1301 | for index in indices { |
| 1302 | scroller.remeasure_items(index..index + 1, cx); |
| 1303 | } |
| 1304 | }); |
| 1305 | cx.notify(); |
| 1306 | } |
| 1307 | |
| 1308 | fn apply_pending_jump(&mut self, cx: &mut Context<Self>) { |
| 1309 | let Some(conversation_id) = self |
| 1310 | .current |
| 1311 | .as_ref() |
| 1312 | .map(|current| current.selection.conversation_id().to_owned()) |
| 1313 | else { |
| 1314 | return; |
| 1315 | }; |
| 1316 | let jump = self |
| 1317 | .app |
| 1318 | .update(cx, |state, _| match state.pending_jump.take() { |
| 1319 | Some((target, message_id)) if target == conversation_id => Some(message_id), |
| 1320 | other => { |
| 1321 | state.pending_jump = other; |
| 1322 | None |
| 1323 | } |
| 1324 | }); |
| 1325 | if let Some(message_id) = jump { |
| 1326 | self.jump_to_message(&conversation_id, &message_id, cx); |
| 1327 | } |
| 1328 | } |
| 1329 | |
| 1330 | fn jump_to_message(&mut self, conversation_id: &str, message_id: &str, cx: &mut Context<Self>) { |
| 1331 | self.jump_to_post(conversation_id, message_id, true, cx); |
| 1332 | } |
| 1333 | |
| 1334 | fn jump_to_post( |
| 1335 | &mut self, |
| 1336 | conversation_id: &str, |
| 1337 | message_id: &str, |
| 1338 | refresh_replies: bool, |
| 1339 | cx: &mut Context<Self>, |
| 1340 | ) { |
| 1341 | let store = self.app.read(cx).store.clone(); |
| 1342 | let record = store |
| 1343 | .messages_by_id(conversation_id, &[message_id.to_owned()]) |
| 1344 | .ok() |
| 1345 | .and_then(|mut found| found.remove(message_id)); |
| 1346 | if self.chat_id().as_deref() == Some(conversation_id) { |
| 1347 | let created_at = record.as_ref().map(|record| record.created_at); |
| 1348 | match self.jump_plan_for(conversation_id, created_at, cx) { |
| 1349 | JumpPlan::LoadWindow => { |
| 1350 | self.open_detached(conversation_id.to_owned(), message_id.to_owned(), cx); |
| 1351 | return; |
| 1352 | } |
| 1353 | plan => { |
| 1354 | if let Some(current) = self.current.as_mut() { |
| 1355 | current.window_request = None; |
| 1356 | if plan == JumpPlan::LeaveWindow { |
| 1357 | current.detached = None; |
| 1358 | } |
| 1359 | } |
| 1360 | } |
| 1361 | } |
| 1362 | } |
| 1363 | let Some(record) = record else { |
| 1364 | self.open_unloaded_post(message_id, cx); |
| 1365 | return; |
| 1366 | }; |
| 1367 | let messages_from_target = store |
| 1368 | .messages(conversation_id, None, JUMP_SCAN_LIMIT) |
| 1369 | .ok() |
| 1370 | .and_then(|all| { |
| 1371 | all.iter() |
| 1372 | .position(|candidate| candidate.message_id == message_id) |
| 1373 | .map(|position| all.len() - position) |
| 1374 | }) |
| 1375 | .unwrap_or_default(); |
| 1376 | let row_key = record |
| 1377 | .reply_to_id |
| 1378 | .clone() |
| 1379 | .unwrap_or_else(|| message_id.to_owned()); |
| 1380 | let Some(current) = self.current.as_mut() else { |
| 1381 | return; |
| 1382 | }; |
| 1383 | current.loaded_limit = current |
| 1384 | .loaded_limit |
| 1385 | .max(messages_from_target + JUMP_CONTEXT_MESSAGES); |
| 1386 | current.first_unread = None; |
| 1387 | let in_channel = matches!(current.selection, Selection::Channel(_)); |
| 1388 | if in_channel { |
| 1389 | self.pane = ChannelPane::Posts; |
| 1390 | self.enter_feed_mode(); |
| 1391 | self.stop_typing(cx); |
| 1392 | self.enter_thread_mode(row_key.clone(), cx); |
| 1393 | } |
| 1394 | self.reload_pending(cx); |
| 1395 | self.rebuild(true, cx); |
| 1396 | if in_channel { |
| 1397 | if refresh_replies { |
| 1398 | self.refresh_thread(row_key.clone(), cx); |
| 1399 | } |
| 1400 | let shown_alone = self.rows.borrow().iter().any(|row| row.key() == message_id); |
| 1401 | if shown_alone { |
| 1402 | self.reveal_row(message_id.to_owned(), message_id.to_owned(), cx); |
| 1403 | return; |
| 1404 | } |
| 1405 | } |
| 1406 | self.reveal_row(row_key, message_id.to_owned(), cx); |
| 1407 | } |
| 1408 | |
| 1409 | fn open_unloaded_post(&mut self, message_id: &str, cx: &mut Context<Self>) { |
| 1410 | let state = self.app.read(cx); |
| 1411 | let (Some(engine), Some(current)) = (state.engine.clone(), self.current.as_ref()) else { |
| 1412 | return; |
| 1413 | }; |
| 1414 | if !matches!(current.selection, Selection::Channel(_)) |
| 1415 | || state.mode.demo |
| 1416 | || self.fetched_jump.as_deref() == Some(message_id) |
| 1417 | { |
| 1418 | return; |
| 1419 | } |
| 1420 | self.fetched_jump = Some(message_id.to_owned()); |
| 1421 | let conversation_id = current.selection.conversation_id().to_owned(); |
| 1422 | let (task_conversation_id, task_message_id) = |
| 1423 | (conversation_id.clone(), message_id.to_owned()); |
| 1424 | let receiver = runtime::spawn(async move { |
| 1425 | engine |
| 1426 | .open_post(&task_conversation_id, &task_message_id) |
| 1427 | .await |
| 1428 | }); |
| 1429 | let message_id = message_id.to_owned(); |
| 1430 | self.show_loading_post(message_id.clone(), cx); |
| 1431 | cx.spawn(async move |this, cx| { |
| 1432 | let result = receiver.await; |
| 1433 | this.update(cx, |this, cx| { |
| 1434 | this.finish_loading_post(&conversation_id, &message_id, result, cx) |
| 1435 | }) |
| 1436 | .ok(); |
| 1437 | }) |
| 1438 | .detach(); |
| 1439 | } |
| 1440 | |
| 1441 | fn show_loading_post(&mut self, message_id: String, cx: &mut Context<Self>) { |
| 1442 | self.pane = ChannelPane::Posts; |
| 1443 | self.enter_feed_mode(); |
| 1444 | self.stop_typing(cx); |
| 1445 | self.enter_thread_mode(message_id.clone(), cx); |
| 1446 | self.loading_post = Some(message_id); |
| 1447 | self.rebuild(true, cx); |
| 1448 | } |
| 1449 | |
| 1450 | fn finish_loading_post( |
| 1451 | &mut self, |
| 1452 | conversation_id: &str, |
| 1453 | message_id: &str, |
| 1454 | result: Result< |
| 1455 | teams_core::Result<Option<teams_core::OpenedPost>>, |
| 1456 | tokio::sync::oneshot::error::RecvError, |
| 1457 | >, |
| 1458 | cx: &mut Context<Self>, |
| 1459 | ) { |
| 1460 | if self.loading_post.as_deref() != Some(message_id) { |
| 1461 | return; |
| 1462 | } |
| 1463 | self.loading_post = None; |
| 1464 | if self.conversation_id().as_deref() != Some(conversation_id) { |
| 1465 | return; |
| 1466 | } |
| 1467 | let still_waiting = self |
| 1468 | .current |
| 1469 | .as_ref() |
| 1470 | .is_some_and(|current| current.mode == ViewMode::Thread(message_id.to_owned())); |
| 1471 | match result { |
| 1472 | Ok(Ok(Some(post))) if still_waiting => { |
| 1473 | self.jump_to_post(conversation_id, message_id, !post.replies_loaded, cx) |
| 1474 | } |
| 1475 | Ok(Ok(Some(_))) => self.rebuild(true, cx), |
| 1476 | failure => { |
| 1477 | if let Ok(Err(error)) = &failure { |
| 1478 | self.notice = Some(format!("Could not load the post: {}", short_error(error))); |
| 1479 | } |
| 1480 | if still_waiting { |
| 1481 | self.back_to_threads(cx); |
| 1482 | } else { |
| 1483 | cx.notify(); |
| 1484 | } |
| 1485 | } |
| 1486 | } |
| 1487 | } |
| 1488 | |
| 1489 | fn reveal_row(&mut self, row_key: String, message_key: String, cx: &mut Context<Self>) { |
| 1490 | let index = self |
| 1491 | .rows |
| 1492 | .borrow() |
| 1493 | .iter() |
| 1494 | .position(|row| row.key() == row_key); |
| 1495 | if let Some(index) = index { |
| 1496 | self.scroller |
| 1497 | .update(cx, |scroller, cx| scroller.scroll_to_item(index, cx)); |
| 1498 | self.highlight(message_key, cx); |
| 1499 | } |
| 1500 | } |
| 1501 | |
| 1502 | fn highlight(&mut self, key: String, cx: &mut Context<Self>) { |
| 1503 | self.highlighted_message = Some(key.clone()); |
| 1504 | cx.notify(); |
| 1505 | cx.spawn(async move |this, cx| { |
| 1506 | cx.background_executor().timer(JUMP_HIGHLIGHT_PERIOD).await; |
| 1507 | this.update(cx, |this, cx| { |
| 1508 | if this.highlighted_message.as_deref() == Some(key.as_str()) { |
| 1509 | this.highlighted_message = None; |
| 1510 | cx.notify(); |
| 1511 | } |
| 1512 | }) |
| 1513 | .ok(); |
| 1514 | }) |
| 1515 | .detach(); |
| 1516 | } |
| 1517 | |
| 1518 | pub fn set_composer_text(&mut self, text: &str, window: &mut Window, cx: &mut Context<Self>) { |
| 1519 | self.composer |
| 1520 | .update(cx, |composer, cx| composer.set_text(text, window, cx)); |
| 1521 | self.composer |
| 1522 | .update(cx, |composer, cx| composer.focus(window, cx)); |
| 1523 | } |
| 1524 | |
| 1525 | pub fn focus_composer(&mut self, window: &mut Window, cx: &mut Context<Self>) { |
| 1526 | self.composer |
| 1527 | .update(cx, |composer, cx| composer.focus(window, cx)); |
| 1528 | } |
| 1529 | |
| 1530 | pub fn reply_to_latest_from_others(&mut self, window: &mut Window, cx: &mut Context<Self>) { |
| 1531 | let key = self |
| 1532 | .rows |
| 1533 | .borrow() |
| 1534 | .iter() |
| 1535 | .rev() |
| 1536 | .flat_map(Row::messages) |
| 1537 | .find(|message| !message.own) |
| 1538 | .map(|message| message.key.clone()); |
| 1539 | if let Some(key) = key { |
| 1540 | self.begin_reply(&key, window, cx); |
| 1541 | } |
| 1542 | } |
| 1543 | |
| 1544 | pub fn reply_to_hovered(&mut self, window: &mut Window, cx: &mut Context<Self>) { |
| 1545 | if let Some(message_id) = self.hovered_message.clone() { |
| 1546 | self.begin_reply(&message_id, window, cx); |
| 1547 | } |
| 1548 | } |
| 1549 | |
| 1550 | fn reply_target(&self, message_id: &str, cx: &App) -> Option<(MessageRecord, ReplyPreview)> { |
| 1551 | let conversation_id = self.conversation_id()?; |
| 1552 | let record = self |
| 1553 | .app |
| 1554 | .read(cx) |
| 1555 | .store |
| 1556 | .messages_by_id(&conversation_id, &[message_id.to_owned()]) |
| 1557 | .ok() |
| 1558 | .and_then(|mut found| found.remove(message_id)) |
| 1559 | .filter(|record| !record.deleted)?; |
| 1560 | let preview = ReplyPreview { |
| 1561 | message_id: record.message_id.clone(), |
| 1562 | author: record |
| 1563 | .sender_name |
| 1564 | .clone() |
| 1565 | .unwrap_or_else(|| "Unknown".to_owned()), |
| 1566 | excerpt: reply_excerpt(&record), |
| 1567 | }; |
| 1568 | Some((record, preview)) |
| 1569 | } |
| 1570 | |
| 1571 | fn begin_reply(&mut self, message_id: &str, window: &mut Window, cx: &mut Context<Self>) { |
| 1572 | let Some((record, preview)) = self.reply_target(message_id, cx) else { |
| 1573 | return; |
| 1574 | }; |
| 1575 | if self.in_feed() { |
| 1576 | let root_id = record |
| 1577 | .reply_to_id |
| 1578 | .clone() |
| 1579 | .unwrap_or_else(|| record.message_id.clone()); |
| 1580 | self.open_reply_editor(root_id, Some(preview), window, cx); |
| 1581 | return; |
| 1582 | } |
| 1583 | self.composer.update(cx, |composer, cx| { |
| 1584 | if composer.is_editing() { |
| 1585 | composer.cancel_edit(window, cx); |
| 1586 | } |
| 1587 | composer.set_reply(Some(preview), cx); |
| 1588 | composer.focus(window, cx); |
| 1589 | }); |
| 1590 | } |
| 1591 | |
| 1592 | fn channel_read_until(&self, selection: &Selection, cx: &App) -> Option<DateTime<Utc>> { |
| 1593 | let Selection::Channel(channel_id) = selection else { |
| 1594 | return None; |
| 1595 | }; |
| 1596 | let state = self.app.read(cx); |
| 1597 | if state.mode.demo { |
| 1598 | return crate::demo::channel_read_until(channel_id); |
| 1599 | } |
| 1600 | state.engine.as_ref()?.channel_read_until(channel_id) |
| 1601 | } |
| 1602 | |
| 1603 | fn first_unread_id(&self, selection: &Selection, cx: &App) -> Option<String> { |
| 1604 | let Selection::Chat(chat_id) = selection else { |
| 1605 | return None; |
| 1606 | }; |
| 1607 | let state = self.app.read(cx); |
| 1608 | if state.mode.demo { |
| 1609 | return crate::demo::first_unread(chat_id); |
| 1610 | } |
| 1611 | state.engine.as_ref()?.first_unread_message_id(chat_id) |
| 1612 | } |
| 1613 | |
| 1614 | fn row_context(&self, cx: &App) -> RowContext { |
| 1615 | let now = Local::now(); |
| 1616 | let app = self.app.read(cx); |
| 1617 | let names = local_names(app); |
| 1618 | let card_overrides = self |
| 1619 | .current |
| 1620 | .as_ref() |
| 1621 | .map(|current| app.cards.overrides_for(current.selection.conversation_id())) |
| 1622 | .unwrap_or_default(); |
| 1623 | let translation = self |
| 1624 | .current |
| 1625 | .as_ref() |
| 1626 | .map(|current| { |
| 1627 | app.translation |
| 1628 | .context_for(current.selection.conversation_id()) |
| 1629 | }) |
| 1630 | .unwrap_or_default(); |
| 1631 | RowContext { |
| 1632 | offset: app_base::format::Zone::Local, |
| 1633 | today: now.date_naive(), |
| 1634 | my_user_id: app.store.meta(META_USER_ID).ok().flatten(), |
| 1635 | names, |
| 1636 | pending_reactions: self.pending_reactions.clone(), |
| 1637 | card_overrides, |
| 1638 | translation, |
| 1639 | skin_tone: teams_core::skin_tone(&app.store), |
| 1640 | meeting: self.meeting_info(app), |
| 1641 | } |
| 1642 | } |
| 1643 | |
| 1644 | fn unknown_reactor_ids(&self) -> Vec<String> { |
| 1645 | self.rows |
| 1646 | .borrow() |
| 1647 | .iter() |
| 1648 | .flat_map(Row::messages) |
| 1649 | .flat_map(|message| message.reactions.iter()) |
| 1650 | .flat_map(|chip| chip.reactors.iter()) |
| 1651 | .filter(|reactor| reactor.name == UNKNOWN_REACTOR) |
| 1652 | .filter_map(|reactor| reactor.user_id.clone()) |
| 1653 | .collect() |
| 1654 | } |
| 1655 | |
| 1656 | fn refresh_reactor_names(&mut self, cx: &mut Context<Self>) { |
| 1657 | if self.current.is_none() { |
| 1658 | return; |
| 1659 | } |
| 1660 | let unknown = self.unknown_reactor_ids(); |
| 1661 | if unknown.is_empty() { |
| 1662 | return; |
| 1663 | } |
| 1664 | let names = resolve_names(self.app.read(cx), &unknown); |
| 1665 | if !names.is_empty() { |
| 1666 | self.rebuild(false, cx); |
| 1667 | } |
| 1668 | } |
| 1669 | |
| 1670 | fn drop_stale_reaction_details(&mut self) { |
| 1671 | let Some(details) = self.reaction_details.as_ref() else { |
| 1672 | return; |
| 1673 | }; |
| 1674 | let present = self |
| 1675 | .rows |
| 1676 | .borrow() |
| 1677 | .iter() |
| 1678 | .flat_map(Row::messages) |
| 1679 | .any(|message| { |
| 1680 | message.key == details.message_key |
| 1681 | && message |
| 1682 | .reactions |
| 1683 | .iter() |
| 1684 | .any(|chip| chip.glyph() == details.anchor_glyph) |
| 1685 | }); |
| 1686 | if !present { |
| 1687 | self.reaction_details = None; |
| 1688 | } |
| 1689 | } |
| 1690 | |
| 1691 | fn open_reaction_details(&mut self, message_key: &str, glyph: &str, cx: &mut Context<Self>) { |
| 1692 | let multiple_kinds = self |
| 1693 | .rows |
| 1694 | .borrow() |
| 1695 | .iter() |
| 1696 | .flat_map(Row::messages) |
| 1697 | .any(|message| message.key == message_key && message.reactions.len() > 1); |
| 1698 | self.reaction_details = Some(ReactionDetails { |
| 1699 | message_key: message_key.to_owned(), |
| 1700 | anchor_glyph: glyph.to_owned(), |
| 1701 | tab: multiple_kinds.then(|| glyph.to_owned()), |
| 1702 | }); |
| 1703 | cx.notify(); |
| 1704 | } |
| 1705 | |
| 1706 | fn select_reaction_tab(&mut self, tab: Option<String>, cx: &mut Context<Self>) { |
| 1707 | if let Some(details) = self.reaction_details.as_mut() { |
| 1708 | details.tab = tab; |
| 1709 | cx.notify(); |
| 1710 | } |
| 1711 | } |
| 1712 | |
| 1713 | fn close_reaction_details(&mut self, cx: &mut Context<Self>) { |
| 1714 | if self.reaction_details.take().is_some() { |
| 1715 | cx.notify(); |
| 1716 | } |
| 1717 | } |
| 1718 | |
| 1719 | fn reaction_controls(&self, key: &str, view: WeakEntity<Self>) -> ReactionControls { |
| 1720 | let open = { |
| 1721 | let (view, key) = (view.clone(), key.to_owned()); |
| 1722 | Rc::new(move |glyph: &str, cx: &mut App| { |
| 1723 | view.update(cx, |this, cx| this.open_reaction_details(&key, glyph, cx)) |
| 1724 | .ok(); |
| 1725 | }) |
| 1726 | }; |
| 1727 | let select_tab = { |
| 1728 | let view = view.clone(); |
| 1729 | Rc::new(move |tab: Option<String>, cx: &mut App| { |
| 1730 | view.update(cx, |this, cx| this.select_reaction_tab(tab, cx)) |
| 1731 | .ok(); |
| 1732 | }) |
| 1733 | }; |
| 1734 | let close = Rc::new(move |cx: &mut App| { |
| 1735 | view.update(cx, |this, cx| this.close_reaction_details(cx)) |
| 1736 | .ok(); |
| 1737 | }); |
| 1738 | let popover = self |
| 1739 | .reaction_details |
| 1740 | .as_ref() |
| 1741 | .filter(|details| details.message_key == key) |
| 1742 | .map(|details| ReactionPopover { |
| 1743 | anchor_glyph: details.anchor_glyph.clone(), |
| 1744 | tab: details.tab.clone(), |
| 1745 | }); |
| 1746 | ReactionControls { |
| 1747 | open, |
| 1748 | select_tab, |
| 1749 | close, |
| 1750 | popover, |
| 1751 | } |
| 1752 | } |
| 1753 | |
| 1754 | fn start_info(&self, cx: &App) -> Option<StartInfo> { |
| 1755 | let current = self.current.as_ref()?; |
| 1756 | let app = self.app.read(cx); |
| 1757 | let (user_id, member_name, notes) = match ¤t.selection { |
| 1758 | Selection::Chat(chat_id) |
| 1759 | if app.sidebar.chats.iter().any(|chat| { |
| 1760 | &chat.id == chat_id && chat.kind.eq_ignore_ascii_case("meeting") |
| 1761 | }) => |
| 1762 | { |
| 1763 | return None; |
| 1764 | } |
| 1765 | Selection::Chat(chat_id) => { |
| 1766 | let member = app |
| 1767 | .sidebar |
| 1768 | .chats |
| 1769 | .iter() |
| 1770 | .find(|chat| &chat.id == chat_id) |
| 1771 | .filter(|chat| is_one_on_one(chat)) |
| 1772 | .and_then(|chat| others(chat, app.directory.me.as_ref()).into_iter().next()); |
| 1773 | let (user_id, name) = member.unzip(); |
| 1774 | (user_id.flatten(), name, is_notes_chat(chat_id)) |
| 1775 | } |
| 1776 | Selection::Channel(_) => (None, None, false), |
| 1777 | }; |
| 1778 | Some(StartInfo { |
| 1779 | title: current.title.clone(), |
| 1780 | user_id, |
| 1781 | avatar_name: member_name |
| 1782 | .filter(|name| !name.is_empty()) |
| 1783 | .unwrap_or_else(|| current.title.clone()), |
| 1784 | notes, |
| 1785 | }) |
| 1786 | } |
| 1787 | |
| 1788 | fn notes_space(&self) -> Option<StartInfo> { |
| 1789 | match self.rows.borrow().as_slice() { |
| 1790 | [Row::Start(start)] if start.notes => Some(start.clone()), |
| 1791 | _ => None, |
| 1792 | } |
| 1793 | } |
| 1794 | |
| 1795 | fn watch_feed_scroll(&mut self, cx: &mut Context<Self>) { |
| 1796 | let scroller_id = self.scroller.entity_id(); |
| 1797 | if self |
| 1798 | .scroller_watch |
| 1799 | .as_ref() |
| 1800 | .is_some_and(|(watched, _)| *watched == scroller_id) |
| 1801 | { |
| 1802 | return; |
| 1803 | } |
| 1804 | let subscription = cx.observe(&self.scroller, |this, _, cx| { |
| 1805 | if this.in_feed() { |
| 1806 | cx.notify(); |
| 1807 | } |
| 1808 | }); |
| 1809 | self.scroller_watch = Some((scroller_id, subscription)); |
| 1810 | } |
| 1811 | |
| 1812 | fn rebuild(&mut self, reset: bool, cx: &mut Context<Self>) { |
| 1813 | let started = std::time::Instant::now(); |
| 1814 | self.rebuild_generation += 1; |
| 1815 | match self.rebuild_plan(reset, cx) { |
| 1816 | Some(plan) => { |
| 1817 | let loaded = load_records(&self.app.read(cx).store, &plan); |
| 1818 | let context = self.complete_context(&loaded.records, cx); |
| 1819 | let built = build_rows(&plan, &loaded, &context); |
| 1820 | self.finish_rebuild(plan, loaded, context, built, reset, cx); |
| 1821 | } |
| 1822 | None => self.rebuild_without_conversation(cx), |
| 1823 | } |
| 1824 | app_base::diagnostics::record_if_slow("conversation rebuild", started.elapsed()); |
| 1825 | } |
| 1826 | |
| 1827 | fn rebuild_in_background(&mut self, cx: &mut Context<Self>) { |
| 1828 | let Some(plan) = self.rebuild_plan(false, cx) else { |
| 1829 | return self.rebuild(false, cx); |
| 1830 | }; |
| 1831 | self.rebuild_generation += 1; |
| 1832 | let generation = self.rebuild_generation; |
| 1833 | let store = self.app.read(cx).store.clone(); |
| 1834 | cx.spawn(async move |this, cx| { |
| 1835 | let (plan, loaded) = cx |
| 1836 | .background_spawn(async move { |
| 1837 | let loaded = load_records(&store, &plan); |
| 1838 | (plan, loaded) |
| 1839 | }) |
| 1840 | .await; |
| 1841 | let Ok(Some(context)) = this.update(cx, |view, cx| { |
| 1842 | view.rebuild_still_current(generation, &plan) |
| 1843 | .then(|| view.complete_context(&loaded.records, cx)) |
| 1844 | }) else { |
| 1845 | return; |
| 1846 | }; |
| 1847 | let (plan, loaded, context, built) = cx |
| 1848 | .background_spawn(async move { |
| 1849 | let built = build_rows(&plan, &loaded, &context); |
| 1850 | (plan, loaded, context, built) |
| 1851 | }) |
| 1852 | .await; |
| 1853 | this.update(cx, |view, cx| { |
| 1854 | if view.rebuild_still_current(generation, &plan) { |
| 1855 | let started = std::time::Instant::now(); |
| 1856 | view.finish_rebuild(plan, loaded, context, built, false, cx); |
| 1857 | app_base::diagnostics::record_if_slow( |
| 1858 | "conversation rebuild apply", |
| 1859 | started.elapsed(), |
| 1860 | ); |
| 1861 | } |
| 1862 | }) |
| 1863 | .ok(); |
| 1864 | }) |
| 1865 | .detach(); |
| 1866 | } |
| 1867 | |
| 1868 | fn rebuild_still_current(&self, generation: u64, plan: &RebuildPlan) -> bool { |
| 1869 | generation == self.rebuild_generation |
| 1870 | && self.current.as_ref().is_some_and(|current| { |
| 1871 | current.selection.conversation_id() == plan.conversation_id |
| 1872 | && current.mode == plan.mode |
| 1873 | }) |
| 1874 | } |
| 1875 | |
| 1876 | fn rebuild_without_conversation(&mut self, cx: &mut Context<Self>) { |
| 1877 | let mut rows: Vec<Row> = self |
| 1878 | .pending |
| 1879 | .iter() |
| 1880 | .cloned() |
| 1881 | .map(|row| Row::Message(Box::new(row))) |
| 1882 | .collect(); |
| 1883 | assign_series(&mut rows); |
| 1884 | self.apply_rows(rows, true, cx); |
| 1885 | } |
| 1886 | |
| 1887 | fn rebuild_plan(&self, reset: bool, cx: &App) -> Option<RebuildPlan> { |
| 1888 | let current = self.current.as_ref()?; |
| 1889 | let limit = if reset { |
| 1890 | current.loaded_limit |
| 1891 | } else { |
| 1892 | current |
| 1893 | .loaded_limit |
| 1894 | .max(current.loaded_count + GROWTH_HEADROOM) |
| 1895 | }; |
| 1896 | let thread_root = match ¤t.mode { |
| 1897 | ViewMode::Thread(root_id) => Some(root_id.clone()), |
| 1898 | ViewMode::Flat | ViewMode::ThreadList => None, |
| 1899 | }; |
| 1900 | Some(RebuildPlan { |
| 1901 | conversation_id: current.selection.conversation_id().to_owned(), |
| 1902 | is_chat: matches!(current.selection, Selection::Chat(_)), |
| 1903 | mode: current.mode.clone(), |
| 1904 | limit, |
| 1905 | loading_post: thread_root.is_some() && thread_root == self.loading_post, |
| 1906 | thread_root, |
| 1907 | window: current.detached.as_ref().map(|detached| { |
| 1908 | ( |
| 1909 | detached.oldest, |
| 1910 | detached.newest, |
| 1911 | detached.older_cursor.is_some(), |
| 1912 | detached.newer_cursor.is_some(), |
| 1913 | ) |
| 1914 | }), |
| 1915 | syncing_messages: current.sync.messages, |
| 1916 | older_blocked: current.older_blocked, |
| 1917 | can_load_older: self.app.read(cx).engine.is_some(), |
| 1918 | start: self.start_info(cx), |
| 1919 | }) |
| 1920 | } |
| 1921 | |
| 1922 | fn complete_context(&self, records: &[MessageRecord], cx: &App) -> RowContext { |
| 1923 | let mut context = self.row_context(cx); |
| 1924 | let unnamed_ids: Vec<String> = records |
| 1925 | .iter() |
| 1926 | .flat_map(teams_core::reactions) |
| 1927 | .filter(|reaction| reaction.user_name.is_none()) |
| 1928 | .filter_map(|reaction| reaction.user_id) |
| 1929 | .chain(forward_sender_ids(records)) |
| 1930 | .filter(|user_id| !context.names.contains_key(user_id)) |
| 1931 | .collect(); |
| 1932 | for (user_id, name) in resolve_names(self.app.read(cx), &unnamed_ids) { |
| 1933 | context.names.entry(user_id).or_insert(name); |
| 1934 | } |
| 1935 | for record in records { |
| 1936 | if let (Some(user_id), Some(name)) = (&record.sender_id, &record.sender_name) { |
| 1937 | context |
| 1938 | .names |
| 1939 | .entry(user_id.clone()) |
| 1940 | .or_insert_with(|| name.clone()); |
| 1941 | } |
| 1942 | } |
| 1943 | context |
| 1944 | } |
| 1945 | |
| 1946 | fn finish_rebuild( |
| 1947 | &mut self, |
| 1948 | plan: RebuildPlan, |
| 1949 | loaded: LoadedRecords, |
| 1950 | context: RowContext, |
| 1951 | built: BuiltRows, |
| 1952 | reset: bool, |
| 1953 | cx: &mut Context<Self>, |
| 1954 | ) { |
| 1955 | let records = loaded.records; |
| 1956 | let current = self.current.as_mut().expect("checked by the plan"); |
| 1957 | let first_unread = current.first_unread.clone(); |
| 1958 | let posts_new_after = current.posts_new_after; |
| 1959 | current.loaded_limit = plan.limit; |
| 1960 | if plan.thread_root.is_none() { |
| 1961 | current.loaded_count = records.len(); |
| 1962 | } |
| 1963 | current.has_older = built.has_older; |
| 1964 | let in_feed = current.mode == ViewMode::ThreadList; |
| 1965 | let appear_scope = format!("{}/{:?}", plan.conversation_id, current.mode); |
| 1966 | let appear_live = |
| 1967 | !built.placeholders && !in_feed && plan.window.is_none() && !current.sync.messages; |
| 1968 | let mut rows = built.rows; |
| 1969 | let local: Vec<MessageRow> = if plan.window.is_some() { |
| 1970 | Vec::new() |
| 1971 | } else { |
| 1972 | self.pending |
| 1973 | .iter() |
| 1974 | .cloned() |
| 1975 | .chain(self.scheduled_rows(context.my_user_id.as_deref(), cx)) |
| 1976 | .collect() |
| 1977 | }; |
| 1978 | if in_feed { |
| 1979 | place_local_rows(&mut rows, local); |
| 1980 | } else { |
| 1981 | rows.extend(local.into_iter().map(|row| Row::Message(Box::new(row)))); |
| 1982 | } |
| 1983 | let (mut receipts, receipt_labels) = self.receipts(&records, cx); |
| 1984 | if self.track_receipt_fades(&mut receipts) { |
| 1985 | self.schedule_fade_end(cx); |
| 1986 | } |
| 1987 | for row in rows.iter_mut() { |
| 1988 | if let Row::Message(message) = row |
| 1989 | && message.own |
| 1990 | { |
| 1991 | message.receipt = receipts.get(&message.key).copied().unwrap_or_default(); |
| 1992 | message.receipt_label = receipt_labels.get(&message.key).cloned(); |
| 1993 | if let Some((_, previews)) = self |
| 1994 | .sent_previews |
| 1995 | .iter() |
| 1996 | .find(|(key, _)| key == &message.key) |
| 1997 | { |
| 1998 | message.sent_previews = previews.clone(); |
| 1999 | } |
| 2000 | } |
| 2001 | } |
| 2002 | if let Some(read_until) = posts_new_after.filter(|_| in_feed) { |
| 2003 | for row in rows.iter_mut() { |
| 2004 | if let Row::Post(post) = row { |
| 2005 | post.mark_new_after(read_until); |
| 2006 | } |
| 2007 | } |
| 2008 | } |
| 2009 | if let Some(unread_id) = &first_unread { |
| 2010 | for row in rows.iter_mut() { |
| 2011 | if let Row::Message(message) = row |
| 2012 | && &message.key == unread_id |
| 2013 | { |
| 2014 | message.new_marker = true; |
| 2015 | } |
| 2016 | } |
| 2017 | } |
| 2018 | assign_series(&mut rows); |
| 2019 | self.track_appearing(&rows, &appear_scope, appear_live, reset); |
| 2020 | self.apply_rows(rows, reset, cx); |
| 2021 | if self.is_detached() { |
| 2022 | self.scroller |
| 2023 | .update(cx, |scroller, _| scroller.stop_following()); |
| 2024 | } |
| 2025 | } |
| 2026 | |
| 2027 | fn apply_rows(&mut self, mut new_rows: Vec<Row>, reset: bool, cx: &mut Context<Self>) { |
| 2028 | for row in &mut new_rows { |
| 2029 | if let Row::Message(message) = row |
| 2030 | && message.delivery == Delivery::Delivered |
| 2031 | && self.saving_edits.contains(&message.key) |
| 2032 | { |
| 2033 | message.delivery = Delivery::Sending; |
| 2034 | } |
| 2035 | } |
| 2036 | let old_rows = std::mem::replace(&mut *self.rows.borrow_mut(), new_rows); |
| 2037 | self.drop_stale_reaction_details(); |
| 2038 | let new_len = self.rows.borrow().len(); |
| 2039 | let in_feed = self.in_feed(); |
| 2040 | let older_anchor = self.older_anchor.take().and_then(|key| { |
| 2041 | let top_was_loader = matches!(old_rows.first(), Some(Row::LoadOlder)); |
| 2042 | let index = self.rows.borrow().iter().position(|row| row.key() == key)?; |
| 2043 | (top_was_loader && !in_feed).then_some(index) |
| 2044 | }); |
| 2045 | if reset { |
| 2046 | let marker_index = self |
| 2047 | .rows |
| 2048 | .borrow() |
| 2049 | .iter() |
| 2050 | .position(|row| matches!(row, Row::Message(message) if message.new_marker)); |
| 2051 | self.feed_at_top = true; |
| 2052 | self.scroller.update(cx, |scroller, cx| { |
| 2053 | scroller.reset(new_len, cx); |
| 2054 | if in_feed { |
| 2055 | scroller.scroll_to_item(0, cx); |
| 2056 | return; |
| 2057 | } |
| 2058 | match marker_index { |
| 2059 | Some(index) if !scroller.scroll_to_item(index, cx) => { |
| 2060 | scroller.scroll_to_end(cx) |
| 2061 | } |
| 2062 | Some(_) => {} |
| 2063 | None => scroller.scroll_to_end(cx), |
| 2064 | } |
| 2065 | }); |
| 2066 | cx.notify(); |
| 2067 | return; |
| 2068 | } |
| 2069 | let (splice, changed) = { |
| 2070 | let new_rows = self.rows.borrow(); |
| 2071 | let old_keys: Vec<String> = old_rows.iter().map(|row| row.key().to_owned()).collect(); |
| 2072 | let new_keys: Vec<String> = new_rows.iter().map(|row| row.key().to_owned()).collect(); |
| 2073 | ( |
| 2074 | diff_keys(&old_keys, &new_keys), |
| 2075 | changed_indices(&old_rows, &new_rows), |
| 2076 | ) |
| 2077 | }; |
| 2078 | let top_changed = splice |
| 2079 | .as_ref() |
| 2080 | .is_some_and(|splice| splice.range.start == 0); |
| 2081 | let keep_top = in_feed && (old_rows.is_empty() || (self.feed_at_top && top_changed)); |
| 2082 | self.scroller.update(cx, |scroller, cx| { |
| 2083 | if let Some(splice) = splice |
| 2084 | && !scroller.splice(splice.range, splice.count, cx) |
| 2085 | { |
| 2086 | scroller.reset(new_len, cx); |
| 2087 | } |
| 2088 | if keep_top { |
| 2089 | scroller.scroll_to_item(0, cx); |
| 2090 | } else if let Some(index) = older_anchor { |
| 2091 | scroller.scroll_to_item(index, cx); |
| 2092 | } |
| 2093 | for index in changed { |
| 2094 | scroller.remeasure_items(index..index + 1, cx); |
| 2095 | } |
| 2096 | }); |
| 2097 | cx.notify(); |
| 2098 | } |
| 2099 | |
| 2100 | fn start_fetch(&mut self, cx: &mut Context<Self>) { |
| 2101 | let state = self.app.read(cx); |
| 2102 | let (engine, demo_sync) = (state.engine.clone(), state.mode.demo_sync); |
| 2103 | if engine.is_none() && demo_sync.is_none() { |
| 2104 | return; |
| 2105 | } |
| 2106 | let Some(current) = self.current.as_mut() else { |
| 2107 | return; |
| 2108 | }; |
| 2109 | if current.fetched { |
| 2110 | return; |
| 2111 | } |
| 2112 | current.fetched = true; |
| 2113 | let is_chat = matches!(current.selection, Selection::Chat(_)); |
| 2114 | let newest_cached = self |
| 2115 | .rows |
| 2116 | .borrow() |
| 2117 | .iter() |
| 2118 | .flat_map(Row::messages) |
| 2119 | .filter(|message| !message.key.starts_with(PENDING_KEY_PREFIX)) |
| 2120 | .map(|message| message.created_at) |
| 2121 | .max(); |
| 2122 | let current = self.current.as_mut().expect("checked above"); |
| 2123 | if current.sync.failed { |
| 2124 | self.notice = None; |
| 2125 | } |
| 2126 | current.sync = SyncProgress { |
| 2127 | messages: true, |
| 2128 | receipts: is_chat, |
| 2129 | empty_cache: newest_cached.is_none(), |
| 2130 | newest_cached, |
| 2131 | started_at: Utc::now(), |
| 2132 | ..SyncProgress::default() |
| 2133 | }; |
| 2134 | self.sync_generation += 1; |
| 2135 | let generation = self.sync_generation; |
| 2136 | let conversation_id = current.selection.conversation_id().to_owned(); |
| 2137 | match (engine, demo_sync) { |
| 2138 | (Some(engine), _) => { |
| 2139 | let receiver = |
| 2140 | runtime::spawn(async move { engine.fetch_newer(&conversation_id).await }); |
| 2141 | cx.spawn(async move |this, cx| { |
| 2142 | let result = receiver.await; |
| 2143 | this.update(cx, |this, cx| this.finish_fetch(generation, result, cx)) |
| 2144 | .ok(); |
| 2145 | }) |
| 2146 | .detach(); |
| 2147 | } |
| 2148 | (None, Some(delay)) => self.simulate_sync(generation, delay, cx), |
| 2149 | (None, None) => {} |
| 2150 | } |
| 2151 | self.schedule_progress(generation, cx); |
| 2152 | self.rebuild(false, cx); |
| 2153 | self.mark_read(ReadTrigger::Incoming, cx); |
| 2154 | self.refresh_receipts(generation, cx); |
| 2155 | } |
| 2156 | |
| 2157 | fn simulate_sync(&mut self, generation: u64, delay: Duration, cx: &mut Context<Self>) { |
| 2158 | cx.spawn(async move |this, cx| { |
| 2159 | cx.background_executor().timer(delay).await; |
| 2160 | this.update(cx, |this, cx| { |
| 2161 | if let Some(sync) = this.sync_of(generation) { |
| 2162 | sync.receipts = false; |
| 2163 | } |
| 2164 | this.finish_fetch(generation, Ok(Ok(teams_core::Delta::default())), cx) |
| 2165 | }) |
| 2166 | .ok(); |
| 2167 | }) |
| 2168 | .detach(); |
| 2169 | } |
| 2170 | |
| 2171 | fn sync_of(&mut self, generation: u64) -> Option<&mut SyncProgress> { |
| 2172 | if generation != self.sync_generation { |
| 2173 | return None; |
| 2174 | } |
| 2175 | self.current.as_mut().map(|current| &mut current.sync) |
| 2176 | } |
| 2177 | |
| 2178 | fn schedule_progress(&mut self, generation: u64, cx: &mut Context<Self>) { |
| 2179 | cx.spawn(async move |this, cx| { |
| 2180 | let stages = [ |
| 2181 | (PROGRESS_DELAY, Duration::ZERO), |
| 2182 | (SLOW_AFTER, PROGRESS_DELAY), |
| 2183 | ]; |
| 2184 | for (stage, (at, previous)) in stages.into_iter().enumerate() { |
| 2185 | cx.background_executor().timer(at - previous).await; |
| 2186 | let running = this |
| 2187 | .update(cx, |this, cx| this.advance_progress(generation, stage, cx)) |
| 2188 | .unwrap_or(false); |
| 2189 | if !running { |
| 2190 | return; |
| 2191 | } |
| 2192 | } |
| 2193 | }) |
| 2194 | .detach(); |
| 2195 | } |
| 2196 | |
| 2197 | fn advance_progress(&mut self, generation: u64, stage: usize, cx: &mut Context<Self>) -> bool { |
| 2198 | let Some(sync) = self.sync_of(generation) else { |
| 2199 | return false; |
| 2200 | }; |
| 2201 | if !sync.running() || !sync.empty_cache { |
| 2202 | return false; |
| 2203 | } |
| 2204 | match stage { |
| 2205 | 0 => sync.shown_at = Some(Instant::now()), |
| 2206 | _ => sync.slow = true, |
| 2207 | } |
| 2208 | cx.notify(); |
| 2209 | true |
| 2210 | } |
| 2211 | |
| 2212 | fn finish_fetch( |
| 2213 | &mut self, |
| 2214 | generation: u64, |
| 2215 | result: Result< |
| 2216 | teams_core::Result<teams_core::Delta>, |
| 2217 | tokio::sync::oneshot::error::RecvError, |
| 2218 | >, |
| 2219 | cx: &mut Context<Self>, |
| 2220 | ) { |
| 2221 | let Some(sync) = self.sync_of(generation) else { |
| 2222 | return; |
| 2223 | }; |
| 2224 | sync.messages = false; |
| 2225 | let (newest_cached, started_at) = (sync.newest_cached, sync.started_at); |
| 2226 | match result { |
| 2227 | Ok(Ok(_)) => { |
| 2228 | if let Some(current) = self.current.as_mut() { |
| 2229 | current.quiet_retries = 0; |
| 2230 | } |
| 2231 | self.mark_fetched_new(newest_cached, started_at, cx) |
| 2232 | } |
| 2233 | Ok(Err(error)) if Self::denies_access(&error) => self.fail_access(generation), |
| 2234 | Ok(Err(error)) => self.fail_sync(generation, short_error(&error), cx), |
| 2235 | Err(error) => self.fail_sync(generation, short_error(&error), cx), |
| 2236 | } |
| 2237 | self.rebuild(false, cx); |
| 2238 | self.settle_progress(generation, cx); |
| 2239 | } |
| 2240 | |
| 2241 | fn fail_sync(&mut self, generation: u64, error: String, cx: &mut Context<Self>) { |
| 2242 | let quiet_retry = self.current.as_mut().and_then(|current| { |
| 2243 | let delay = QUIET_RETRY_DELAYS.get(current.quiet_retries).copied()?; |
| 2244 | current.quiet_retries += 1; |
| 2245 | Some(delay) |
| 2246 | }); |
| 2247 | if let Some(delay) = quiet_retry { |
| 2248 | cx.spawn(async move |this, cx| { |
| 2249 | cx.background_executor().timer(delay).await; |
| 2250 | this.update(cx, |this, cx| { |
| 2251 | if this.sync_generation == generation { |
| 2252 | this.retry_fetch(cx); |
| 2253 | } |
| 2254 | }) |
| 2255 | .ok(); |
| 2256 | }) |
| 2257 | .detach(); |
| 2258 | return; |
| 2259 | } |
| 2260 | if let Some(sync) = self.sync_of(generation) { |
| 2261 | sync.failed = true; |
| 2262 | sync.receipts = false; |
| 2263 | } |
| 2264 | self.notice = Some(format!("Could not refresh: {error}")); |
| 2265 | } |
| 2266 | |
| 2267 | fn denies_access(error: &teams_core::Error) -> bool { |
| 2268 | matches!(error, teams_core::Error::Graph(graph::Error::NotAMember)) |
| 2269 | } |
| 2270 | |
| 2271 | fn fail_access(&mut self, generation: u64) { |
| 2272 | if let Some(sync) = self.sync_of(generation) { |
| 2273 | sync.failed = true; |
| 2274 | sync.receipts = false; |
| 2275 | } |
| 2276 | self.notice = Some("No access to this chat".to_owned()); |
| 2277 | } |
| 2278 | |
| 2279 | fn mark_fetched_new( |
| 2280 | &mut self, |
| 2281 | newest_cached: Option<DateTime<Utc>>, |
| 2282 | started_at: DateTime<Utc>, |
| 2283 | cx: &mut Context<Self>, |
| 2284 | ) { |
| 2285 | let Some(newest_cached) = newest_cached else { |
| 2286 | return; |
| 2287 | }; |
| 2288 | let Some(my_user_id) = self.row_context(cx).my_user_id else { |
| 2289 | return; |
| 2290 | }; |
| 2291 | let store = self.app.read(cx).store.clone(); |
| 2292 | let Some(current) = self.current.as_mut() else { |
| 2293 | return; |
| 2294 | }; |
| 2295 | if current.first_unread.is_some() || current.mode != ViewMode::Flat { |
| 2296 | return; |
| 2297 | } |
| 2298 | let limit = current |
| 2299 | .loaded_limit |
| 2300 | .max(current.loaded_count + GROWTH_HEADROOM); |
| 2301 | current.first_unread = store |
| 2302 | .messages(current.selection.conversation_id(), None, limit) |
| 2303 | .unwrap_or_default() |
| 2304 | .into_iter() |
| 2305 | .find(|record| { |
| 2306 | missed_before_fetch(record.created_at, newest_cached, started_at) |
| 2307 | && record.sender_id.as_ref() != Some(&my_user_id) |
| 2308 | }) |
| 2309 | .map(|record| record.message_id); |
| 2310 | } |
| 2311 | |
| 2312 | fn settle_progress(&mut self, generation: u64, cx: &mut Context<Self>) { |
| 2313 | let Some(sync) = self.sync_of(generation) else { |
| 2314 | return; |
| 2315 | }; |
| 2316 | if sync.running() { |
| 2317 | return; |
| 2318 | } |
| 2319 | let remaining = sync |
| 2320 | .shown_at |
| 2321 | .map(|shown_at| PROGRESS_MIN_VISIBLE.saturating_sub(shown_at.elapsed())) |
| 2322 | .unwrap_or_default(); |
| 2323 | if remaining.is_zero() { |
| 2324 | sync.shown_at = None; |
| 2325 | sync.slow = false; |
| 2326 | cx.notify(); |
| 2327 | return; |
| 2328 | } |
| 2329 | cx.spawn(async move |this, cx| { |
| 2330 | cx.background_executor().timer(remaining).await; |
| 2331 | this.update(cx, |this, cx| { |
| 2332 | if let Some(sync) = this.sync_of(generation) { |
| 2333 | sync.shown_at = None; |
| 2334 | sync.slow = false; |
| 2335 | cx.notify(); |
| 2336 | } |
| 2337 | }) |
| 2338 | .ok(); |
| 2339 | }) |
| 2340 | .detach(); |
| 2341 | } |
| 2342 | |
| 2343 | fn retry_fetch(&mut self, cx: &mut Context<Self>) { |
| 2344 | if let Some(current) = self.current.as_mut() { |
| 2345 | current.fetched = false; |
| 2346 | } |
| 2347 | self.start_fetch(cx); |
| 2348 | } |
| 2349 | |
| 2350 | fn receipts( |
| 2351 | &self, |
| 2352 | records: &[MessageRecord], |
| 2353 | cx: &App, |
| 2354 | ) -> (HashMap<String, Receipt>, HashMap<String, String>) { |
| 2355 | let settings = self.app.read(cx).account_chat_settings(); |
| 2356 | let (mut receipts, mut labels) = self.raw_receipts(records, cx); |
| 2357 | for receipt in receipts.values_mut() { |
| 2358 | *receipt = shown_receipt(&settings, *receipt); |
| 2359 | } |
| 2360 | labels.retain(|key, _| { |
| 2361 | receipts |
| 2362 | .get(key) |
| 2363 | .is_some_and(|receipt| *receipt != Receipt::Hidden) |
| 2364 | }); |
| 2365 | (receipts, labels) |
| 2366 | } |
| 2367 | |
| 2368 | fn raw_receipts( |
| 2369 | &self, |
| 2370 | records: &[MessageRecord], |
| 2371 | cx: &App, |
| 2372 | ) -> (HashMap<String, Receipt>, HashMap<String, String>) { |
| 2373 | let state = self.app.read(cx); |
| 2374 | if state.mode.demo { |
| 2375 | let read_ids = crate::demo::read_message_ids(records); |
| 2376 | let receipts: HashMap<String, Receipt> = records |
| 2377 | .iter() |
| 2378 | .map(|record| { |
| 2379 | let receipt = if read_ids.contains(&record.message_id) { |
| 2380 | Receipt::Read |
| 2381 | } else { |
| 2382 | Receipt::Sent |
| 2383 | }; |
| 2384 | (record.message_id.clone(), receipt) |
| 2385 | }) |
| 2386 | .collect(); |
| 2387 | let labels = receipts |
| 2388 | .iter() |
| 2389 | .map(|(key, receipt)| { |
| 2390 | let label = if *receipt == Receipt::Read { |
| 2391 | "Seen" |
| 2392 | } else { |
| 2393 | "Sent" |
| 2394 | }; |
| 2395 | (key.clone(), label.to_owned()) |
| 2396 | }) |
| 2397 | .collect(); |
| 2398 | return (receipts, labels); |
| 2399 | } |
| 2400 | let Some(engine) = state.engine.as_ref() else { |
| 2401 | return (HashMap::new(), HashMap::new()); |
| 2402 | }; |
| 2403 | let mut receipts = HashMap::new(); |
| 2404 | let mut labels = HashMap::new(); |
| 2405 | for record in records { |
| 2406 | let receipt_state = engine.receipt_state_for(record); |
| 2407 | if let Some(label) = crate::rows::receipt_label(&receipt_state) { |
| 2408 | labels.insert(record.message_id.clone(), label); |
| 2409 | } |
| 2410 | let receipt = match receipt_state { |
| 2411 | teams_core::ReceiptState::Read { .. } => Receipt::Read, |
| 2412 | teams_core::ReceiptState::Sent => Receipt::Sent, |
| 2413 | teams_core::ReceiptState::Unknown => Receipt::Hidden, |
| 2414 | }; |
| 2415 | receipts.insert(record.message_id.clone(), receipt); |
| 2416 | } |
| 2417 | (receipts, labels) |
| 2418 | } |
| 2419 | |
| 2420 | fn track_receipt_fades(&mut self, receipts: &mut HashMap<String, Receipt>) -> bool { |
| 2421 | let Some(current) = self.current.as_ref() else { |
| 2422 | return false; |
| 2423 | }; |
| 2424 | let settled = !current.sync.receipts; |
| 2425 | let conversation_id = current.selection.conversation_id().to_owned(); |
| 2426 | self.receipt_fade |
| 2427 | .apply(&conversation_id, settled, receipts, Instant::now()) |
| 2428 | } |
| 2429 | |
| 2430 | fn track_appearing(&mut self, rows: &[Row], scope: &str, live: bool, reset: bool) { |
| 2431 | let sending: HashSet<&str> = self.pending.iter().map(|row| row.key.as_str()).collect(); |
| 2432 | let candidates: Vec<appear_tracker::Candidate> = rows |
| 2433 | .iter() |
| 2434 | .filter_map(|row| match row { |
| 2435 | Row::Message(message) => Some(appear_tracker::Candidate { |
| 2436 | key: &message.key, |
| 2437 | own: message.own, |
| 2438 | origin: if sending.contains(message.key.as_str()) { |
| 2439 | appear_tracker::Origin::Sending |
| 2440 | } else if message.key.starts_with(SCHEDULED_KEY_PREFIX) { |
| 2441 | appear_tracker::Origin::Scheduled |
| 2442 | } else { |
| 2443 | appear_tracker::Origin::Server |
| 2444 | }, |
| 2445 | }), |
| 2446 | _ => None, |
| 2447 | }) |
| 2448 | .collect(); |
| 2449 | self.appear_tracker |
| 2450 | .apply(scope, live, reset, &candidates, Instant::now()); |
| 2451 | } |
| 2452 | |
| 2453 | fn schedule_fade_end(&mut self, cx: &mut Context<Self>) { |
| 2454 | cx.spawn(async move |this, cx| { |
| 2455 | cx.background_executor() |
| 2456 | .timer(receipt_fade::RECEIPT_FADE) |
| 2457 | .await; |
| 2458 | this.update(cx, |this, cx| this.rebuild(false, cx)).ok(); |
| 2459 | }) |
| 2460 | .detach(); |
| 2461 | } |
| 2462 | |
| 2463 | fn refresh_receipts(&mut self, generation: u64, cx: &mut Context<Self>) { |
| 2464 | let state = self.app.read(cx); |
| 2465 | let (Some(engine), Some(Selection::Chat(chat_id))) = ( |
| 2466 | state.engine.clone(), |
| 2467 | self.current.as_ref().map(|c| c.selection.clone()), |
| 2468 | ) else { |
| 2469 | return; |
| 2470 | }; |
| 2471 | let receiver = runtime::spawn(async move { engine.refresh_receipts(&chat_id).await }); |
| 2472 | cx.spawn(async move |this, cx| { |
| 2473 | receiver.await.ok(); |
| 2474 | this.update(cx, |this, cx| { |
| 2475 | let Some(sync) = this.sync_of(generation) else { |
| 2476 | return; |
| 2477 | }; |
| 2478 | sync.receipts = false; |
| 2479 | this.rebuild(false, cx); |
| 2480 | this.settle_progress(generation, cx); |
| 2481 | }) |
| 2482 | .ok(); |
| 2483 | }) |
| 2484 | .detach(); |
| 2485 | } |
| 2486 | |
| 2487 | fn mark_read(&mut self, trigger: ReadTrigger, cx: &mut Context<Self>) { |
| 2488 | if self.draft_active { |
| 2489 | return; |
| 2490 | } |
| 2491 | let state = self.app.read(cx); |
| 2492 | let Some(selection) = self.current.as_ref().map(|c| c.selection.clone()) else { |
| 2493 | return; |
| 2494 | }; |
| 2495 | if (state.engine.is_none() && !state.mode.demo) || state.mode.read_only { |
| 2496 | return; |
| 2497 | } |
| 2498 | let unread = match &selection { |
| 2499 | Selection::Chat(chat_id) => { |
| 2500 | state |
| 2501 | .sidebar |
| 2502 | .chats |
| 2503 | .iter() |
| 2504 | .any(|chat| &chat.id == chat_id && chat.unread) |
| 2505 | && !state.keeps_unread(chat_id) |
| 2506 | } |
| 2507 | Selection::Channel(channel_id) => channel_unread(&state.sidebar, channel_id), |
| 2508 | }; |
| 2509 | let Some(plan) = plan_read(self.window_active, unread, trigger) else { |
| 2510 | return; |
| 2511 | }; |
| 2512 | self.app.update(cx, |state, cx| match &selection { |
| 2513 | Selection::Chat(chat_id) => { |
| 2514 | state.ack_urgent_messages(chat_id, cx); |
| 2515 | state.mark_chat_read(chat_id, cx) |
| 2516 | } |
| 2517 | Selection::Channel(channel_id) => state.mark_channel_read(channel_id, cx), |
| 2518 | }); |
| 2519 | if plan.clear_divider |
| 2520 | && let Some(current) = self.current.as_mut() |
| 2521 | && current.first_unread.take().is_some() |
| 2522 | { |
| 2523 | self.rebuild(false, cx); |
| 2524 | } |
| 2525 | } |
| 2526 | |
| 2527 | fn request_older(&mut self, cx: &mut Context<Self>) { |
| 2528 | if self.is_detached() { |
| 2529 | return self.request_window_page(WindowDirection::Backward, cx); |
| 2530 | } |
| 2531 | let Some(engine) = self.app.read(cx).engine.clone() else { |
| 2532 | return; |
| 2533 | }; |
| 2534 | let Some(current) = self.current.as_mut() else { |
| 2535 | return; |
| 2536 | }; |
| 2537 | if current.loading_older || current.older_blocked || !current.has_older { |
| 2538 | return; |
| 2539 | } |
| 2540 | current.loading_older = true; |
| 2541 | let conversation_id = current.selection.conversation_id().to_owned(); |
| 2542 | let task_id = conversation_id.clone(); |
| 2543 | let receiver = runtime::spawn(async move { engine.load_older(&task_id).await }); |
| 2544 | cx.spawn(async move |this, cx| { |
| 2545 | let result = receiver.await; |
| 2546 | this.update(cx, |this, cx| { |
| 2547 | this.finish_older(&conversation_id, result, cx) |
| 2548 | }) |
| 2549 | .ok(); |
| 2550 | }) |
| 2551 | .detach(); |
| 2552 | } |
| 2553 | |
| 2554 | fn finish_older( |
| 2555 | &mut self, |
| 2556 | conversation_id: &str, |
| 2557 | result: Result< |
| 2558 | teams_core::Result<Vec<MessageRecord>>, |
| 2559 | tokio::sync::oneshot::error::RecvError, |
| 2560 | >, |
| 2561 | cx: &mut Context<Self>, |
| 2562 | ) { |
| 2563 | let Some(current) = self.current.as_mut() else { |
| 2564 | return; |
| 2565 | }; |
| 2566 | if current.selection.conversation_id() != conversation_id { |
| 2567 | return; |
| 2568 | } |
| 2569 | current.loading_older = false; |
| 2570 | match result { |
| 2571 | Ok(Ok(records)) if !records.is_empty() => { |
| 2572 | app_base::diagnostics::record(&format!( |
| 2573 | "chat: older page, {} messages", |
| 2574 | records.len() |
| 2575 | )); |
| 2576 | current.loaded_limit = current.loaded_count + records.len(); |
| 2577 | self.older_anchor = self |
| 2578 | .rows |
| 2579 | .borrow() |
| 2580 | .iter() |
| 2581 | .find(|row| matches!(row, Row::Message(_) | Row::Post(_))) |
| 2582 | .map(|row| row.key().to_owned()); |
| 2583 | } |
| 2584 | Ok(Ok(_)) => { |
| 2585 | app_base::diagnostics::record("chat: no older messages"); |
| 2586 | current.older_blocked = true; |
| 2587 | } |
| 2588 | Ok(Err(error)) => { |
| 2589 | app_base::diagnostics::record(&format!( |
| 2590 | "chat: older page failed: {}", |
| 2591 | short_error(&error) |
| 2592 | )); |
| 2593 | current.older_blocked = true; |
| 2594 | self.notice = Some(format!( |
| 2595 | "Could not load older messages: {}", |
| 2596 | short_error(&error) |
| 2597 | )); |
| 2598 | } |
| 2599 | Err(_) => current.older_blocked = true, |
| 2600 | } |
| 2601 | self.rebuild(false, cx); |
| 2602 | } |
| 2603 | |
| 2604 | fn clear_pending(&mut self) { |
| 2605 | self.pending.clear(); |
| 2606 | self.pending_outgoing.clear(); |
| 2607 | } |
| 2608 | |
| 2609 | fn outbox_route(&self, reply_root: Option<String>) -> (OutboxTarget, Option<String>) { |
| 2610 | if let Some(root_id) = reply_root { |
| 2611 | return (OutboxTarget::Thread, Some(root_id)); |
| 2612 | } |
| 2613 | match self.current.as_ref().map(|current| ¤t.mode) { |
| 2614 | Some(ViewMode::Thread(root_id)) => (OutboxTarget::Thread, Some(root_id.clone())), |
| 2615 | Some(ViewMode::ThreadList) => (OutboxTarget::Post, None), |
| 2616 | _ => (OutboxTarget::Flat, None), |
| 2617 | } |
| 2618 | } |
| 2619 | |
| 2620 | fn reload_pending(&mut self, cx: &App) { |
| 2621 | self.clear_pending(); |
| 2622 | let state = self.app.read(cx); |
| 2623 | if state.mode.demo || state.mode.read_only { |
| 2624 | return; |
| 2625 | } |
| 2626 | let Some(current) = self.current.as_ref() else { |
| 2627 | return; |
| 2628 | }; |
| 2629 | let conversation_id = current.selection.conversation_id().to_owned(); |
| 2630 | let Ok(records) = state.store.outbox_for_conversation(&conversation_id) else { |
| 2631 | return; |
| 2632 | }; |
| 2633 | let my_user_id = state.store.meta(META_USER_ID).ok().flatten(); |
| 2634 | let mut pending = Vec::new(); |
| 2635 | for record in records { |
| 2636 | if !shown_in_view(¤t.mode, &record) { |
| 2637 | continue; |
| 2638 | } |
| 2639 | let Some(outgoing) = outgoing_of(&record) else { |
| 2640 | continue; |
| 2641 | }; |
| 2642 | let delivery = match record.state { |
| 2643 | OutboxState::Sending => Delivery::Sending, |
| 2644 | OutboxState::Failed => Delivery::Failed(record.last_error.unwrap_or_default()), |
| 2645 | }; |
| 2646 | let mut row = pending_row( |
| 2647 | record.id.clone(), |
| 2648 | conversation_id.clone(), |
| 2649 | my_user_id.clone(), |
| 2650 | record.created_at, |
| 2651 | &outgoing, |
| 2652 | delivery, |
| 2653 | ); |
| 2654 | row.reply_root = record.thread_root_id.clone(); |
| 2655 | pending.push((row, record.id, outgoing)); |
| 2656 | } |
| 2657 | for (row, id, outgoing) in pending { |
| 2658 | self.pending.push(row); |
| 2659 | self.pending_outgoing.insert(id, outgoing); |
| 2660 | } |
| 2661 | } |
| 2662 | |
| 2663 | fn discard_outbox_row(&self, id: &str, cx: &mut Context<Self>) { |
| 2664 | self.app.read(cx).store.delete_outbox(id).ok(); |
| 2665 | self.app |
| 2666 | .update(cx, |state, cx| state.refresh_local_previews(cx)); |
| 2667 | } |
| 2668 | |
| 2669 | fn delete_pending(&mut self, key: &str, cx: &mut Context<Self>) { |
| 2670 | self.pending.retain(|row| row.key != key); |
| 2671 | self.pending_outgoing.remove(key); |
| 2672 | self.discard_outbox_row(key, cx); |
| 2673 | self.rebuild(false, cx); |
| 2674 | } |
| 2675 | |
| 2676 | fn send(&mut self, outgoing: Outgoing, window: &mut Window, cx: &mut Context<Self>) { |
| 2677 | self.leave_detached(cx); |
| 2678 | self.send_as(new_outbox_id(), outgoing, None, window, cx); |
| 2679 | } |
| 2680 | |
| 2681 | fn send_in_thread( |
| 2682 | &mut self, |
| 2683 | root_id: String, |
| 2684 | outgoing: Outgoing, |
| 2685 | window: &mut Window, |
| 2686 | cx: &mut Context<Self>, |
| 2687 | ) { |
| 2688 | self.send_as(new_outbox_id(), outgoing, Some(root_id), window, cx); |
| 2689 | } |
| 2690 | |
| 2691 | fn send_as( |
| 2692 | &mut self, |
| 2693 | id: String, |
| 2694 | outgoing: Outgoing, |
| 2695 | reply_root: Option<String>, |
| 2696 | window: &mut Window, |
| 2697 | cx: &mut Context<Self>, |
| 2698 | ) { |
| 2699 | let state = self.app.read(cx); |
| 2700 | let (mode, engine, store) = (state.mode, state.engine.clone(), state.store.clone()); |
| 2701 | let Some(current) = self.current.as_ref() else { |
| 2702 | return; |
| 2703 | }; |
| 2704 | if mode.read_only || mode.demo { |
| 2705 | self.restore_unsent( |
| 2706 | "Read-only mode: nothing was sent", |
| 2707 | &outgoing, |
| 2708 | reply_root.as_deref(), |
| 2709 | window, |
| 2710 | cx, |
| 2711 | ); |
| 2712 | return; |
| 2713 | } |
| 2714 | let Some(engine) = engine else { |
| 2715 | self.discard_outbox_row(&id, cx); |
| 2716 | self.restore_unsent( |
| 2717 | "Not connected yet: nothing was sent", |
| 2718 | &outgoing, |
| 2719 | reply_root.as_deref(), |
| 2720 | window, |
| 2721 | cx, |
| 2722 | ); |
| 2723 | return; |
| 2724 | }; |
| 2725 | let conversation_id = current.selection.conversation_id().to_owned(); |
| 2726 | let (target, thread_root_id) = self.outbox_route(reply_root); |
| 2727 | let created_at = Utc::now(); |
| 2728 | self.push_pending( |
| 2729 | id.clone(), |
| 2730 | created_at, |
| 2731 | &outgoing, |
| 2732 | thread_root_id.clone(), |
| 2733 | cx, |
| 2734 | ); |
| 2735 | if let Some(record) = outbox_record( |
| 2736 | &id, |
| 2737 | &conversation_id, |
| 2738 | target, |
| 2739 | thread_root_id.as_deref(), |
| 2740 | &outgoing, |
| 2741 | created_at, |
| 2742 | ) { |
| 2743 | store.put_outbox(&record).ok(); |
| 2744 | } |
| 2745 | self.notice = None; |
| 2746 | self.rebuild(false, cx); |
| 2747 | if self.in_feed() { |
| 2748 | if thread_root_id.is_none() { |
| 2749 | self.scroller |
| 2750 | .update(cx, |scroller, cx| scroller.scroll_to_item(0, cx)); |
| 2751 | } |
| 2752 | } else { |
| 2753 | self.scroller |
| 2754 | .update(cx, |scroller, cx| scroller.scroll_to_end(cx)); |
| 2755 | } |
| 2756 | |
| 2757 | let receiver = runtime::spawn(async move { |
| 2758 | deliver( |
| 2759 | &engine, |
| 2760 | &conversation_id, |
| 2761 | target, |
| 2762 | thread_root_id.as_deref(), |
| 2763 | &outgoing, |
| 2764 | ) |
| 2765 | .await |
| 2766 | }); |
| 2767 | cx.spawn_in(window, async move |this, cx| { |
| 2768 | let result = receiver.await; |
| 2769 | this.update(cx, |this, cx| this.finish_send(&id, result, cx)) |
| 2770 | .ok(); |
| 2771 | }) |
| 2772 | .detach(); |
| 2773 | } |
| 2774 | |
| 2775 | fn restore_unsent( |
| 2776 | &mut self, |
| 2777 | message: &str, |
| 2778 | outgoing: &Outgoing, |
| 2779 | reply_root: Option<&str>, |
| 2780 | window: &mut Window, |
| 2781 | cx: &mut Context<Self>, |
| 2782 | ) { |
| 2783 | self.notice = Some(message.to_owned()); |
| 2784 | if let (Some(root_id), true) = (reply_root, self.in_feed()) { |
| 2785 | self.open_reply_editor(root_id.to_owned(), outgoing.reply.clone(), window, cx); |
| 2786 | if let Some(composer) = self.reply_composer.clone() { |
| 2787 | composer.update(cx, |composer, cx| composer.restore(outgoing, window, cx)); |
| 2788 | } |
| 2789 | } else { |
| 2790 | self.show_main_composer(cx); |
| 2791 | self.restore_subject(outgoing.subject.as_deref(), window, cx); |
| 2792 | self.composer |
| 2793 | .update(cx, |composer, cx| composer.restore(outgoing, window, cx)); |
| 2794 | } |
| 2795 | cx.notify(); |
| 2796 | } |
| 2797 | |
| 2798 | fn next_pending_key(&mut self) -> String { |
| 2799 | self.pending_counter += 1; |
| 2800 | format!("{PENDING_KEY_PREFIX}{}", self.pending_counter) |
| 2801 | } |
| 2802 | |
| 2803 | fn push_pending( |
| 2804 | &mut self, |
| 2805 | key: String, |
| 2806 | created_at: DateTime<Utc>, |
| 2807 | outgoing: &Outgoing, |
| 2808 | reply_root: Option<String>, |
| 2809 | cx: &App, |
| 2810 | ) { |
| 2811 | let my_user_id = self.row_context(cx).my_user_id; |
| 2812 | let conversation_id = self |
| 2813 | .current |
| 2814 | .as_ref() |
| 2815 | .map(|current| current.selection.conversation_id().to_owned()) |
| 2816 | .unwrap_or_default(); |
| 2817 | let mut row = pending_row( |
| 2818 | key.clone(), |
| 2819 | conversation_id, |
| 2820 | my_user_id, |
| 2821 | created_at, |
| 2822 | outgoing, |
| 2823 | Delivery::Sending, |
| 2824 | ); |
| 2825 | row.reply_root = reply_root; |
| 2826 | self.pending.push(row); |
| 2827 | self.pending_outgoing.insert(key, outgoing.clone()); |
| 2828 | } |
| 2829 | |
| 2830 | fn send_new_chat(&mut self, outgoing: Outgoing, window: &mut Window, cx: &mut Context<Self>) { |
| 2831 | let (recipients, topic) = { |
| 2832 | let draft = self.draft.read(cx); |
| 2833 | let topic = Some(draft.group_name(cx)).filter(|name| !name.is_empty()); |
| 2834 | (draft.chips().to_vec(), topic) |
| 2835 | }; |
| 2836 | if recipients.is_empty() { |
| 2837 | self.restore_unsent("Add at least one person first", &outgoing, None, window, cx); |
| 2838 | self.draft |
| 2839 | .update(cx, |draft, cx| draft.focus_query(window, cx)); |
| 2840 | return; |
| 2841 | } |
| 2842 | if self |
| 2843 | .pending |
| 2844 | .iter() |
| 2845 | .any(|row| matches!(row.delivery, Delivery::Sending)) |
| 2846 | { |
| 2847 | self.restore_unsent("Still creating the chat", &outgoing, None, window, cx); |
| 2848 | return; |
| 2849 | } |
| 2850 | let state = self.app.read(cx); |
| 2851 | let (mode, engine) = (state.mode, state.engine.clone()); |
| 2852 | if mode.read_only { |
| 2853 | self.restore_unsent( |
| 2854 | "Read-only mode: nothing was sent", |
| 2855 | &outgoing, |
| 2856 | None, |
| 2857 | window, |
| 2858 | cx, |
| 2859 | ); |
| 2860 | return; |
| 2861 | } |
| 2862 | if mode.demo && outgoing.has_attachments() { |
| 2863 | self.restore_unsent( |
| 2864 | "Read-only mode: nothing was sent", |
| 2865 | &outgoing, |
| 2866 | None, |
| 2867 | window, |
| 2868 | cx, |
| 2869 | ); |
| 2870 | return; |
| 2871 | } |
| 2872 | if mode.demo { |
| 2873 | self.send_demo_new_chat(&outgoing, &recipients, topic.as_deref(), cx); |
| 2874 | return; |
| 2875 | } |
| 2876 | let Some(engine) = engine else { |
| 2877 | self.restore_unsent( |
| 2878 | "Not connected yet: nothing was sent", |
| 2879 | &outgoing, |
| 2880 | None, |
| 2881 | window, |
| 2882 | cx, |
| 2883 | ); |
| 2884 | return; |
| 2885 | }; |
| 2886 | let user_ids: Vec<String> = recipients.into_iter().map(|pick| pick.user_id).collect(); |
| 2887 | let known_chat = self |
| 2888 | .draft_created |
| 2889 | .as_ref() |
| 2890 | .filter(|(created_for, _)| *created_for == user_ids) |
| 2891 | .map(|(_, chat_id)| chat_id.clone()) |
| 2892 | .or_else(|| self.draft_target_chat(cx)); |
| 2893 | self.pending.clear(); |
| 2894 | self.pending_outgoing.clear(); |
| 2895 | self.draft_error = None; |
| 2896 | self.notice = None; |
| 2897 | let key = self.next_pending_key(); |
| 2898 | self.push_pending(key.clone(), Utc::now(), &outgoing, None, cx); |
| 2899 | self.rebuild(true, cx); |
| 2900 | let recipients_for_finish = user_ids.clone(); |
| 2901 | let receiver = runtime::spawn(async move { |
| 2902 | let chat_id = match known_chat { |
| 2903 | Some(chat_id) => chat_id, |
| 2904 | None => { |
| 2905 | let created = match user_ids.as_slice() { |
| 2906 | [user_id] => engine.create_one_on_one(user_id).await, |
| 2907 | _ => engine.create_group(&user_ids, topic.as_deref()).await, |
| 2908 | }; |
| 2909 | match created { |
| 2910 | Ok(chat_id) => chat_id, |
| 2911 | Err(error) => return (None, Some(short_error(&error))), |
| 2912 | } |
| 2913 | } |
| 2914 | }; |
| 2915 | let sent = engine |
| 2916 | .send_message_with_extras( |
| 2917 | &chat_id, |
| 2918 | &outgoing.html(), |
| 2919 | None, |
| 2920 | &outgoing.mentions, |
| 2921 | &outgoing.extras(), |
| 2922 | ) |
| 2923 | .await; |
| 2924 | if let (Ok(record), Some(preview)) = (&sent, &outgoing.link_preview) { |
| 2925 | let _ = engine |
| 2926 | .attach_link_preview(&chat_id, &record.message_id, preview) |
| 2927 | .await; |
| 2928 | } |
| 2929 | (Some(chat_id), sent.err().map(|error| short_error(&error))) |
| 2930 | }); |
| 2931 | cx.spawn_in(window, async move |this, cx| { |
| 2932 | let result = receiver.await; |
| 2933 | this.update_in(cx, |this, _, cx| { |
| 2934 | this.finish_new_chat(&key, recipients_for_finish, result, cx) |
| 2935 | }) |
| 2936 | .ok(); |
| 2937 | }) |
| 2938 | .detach(); |
| 2939 | } |
| 2940 | |
| 2941 | fn finish_new_chat( |
| 2942 | &mut self, |
| 2943 | key: &str, |
| 2944 | user_ids: Vec<String>, |
| 2945 | result: Result<(Option<String>, Option<String>), tokio::sync::oneshot::error::RecvError>, |
| 2946 | cx: &mut Context<Self>, |
| 2947 | ) { |
| 2948 | if !self.draft_active || !self.pending.iter().any(|row| row.key == key) { |
| 2949 | return; |
| 2950 | } |
| 2951 | let (chat_id, error) = result.unwrap_or((None, Some("cancelled".to_owned()))); |
| 2952 | self.draft_created = chat_id.clone().map(|chat_id| (user_ids, chat_id)); |
| 2953 | match (chat_id, error) { |
| 2954 | (Some(chat_id), None) => { |
| 2955 | self.app.update(cx, |state, cx| { |
| 2956 | state.reload_sidebar(cx); |
| 2957 | state.select(Selection::Chat(chat_id), cx); |
| 2958 | }); |
| 2959 | } |
| 2960 | (created, error) => { |
| 2961 | if let Some(row) = self.pending.iter_mut().find(|row| row.key == key) { |
| 2962 | row.delivery = Delivery::Failed(error.unwrap_or_default()); |
| 2963 | } |
| 2964 | self.draft_error = Some( |
| 2965 | if created.is_some() { |
| 2966 | "Couldn't send the message." |
| 2967 | } else { |
| 2968 | "Couldn't create the chat." |
| 2969 | } |
| 2970 | .to_owned(), |
| 2971 | ); |
| 2972 | self.rebuild(false, cx); |
| 2973 | } |
| 2974 | } |
| 2975 | } |
| 2976 | |
| 2977 | fn send_demo_new_chat( |
| 2978 | &mut self, |
| 2979 | outgoing: &Outgoing, |
| 2980 | recipients: &[super::new_chat::Pick], |
| 2981 | topic: Option<&str>, |
| 2982 | cx: &mut Context<Self>, |
| 2983 | ) { |
| 2984 | let (store, chats) = { |
| 2985 | let state = self.app.read(cx); |
| 2986 | (state.store.clone(), state.sidebar.chats.clone()) |
| 2987 | }; |
| 2988 | let people: Vec<(String, String)> = recipients |
| 2989 | .iter() |
| 2990 | .map(|pick| (pick.user_id.clone(), pick.name.clone())) |
| 2991 | .collect(); |
| 2992 | let (chat, message) = match self.draft_target_chat(cx) { |
| 2993 | Some(chat_id) => { |
| 2994 | let Some(chat) = chats.iter().find(|chat| chat.id == chat_id) else { |
| 2995 | return; |
| 2996 | }; |
| 2997 | crate::demo::send_in_chat(chat, &outgoing.text(), &outgoing.html(), Utc::now()) |
| 2998 | } |
| 2999 | None => crate::demo::new_chat( |
| 3000 | &chats, |
| 3001 | &people, |
| 3002 | topic, |
| 3003 | &outgoing.text(), |
| 3004 | &outgoing.html(), |
| 3005 | Utc::now(), |
| 3006 | ), |
| 3007 | }; |
| 3008 | let _ = store.upsert_chats(std::slice::from_ref(&chat)); |
| 3009 | let _ = store.upsert_messages(std::slice::from_ref(&message)); |
| 3010 | self.app.update(cx, |state, cx| { |
| 3011 | state.reload_sidebar(cx); |
| 3012 | state.select(Selection::Chat(chat.id.clone()), cx); |
| 3013 | }); |
| 3014 | } |
| 3015 | |
| 3016 | fn conversation_id(&self) -> Option<String> { |
| 3017 | self.current |
| 3018 | .as_ref() |
| 3019 | .map(|current| current.selection.conversation_id().to_owned()) |
| 3020 | } |
| 3021 | |
| 3022 | fn own_record(&self, message_id: &str, cx: &App) -> Option<MessageRecord> { |
| 3023 | let conversation_id = self.conversation_id()?; |
| 3024 | let my_user_id = self.row_context(cx).my_user_id?; |
| 3025 | self.app |
| 3026 | .read(cx) |
| 3027 | .store |
| 3028 | .messages_by_id(&conversation_id, &[message_id.to_owned()]) |
| 3029 | .ok()? |
| 3030 | .remove(message_id) |
| 3031 | .filter(|record| !record.deleted && record.sender_id.as_deref() == Some(&my_user_id)) |
| 3032 | } |
| 3033 | |
| 3034 | fn set_hover(&mut self, slot: HoverSlot, key: &str, hovered: bool, cx: &mut Context<Self>) { |
| 3035 | if hovered && matches!(slot, HoverSlot::Row) { |
| 3036 | self.request_post_follow(key, cx); |
| 3037 | } |
| 3038 | if !hovered { |
| 3039 | if self.hovered_message.as_deref() == Some(key) |
| 3040 | || self.toolbar_hovered.as_deref() == Some(key) |
| 3041 | { |
| 3042 | self.leave_hover_later(key.to_owned(), cx); |
| 3043 | } |
| 3044 | return; |
| 3045 | } |
| 3046 | self.hover_leave_serial += 1; |
| 3047 | match slot { |
| 3048 | HoverSlot::Row => self.hovered_message = Some(key.to_owned()), |
| 3049 | HoverSlot::Toolbar => self.toolbar_hovered = Some(key.to_owned()), |
| 3050 | } |
| 3051 | if matches!(slot, HoverSlot::Row) |
| 3052 | && self |
| 3053 | .toolbar_hovered |
| 3054 | .as_deref() |
| 3055 | .is_some_and(|toolbar| toolbar != key) |
| 3056 | { |
| 3057 | self.toolbar_hovered = None; |
| 3058 | } |
| 3059 | if let Some(suppressed) = self.toolbar_suppressed.as_deref() |
| 3060 | && self.hovered_message.as_deref() != Some(suppressed) |
| 3061 | && self.toolbar_hovered.as_deref() != Some(suppressed) |
| 3062 | { |
| 3063 | self.toolbar_suppressed = None; |
| 3064 | } |
| 3065 | cx.notify(); |
| 3066 | } |
| 3067 | |
| 3068 | fn leave_hover_later(&mut self, key: String, cx: &mut Context<Self>) { |
| 3069 | self.hover_leave_serial += 1; |
| 3070 | let serial = self.hover_leave_serial; |
| 3071 | cx.spawn(async move |this, cx| { |
| 3072 | cx.background_executor().timer(HOVER_LEAVE_GRACE).await; |
| 3073 | this.update(cx, |this, cx| { |
| 3074 | if this.hover_leave_serial != serial { |
| 3075 | return; |
| 3076 | } |
| 3077 | if this.hovered_message.as_deref() == Some(key.as_str()) { |
| 3078 | this.hovered_message = None; |
| 3079 | } |
| 3080 | if this.toolbar_hovered.as_deref() == Some(key.as_str()) { |
| 3081 | this.toolbar_hovered = None; |
| 3082 | } |
| 3083 | if this.toolbar_suppressed.as_deref() == Some(key.as_str()) { |
| 3084 | this.toolbar_suppressed = None; |
| 3085 | } |
| 3086 | cx.notify(); |
| 3087 | }) |
| 3088 | .ok(); |
| 3089 | }) |
| 3090 | .detach(); |
| 3091 | } |
| 3092 | |
| 3093 | fn suppress_toolbar(&mut self, key: &str, cx: &mut Context<Self>) { |
| 3094 | if self.toolbar_hovered.as_deref() == Some(key) { |
| 3095 | self.toolbar_hovered = None; |
| 3096 | } |
| 3097 | self.toolbar_suppressed = Some(key.to_owned()); |
| 3098 | cx.notify(); |
| 3099 | } |
| 3100 | |
| 3101 | fn set_pinned(&mut self, key: &str, open: bool, cx: &mut Context<Self>) { |
| 3102 | if open { |
| 3103 | self.toolbar_pinned = Some(key.to_owned()); |
| 3104 | } else if self.toolbar_pinned.as_deref() == Some(key) { |
| 3105 | self.toolbar_pinned = None; |
| 3106 | } |
| 3107 | cx.notify(); |
| 3108 | } |
| 3109 | |
| 3110 | fn toolbar_visible(&self, key: &str, cx: &App) -> bool { |
| 3111 | let row_trigger = (!self.app.read(cx).actions_on_right_click) |
| 3112 | .then_some(self.hovered_message.as_deref()) |
| 3113 | .flatten(); |
| 3114 | self.toolbar_suppressed.as_deref() != Some(key) |
| 3115 | && self |
| 3116 | .toolbar_pinned |
| 3117 | .as_deref() |
| 3118 | .or(self.toolbar_hovered.as_deref()) |
| 3119 | .or(row_trigger) |
| 3120 | == Some(key) |
| 3121 | } |
| 3122 | |
| 3123 | pub(crate) fn context_menu_for(&self, key: &str) -> Option<OpenContextMenu> { |
| 3124 | self.context_menu.clone().filter(|open| open.key == key) |
| 3125 | } |
| 3126 | |
| 3127 | fn open_context_menu(&mut self, key: &str, position: Point<Pixels>, cx: &mut Context<Self>) { |
| 3128 | self.context_serial += 1; |
| 3129 | self.context_menu = Some(OpenContextMenu::new(key, position, self.context_serial)); |
| 3130 | cx.notify(); |
| 3131 | } |
| 3132 | |
| 3133 | fn close_context_menu(&mut self, from_outside: bool, cx: &mut Context<Self>) { |
| 3134 | let Some(open) = &self.context_menu else { |
| 3135 | return; |
| 3136 | }; |
| 3137 | if from_outside && self.toolbar_pinned.as_deref() == Some(open.key.as_str()) { |
| 3138 | return; |
| 3139 | } |
| 3140 | self.context_menu = None; |
| 3141 | cx.notify(); |
| 3142 | } |
| 3143 | |
| 3144 | fn toggle_reaction(&mut self, message_id: &str, glyph: &str, cx: &mut Context<Self>) { |
| 3145 | self.change_reaction(message_id, glyph, false, None, cx); |
| 3146 | } |
| 3147 | |
| 3148 | fn change_reaction( |
| 3149 | &mut self, |
| 3150 | message_id: &str, |
| 3151 | glyph: &str, |
| 3152 | only_add: bool, |
| 3153 | accepted_proposal: Option<String>, |
| 3154 | cx: &mut Context<Self>, |
| 3155 | ) -> ReactionAttempt { |
| 3156 | let Some(conversation_id) = self.conversation_id() else { |
| 3157 | return ReactionAttempt::Blocked; |
| 3158 | }; |
| 3159 | let state = self.app.read(cx); |
| 3160 | let (mode, engine, store) = (state.mode, state.engine.clone(), state.store.clone()); |
| 3161 | let engine = engine.filter(|_| !mode.read_only); |
| 3162 | if !mode.demo && engine.is_none() { |
| 3163 | self.show_notice("Read-only mode: reaction not sent", cx); |
| 3164 | return ReactionAttempt::Blocked; |
| 3165 | } |
| 3166 | let my_user_id = self.row_context(cx).my_user_id; |
| 3167 | let Some(mut record) = store |
| 3168 | .messages_by_id(&conversation_id, &[message_id.to_owned()]) |
| 3169 | .ok() |
| 3170 | .and_then(|mut found| found.remove(message_id)) |
| 3171 | else { |
| 3172 | return ReactionAttempt::MissingMessage; |
| 3173 | }; |
| 3174 | let wanted = reaction_glyph(glyph); |
| 3175 | let mut visible = teams_core::reactions(&record); |
| 3176 | apply_pending_reactions( |
| 3177 | &mut visible, |
| 3178 | self.pending_reactions.get(message_id), |
| 3179 | my_user_id.as_deref(), |
| 3180 | Utc::now(), |
| 3181 | ); |
| 3182 | let added = !has_own_reaction(&visible, glyph, my_user_id.as_deref()); |
| 3183 | if only_add && !added { |
| 3184 | return ReactionAttempt::AlreadyThere; |
| 3185 | } |
| 3186 | let skin_tone = teams_core::skin_tone(&store); |
| 3187 | let wanted = if added { |
| 3188 | wanted |
| 3189 | } else { |
| 3190 | let own = own_reaction_glyph(&visible, glyph, my_user_id.as_deref()).unwrap_or(wanted); |
| 3191 | if emoticons::without_tone(&own) == own { |
| 3192 | emoticons::with_tone(&own, skin_tone) |
| 3193 | } else { |
| 3194 | own |
| 3195 | } |
| 3196 | }; |
| 3197 | let glyph = wanted.as_str(); |
| 3198 | let Some(engine) = engine.filter(|_| !mode.demo) else { |
| 3199 | let mut reactions = teams_core::reactions(&record); |
| 3200 | set_own_reaction( |
| 3201 | &mut reactions, |
| 3202 | glyph, |
| 3203 | my_user_id.as_deref(), |
| 3204 | added, |
| 3205 | Utc::now(), |
| 3206 | ); |
| 3207 | record.reactions_json = serde_json::to_string(&reactions).unwrap_or_default(); |
| 3208 | let _ = store.upsert_messages(std::slice::from_ref(&record)); |
| 3209 | if added { |
| 3210 | self.record_reaction_use(glyph, cx); |
| 3211 | } |
| 3212 | self.rebuild(false, cx); |
| 3213 | return ReactionAttempt::Acted; |
| 3214 | }; |
| 3215 | let entries = self |
| 3216 | .pending_reactions |
| 3217 | .entry(message_id.to_owned()) |
| 3218 | .or_default(); |
| 3219 | entries.retain(|(pending, _)| { |
| 3220 | emoticons::without_tone(pending) != emoticons::without_tone(&wanted) |
| 3221 | }); |
| 3222 | entries.push((wanted.clone(), added)); |
| 3223 | self.rebuild(false, cx); |
| 3224 | let (message_id, glyph) = (message_id.to_owned(), glyph.to_owned()); |
| 3225 | let reaction_type = api_reaction(&wanted); |
| 3226 | let receiver = runtime::spawn({ |
| 3227 | let (conversation_id, message_id) = (conversation_id.clone(), message_id.clone()); |
| 3228 | async move { |
| 3229 | if added { |
| 3230 | engine |
| 3231 | .set_reaction(&conversation_id, &message_id, &reaction_type) |
| 3232 | .await |
| 3233 | } else { |
| 3234 | engine |
| 3235 | .unset_reaction(&conversation_id, &message_id, &reaction_type) |
| 3236 | .await |
| 3237 | } |
| 3238 | } |
| 3239 | }); |
| 3240 | cx.spawn(async move |this, cx| { |
| 3241 | let outcome = match receiver.await { |
| 3242 | Ok(Ok(())) => Ok(()), |
| 3243 | Ok(Err(error)) => Err(short_error(&error)), |
| 3244 | Err(_) => Err("cancelled".to_owned()), |
| 3245 | }; |
| 3246 | this.update(cx, |this, cx| { |
| 3247 | this.finish_reaction( |
| 3248 | &conversation_id, |
| 3249 | &message_id, |
| 3250 | &glyph, |
| 3251 | added, |
| 3252 | accepted_proposal.as_deref(), |
| 3253 | outcome, |
| 3254 | cx, |
| 3255 | ) |
| 3256 | }) |
| 3257 | .ok(); |
| 3258 | }) |
| 3259 | .detach(); |
| 3260 | ReactionAttempt::Started |
| 3261 | } |
| 3262 | |
| 3263 | fn finish_reaction( |
| 3264 | &mut self, |
| 3265 | conversation_id: &str, |
| 3266 | message_id: &str, |
| 3267 | glyph: &str, |
| 3268 | added: bool, |
| 3269 | accepted_proposal: Option<&str>, |
| 3270 | outcome: Result<(), String>, |
| 3271 | cx: &mut Context<Self>, |
| 3272 | ) { |
| 3273 | let wanted = reaction_glyph(glyph); |
| 3274 | let store = self.app.read(cx).store.clone(); |
| 3275 | if let Some(proposal_id) = accepted_proposal.filter(|_| outcome.is_ok()) { |
| 3276 | crate::agent_api::resolve_reaction_proposal( |
| 3277 | conversation_id, |
| 3278 | message_id, |
| 3279 | proposal_id, |
| 3280 | mcp::ReactionOutcome::Accepted, |
| 3281 | cx, |
| 3282 | ); |
| 3283 | } |
| 3284 | if outcome.is_ok() { |
| 3285 | let my_user_id = self.row_context(cx).my_user_id; |
| 3286 | if let Some(mut record) = store |
| 3287 | .messages_by_id(conversation_id, &[message_id.to_owned()]) |
| 3288 | .ok() |
| 3289 | .and_then(|mut found| found.remove(message_id)) |
| 3290 | { |
| 3291 | let mut reactions = teams_core::reactions(&record); |
| 3292 | set_own_reaction( |
| 3293 | &mut reactions, |
| 3294 | glyph, |
| 3295 | my_user_id.as_deref(), |
| 3296 | added, |
| 3297 | Utc::now(), |
| 3298 | ); |
| 3299 | record.reactions_json = serde_json::to_string(&reactions).unwrap_or_default(); |
| 3300 | let _ = store.upsert_messages(std::slice::from_ref(&record)); |
| 3301 | } |
| 3302 | if added { |
| 3303 | self.record_reaction_use(glyph, cx); |
| 3304 | } |
| 3305 | } |
| 3306 | if let Some(entries) = self.pending_reactions.get_mut(message_id) { |
| 3307 | entries.retain(|entry| *entry != (wanted.clone(), added)); |
| 3308 | if entries.is_empty() { |
| 3309 | self.pending_reactions.remove(message_id); |
| 3310 | } |
| 3311 | } |
| 3312 | self.rebuild(false, cx); |
| 3313 | if let Err(error) = outcome { |
| 3314 | self.show_notice(&format!("Reaction failed: {error}"), cx); |
| 3315 | } |
| 3316 | } |
| 3317 | |
| 3318 | fn record_reaction_use(&mut self, glyph: &str, cx: &mut Context<Self>) { |
| 3319 | if chatsvc::custom_emoji(glyph).is_some() { |
| 3320 | return; |
| 3321 | } |
| 3322 | let store = self.app.read(cx).store.clone(); |
| 3323 | self.recent.update(cx, |recent, cx| { |
| 3324 | recent.reload(&store); |
| 3325 | recent.record_use(&emoticons::without_tone(glyph)); |
| 3326 | recent.save(&store); |
| 3327 | cx.notify(); |
| 3328 | }); |
| 3329 | } |
| 3330 | |
| 3331 | fn begin_edit(&mut self, message_id: &str, window: &mut Window, cx: &mut Context<Self>) { |
| 3332 | let Some(record) = self.own_record(message_id, cx) else { |
| 3333 | return; |
| 3334 | }; |
| 3335 | let edit = EditPreview { |
| 3336 | message_id: record.message_id.clone(), |
| 3337 | excerpt: reply_excerpt(&record), |
| 3338 | scheduled: None, |
| 3339 | }; |
| 3340 | let in_chat = matches!( |
| 3341 | self.current.as_ref().map(|current| ¤t.selection), |
| 3342 | Some(Selection::Chat(_)) |
| 3343 | ); |
| 3344 | let mentions = if in_chat { |
| 3345 | teams_core::user_mention_inputs(&record) |
| 3346 | } else { |
| 3347 | Vec::new() |
| 3348 | }; |
| 3349 | let draft = Outgoing { |
| 3350 | banner: None, |
| 3351 | draft: message_draft(&record), |
| 3352 | subject: None, |
| 3353 | mentions, |
| 3354 | reply: None, |
| 3355 | edit: Some(edit), |
| 3356 | images: Vec::new(), |
| 3357 | files: Vec::new(), |
| 3358 | link_preview: None, |
| 3359 | importance: Default::default(), |
| 3360 | }; |
| 3361 | self.show_main_composer(cx); |
| 3362 | self.composer |
| 3363 | .update(cx, |composer, cx| composer.begin_edit(draft, window, cx)); |
| 3364 | } |
| 3365 | |
| 3366 | fn edit_last_own(&mut self, window: &mut Window, cx: &mut Context<Self>) { |
| 3367 | if self.draft_active { |
| 3368 | return; |
| 3369 | } |
| 3370 | let key = self |
| 3371 | .rows |
| 3372 | .borrow() |
| 3373 | .iter() |
| 3374 | .flat_map(Row::messages) |
| 3375 | .filter(|message| { |
| 3376 | message.own && !message.deleted && !message.key.starts_with(PENDING_KEY_PREFIX) |
| 3377 | }) |
| 3378 | .max_by_key(|message| message.created_at) |
| 3379 | .map(|message| message.key.clone()); |
| 3380 | if let Some(key) = key { |
| 3381 | self.begin_edit(&key, window, cx); |
| 3382 | } |
| 3383 | } |
| 3384 | |
| 3385 | fn send_edit(&mut self, outgoing: Outgoing, window: &mut Window, cx: &mut Context<Self>) { |
| 3386 | let Some(edit) = outgoing.edit.clone() else { |
| 3387 | return; |
| 3388 | }; |
| 3389 | let Some(conversation_id) = self.conversation_id() else { |
| 3390 | return; |
| 3391 | }; |
| 3392 | let state = self.app.read(cx); |
| 3393 | let (mode, engine, store) = (state.mode, state.engine.clone(), state.store.clone()); |
| 3394 | if mode.demo { |
| 3395 | if let Some(mut record) = self.own_record(&edit.message_id, cx) { |
| 3396 | record.body_html = outgoing.html(); |
| 3397 | record.edited_at = Some(Utc::now()); |
| 3398 | let _ = store.upsert_messages(std::slice::from_ref(&record)); |
| 3399 | self.rebuild(false, cx); |
| 3400 | } |
| 3401 | return; |
| 3402 | } |
| 3403 | let Some(engine) = engine.filter(|_| !mode.read_only) else { |
| 3404 | self.notice = Some("Read-only mode: edit not saved".to_owned()); |
| 3405 | self.show_main_composer(cx); |
| 3406 | self.composer |
| 3407 | .update(cx, |composer, cx| composer.restore(&outgoing, window, cx)); |
| 3408 | cx.notify(); |
| 3409 | return; |
| 3410 | }; |
| 3411 | let original = self.own_record(&edit.message_id, cx); |
| 3412 | if let Some(mut record) = original.clone() { |
| 3413 | record.body_html = outgoing.html(); |
| 3414 | record.edited_at = Some(Utc::now()); |
| 3415 | let _ = store.upsert_messages(std::slice::from_ref(&record)); |
| 3416 | } |
| 3417 | let message_id = edit.message_id.clone(); |
| 3418 | self.saving_edits.insert(message_id.clone()); |
| 3419 | self.rebuild(false, cx); |
| 3420 | let sent = outgoing.clone(); |
| 3421 | let edited_conversation = conversation_id.clone(); |
| 3422 | let receiver = runtime::spawn(async move { |
| 3423 | engine |
| 3424 | .edit_message_html( |
| 3425 | &conversation_id, |
| 3426 | &edit.message_id, |
| 3427 | &sent.html(), |
| 3428 | &sent.mentions, |
| 3429 | ) |
| 3430 | .await |
| 3431 | }); |
| 3432 | cx.spawn_in(window, async move |this, cx| { |
| 3433 | let failed = !matches!(receiver.await, Ok(Ok(()))); |
| 3434 | this.update_in(cx, |this, window, cx| { |
| 3435 | this.saving_edits.remove(&message_id); |
| 3436 | if failed && let Some(record) = &original { |
| 3437 | let _ = store.upsert_messages(std::slice::from_ref(record)); |
| 3438 | } |
| 3439 | this.rebuild(false, cx); |
| 3440 | let same_chat = this.conversation_id().as_deref() == Some(&*edited_conversation); |
| 3441 | if failed && !same_chat { |
| 3442 | this.notice = Some("Edit failed".to_owned()); |
| 3443 | cx.notify(); |
| 3444 | } else if failed { |
| 3445 | this.show_main_composer(cx); |
| 3446 | if !this.composer.read(cx).is_editing() { |
| 3447 | this.notice = |
| 3448 | Some("Edit failed: your text is back in the composer".to_owned()); |
| 3449 | this.composer.update(cx, |composer, cx| { |
| 3450 | composer.begin_edit(outgoing.clone(), window, cx) |
| 3451 | }); |
| 3452 | } else { |
| 3453 | this.notice = Some("Edit failed".to_owned()); |
| 3454 | } |
| 3455 | cx.notify(); |
| 3456 | } |
| 3457 | }) |
| 3458 | .ok(); |
| 3459 | }) |
| 3460 | .detach(); |
| 3461 | } |
| 3462 | |
| 3463 | fn copy_text(&self, message_id: &str, cx: &mut Context<Self>) { |
| 3464 | let Some(conversation_id) = self.conversation_id() else { |
| 3465 | return; |
| 3466 | }; |
| 3467 | let record = self |
| 3468 | .app |
| 3469 | .read(cx) |
| 3470 | .store |
| 3471 | .messages_by_id(&conversation_id, &[message_id.to_owned()]) |
| 3472 | .ok() |
| 3473 | .and_then(|mut found| found.remove(message_id)); |
| 3474 | if let Some(record) = record { |
| 3475 | cx.write_to_clipboard(ClipboardItem::new_string(message_text(&record))); |
| 3476 | } |
| 3477 | } |
| 3478 | |
| 3479 | fn refresh_card( |
| 3480 | &mut self, |
| 3481 | key: &str, |
| 3482 | card_index: usize, |
| 3483 | action: ExecuteAction, |
| 3484 | cx: &mut Context<Self>, |
| 3485 | ) { |
| 3486 | let Some(conversation_id) = self.conversation_id() else { |
| 3487 | return; |
| 3488 | }; |
| 3489 | let scope = CardScope::message(&conversation_id, key, card_index); |
| 3490 | self.app.update(cx, |state, cx| { |
| 3491 | state.refresh_card_manually(scope, action, cx) |
| 3492 | }); |
| 3493 | } |
| 3494 | |
| 3495 | pub(super) fn translate_message(&mut self, key: &str, cx: &mut Context<Self>) { |
| 3496 | let Some(conversation_id) = self.conversation_id() else { |
| 3497 | return; |
| 3498 | }; |
| 3499 | self.app.update(cx, |state, cx| { |
| 3500 | state.translate_message(&conversation_id, key, cx) |
| 3501 | }); |
| 3502 | } |
| 3503 | |
| 3504 | fn show_notice(&mut self, message: &str, cx: &mut Context<Self>) { |
| 3505 | self.app.update(cx, |state, cx| { |
| 3506 | state.raise_notice(message.to_owned(), None, cx) |
| 3507 | }); |
| 3508 | } |
| 3509 | |
| 3510 | fn delegate_entries(&self, key: &str, cx: &App) -> (Vec<DelegateEntry>, Option<Action>) { |
| 3511 | let (Some(conversation_id), Some(runs)) = |
| 3512 | (self.conversation_id(), crate::agents::runs_handle(cx)) |
| 3513 | else { |
| 3514 | return (Vec::new(), None); |
| 3515 | }; |
| 3516 | if key.starts_with(PENDING_KEY_PREFIX) || key.starts_with(SCHEDULED_KEY_PREFIX) { |
| 3517 | return (Vec::new(), None); |
| 3518 | } |
| 3519 | let settings = runs.read(cx).settings(); |
| 3520 | let entries: Vec<DelegateEntry> = settings |
| 3521 | .actions |
| 3522 | .iter() |
| 3523 | .map(|action| { |
| 3524 | let request = StartRequest { |
| 3525 | action_id: action.id.clone(), |
| 3526 | conversation_id: conversation_id.clone(), |
| 3527 | message_id: key.to_owned(), |
| 3528 | }; |
| 3529 | let agent_name = settings |
| 3530 | .agents |
| 3531 | .iter() |
| 3532 | .find(|agent| agent.id == action.agent_id) |
| 3533 | .map(|agent| agent.name.clone()) |
| 3534 | .unwrap_or_default(); |
| 3535 | DelegateEntry { |
| 3536 | label: action.label.clone(), |
| 3537 | agent_name, |
| 3538 | action: Rc::new(move |_, cx| crate::agents::delegate(request.clone(), cx)), |
| 3539 | } |
| 3540 | }) |
| 3541 | .collect(); |
| 3542 | let manage_agents = (!entries.is_empty()).then(|| { |
| 3543 | Rc::new(|_: &mut Window, cx: &mut App| crate::agents::open_settings(cx)) as Action |
| 3544 | }); |
| 3545 | (entries, manage_agents) |
| 3546 | } |
| 3547 | |
| 3548 | fn refresh_agent_runs(&mut self, cx: &App) -> (bool, HashSet<String>) { |
| 3549 | let Some(conversation_id) = self.conversation_id() else { |
| 3550 | return (false, HashSet::new()); |
| 3551 | }; |
| 3552 | let Some(runs) = crate::agents::runs_handle(cx) else { |
| 3553 | return (false, HashSet::new()); |
| 3554 | }; |
| 3555 | let mut snapshot: HashMap<String, AgentRun> = HashMap::new(); |
| 3556 | for run in runs.read(cx).for_conversation(&conversation_id) { |
| 3557 | snapshot.insert(run.message_id.clone(), run.clone()); |
| 3558 | } |
| 3559 | let layouts: HashMap<String, (usize, bool)> = snapshot |
| 3560 | .iter() |
| 3561 | .map(|(key, run)| (key.clone(), run.layout_key())) |
| 3562 | .collect(); |
| 3563 | let layout_changed: HashSet<String> = layouts |
| 3564 | .iter() |
| 3565 | .filter(|(key, layout)| self.agent_layouts.get(*key) != Some(layout)) |
| 3566 | .map(|(key, _)| key.clone()) |
| 3567 | .chain( |
| 3568 | self.agent_layouts |
| 3569 | .keys() |
| 3570 | .filter(|key| !layouts.contains_key(*key)) |
| 3571 | .cloned(), |
| 3572 | ) |
| 3573 | .collect(); |
| 3574 | self.agent_layouts = layouts; |
| 3575 | let snapshot_changed = *self.agent_runs.borrow() != snapshot; |
| 3576 | *self.agent_runs.borrow_mut() = snapshot; |
| 3577 | (snapshot_changed, layout_changed) |
| 3578 | } |
| 3579 | |
| 3580 | fn sync_agent_chips(&mut self, cx: &mut Context<Self>) { |
| 3581 | let (snapshot_changed, changed) = self.refresh_agent_runs(cx); |
| 3582 | let any_running = self.agent_runs.borrow().values().any(AgentRun::is_running); |
| 3583 | self.remeasure_rows_of(&changed, cx); |
| 3584 | if snapshot_changed || any_running { |
| 3585 | cx.notify(); |
| 3586 | } |
| 3587 | } |
| 3588 | |
| 3589 | fn remeasure_rows_of(&mut self, keys: &HashSet<String>, cx: &mut Context<Self>) { |
| 3590 | if keys.is_empty() { |
| 3591 | return; |
| 3592 | } |
| 3593 | let indices: Vec<usize> = self |
| 3594 | .rows |
| 3595 | .borrow() |
| 3596 | .iter() |
| 3597 | .enumerate() |
| 3598 | .filter(|(_, row)| row.messages().any(|message| keys.contains(&message.key))) |
| 3599 | .map(|(index, _)| index) |
| 3600 | .collect(); |
| 3601 | self.scroller.update(cx, |scroller, cx| { |
| 3602 | for index in indices { |
| 3603 | scroller.remeasure_items(index..index + 1, cx); |
| 3604 | } |
| 3605 | }); |
| 3606 | } |
| 3607 | |
| 3608 | fn message_menu( |
| 3609 | &self, |
| 3610 | message: &MessageRow, |
| 3611 | mine: HashSet<String>, |
| 3612 | refresh: Option<(usize, ExecuteAction)>, |
| 3613 | view: WeakEntity<Self>, |
| 3614 | cx: &App, |
| 3615 | ) -> MessageMenu { |
| 3616 | let (key, own) = (message.key.as_str(), message.own); |
| 3617 | let translate_label = match message.translation { |
| 3618 | Some(TranslationLine::Translated { |
| 3619 | showing_original: false, |
| 3620 | .. |
| 3621 | }) => "See original", |
| 3622 | Some(TranslationLine::Translated { .. }) => "See translation", |
| 3623 | _ => "Translate", |
| 3624 | }; |
| 3625 | let action = |run: fn(&mut Self, &str, &mut Window, &mut Context<Self>)| -> Action { |
| 3626 | let (view, key) = (view.clone(), key.to_owned()); |
| 3627 | Rc::new(move |window, cx| { |
| 3628 | view.update(cx, |this, cx| { |
| 3629 | this.set_pinned(&key, false, cx); |
| 3630 | run(this, &key, window, cx) |
| 3631 | }) |
| 3632 | .ok(); |
| 3633 | }) |
| 3634 | }; |
| 3635 | let react: PickHandler = { |
| 3636 | let (view, key) = (view.clone(), key.to_owned()); |
| 3637 | Rc::new(move |glyph, _, cx| { |
| 3638 | view.update(cx, |this, cx| this.toggle_reaction(&key, glyph, cx)) |
| 3639 | .ok(); |
| 3640 | }) |
| 3641 | }; |
| 3642 | let hover = { |
| 3643 | let (view, key) = (view.clone(), key.to_owned()); |
| 3644 | Rc::new(move |hovered: bool, cx: &mut App| { |
| 3645 | view.update(cx, |this, cx| { |
| 3646 | this.set_hover(HoverSlot::Toolbar, &key, hovered, cx) |
| 3647 | }) |
| 3648 | .ok(); |
| 3649 | }) |
| 3650 | }; |
| 3651 | let pin = { |
| 3652 | let (view, key) = (view.clone(), key.to_owned()); |
| 3653 | Rc::new(move |open: bool, _: &mut Window, cx: &mut App| { |
| 3654 | view.update(cx, |this, cx| this.set_pinned(&key, open, cx)) |
| 3655 | .ok(); |
| 3656 | }) |
| 3657 | }; |
| 3658 | let refresh_card = refresh.map(|(card_index, refresh_action)| { |
| 3659 | let (view, key) = (view.clone(), key.to_owned()); |
| 3660 | Rc::new(move |_: &mut Window, cx: &mut App| { |
| 3661 | view.update(cx, |this, cx| { |
| 3662 | this.refresh_card(&key, card_index, refresh_action.clone(), cx) |
| 3663 | }) |
| 3664 | .ok(); |
| 3665 | }) as Action |
| 3666 | }); |
| 3667 | let chat_id = self.chat_id(); |
| 3668 | let state = self.app.read(cx); |
| 3669 | let saved = self |
| 3670 | .conversation_id() |
| 3671 | .is_some_and(|conversation_id| state.is_saved(&conversation_id, key)); |
| 3672 | let quick = state.quick_reactions(); |
| 3673 | let in_chat = chat_id.is_some(); |
| 3674 | let pin_conversation_id = self.pin_conversation_id(); |
| 3675 | let pinned = pin_conversation_id |
| 3676 | .as_deref() |
| 3677 | .is_some_and(|conversation_id| state.is_pinned(conversation_id, key)); |
| 3678 | let pinnable = in_chat |
| 3679 | || pin_conversation_id.as_deref().is_some_and(|channel_id| { |
| 3680 | state |
| 3681 | .message_record(channel_id, key) |
| 3682 | .is_some_and(|record| record.reply_to_id.is_none()) |
| 3683 | }); |
| 3684 | let (delegate, manage_agents) = self.delegate_entries(key, cx); |
| 3685 | let post_followed = self.post_follow(key, cx); |
| 3686 | MessageMenu { |
| 3687 | key: key.to_owned(), |
| 3688 | react, |
| 3689 | reply: Some(action(|this, key, window, cx| { |
| 3690 | this.begin_reply(key, window, cx) |
| 3691 | })), |
| 3692 | forward: Some(action(|this, key, _, cx| this.forward_message(key, cx))), |
| 3693 | forward_multiple: in_chat |
| 3694 | .then(|| action(|this, key, _, cx| this.start_forward_selection(key, cx))), |
| 3695 | copy_link: Some(action(|this, key, _, cx| this.copy_link(key, cx))), |
| 3696 | copy: action(|this, key, _, cx| this.copy_text(key, cx)), |
| 3697 | translate: Some(action(|this, key, _, cx| this.translate_message(key, cx))), |
| 3698 | translate_label, |
| 3699 | share_to_outlook: Some(action(|this, key, _, cx| this.share_to_outlook(key, cx))), |
| 3700 | save: Some(action(|this, key, _, cx| this.toggle_saved(key, cx))), |
| 3701 | saved, |
| 3702 | toggle_pinned: pinnable |
| 3703 | .then(|| action(|this, key, window, cx| this.toggle_pinned(key, window, cx))), |
| 3704 | pinned, |
| 3705 | toggle_follow: post_followed |
| 3706 | .map(|_| action(|this, key, _, cx| this.toggle_post_follow(key, cx))), |
| 3707 | followed: post_followed.unwrap_or_default(), |
| 3708 | edit: own.then(|| action(|this, key, window, cx| this.begin_edit(key, window, cx))), |
| 3709 | delete: own.then(|| action(|this, key, _, cx| this.delete(key, cx))), |
| 3710 | mark_unread: (in_chat && !own) |
| 3711 | .then(|| action(|this, key, _, cx| this.mark_unread_from(key, cx))), |
| 3712 | refresh_card, |
| 3713 | delegate, |
| 3714 | manage_agents, |
| 3715 | pin, |
| 3716 | hover, |
| 3717 | picker: self.picker.clone(), |
| 3718 | quick, |
| 3719 | mine, |
| 3720 | done: action(|this, key, _, cx| this.suppress_toolbar(key, cx)), |
| 3721 | } |
| 3722 | } |
| 3723 | |
| 3724 | fn retry(&mut self, key: &str, window: &mut Window, cx: &mut Context<Self>) { |
| 3725 | let Some(outgoing) = self.pending_outgoing.remove(key) else { |
| 3726 | return; |
| 3727 | }; |
| 3728 | let reply_root = self |
| 3729 | .pending |
| 3730 | .iter() |
| 3731 | .find(|row| row.key == key) |
| 3732 | .and_then(|row| row.reply_root.clone()); |
| 3733 | self.pending.retain(|row| row.key != key); |
| 3734 | if self.draft_active { |
| 3735 | self.send_new_chat(outgoing, window, cx); |
| 3736 | } else { |
| 3737 | self.send_as(key.to_owned(), outgoing, reply_root, window, cx); |
| 3738 | } |
| 3739 | } |
| 3740 | |
| 3741 | fn finish_send( |
| 3742 | &mut self, |
| 3743 | key: &str, |
| 3744 | result: Result<teams_core::Result<Delivered>, tokio::sync::oneshot::error::RecvError>, |
| 3745 | cx: &mut Context<Self>, |
| 3746 | ) { |
| 3747 | if matches!(result, Ok(Ok(Delivered::WithoutBanner))) { |
| 3748 | self.notice = Some(POSTED_WITHOUT_BANNER.to_owned()); |
| 3749 | } |
| 3750 | if let Ok(Ok(Delivered::Complete { |
| 3751 | message_id: Some(message_id), |
| 3752 | })) = &result |
| 3753 | { |
| 3754 | self.keep_sent_previews(key, message_id); |
| 3755 | } |
| 3756 | let failure = match result { |
| 3757 | Ok(Ok(_)) => None, |
| 3758 | Ok(Err(error)) => Some(short_error(&error)), |
| 3759 | Err(error) => Some(short_error(&error)), |
| 3760 | }; |
| 3761 | let store = self.app.read(cx).store.clone(); |
| 3762 | match failure { |
| 3763 | None => { |
| 3764 | store.delete_outbox(key).ok(); |
| 3765 | self.pending.retain(|row| row.key != key); |
| 3766 | self.pending_outgoing.remove(key); |
| 3767 | } |
| 3768 | Some(error) => { |
| 3769 | store.mark_outbox_failed(key, &error).ok(); |
| 3770 | if let Some(row) = self.pending.iter_mut().find(|row| row.key == key) { |
| 3771 | row.delivery = Delivery::Failed(error.clone()); |
| 3772 | self.notice = Some(format!("Not sent: {error}")); |
| 3773 | } |
| 3774 | } |
| 3775 | } |
| 3776 | self.app |
| 3777 | .update(cx, |state, cx| state.refresh_local_previews(cx)); |
| 3778 | self.rebuild(false, cx); |
| 3779 | } |
| 3780 | |
| 3781 | fn keep_sent_previews(&mut self, pending_key: &str, message_id: &str) { |
| 3782 | let Some(row) = self.pending.iter().find(|row| row.key == pending_key) else { |
| 3783 | return; |
| 3784 | }; |
| 3785 | if row.local_images.is_empty() { |
| 3786 | return; |
| 3787 | } |
| 3788 | self.sent_previews |
| 3789 | .push_back((message_id.to_owned(), row.local_images.clone())); |
| 3790 | if self.sent_previews.len() > SENT_PREVIEWS_KEPT { |
| 3791 | self.sent_previews.pop_front(); |
| 3792 | } |
| 3793 | } |
| 3794 | |
| 3795 | fn render_drop_overlay(&self, cx: &mut Context<Self>) -> impl IntoElement { |
| 3796 | let target = match self.current.as_ref().map(|current| ¤t.selection) { |
| 3797 | Some(Selection::Chat(_)) => DropTarget::Chat, |
| 3798 | Some(Selection::Channel(_)) if self.shared_active() => DropTarget::Library, |
| 3799 | Some(Selection::Channel(_)) => DropTarget::Channel, |
| 3800 | None => DropTarget::NewChat, |
| 3801 | }; |
| 3802 | let title = self |
| 3803 | .current |
| 3804 | .as_ref() |
| 3805 | .map(|current| current.title.as_str()) |
| 3806 | .unwrap_or_default(); |
| 3807 | let (heading, detail) = drop_overlay_text(title, target, self.drag_file_count.max(1)); |
| 3808 | let composer = self.composer.clone(); |
| 3809 | div() |
| 3810 | .id("drop-overlay") |
| 3811 | .absolute() |
| 3812 | .top_0() |
| 3813 | .left_0() |
| 3814 | .size_full() |
| 3815 | .opacity(0.) |
| 3816 | .bg(theme::drop_background()) |
| 3817 | .drag_over::<ExternalPaths>(move |style, _, _, cx| { |
| 3818 | if composer.read(cx).is_editing() { |
| 3819 | style |
| 3820 | } else { |
| 3821 | style.opacity(1.) |
| 3822 | } |
| 3823 | }) |
| 3824 | .on_drag_move( |
| 3825 | cx.listener(|this, event: &DragMoveEvent<ExternalPaths>, _, cx| { |
| 3826 | let count = event.drag(cx).paths().len(); |
| 3827 | if this.drag_file_count != count { |
| 3828 | this.drag_file_count = count; |
| 3829 | cx.notify(); |
| 3830 | } |
| 3831 | }), |
| 3832 | ) |
| 3833 | .on_drop(cx.listener(|this, dropped: &ExternalPaths, window, cx| { |
| 3834 | let paths = dropped.paths().to_vec(); |
| 3835 | if this.shared_active() { |
| 3836 | this.upload_paths(paths, cx); |
| 3837 | return; |
| 3838 | } |
| 3839 | this.composer |
| 3840 | .update(cx, |composer, cx| composer.add_paths(paths, window, cx)); |
| 3841 | })) |
| 3842 | .child( |
| 3843 | div() |
| 3844 | .absolute() |
| 3845 | .inset(px(12.)) |
| 3846 | .rounded(px(12.)) |
| 3847 | .border_2() |
| 3848 | .border_color(theme::accent()) |
| 3849 | .flex() |
| 3850 | .items_center() |
| 3851 | .justify_center() |
| 3852 | .child( |
| 3853 | v_flex() |
| 3854 | .items_center() |
| 3855 | .gap(px(6.)) |
| 3856 | .px(px(24.)) |
| 3857 | .py(px(16.)) |
| 3858 | .rounded(px(12.)) |
| 3859 | .bg(black().opacity(0.55)) |
| 3860 | .child(symbol("upload", 28., theme::accent_text())) |
| 3861 | .child( |
| 3862 | div() |
| 3863 | .text_size(px(15.)) |
| 3864 | .font_weight(FontWeight::SEMIBOLD) |
| 3865 | .text_color(theme::text_strong()) |
| 3866 | .child(heading), |
| 3867 | ) |
| 3868 | .child( |
| 3869 | div() |
| 3870 | .text_size(px(12.)) |
| 3871 | .text_color(theme::text_muted()) |
| 3872 | .child(detail), |
| 3873 | ), |
| 3874 | ), |
| 3875 | ) |
| 3876 | } |
| 3877 | |
| 3878 | fn render_draft_error(&self, cx: &mut Context<Self>) -> Option<Div> { |
| 3879 | let message = self.draft_error.clone()?; |
| 3880 | let failed_key = self |
| 3881 | .pending |
| 3882 | .iter() |
| 3883 | .find(|row| matches!(row.delivery, Delivery::Failed(_))) |
| 3884 | .map(|row| row.key.clone())?; |
| 3885 | Some( |
| 3886 | h_flex() |
| 3887 | .w_full() |
| 3888 | .flex_none() |
| 3889 | .px(px(20.)) |
| 3890 | .py(px(4.)) |
| 3891 | .gap(px(8.)) |
| 3892 | .items_center() |
| 3893 | .text_size(px(12.)) |
| 3894 | .text_color(theme::red_soft()) |
| 3895 | .child(message) |
| 3896 | .child( |
| 3897 | Button::new("retry-new-chat") |
| 3898 | .ghost() |
| 3899 | .compact() |
| 3900 | .label(tr("common.retry")) |
| 3901 | .on_click(cx.listener(move |this, _, window, cx| { |
| 3902 | this.retry(&failed_key, window, cx) |
| 3903 | })), |
| 3904 | ), |
| 3905 | ) |
| 3906 | } |
| 3907 | |
| 3908 | pub fn open_find(&mut self, window: &mut Window, cx: &mut Context<Self>) { |
| 3909 | if self.pinned || self.draft_active { |
| 3910 | return; |
| 3911 | } |
| 3912 | let Some(selection) = self |
| 3913 | .current |
| 3914 | .as_ref() |
| 3915 | .map(|current| current.selection.clone()) |
| 3916 | else { |
| 3917 | return; |
| 3918 | }; |
| 3919 | self.channel_details_open = false; |
| 3920 | self.close_copilot(); |
| 3921 | if let Some(panel) = &self.find_panel { |
| 3922 | panel.update(cx, |panel, cx| panel.focus(window, cx)); |
| 3923 | return; |
| 3924 | } |
| 3925 | let app = self.app.clone(); |
| 3926 | let panel = cx.new(|cx| FindPanel::new(app, selection.clone(), window, cx)); |
| 3927 | let subscription = cx.subscribe_in( |
| 3928 | &panel, |
| 3929 | window, |
| 3930 | move |this, _, event: &FindPanelEvent, window, cx| match event { |
| 3931 | FindPanelEvent::Close => this.close_find(window, cx), |
| 3932 | FindPanelEvent::Jump(message_id) => { |
| 3933 | let (selection, message_id) = (selection.clone(), message_id.clone()); |
| 3934 | this.app.update(cx, |state, cx| { |
| 3935 | state.jump_to_message(selection, message_id, cx) |
| 3936 | }); |
| 3937 | } |
| 3938 | }, |
| 3939 | ); |
| 3940 | panel.update(cx, |panel, cx| panel.focus(window, cx)); |
| 3941 | self.find_panel = Some(panel); |
| 3942 | self.find_subscription = Some(subscription); |
| 3943 | cx.notify(); |
| 3944 | } |
| 3945 | |
| 3946 | fn close_find(&mut self, window: &mut Window, cx: &mut Context<Self>) { |
| 3947 | self.find_panel = None; |
| 3948 | self.find_subscription = None; |
| 3949 | self.composer |
| 3950 | .update(cx, |composer, cx| composer.focus(window, cx)); |
| 3951 | cx.notify(); |
| 3952 | } |
| 3953 | |
| 3954 | fn header_width(&self, window: &Window) -> f32 { |
| 3955 | match self.header_width.get() { |
| 3956 | width if width > 0. => width, |
| 3957 | _ => { |
| 3958 | f32::from(window.viewport_size().width) |
| 3959 | - RAIL_WIDTH |
| 3960 | - SIDEBAR_WIDTH |
| 3961 | - 2. * ISLAND_GAP |
| 3962 | } |
| 3963 | } |
| 3964 | } |
| 3965 | |
| 3966 | fn tabs_below_title(&self, window: &Window) -> bool { |
| 3967 | self.header_width(window) < TABS_INLINE_MIN_WIDTH |
| 3968 | } |
| 3969 | |
| 3970 | fn render_header( |
| 3971 | &self, |
| 3972 | current: &Current, |
| 3973 | tabs: Option<AnyElement>, |
| 3974 | cx: &mut Context<Self>, |
| 3975 | ) -> impl IntoElement { |
| 3976 | let app = self.app.read(cx); |
| 3977 | let directory = &app.directory; |
| 3978 | let me = directory.me.as_ref(); |
| 3979 | let mut lead: Option<AnyElement> = None; |
| 3980 | let mut subline = String::new(); |
| 3981 | let mut stack: Option<AnyElement> = None; |
| 3982 | let mut rename: Option<AnyElement> = None; |
| 3983 | let mut names_title: Option<Div> = None; |
| 3984 | let mut header_title = current.title.clone(); |
| 3985 | match ¤t.selection { |
| 3986 | Selection::Chat(chat_id) => { |
| 3987 | if let Some(chat) = app.sidebar.chats.iter().find(|chat| &chat.id == chat_id) { |
| 3988 | let faces = others(chat, me); |
| 3989 | let face = |(user_id, name): (Option<String>, String)| Face { user_id, name }; |
| 3990 | let first = faces.first().cloned().map(face); |
| 3991 | if let Some(bot) = one_on_one_bot(chat, me) { |
| 3992 | lead = Some(bot_tile(directory, &face(bot), 36.)); |
| 3993 | } else if is_one_on_one(chat) || faces.len() < 2 { |
| 3994 | let name = first |
| 3995 | .as_ref() |
| 3996 | .map_or(current.title.as_str(), |face| face.name.as_str()); |
| 3997 | let user_id = first.as_ref().and_then(|face| face.user_id.as_deref()); |
| 3998 | let avatar = person_avatar(directory, user_id, name, 36.); |
| 3999 | let presence = user_id.map(|id| directory.presence_of(id)); |
| 4000 | let avatar = match presence { |
| 4001 | Some(kind) => with_presence(avatar, kind, 36., theme::surface()) |
| 4002 | .into_any_element(), |
| 4003 | None => avatar, |
| 4004 | }; |
| 4005 | lead = Some( |
| 4006 | opens_profile(div().id("header-avatar").flex_none(), user_id) |
| 4007 | .child(avatar) |
| 4008 | .into_any_element(), |
| 4009 | ); |
| 4010 | subline = match (user_id, presence) { |
| 4011 | (Some(id), Some(presence)) => { |
| 4012 | directory.status_label(id, presence).to_owned() |
| 4013 | } |
| 4014 | _ => String::new(), |
| 4015 | }; |
| 4016 | if is_one_on_one(chat) && !is_notes_chat(chat_id) { |
| 4017 | stack = Some(add_people_button(&self.app, chat_id)); |
| 4018 | } |
| 4019 | } else { |
| 4020 | if chat.kind == GROUP_KIND |
| 4021 | && me.is_some_and(|me| { |
| 4022 | chat.members |
| 4023 | .iter() |
| 4024 | .any(|member| member.user_id.as_deref() == Some(&me.user_id)) |
| 4025 | }) |
| 4026 | { |
| 4027 | rename = Some(group_name_button( |
| 4028 | &self.app, |
| 4029 | &self.group_name, |
| 4030 | chat_id, |
| 4031 | &chat.title, |
| 4032 | )); |
| 4033 | } |
| 4034 | if chat.title.trim().is_empty() { |
| 4035 | names_title = Some(chat_header_actions::group_title(directory, &faces)); |
| 4036 | } |
| 4037 | let pair = AvatarSpec::Pair(face(faces[0].clone()), face(faces[1].clone())); |
| 4038 | lead = Some(spec_avatar(directory, &pair, 36., theme::surface())); |
| 4039 | stack = Some(members_popover( |
| 4040 | &self.app, |
| 4041 | chat_id, |
| 4042 | chat_header_actions::member_count(chat.members.len()) |
| 4043 | .debug_selector(|| MEMBERS_BUTTON.into()), |
| 4044 | )); |
| 4045 | } |
| 4046 | } |
| 4047 | } |
| 4048 | Selection::Channel(channel_id) => { |
| 4049 | let team = app.sidebar.teams.iter().find(|entry| { |
| 4050 | entry |
| 4051 | .channels |
| 4052 | .iter() |
| 4053 | .any(|channel| &channel.id == channel_id) |
| 4054 | }); |
| 4055 | let (name, key) = team |
| 4056 | .map_or((current.title.as_str(), current.title.as_str()), |entry| { |
| 4057 | (entry.team.name.as_str(), entry.team.id.as_str()) |
| 4058 | }); |
| 4059 | let photo = team.and_then(|entry| { |
| 4060 | app.directory |
| 4061 | .avatar(&teams_core::team_avatar_key(&entry.team.id)) |
| 4062 | }); |
| 4063 | lead = Some(team_avatar(photo, name, key, 36., 9.)); |
| 4064 | if let Some(channel) = team.and_then(|entry| { |
| 4065 | entry |
| 4066 | .channels |
| 4067 | .iter() |
| 4068 | .find(|channel| &channel.id == channel_id) |
| 4069 | }) { |
| 4070 | header_title = channel.name.clone(); |
| 4071 | } |
| 4072 | } |
| 4073 | } |
| 4074 | let mut header = h_flex() |
| 4075 | .w_full() |
| 4076 | .h(px(HEADER_HEIGHT)) |
| 4077 | .flex_none() |
| 4078 | .px(px(HEADER_PADDING)) |
| 4079 | .gap(px(HEADER_GAP)) |
| 4080 | .items_center(); |
| 4081 | if matches!(current.mode, ViewMode::Thread(_)) { |
| 4082 | header = header.child( |
| 4083 | h_flex() |
| 4084 | .id("back-to-channel") |
| 4085 | .flex_none() |
| 4086 | .gap(px(2.)) |
| 4087 | .pl(px(4.)) |
| 4088 | .pr(px(8.)) |
| 4089 | .py(px(4.)) |
| 4090 | .items_center() |
| 4091 | .rounded(px(6.)) |
| 4092 | .cursor_pointer() |
| 4093 | .text_size(px(13.)) |
| 4094 | .font_weight(FontWeight::SEMIBOLD) |
| 4095 | .text_color(theme::accent_text()) |
| 4096 | .hover(|button| button.bg(theme::row_hover())) |
| 4097 | .on_click(cx.listener(|this, _, _, cx| this.back_to_threads(cx))) |
| 4098 | .child(icon(IconName::ChevronLeft, 16., theme::accent_text())) |
| 4099 | .child("Back to channel"), |
| 4100 | ); |
| 4101 | } |
| 4102 | let channel_note = match current.mode { |
| 4103 | ViewMode::Flat => None, |
| 4104 | ViewMode::ThreadList => Some("Posts"), |
| 4105 | ViewMode::Thread(_) => Some("Conversation"), |
| 4106 | }; |
| 4107 | if let Some(note) = channel_note { |
| 4108 | subline = note.to_owned(); |
| 4109 | } |
| 4110 | let call_controls = if self.pinned && !self.popped_out { |
| 4111 | Vec::new() |
| 4112 | } else { |
| 4113 | header_call_controls(&self.app, app, ¤t.selection, self.header_overflows()) |
| 4114 | }; |
| 4115 | let sync = ¤t.sync; |
| 4116 | let loading_window = current.window_request.is_some(); |
| 4117 | if sync.failed { |
| 4118 | subline = "Not up to date".to_owned(); |
| 4119 | } else if loading_window { |
| 4120 | subline = "Loading message ...".to_owned(); |
| 4121 | } else if sync.shown_at.is_some() { |
| 4122 | subline = if sync.slow { |
| 4123 | "Taking longer than usual ..." |
| 4124 | } else { |
| 4125 | "Loading messages ..." |
| 4126 | } |
| 4127 | .to_owned(); |
| 4128 | } |
| 4129 | let inline_tabs = tabs.map(|tabs| h_flex().h_full().flex_1().child(tabs)); |
| 4130 | let overflow = self.header_overflows(); |
| 4131 | let divided = !overflow |
| 4132 | && matches!(current.selection, Selection::Chat(_)) |
| 4133 | && (!call_controls.is_empty() || stack.is_some()); |
| 4134 | header |
| 4135 | .relative() |
| 4136 | .child(record_width(&self.header_width)) |
| 4137 | .when(sync.shown_at.is_some() || loading_window, |header| { |
| 4138 | header.child(progress_bar()) |
| 4139 | }) |
| 4140 | .when(overflow, |header| header.gap(px(HEADER_COMPACT_GAP))) |
| 4141 | .when(!overflow, |header| header.children(lead)) |
| 4142 | .child( |
| 4143 | v_flex() |
| 4144 | .map(|column| match inline_tabs { |
| 4145 | Some(_) => column.flex_shrink(1.).max_w(px(HEADER_TITLE_MAX_WIDTH)), |
| 4146 | None => column.flex_1(), |
| 4147 | }) |
| 4148 | .min_w_0() |
| 4149 | .overflow_hidden() |
| 4150 | .gap(px(1.)) |
| 4151 | .child( |
| 4152 | h_flex() |
| 4153 | .min_w_0() |
| 4154 | .gap(px(2.)) |
| 4155 | .items_center() |
| 4156 | .child( |
| 4157 | div() |
| 4158 | .truncate() |
| 4159 | .text_size(px(15.)) |
| 4160 | .font_weight(FontWeight::SEMIBOLD) |
| 4161 | .text_color(theme::text()) |
| 4162 | .map(|title| match names_title { |
| 4163 | Some(names) => title.child(names), |
| 4164 | None => title.child(header_title), |
| 4165 | }), |
| 4166 | ) |
| 4167 | .children(rename), |
| 4168 | ) |
| 4169 | .when(!subline.is_empty(), |column| { |
| 4170 | column.child( |
| 4171 | div() |
| 4172 | .text_size(px(12.)) |
| 4173 | .text_color(theme::text_muted()) |
| 4174 | .child(subline), |
| 4175 | ) |
| 4176 | }), |
| 4177 | ) |
| 4178 | .children(inline_tabs) |
| 4179 | .children(call_controls) |
| 4180 | .children(stack) |
| 4181 | .when(divided, |header| header.child(header_divider())) |
| 4182 | .when(!overflow, |header| { |
| 4183 | header |
| 4184 | .children(self.copilot_button(cx)) |
| 4185 | .children(self.chat_find_button(cx)) |
| 4186 | }) |
| 4187 | .children(self.channel_find_button(cx)) |
| 4188 | .when(!overflow, |header| { |
| 4189 | header.children(self.channel_details_button(cx)) |
| 4190 | }) |
| 4191 | .children(self.channel_more_button(cx)) |
| 4192 | .children(self.chat_more_button(overflow, cx)) |
| 4193 | .children(self.pop_in_button(current)) |
| 4194 | } |
| 4195 | |
| 4196 | fn pop_in_button(&self, current: &Current) -> Option<AnyElement> { |
| 4197 | let Selection::Chat(chat_id) = ¤t.selection else { |
| 4198 | return None; |
| 4199 | }; |
| 4200 | if !self.popped_out { |
| 4201 | return None; |
| 4202 | } |
| 4203 | let (app, chat_id) = (self.app.clone(), chat_id.clone()); |
| 4204 | Some( |
| 4205 | Button::new("chat-pop-in") |
| 4206 | .icon(IconName::SquareArrowDownLeft) |
| 4207 | .ghost() |
| 4208 | .size(px(POP_IN_BUTTON_SIZE)) |
| 4209 | .rounded(px(8.)) |
| 4210 | .tooltip(chat_pop_out::POP_IN_TOOLTIP) |
| 4211 | .on_click(move |_, window, cx| { |
| 4212 | chat_pop_out::return_to_main_window(&app, &chat_id, window, cx) |
| 4213 | }) |
| 4214 | .into_any_element(), |
| 4215 | ) |
| 4216 | } |
| 4217 | } |
| 4218 | |
| 4219 | const GROUP_KIND: &str = "group"; |
| 4220 | const HEADER_DIVIDER_HEIGHT: f32 = 20.; |
| 4221 | |
| 4222 | fn header_divider() -> Div { |
| 4223 | div() |
| 4224 | .w(px(1.)) |
| 4225 | .h(px(HEADER_DIVIDER_HEIGHT)) |
| 4226 | .mx(px(4.)) |
| 4227 | .flex_none() |
| 4228 | .bg(theme::border()) |
| 4229 | } |
| 4230 | |
| 4231 | impl Render for ConversationView { |
| 4232 | fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement { |
| 4233 | let background = theme::surface(); |
| 4234 | let mut root = v_flex() |
| 4235 | .when(!self.pinned, |root| { |
| 4236 | root.debug_selector(|| "shell/islands/main".into()) |
| 4237 | }) |
| 4238 | .flex_1() |
| 4239 | .w_full() |
| 4240 | .min_w_0() |
| 4241 | .h_full() |
| 4242 | .rounded(px(10.)) |
| 4243 | .bg(background) |
| 4244 | .overflow_hidden() |
| 4245 | .track_focus(&self.focus_handle); |
| 4246 | let drafting = self.draft_active; |
| 4247 | if self.current.is_none() && !drafting { |
| 4248 | return root |
| 4249 | .items_center() |
| 4250 | .justify_center() |
| 4251 | .text_color(theme::text_muted()) |
| 4252 | .child("Select a chat or channel") |
| 4253 | .into_any_element(); |
| 4254 | } |
| 4255 | let in_feed = self.in_feed(); |
| 4256 | let detached = self.is_detached(); |
| 4257 | let rows_seen = self.feed_rows_seen.replace(usize::MAX); |
| 4258 | if in_feed && rows_seen != usize::MAX { |
| 4259 | self.feed_at_top = rows_seen == 0; |
| 4260 | } |
| 4261 | let forward_selection = self.active_forward_selection(cx); |
| 4262 | let rows = self.rows.clone(); |
| 4263 | let rows_seen = self.feed_rows_seen.clone(); |
| 4264 | let env = RenderEnv { |
| 4265 | view: cx.weak_entity(), |
| 4266 | app: self.app.clone(), |
| 4267 | highlighted: self.highlighted_message.clone(), |
| 4268 | drafting, |
| 4269 | reply_editor: self.reply_root.clone().zip(self.reply_composer.clone()), |
| 4270 | timeline: self.timeline_bounds.clone(), |
| 4271 | agent_runs: self.agent_runs.clone(), |
| 4272 | reaction_proposals: self.reaction_proposals.clone(), |
| 4273 | forward_selection: forward_selection.clone(), |
| 4274 | appearing: self.appear_tracker.entering(Instant::now()), |
| 4275 | }; |
| 4276 | let pin_banner = self |
| 4277 | .render_pin_banner(cx) |
| 4278 | .filter(|_| !drafting && !self.shared_active() && !self.recap_active()); |
| 4279 | let timeline_top_inset = pin_banner_inset(pin_banner.is_some()); |
| 4280 | let comfy = self.app.read(cx).account_chat_settings().message_density |
| 4281 | != teams_core::MessageDensity::Compact; |
| 4282 | let typing_line = self.render_typing_line(cx); |
| 4283 | let typing_offset = { |
| 4284 | let scroller = self.scroller.read(cx); |
| 4285 | let last_row = scroller.item_count().checked_sub(1); |
| 4286 | typing_bottom_offset( |
| 4287 | self.timeline_bounds.get(), |
| 4288 | last_row.and_then(|index| scroller.item_bounds(index)), |
| 4289 | ) |
| 4290 | }; |
| 4291 | let scroller = MessageScroller::new( |
| 4292 | "messages", |
| 4293 | self.scroller.clone(), |
| 4294 | move |index, window, cx| { |
| 4295 | rows_seen.set(rows_seen.get().min(index)); |
| 4296 | let rows = rows.borrow(); |
| 4297 | let row = match rows.get(index) { |
| 4298 | Some(Row::Message(message)) => { |
| 4299 | let actions = env.message_actions(message, window, cx); |
| 4300 | let state = env.app.read(cx); |
| 4301 | render_message_row(message, index, actions, &state.directory, cx) |
| 4302 | } |
| 4303 | Some(Row::Post(post)) => { |
| 4304 | let actions = env.post_actions(post, window, cx); |
| 4305 | let state = env.app.read(cx); |
| 4306 | render_post(post, index, actions, &state.directory, cx) |
| 4307 | } |
| 4308 | Some(Row::Skeleton(skeleton)) => render_skeleton_row(skeleton, index), |
| 4309 | Some(Row::LoadOlder) => { |
| 4310 | let view = env.view.clone(); |
| 4311 | cx.defer(move |cx| { |
| 4312 | view.update(cx, |this, cx| this.request_older(cx)).ok(); |
| 4313 | }); |
| 4314 | h_flex() |
| 4315 | .w_full() |
| 4316 | .py_2() |
| 4317 | .gap(px(6.)) |
| 4318 | .justify_center() |
| 4319 | .text_size(px(12.)) |
| 4320 | .text_color(theme::text_muted()) |
| 4321 | .child(icon(IconName::Loader, 12., theme::text_muted())) |
| 4322 | .child("Loading older messages") |
| 4323 | .into_any_element() |
| 4324 | } |
| 4325 | Some(Row::LoadNewer) => { |
| 4326 | let view = env.view.clone(); |
| 4327 | cx.defer(move |cx| { |
| 4328 | view.update(cx, |this, cx| { |
| 4329 | this.request_window_page(WindowDirection::Forward, cx) |
| 4330 | }) |
| 4331 | .ok(); |
| 4332 | }); |
| 4333 | h_flex() |
| 4334 | .w_full() |
| 4335 | .py_2() |
| 4336 | .gap(px(6.)) |
| 4337 | .justify_center() |
| 4338 | .text_size(px(12.)) |
| 4339 | .text_color(theme::text_muted()) |
| 4340 | .child(icon(IconName::Loader, 12., theme::text_muted())) |
| 4341 | .child("Loading newer messages") |
| 4342 | .into_any_element() |
| 4343 | } |
| 4344 | Some(Row::Start(start)) => { |
| 4345 | let state = env.app.read(cx); |
| 4346 | v_flex() |
| 4347 | .w_full() |
| 4348 | .items_center() |
| 4349 | .gap(px(8.)) |
| 4350 | .pt(px(32.)) |
| 4351 | .pb(px(16.)) |
| 4352 | .child(match start.user_id.as_deref() { |
| 4353 | Some(user_id) if is_bot_id(user_id) => bot_tile( |
| 4354 | &state.directory, |
| 4355 | &Face { |
| 4356 | user_id: Some(user_id.to_owned()), |
| 4357 | name: start.title.clone(), |
| 4358 | }, |
| 4359 | 56., |
| 4360 | ), |
| 4361 | user_id => person_avatar( |
| 4362 | &state.directory, |
| 4363 | user_id, |
| 4364 | &start.avatar_name, |
| 4365 | 56., |
| 4366 | ), |
| 4367 | }) |
| 4368 | .child( |
| 4369 | div() |
| 4370 | .text_size(px(16.)) |
| 4371 | .font_weight(FontWeight::SEMIBOLD) |
| 4372 | .text_color(theme::text()) |
| 4373 | .child(start.title.clone()), |
| 4374 | ) |
| 4375 | .child( |
| 4376 | div() |
| 4377 | .text_size(px(12.)) |
| 4378 | .text_color(theme::text_muted()) |
| 4379 | .child("This is the beginning of the conversation"), |
| 4380 | ) |
| 4381 | .into_any_element() |
| 4382 | } |
| 4383 | None => div().into_any_element(), |
| 4384 | }; |
| 4385 | runway(row, comfy) |
| 4386 | }, |
| 4387 | ) |
| 4388 | .with_row_style(StyleRefinement::default().px(px(0.)).pb(px(0.))) |
| 4389 | .wheel_mask(false) |
| 4390 | .jump_button(!in_feed) |
| 4391 | .force_jump_button(detached) |
| 4392 | .with_jump_button_label(jump::JUMP_TO_NEWEST_LABEL) |
| 4393 | .with_jump_button_renderer(|button| { |
| 4394 | button |
| 4395 | .label(jump::JUMP_TO_NEWEST_LABEL) |
| 4396 | .tooltip(jump::jump_to_newest_tooltip()) |
| 4397 | .border_0() |
| 4398 | .bg(theme::accent()) |
| 4399 | .text_color(theme::on_accent()) |
| 4400 | }) |
| 4401 | .when(detached, |scroller| { |
| 4402 | let view = cx.weak_entity(); |
| 4403 | scroller.on_jump(move |_, cx| { |
| 4404 | view.update(cx, |this, cx| this.leave_detached(cx)).ok(); |
| 4405 | }) |
| 4406 | }) |
| 4407 | .with_list_style(StyleRefinement::default().pb(px(if typing_line.is_some() { |
| 4408 | TYPING_TOP_GAP + TYPING_BUBBLE_HEIGHT + TYPING_BOTTOM_GAP |
| 4409 | } else if in_feed { |
| 4410 | 0. |
| 4411 | } else { |
| 4412 | COMPOSER_TOP_GAP |
| 4413 | }))) |
| 4414 | .with_top_inset(timeline_top_inset); |
| 4415 | |
| 4416 | root = match self.current.as_ref().filter(|_| !drafting) { |
| 4417 | Some(current) => { |
| 4418 | let tab_bar = self |
| 4419 | .render_tab_bar(window, cx) |
| 4420 | .or_else(|| self.render_chat_tab_bar(cx)); |
| 4421 | let (inline_tabs, tabs_row) = match tab_bar { |
| 4422 | Some(tabs) if self.tabs_below_title(window) => (None, Some(tabs)), |
| 4423 | tabs => (tabs, None), |
| 4424 | }; |
| 4425 | root.child(self.render_header(current, inline_tabs, cx)) |
| 4426 | .children(tabs_row.map(|tabs| { |
| 4427 | h_flex() |
| 4428 | .w_full() |
| 4429 | .h(px(TABS_ROW_HEIGHT)) |
| 4430 | .flex_none() |
| 4431 | .px(px(HEADER_PADDING)) |
| 4432 | .child(tabs) |
| 4433 | })) |
| 4434 | .child(edge_divider(HEADER_DIVIDER)) |
| 4435 | } |
| 4436 | None => root.child(self.draft.clone()), |
| 4437 | }; |
| 4438 | let sync_failed = self |
| 4439 | .current |
| 4440 | .as_ref() |
| 4441 | .is_some_and(|current| current.sync.failed); |
| 4442 | if let Some(notice) = &self.notice { |
| 4443 | root = root.child( |
| 4444 | h_flex() |
| 4445 | .px(px(20.)) |
| 4446 | .py_1() |
| 4447 | .gap(px(8.)) |
| 4448 | .items_center() |
| 4449 | .text_xs() |
| 4450 | .bg(theme::amber().opacity(0.15)) |
| 4451 | .text_color(theme::amber()) |
| 4452 | .child(div().flex_1().min_w_0().child(notice.clone())) |
| 4453 | .when(sync_failed, |bar| { |
| 4454 | bar.child( |
| 4455 | Button::new("retry-fetch") |
| 4456 | .ghost() |
| 4457 | .compact() |
| 4458 | .label(tr("common.retry")) |
| 4459 | .on_click(cx.listener(|this, _, _, cx| this.retry_fetch(cx))), |
| 4460 | ) |
| 4461 | }), |
| 4462 | ); |
| 4463 | } |
| 4464 | let recap_active = self.recap_active(); |
| 4465 | let empty_channel = in_feed && self.rows.borrow().is_empty(); |
| 4466 | let new_post = self.render_new_post(cx); |
| 4467 | self.watch_feed_scroll(cx); |
| 4468 | let feed_scrolled = new_post.is_some() && self.scroller.read(cx).is_scrolled_from_top(); |
| 4469 | let body = if drafting && self.current.is_none() && self.rows.borrow().is_empty() { |
| 4470 | let draft = self.draft.read(cx); |
| 4471 | let group_name = draft.group_name(cx); |
| 4472 | draft.render_empty(&self.app.read(cx).directory, &group_name) |
| 4473 | } else if self.shared_active() { |
| 4474 | self.render_shared(cx) |
| 4475 | } else if self.recap_active() { |
| 4476 | self.render_recap(cx) |
| 4477 | } else if let Some(start) = self.notes_space() { |
| 4478 | notes_space::render_notes_space(&self.app.read(cx).directory, &start) |
| 4479 | } else if empty_channel { |
| 4480 | div() |
| 4481 | .size_full() |
| 4482 | .flex() |
| 4483 | .items_center() |
| 4484 | .justify_center() |
| 4485 | .text_size(px(13.)) |
| 4486 | .text_color(theme::text_muted()) |
| 4487 | .child("No posts in this channel yet") |
| 4488 | .into_any_element() |
| 4489 | } else { |
| 4490 | scroller.into_any_element() |
| 4491 | }; |
| 4492 | let channel_details = self.render_channel_details(cx); |
| 4493 | let copilot_island = self.render_copilot(cx); |
| 4494 | let find_panel = self.find_panel.clone().filter(|_| !drafting); |
| 4495 | let find_overlays = self.header_width(window) < FIND_PANEL_WIDTH + FIND_MIN_TIMELINE_WIDTH; |
| 4496 | let content = v_flex() |
| 4497 | .relative() |
| 4498 | .flex_1() |
| 4499 | .min_w_0() |
| 4500 | .min_h_0() |
| 4501 | .children(new_post.map(|post| runway(post, comfy))) |
| 4502 | .child( |
| 4503 | div() |
| 4504 | .debug_selector(|| TIMELINE_TAG.into()) |
| 4505 | .relative() |
| 4506 | .flex_1() |
| 4507 | .min_h_0() |
| 4508 | .child(record_bounds(&self.timeline_bounds, timeline_top_inset)) |
| 4509 | .on_scroll_wheel(cx.listener(|this, _, _, cx| this.close_reaction_details(cx))) |
| 4510 | .child(body) |
| 4511 | .children(typing_line.map(|line| { |
| 4512 | div() |
| 4513 | .absolute() |
| 4514 | .left_0() |
| 4515 | .right_0() |
| 4516 | .bottom(px(typing_offset)) |
| 4517 | .child(runway(line, comfy)) |
| 4518 | })) |
| 4519 | .when(feed_scrolled, |timeline| { |
| 4520 | timeline.child( |
| 4521 | edge_divider(FEED_EDGE_TAG) |
| 4522 | .absolute() |
| 4523 | .top_0() |
| 4524 | .left_0() |
| 4525 | .right_0(), |
| 4526 | ) |
| 4527 | }) |
| 4528 | .children(pin_banner.map(|banner| pin_banner_overlay(runway(banner, comfy)))), |
| 4529 | ) |
| 4530 | .children( |
| 4531 | self.render_draft_error(cx) |
| 4532 | .map(|error| runway(error, comfy)), |
| 4533 | ) |
| 4534 | .when( |
| 4535 | !in_feed && !recap_active && !self.shared_active(), |
| 4536 | |column| match &forward_selection { |
| 4537 | Some(snapshot) => { |
| 4538 | column.child(runway(self.render_forward_footer(snapshot, cx), comfy)) |
| 4539 | } |
| 4540 | None => { |
| 4541 | column.child(runway( |
| 4542 | v_flex() |
| 4543 | .w_full() |
| 4544 | .children(self.render_absence_notice(cx)) |
| 4545 | .children(self.render_proposal_card(cx).map(|card| { |
| 4546 | div().w_full().pb(px(COMPOSER_TOP_GAP)).child(card) |
| 4547 | })) |
| 4548 | .child(self.composer.clone()), |
| 4549 | comfy, |
| 4550 | )) |
| 4551 | } |
| 4552 | }, |
| 4553 | ) |
| 4554 | .child(self.render_drop_overlay(cx)); |
| 4555 | let root = root |
| 4556 | .child( |
| 4557 | h_flex() |
| 4558 | .relative() |
| 4559 | .flex_1() |
| 4560 | .min_h_0() |
| 4561 | .w_full() |
| 4562 | .items_stretch() |
| 4563 | .child(content) |
| 4564 | .children(channel_details) |
| 4565 | .children(find_panel.map(|panel| { |
| 4566 | div() |
| 4567 | .h_full() |
| 4568 | .w(px(FIND_PANEL_WIDTH)) |
| 4569 | .min_w_0() |
| 4570 | .flex_shrink(1.) |
| 4571 | .when(find_overlays, |slot| { |
| 4572 | slot.absolute().top_0().right_0().bottom_0().max_w_full() |
| 4573 | }) |
| 4574 | .child(panel) |
| 4575 | })), |
| 4576 | ) |
| 4577 | .capture_action(cx.listener(|this, _: &Escape, _, cx| { |
| 4578 | if this.reaction_details.is_some() { |
| 4579 | this.close_reaction_details(cx); |
| 4580 | cx.stop_propagation(); |
| 4581 | } |
| 4582 | })) |
| 4583 | .on_action(cx.listener(|this, _: &Escape, window, cx| { |
| 4584 | if this.draft_active { |
| 4585 | if this.draft.read(cx).has_focus(window, cx) { |
| 4586 | this.draft.update(cx, |draft, cx| draft.escape(cx)); |
| 4587 | } |
| 4588 | } else if this.cancel_forward_selection(cx) { |
| 4589 | } else if !this.on_escape(window, cx) { |
| 4590 | cx.propagate(); |
| 4591 | } |
| 4592 | })); |
| 4593 | let root = root.child(crate::inline_video::sentinel()); |
| 4594 | match copilot_island { |
| 4595 | Some(island) => h_flex() |
| 4596 | .size_full() |
| 4597 | .min_w_0() |
| 4598 | .gap(px(ISLAND_GAP)) |
| 4599 | .child(root) |
| 4600 | .child(island) |
| 4601 | .into_any_element(), |
| 4602 | None => root.into_any_element(), |
| 4603 | } |
| 4604 | } |
| 4605 | } |
| 4606 | |
| 4607 | /// Right under the last row like one more message; once that row scrolls below the view it sticks above the composer. |
| 4608 | fn typing_bottom_offset(timeline: Option<Bounds<Pixels>>, last_row: Option<Bounds<Pixels>>) -> f32 { |
| 4609 | let (Some(timeline), Some(last_row)) = (timeline, last_row) else { |
| 4610 | return TYPING_BOTTOM_GAP; |
| 4611 | }; |
| 4612 | let under_last_row = |
| 4613 | f32::from(timeline.bottom() - last_row.bottom()) - TYPING_TOP_GAP - TYPING_BUBBLE_HEIGHT; |
| 4614 | under_last_row.max(TYPING_BOTTOM_GAP) |
| 4615 | } |
| 4616 | |
| 4617 | fn typing_dots() -> impl IntoElement { |
| 4618 | h_flex() |
| 4619 | .gap(px(3.)) |
| 4620 | .flex_none() |
| 4621 | .children((0..TYPING_DOT_COUNT).map(|index| { |
| 4622 | let phase_offset = index as f32 / TYPING_DOT_COUNT as f32; |
| 4623 | div() |
| 4624 | .size(px(TYPING_DOT_SIZE)) |
| 4625 | .rounded_full() |
| 4626 | .bg(theme::text_muted()) |
| 4627 | .with_animation( |
| 4628 | ElementId::NamedInteger("typing-dot".into(), index as u64), |
| 4629 | Animation::new(TYPING_PULSE_PERIOD).repeat(), |
| 4630 | move |dot, delta| { |
| 4631 | let wave = 1. - ((delta - phase_offset).rem_euclid(1.) * 2. - 1.).abs(); |
| 4632 | dot.opacity(0.3 + 0.7 * wave) |
| 4633 | }, |
| 4634 | ) |
| 4635 | })) |
| 4636 | } |
| 4637 | |
| 4638 | fn record_width(width: &Rc<Cell<f32>>) -> impl IntoElement { |
| 4639 | let width = width.clone(); |
| 4640 | canvas( |
| 4641 | move |bounds, window, _| { |
| 4642 | let measured = f32::from(bounds.size.width); |
| 4643 | if (measured - width.replace(measured)).abs() > 0.5 { |
| 4644 | window.request_animation_frame(); |
| 4645 | } |
| 4646 | }, |
| 4647 | |_, _, _, _| {}, |
| 4648 | ) |
| 4649 | .absolute() |
| 4650 | .top_0() |
| 4651 | .left_0() |
| 4652 | .size_full() |
| 4653 | } |
| 4654 | |
| 4655 | fn progress_bar() -> impl IntoElement { |
| 4656 | div() |
| 4657 | .absolute() |
| 4658 | .left_0() |
| 4659 | .right_0() |
| 4660 | .bottom(px(-1.)) |
| 4661 | .h(px(2.)) |
| 4662 | .overflow_hidden() |
| 4663 | .bg(theme::border()) |
| 4664 | .child( |
| 4665 | div() |
| 4666 | .absolute() |
| 4667 | .top_0() |
| 4668 | .h_full() |
| 4669 | .w(relative(PROGRESS_BAR_WIDTH)) |
| 4670 | .bg(theme::own_read()) |
| 4671 | .with_animation( |
| 4672 | "sync-progress", |
| 4673 | Animation::new(PROGRESS_BAR_PERIOD).repeat(), |
| 4674 | |bar, delta| { |
| 4675 | let travel = 1. + PROGRESS_BAR_WIDTH; |
| 4676 | // The 0.3 phase keeps the bar on screen in the static reduce-motion frame (delta 0). |
| 4677 | bar.left(relative( |
| 4678 | (delta + 0.3).fract() * travel - PROGRESS_BAR_WIDTH, |
| 4679 | )) |
| 4680 | }, |
| 4681 | ), |
| 4682 | ) |
| 4683 | } |
| 4684 | |
| 4685 | fn add_people_button(app: &Entity<AppState>, chat_id: &str) -> AnyElement { |
| 4686 | let (app, chat_id) = (app.clone(), chat_id.to_owned()); |
| 4687 | Button::new("chat-add-people") |
| 4688 | .ghost() |
| 4689 | .compact() |
| 4690 | .icon(IconName::UserPlus) |
| 4691 | .tooltip("Add people") |
| 4692 | .on_click(move |_, window, cx| open_add_people(&app, &chat_id, true, window, cx)) |
| 4693 | .into_any_element() |
| 4694 | } |
| 4695 | |
| 4696 | fn runway(content: impl IntoElement, comfy: bool) -> AnyElement { |
| 4697 | if comfy { |
| 4698 | h_flex() |
| 4699 | .w_full() |
| 4700 | .justify_center() |
| 4701 | .child(div().w_full().max_w(px(COMFY_RUNWAY_WIDTH)).child(content)) |
| 4702 | .into_any_element() |
| 4703 | } else { |
| 4704 | content.into_any_element() |
| 4705 | } |
| 4706 | } |
| 4707 | |
| 4708 | #[cfg(test)] |
| 4709 | mod tests { |
| 4710 | use std::sync::Arc; |
| 4711 | |
| 4712 | use super::{ |
| 4713 | DropTarget, HOVER_LEAVE_GRACE, HoverSlot, drop_overlay_text, typing_bottom_offset, |
| 4714 | }; |
| 4715 | |
| 4716 | use gpui_kit::test::TestWindowExt as _; |
| 4717 | use gpui_kit::{AppContext as _, TestAppContext, WindowOptions}; |
| 4718 | use store::Store; |
| 4719 | |
| 4720 | use super::ConversationView; |
| 4721 | use crate::app_state::{AppState, Mode, Selection}; |
| 4722 | |
| 4723 | #[test] |
| 4724 | fn chat_overlay_names_the_chat_and_the_onedrive() { |
| 4725 | assert_eq!( |
| 4726 | drop_overlay_text("Mara Lin", DropTarget::Chat, 3), |
| 4727 | ( |
| 4728 | "Drop to attach to Mara Lin".to_owned(), |
| 4729 | "3 files. Images go into the message, other files to your OneDrive.".to_owned() |
| 4730 | ) |
| 4731 | ); |
| 4732 | } |
| 4733 | |
| 4734 | #[test] |
| 4735 | fn channel_overlay_points_to_the_channel_files() { |
| 4736 | let (_, detail) = drop_overlay_text("Squad / General", DropTarget::Channel, 1); |
| 4737 | assert_eq!( |
| 4738 | detail, |
| 4739 | "1 file. Images go into the message, other files to the channel's Files." |
| 4740 | ); |
| 4741 | } |
| 4742 | |
| 4743 | #[test] |
| 4744 | fn new_chat_overlay_says_files_wait_for_the_chat() { |
| 4745 | let (heading, detail) = drop_overlay_text("", DropTarget::NewChat, 2); |
| 4746 | assert_eq!(heading, "Drop to attach to the new chat"); |
| 4747 | assert_eq!( |
| 4748 | detail, |
| 4749 | "2 files. Images go into the message. Files can be added once the chat exists." |
| 4750 | ); |
| 4751 | } |
| 4752 | |
| 4753 | #[gpui_kit::test] |
| 4754 | fn the_toolbar_outlives_a_short_leave_and_moves_at_once(cx: &mut TestAppContext) { |
| 4755 | cx.update(gpui_kit::init); |
| 4756 | let store = Arc::new(Store::open_in_memory().unwrap()); |
| 4757 | crate::demo::seed(&store); |
| 4758 | let view = cx.update(|cx| { |
| 4759 | let app = cx.new(|_| AppState::new(store, Mode::default())); |
| 4760 | gpui_kit::open_window(WindowOptions::default(), cx, |window, cx| { |
| 4761 | cx.new(|cx| ConversationView::new(app, window, cx)) |
| 4762 | }) |
| 4763 | .unwrap() |
| 4764 | .1 |
| 4765 | }); |
| 4766 | let hover = |cx: &mut TestAppContext, slot: HoverSlot, key: &str, hovered: bool| { |
| 4767 | cx.update(|cx| view.update(cx, |this, cx| this.set_hover(slot, key, hovered, cx))); |
| 4768 | }; |
| 4769 | let shown = |cx: &mut TestAppContext| { |
| 4770 | cx.update(|cx| { |
| 4771 | let this = view.read(cx); |
| 4772 | (this.hovered_message.clone(), this.toolbar_hovered.clone()) |
| 4773 | }) |
| 4774 | }; |
| 4775 | hover(cx, HoverSlot::Row, "a", true); |
| 4776 | hover(cx, HoverSlot::Row, "a", false); |
| 4777 | assert_eq!(shown(cx).0.as_deref(), Some("a")); |
| 4778 | hover(cx, HoverSlot::Toolbar, "a", true); |
| 4779 | cx.executor().advance_clock(HOVER_LEAVE_GRACE * 2); |
| 4780 | assert_eq!(shown(cx), (Some("a".into()), Some("a".into()))); |
| 4781 | hover(cx, HoverSlot::Toolbar, "a", false); |
| 4782 | cx.executor().advance_clock(HOVER_LEAVE_GRACE * 2); |
| 4783 | assert_eq!(shown(cx), (None, None)); |
| 4784 | hover(cx, HoverSlot::Row, "a", true); |
| 4785 | hover(cx, HoverSlot::Row, "a", false); |
| 4786 | hover(cx, HoverSlot::Row, "b", true); |
| 4787 | assert_eq!(shown(cx).0.as_deref(), Some("b")); |
| 4788 | cx.executor().advance_clock(HOVER_LEAVE_GRACE * 2); |
| 4789 | assert_eq!(shown(cx).0.as_deref(), Some("b")); |
| 4790 | } |
| 4791 | |
| 4792 | #[gpui_kit::test] |
| 4793 | fn demo_draft_creates_a_chat_and_selects_it(cx: &mut TestAppContext) { |
| 4794 | cx.update(gpui_kit::init); |
| 4795 | let store = Arc::new(Store::open_in_memory().unwrap()); |
| 4796 | crate::demo::seed(&store); |
| 4797 | let mode = Mode { |
| 4798 | demo: true, |
| 4799 | read_only: false, |
| 4800 | demo_sync: None, |
| 4801 | }; |
| 4802 | let (handle, app, _view) = cx.update(|cx| { |
| 4803 | let app = cx.new(|_| { |
| 4804 | let mut state = AppState::new(store, mode); |
| 4805 | crate::demo::seed_directory(&mut state); |
| 4806 | state.selection = Some(crate::demo::first_selection()); |
| 4807 | state |
| 4808 | }); |
| 4809 | let shared = app.clone(); |
| 4810 | let (handle, view) = |
| 4811 | gpui_kit::open_window(WindowOptions::default(), cx, |window, cx| { |
| 4812 | cx.new(|cx| ConversationView::new(shared, window, cx)) |
| 4813 | }) |
| 4814 | .unwrap(); |
| 4815 | (handle, app, view) |
| 4816 | }); |
| 4817 | cx.update(|cx| app.update(cx, |state, cx| state.start_new_chat(cx))); |
| 4818 | let step = |cx: &mut TestAppContext, run: fn(&mut gpui_kit::Window, &mut gpui_kit::App)| { |
| 4819 | cx.update_window(handle, move |_, window, cx| { |
| 4820 | run(window, cx); |
| 4821 | window.render_frame(cx); |
| 4822 | }) |
| 4823 | .unwrap(); |
| 4824 | }; |
| 4825 | step(cx, |window, cx| window.render_frame(cx)); |
| 4826 | step(cx, |window, cx| window.input("lea", cx)); |
| 4827 | step(cx, |window, cx| window.press("enter", cx)); |
| 4828 | step(cx, |window, cx| window.press("enter", cx)); |
| 4829 | step(cx, |window, cx| window.input("Hello Lea", cx)); |
| 4830 | step(cx, |window, cx| window.press("enter", cx)); |
| 4831 | cx.update(|cx| { |
| 4832 | let state = app.read(cx); |
| 4833 | assert!(!state.new_chat); |
| 4834 | assert_eq!( |
| 4835 | state.selection, |
| 4836 | Some(Selection::Chat("demo-chat-new-1".to_owned())) |
| 4837 | ); |
| 4838 | let chat = state |
| 4839 | .sidebar |
| 4840 | .chats |
| 4841 | .iter() |
| 4842 | .find(|chat| chat.id == "demo-chat-new-1") |
| 4843 | .expect("new chat in the sidebar"); |
| 4844 | assert_eq!(chat.last_message_preview.as_deref(), Some("Hello Lea")); |
| 4845 | }); |
| 4846 | } |
| 4847 | |
| 4848 | #[gpui_kit::test] |
| 4849 | fn escape_closes_the_draft_and_keeps_the_selection(cx: &mut TestAppContext) { |
| 4850 | cx.update(gpui_kit::init); |
| 4851 | let store = Arc::new(Store::open_in_memory().unwrap()); |
| 4852 | crate::demo::seed(&store); |
| 4853 | let mode = Mode { |
| 4854 | demo: true, |
| 4855 | read_only: false, |
| 4856 | demo_sync: None, |
| 4857 | }; |
| 4858 | let (handle, app) = cx.update(|cx| { |
| 4859 | let app = cx.new(|_| { |
| 4860 | let mut state = AppState::new(store, mode); |
| 4861 | crate::demo::seed_directory(&mut state); |
| 4862 | state.selection = Some(crate::demo::first_selection()); |
| 4863 | state |
| 4864 | }); |
| 4865 | let shared = app.clone(); |
| 4866 | let (handle, _) = gpui_kit::open_window(WindowOptions::default(), cx, |window, cx| { |
| 4867 | cx.new(|cx| ConversationView::new(shared, window, cx)) |
| 4868 | }) |
| 4869 | .unwrap(); |
| 4870 | (handle, app) |
| 4871 | }); |
| 4872 | cx.update(|cx| app.update(cx, |state, cx| state.start_new_chat(cx))); |
| 4873 | cx.update_window(handle, |_, window, cx| { |
| 4874 | window.render_frame(cx); |
| 4875 | window.press("escape", cx); |
| 4876 | window.render_frame(cx); |
| 4877 | window.press("escape", cx); |
| 4878 | window.render_frame(cx); |
| 4879 | }) |
| 4880 | .unwrap(); |
| 4881 | cx.update(|cx| { |
| 4882 | let state = app.read(cx); |
| 4883 | assert!(!state.new_chat); |
| 4884 | assert_eq!(state.selection, Some(crate::demo::first_selection())); |
| 4885 | }); |
| 4886 | } |
| 4887 | |
| 4888 | #[test] |
| 4889 | fn the_typing_bubble_follows_the_last_row_and_sticks_above_the_composer() { |
| 4890 | use gpui_kit::{Bounds, point, px, size}; |
| 4891 | let timeline = Bounds::new(point(px(0.), px(100.)), size(px(800.), px(600.))); |
| 4892 | let row_ending_at = |bottom: f32| { |
| 4893 | Some(Bounds::new( |
| 4894 | point(px(0.), px(bottom - 50.)), |
| 4895 | size(px(800.), px(50.)), |
| 4896 | )) |
| 4897 | }; |
| 4898 | assert_eq!( |
| 4899 | typing_bottom_offset(Some(timeline), row_ending_at(400.)), |
| 4900 | 256. |
| 4901 | ); |
| 4902 | assert_eq!( |
| 4903 | typing_bottom_offset(Some(timeline), row_ending_at(900.)), |
| 4904 | 8. |
| 4905 | ); |
| 4906 | assert_eq!(typing_bottom_offset(Some(timeline), None), 8.); |
| 4907 | } |
| 4908 | |
| 4909 | #[test] |
| 4910 | fn no_access_is_a_roster_denial_and_other_errors_still_refresh() { |
| 4911 | assert!(ConversationView::denies_access(&teams_core::Error::Graph( |
| 4912 | graph::Error::NotAMember |
| 4913 | ))); |
| 4914 | assert!(!ConversationView::denies_access( |
| 4915 | &teams_core::Error::Unsupported("refresh") |
| 4916 | )); |
| 4917 | } |
| 4918 | } |