from textual import work from src.services.himalaya import client as himalaya_client @work(exclusive=True) async def archive_current(app): """Archive the current message.""" if not app.current_message_id: app.show_status("No message selected to archive.", "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 archiving 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) # Archive the message using our Himalaya client module success = await himalaya_client.archive_messages([str(current_message_id)]) if success: app.show_status(f"Message {current_message_id} archived.", "success") app.message_store.remove_envelope(current_message_id) app.refresh_list_view_items() # 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 archive message {current_message_id}.", "error")