Browse Source

refactor(ui/chat): batch logout event commands

ayn2op 1 month ago
parent
commit
1583f822e3
2 changed files with 22 additions and 5 deletions
  1. 21 1
      internal/ui/chat/events.go
  2. 1 4
      internal/ui/chat/view.go

+ 21 - 1
internal/ui/chat/events.go

@@ -1,6 +1,11 @@
 package chat
 
-import "github.com/gdamore/tcell/v3"
+import (
+	"log/slog"
+
+	"github.com/ayn2op/tview"
+	"github.com/gdamore/tcell/v3"
+)
 
 type LogoutEvent struct{ tcell.EventTime }
 
@@ -17,3 +22,18 @@ func NewQuitEvent() *QuitEvent {
 	event.SetEventNow()
 	return event
 }
+
+func (v *View) closeState() tcell.Event {
+	if err := v.CloseState(); err != nil {
+		slog.Error("failed to close the session", "err", err)
+		return tcell.NewEventError(err)
+	}
+	return nil
+}
+
+func (v *View) logout() tview.Command {
+	return tview.BatchCommand{
+		tview.EventCommand(v.closeState),
+		tview.EventCommand(func() tcell.Event { return NewLogoutEvent() }),
+	}
+}

+ 1 - 4
internal/ui/chat/view.go

@@ -260,10 +260,7 @@ func (v *View) HandleEvent(event tcell.Event) tview.Command {
 			v.focusNext()
 			return redraw
 		case keybind.Matches(event, v.cfg.Keybinds.Logout.Keybind):
-			if err := v.CloseState(); err != nil {
-				slog.Error("failed to close the session", "err", err)
-			}
-			return tview.EventCommand(func() tcell.Event { return NewLogoutEvent() })
+			return v.logout()
 		case keybind.Matches(event, v.cfg.Keybinds.ToggleGuildsTree.Keybind):
 			v.toggleGuildsTree()
 			return redraw