diff --git a/lazy_github/lib/messages.py b/lazy_github/lib/messages.py index 7493512..5cb83f9 100644 --- a/lazy_github/lib/messages.py +++ b/lazy_github/lib/messages.py @@ -2,7 +2,15 @@ from textual.message import Message -from lazy_github.models.github import Branch, FullPullRequest, Issue, IssueComment, PartialPullRequest, Repository +from lazy_github.models.github import ( + Branch, + FullPullRequest, + Issue, + IssueComment, + Notification, + PartialPullRequest, + Repository, +) class RepoSelected(Message): @@ -89,3 +97,15 @@ class BranchesLoaded(Message): def __init__(self, branches: list[Branch]) -> None: super().__init__() self.branches = branches + + +class NotificationMarkedAsRead(Message): + def __init__(self, notification: Notification) -> None: + super().__init__() + self.notification = notification + + +class NotificationSelected(Message): + def __init__(self, notification: Notification) -> None: + super().__init__() + self.notification = notification diff --git a/lazy_github/ui/screens/notifications.py b/lazy_github/ui/screens/notifications.py index e097530..76c5377 100644 --- a/lazy_github/ui/screens/notifications.py +++ b/lazy_github/ui/screens/notifications.py @@ -2,10 +2,10 @@ from textual.app import ComposeResult from textual.containers import Container from textual.coordinate import Coordinate -from textual.message import Message from textual.screen import ModalScreen -from textual.widgets import Markdown, TabbedContent, TabPane +from textual.widgets import DataTable, Markdown, TabbedContent, TabPane +from lazy_github.lib.messages import NotificationMarkedAsRead, NotificationSelected from lazy_github.lib.bindings import LazyGithubBindings from lazy_github.lib.constants import BULLET_POINT, CHECKMARK from lazy_github.lib.github.notifications import fetch_notifications, mark_notification_as_read @@ -13,12 +13,6 @@ from lazy_github.ui.widgets.common import LazyGithubFooter, SearchableDataTable -class NotificationMarkedAsRead(Message): - def __init__(self, notification: Notification) -> None: - super().__init__() - self.notification = notification - - class _NotificationsTableTabPane(TabPane): def __init__(self, prefix: str, *args, **kwargs) -> None: super().__init__(*args, **kwargs) @@ -46,6 +40,16 @@ def add_notification(self, notification: Notification) -> None: key=str(notification.id), ) + def get_selected_notification(self) -> Notification: + current_row = self.searchable_table.table.cursor_row + id = self.searchable_table.table.get_cell_at(Coordinate(current_row, self.id_column)) + return self.notifications[int(id)] + + @on(DataTable.RowSelected) + def notification_selected(self) -> None: + notification = self.get_selected_notification() + self.post_message(NotificationSelected(notification)) + def on_mount(self) -> None: self.searchable_table.loading = True self.searchable_table.table.cursor_type = "row" @@ -137,7 +141,7 @@ def on_mount(self) -> None: self.load_notifications() -class NotificationsModal(ModalScreen[None]): +class NotificationsModal(ModalScreen[Notification | None]): DEFAULT_CSS = """ NotificationsModal { height: 80%; @@ -160,5 +164,9 @@ def compose(self) -> ComposeResult: yield NotificationsContainer(id="notifications") yield LazyGithubFooter() + @on(NotificationSelected) + async def handle_notification_selected(self, message: NotificationSelected) -> None: + self.dismiss(message.notification) + async def action_close(self) -> None: self.dismiss() diff --git a/lazy_github/ui/screens/primary.py b/lazy_github/ui/screens/primary.py index 07455d0..67aeed5 100644 --- a/lazy_github/ui/screens/primary.py +++ b/lazy_github/ui/screens/primary.py @@ -353,9 +353,12 @@ def compose(self): @work async def action_view_notifications(self) -> None: - await self.app.push_screen_wait(NotificationsModal()) + notification = await self.app.push_screen_wait(NotificationsModal()) self.refresh_notification_count() + if notification: + lg.info(f"Selected notification: {notification}") + async def on_mount(self) -> None: if LazyGithubContext.config.notifications.enabled: self.refresh_notification_count()