diff --git a/src/input/main_input.rs b/src/input/main_input.rs index 32d6b1c..53293f4 100644 --- a/src/input/main_input.rs +++ b/src/input/main_input.rs @@ -181,14 +181,15 @@ async fn handle_message_selection(app: &mut App, key: KeyEv } KeyCode::Char('d') | KeyCode::Char('в') | KeyCode::Delete => { // Показать модалку подтверждения удаления - if let Some(msg) = app.get_selected_message() { - let can_delete = - msg.can_be_deleted_only_for_self() || msg.can_be_deleted_for_all_users(); - if can_delete { - app.chat_state = crate::app::ChatState::DeleteConfirmation { - message_id: msg.id(), - }; - } + let Some(msg) = app.get_selected_message() else { + return; + }; + let can_delete = + msg.can_be_deleted_only_for_self() || msg.can_be_deleted_for_all_users(); + if can_delete { + app.chat_state = crate::app::ChatState::DeleteConfirmation { + message_id: msg.id(), + }; } } KeyCode::Char('r') | KeyCode::Char('к') => { @@ -201,55 +202,57 @@ async fn handle_message_selection(app: &mut App, key: KeyEv } KeyCode::Char('y') | KeyCode::Char('н') => { // Копировать сообщение - if let Some(msg) = app.get_selected_message() { - let text = format_message_for_clipboard(&msg); - match copy_to_clipboard(&text) { - Ok(_) => { - app.status_message = Some("Сообщение скопировано".to_string()); - } - Err(e) => { - app.error_message = Some(format!("Ошибка копирования: {}", e)); - } + let Some(msg) = app.get_selected_message() else { + return; + }; + let text = format_message_for_clipboard(&msg); + match copy_to_clipboard(&text) { + Ok(_) => { + app.status_message = Some("Сообщение скопировано".to_string()); + } + Err(e) => { + app.error_message = Some(format!("Ошибка копирования: {}", e)); } } } KeyCode::Char('e') | KeyCode::Char('у') => { // Открыть emoji picker для добавления реакции - if let Some(msg) = app.get_selected_message() { - let chat_id = app.selected_chat_id.unwrap(); - let message_id = msg.id(); + let Some(msg) = app.get_selected_message() else { + return; + }; + let chat_id = app.selected_chat_id.unwrap(); + let message_id = msg.id(); - app.status_message = Some("Загрузка реакций...".to_string()); - app.needs_redraw = true; + app.status_message = Some("Загрузка реакций...".to_string()); + app.needs_redraw = true; - // Запрашиваем доступные реакции - match with_timeout_msg( - Duration::from_secs(5), - app.td_client - .get_message_available_reactions(chat_id, message_id), - "Таймаут загрузки реакций", - ) - .await - { - Ok(reactions) => { - let reactions: Vec = reactions; - if reactions.is_empty() { - app.error_message = - Some("Реакции недоступны для этого сообщения".to_string()); - app.status_message = None; - app.needs_redraw = true; - } else { - app.enter_reaction_picker_mode(message_id.as_i64(), reactions); - app.status_message = None; - app.needs_redraw = true; - } - } - Err(e) => { - app.error_message = Some(e); + // Запрашиваем доступные реакции + match with_timeout_msg( + Duration::from_secs(5), + app.td_client + .get_message_available_reactions(chat_id, message_id), + "Таймаут загрузки реакций", + ) + .await + { + Ok(reactions) => { + let reactions: Vec = reactions; + if reactions.is_empty() { + app.error_message = + Some("Реакции недоступны для этого сообщения".to_string()); + app.status_message = None; + app.needs_redraw = true; + } else { + app.enter_reaction_picker_mode(message_id.as_i64(), reactions); app.status_message = None; app.needs_redraw = true; } } + Err(e) => { + app.error_message = Some(e); + app.status_message = None; + app.needs_redraw = true; + } } } _ => {} @@ -264,28 +267,39 @@ async fn handle_message_selection(app: &mut App, key: KeyEv /// - В режиме ответа: отменить ответ /// - В открытом чате: сохранить черновик и закрыть чат async fn handle_escape_key(app: &mut App) { + // Early return для режима выбора сообщения if app.is_selecting_message() { - // Отменить выбор сообщения app.chat_state = crate::app::ChatState::Normal; - } else if app.is_editing() { - // Отменить редактирование - app.cancel_editing(); - } else if app.is_replying() { - // Отменить режим ответа - app.cancel_reply(); - } else if app.selected_chat_id.is_some() { - // Сохраняем черновик если есть текст в инпуте - if let Some(chat_id) = app.selected_chat_id { - if !app.message_input.is_empty() && !app.is_editing() && !app.is_replying() { - let draft_text = app.message_input.clone(); - let _ = app.td_client.set_draft_message(chat_id, draft_text).await; - } else if app.message_input.is_empty() { - // Очищаем черновик если инпут пустой - let _ = app.td_client.set_draft_message(chat_id, String::new()).await; - } - } - app.close_chat(); + return; } + + // Early return для режима редактирования + if app.is_editing() { + app.cancel_editing(); + return; + } + + // Early return для режима ответа + if app.is_replying() { + app.cancel_reply(); + return; + } + + // Закрытие чата с сохранением черновика + let Some(chat_id) = app.selected_chat_id else { + return; + }; + + // Сохраняем черновик если есть текст в инпуте + if !app.message_input.is_empty() && !app.is_editing() && !app.is_replying() { + let draft_text = app.message_input.clone(); + let _ = app.td_client.set_draft_message(chat_id, draft_text).await; + } else if app.message_input.is_empty() { + // Очищаем черновик если инпут пустой + let _ = app.td_client.set_draft_message(chat_id, String::new()).await; + } + + app.close_chat(); } /// Редактирование существующего сообщения