from textual.screen import Screen from textual.widgets import Input, Button, Static from textual.containers import Vertical, Horizontal from textual.message import Message from textual import events from rich.text import Text from agentm.logic.diambra_login import login_to_diambra, save_diambra_token from agentm.utils.logger import log_with_caller class LoginView(Screen): class LoginSuccess(Message): """Message indicating login success.""" def compose(self): yield Vertical( Static("🔒 Login to DIAMBRA", classes="header"), Input(placeholder="Email", id="email_input"), Horizontal( Input(placeholder="Password", password=True, id="password_input"), Button("👁", id="toggle_pw", variant="primary"), id="pw_row" ), Button("Login", id="login_button"), Static("", id="status_message"), Static( Text.from_markup( "Don't have an account? [bold blue link=https://old.dev.diambra.ai/register]Register here[/]" ), id="register_link" ), id="login_form" ) def on_button_pressed(self, event: Button.Pressed) -> None: if event.button.id == "toggle_pw": password_input = self.query_one("#password_input", Input) password_input.password = not password_input.password password_input.refresh() # Force UI to update return if event.button.id == "login_button": email_input = self.query_one("#email_input", Input) password_input = self.query_one("#password_input", Input) status = self.query_one("#status_message", Static) email = email_input.value.strip() password = password_input.value.strip() if not email or not password: status.update("[red]❌ Email and password required.[/red]") return status.update("🔐 Logging in...") self.set_interval(0.1, lambda: self.perform_login(email, password), name="login_task") def perform_login(self, email: str, password: str): status = self.query_one("#status_message", Static) try: token = login_to_diambra(email, password) save_diambra_token(token) status.update("[green]✅ Login successful![/green]") log_with_caller("info", "User logged in successfully.") self.post_message(self.LoginSuccess()) except Exception as e: status.update(f"[red]❌ {str(e)}[/red]") log_with_caller("error", f"Login failed: {e}")