Skip to content

Commit c0780ee

Browse files
committed
fix(perf): add onPagination flag to prevent makeing multple requests
1 parent 6d905ee commit c0780ee

File tree

5 files changed

+22
-56
lines changed

5 files changed

+22
-56
lines changed

cmd/root.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,8 @@ func newRootCmd(version string) *cobra.Command {
122122
model.SelectedFile = ""
123123
model.OffsetDate = userChatsResult.OffsetDate
124124
model.OffsetID = userChatsResult.OffsetID
125+
model.OnPagination = false
126+
125127
model.Stories = []types.Stories{}
126128

127129
background := model

internal/telegram/chat/manager.go

Lines changed: 5 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,9 @@ package chat
33
import (
44
"context"
55
"errors"
6-
"fmt"
76
"log/slog"
87
"slices"
98
"strconv"
10-
"time"
119

1210
tea "github.com/charmbracelet/bubbletea"
1311
"github.com/gotd/td/tg"
@@ -335,7 +333,6 @@ func (m *Manager) getAllDialogs(ctx context.Context, offsetDate, offsetID int) (
335333
slog.Error(err.Error())
336334
return nil, err
337335
}
338-
339336
switch d := dialogs.(type) {
340337
case *tg.MessagesDialogs:
341338
allChats = append(allChats, d.Chats...)
@@ -363,7 +360,6 @@ func (m *Manager) getAllDialogs(ctx context.Context, offsetDate, offsetID int) (
363360
}, nil
364361
}
365362
last := d.Messages[len(d.Messages)-1]
366-
367363
switch msg := last.(type) {
368364
case *tg.Message:
369365
return &CligramGetDialogsResponse{
@@ -375,12 +371,13 @@ func (m *Manager) getAllDialogs(ctx context.Context, offsetDate, offsetID int) (
375371
}, nil
376372
default:
377373
return &CligramGetDialogsResponse{
378-
Chats: allChats,
379-
Users: allUsers,
380-
Dialogs: allDialogs,
374+
Chats: allChats,
375+
Users: allUsers,
376+
Dialogs: allDialogs,
377+
OffsetDate: -1,
378+
OffsetID: -1,
381379
}, nil
382380
}
383-
384381
default:
385382
return &CligramGetDialogsResponse{
386383
Chats: allChats,
@@ -435,50 +432,3 @@ type userOnlineStatus struct {
435432
IsOnline bool
436433
LastSeen *string
437434
}
438-
439-
func getUserOnlineStatus(status tg.UserStatusClass) *userOnlineStatus {
440-
if status == nil {
441-
return &userOnlineStatus{
442-
IsOnline: false,
443-
LastSeen: nil,
444-
}
445-
}
446-
switch s := status.(type) {
447-
case *tg.UserStatusOnline:
448-
lastSeen := "online"
449-
return &userOnlineStatus{
450-
IsOnline: true,
451-
LastSeen: &lastSeen,
452-
}
453-
case *tg.UserStatusOffline:
454-
lastSeen := calculateLastSeenHumanReadable(s.WasOnline)
455-
return &userOnlineStatus{
456-
IsOnline: false,
457-
LastSeen: &lastSeen,
458-
}
459-
default:
460-
lastSeen := "last seen long time ago"
461-
return &userOnlineStatus{
462-
IsOnline: false,
463-
LastSeen: &lastSeen,
464-
}
465-
}
466-
}
467-
468-
func calculateLastSeenHumanReadable(wasOnline int) string {
469-
lastSeenTime := time.Unix(int64(wasOnline), 0)
470-
currentTime := time.Now()
471-
diff := currentTime.Sub(lastSeenTime)
472-
473-
if diff.Seconds() < 60 {
474-
return "last seen just now"
475-
}
476-
if diff.Hours() < 24 {
477-
return fmt.Sprintf("last seen at %s", lastSeenTime.Format("03:04 PM"))
478-
}
479-
if diff.Hours() < 48 {
480-
return fmt.Sprintf("last seen yesterday at %s", lastSeenTime.Format("03:04 PM"))
481-
}
482-
483-
return fmt.Sprintf("last seen on %s", lastSeenTime.Format("02/01/2006 03:04 PM"))
484-
}

internal/ui/ui.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,7 @@ func changeSideBarMode(m *Model, msg string) (Model, tea.Cmd) {
296296
if m.FocusedOn == SideBar {
297297
m.ChatUI.ResetSelected()
298298
m.AreWeSwitchingModes = true
299+
m.OnPagination = false
299300
}
300301
areWeInGroupMode := m.Mode == ModeGroups && m.FocusedOn == Mainview
301302
//i don't think 🤔 we should keep the list in memory

internal/ui/update-ui.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -448,9 +448,17 @@ func (m Model) handleListPagination() (Model, tea.Cmd) {
448448
if m.Users.Index() < len(m.Users.VisibleItems())-6 {
449449
return m, nil
450450
}
451+
451452
if m.OffsetDate == -1 || m.OffsetID == -1 {
452453
return m, nil
453454
}
455+
456+
if m.OnPagination {
457+
return m, nil
458+
}
459+
460+
m.OnPagination = true
461+
454462
if m.Mode == ModeUsers || m.Mode == ModeBots {
455463
return m, telegram.Cligram.GetUserChats(telegram.Cligram.Context(), types.ChatType(m.Mode), m.OffsetDate, m.OffsetID)
456464
}
@@ -578,9 +586,11 @@ func (m Model) handleUserChats(msg types.UserChatsMsg) (tea.Model, tea.Cmd) {
578586
}
579587
//TODO: reminder to filter out if the chats gets duplicated
580588
m.Users.SetItems(users)
589+
581590
m.AreWeSwitchingModes = false
582591
m.OffsetDate = msg.Response.OffsetDate
583592
m.OffsetID = msg.Response.OffsetID
593+
m.OnPagination = false
584594
return m, nil
585595
}
586596

@@ -604,6 +614,7 @@ func (m Model) handleUserChannels(msg types.ChannelsMsg) (tea.Model, tea.Cmd) {
604614
m.AreWeSwitchingModes = false
605615
m.OffsetDate = msg.Response.OffsetDate
606616
m.OffsetID = msg.Response.OffsetID
617+
m.OnPagination = false
607618
return m, nil
608619
}
609620

internal/ui/util.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,9 @@ type Model struct {
129129
EditMessage *types.FormattedMessage
130130
SkipNextInput bool
131131
OffsetDate, OffsetID int
132-
Stories []types.Stories
132+
//an indicator that tells there is already on going request kinda debouncing
133+
OnPagination bool
134+
Stories []types.Stories
133135
}
134136

135137
func filterEmptyMessages(msgs [50]types.FormattedMessage) []types.FormattedMessage {

0 commit comments

Comments
 (0)