import asyncio import logging from textual import work from apis.himalaya import client as himalaya_client @work(exclusive=True) async def delete_current(app): """Delete the current message.""" if not app.current_message_id: app.show_status("No message selected to delete.", "error") return # Store the current message ID and index current_message_id = app.current_message_id current_index = app.current_message_index # Find the next message to display after deletion next_id, next_idx = app.message_store.find_next_valid_id(current_index) if next_id is None or next_idx is None: # If there's no next message, try to find a previous one next_id, next_idx = app.message_store.find_prev_valid_id(current_index) # Delete the message using our Himalaya client module success = await himalaya_client.delete_message(current_message_id) if success: app.show_status(f"Message {current_message_id} deleted.", "success") app.message_store.remove_envelope(current_message_id) app.refresh_list_view() # Select the next available message if it exists if next_id is not None and next_idx is not None: app.current_message_id = next_id app.current_message_index = next_idx else: # If there are no other messages, reset the UI app.current_message_id = 0 app.show_status("No more messages available.", "warning") else: app.show_status(f"Failed to delete message {current_message_id}.", "error")