From cac7be6c22bafc0d43fe209813c1fc1ac31a0190 Mon Sep 17 00:00:00 2001 From: Kyren223 Date: Mon, 3 Feb 2025 18:13:15 +0200 Subject: Fixed a bug where users weren't able to scroll higher if they already got to the top message but then more messages have been added --- internal/client/ui/core/chat/chat.go | 20 ++++++++++++++++++-- internal/client/ui/core/state/state.go | 18 +++++++++++++++--- 2 files changed, 33 insertions(+), 5 deletions(-) (limited to 'internal') diff --git a/internal/client/ui/core/chat/chat.go b/internal/client/ui/core/chat/chat.go index 528c02b..9451de7 100644 --- a/internal/client/ui/core/chat/chat.go +++ b/internal/client/ui/core/chat/chat.go @@ -210,6 +210,10 @@ func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) { frequency := frequencies[m.frequencyIndex] member := state.State.Members[*networkId][*state.UserID] + if s, ok := state.State.ChatState[frequency.ID]; ok { + m.maxMessagesHeight = s.MaxHeight + } + lastMsg := state.GetLastMessage(frequency.ID) if m.base != SnapToBottom { m.keepLastRead = false @@ -257,6 +261,10 @@ func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) { } else if m.receiverIndex != -1 { receiverId := state.Data.Peers[m.receiverIndex] + if s, ok := state.State.ChatState[receiverId]; ok { + m.maxMessagesHeight = s.MaxHeight + } + lastMsg := state.GetLastMessage(receiverId) if m.base == SnapToBottom && lastMsg != nil { lastReadMsg := state.Data.LastReadMessage[receiverId] @@ -713,7 +721,7 @@ func (m *Model) ResetBeforeSwitch() { } frequencyId := frequencies[m.frequencyIndex].ID log.Println("Saving frequency", frequencyId) - state.State.ChatState[frequencyId] = state.FrequencyState{ + state.State.ChatState[frequencyId] = state.ChatState{ IncompleteMessage: m.vi.String(), Base: m.base, MaxHeight: m.maxMessagesHeight, @@ -724,7 +732,7 @@ func (m *Model) ResetBeforeSwitch() { } else if m.receiverIndex != -1 { receiverId := state.Data.Peers[m.receiverIndex] log.Println("Saving signal:", receiverId) - state.State.ChatState[receiverId] = state.FrequencyState{ + state.State.ChatState[receiverId] = state.ChatState{ IncompleteMessage: m.vi.String(), Base: m.base, MaxHeight: m.maxMessagesHeight, @@ -951,6 +959,14 @@ func (m *Model) renderMessages(screenHeight int) string { if ok && last == first.ID { m.maxMessagesHeight = height - remainingHeight m.SetIndex(m.index) + if s, ok := state.State.ChatState[*id]; ok { + s.MaxHeight = m.maxMessagesHeight + state.State.ChatState[*id] = s + } else { + state.State.ChatState[*id] = state.ChatState{ + MaxHeight: m.maxMessagesHeight, + } + } } var builder strings.Builder diff --git a/internal/client/ui/core/state/state.go b/internal/client/ui/core/state/state.go index 08b8c55..1115c13 100644 --- a/internal/client/ui/core/state/state.go +++ b/internal/client/ui/core/state/state.go @@ -13,14 +13,14 @@ import ( "github.com/kyren223/eko/pkg/snowflake" ) -type FrequencyState struct { +type ChatState struct { IncompleteMessage string Base int MaxHeight int } type state struct { - ChatState map[snowflake.ID]FrequencyState // key is frequency id or receiver id + ChatState map[snowflake.ID]ChatState // key is frequency id or receiver id LastFrequency map[snowflake.ID]snowflake.ID // key is network id Messages map[snowflake.ID]*btree.BTreeG[data.Message] // key is frequency id or receiver id @@ -34,7 +34,7 @@ type state struct { } var State state = state{ - ChatState: map[snowflake.ID]FrequencyState{}, + ChatState: map[snowflake.ID]ChatState{}, LastFrequency: map[snowflake.ID]snowflake.ID{}, Messages: map[snowflake.ID]*btree.BTreeG[data.Message]{}, Networks: map[snowflake.ID]data.Network{}, @@ -168,6 +168,18 @@ func UpdateMessages(info *packet.MessagesInfo) { } bt.ReplaceOrInsert(message) } + + // Note: this is a naive approach + // Ideally we check each message that was added/removed + // For the frequency/receiver/sender id and only remove that + // But it can be very slow when there are thousands of messages + // TODO: when msg chunking is implemented, consider doing it per-msg + // TODO: consider checking messages count and for small counts use + // the per message approach + for id, state := range State.ChatState { + state.MaxHeight = -1 + State.ChatState[id] = state + } } func UpdateMembers(info *packet.MembersInfo) { -- cgit v1.3.1