refactor(cmd): move selected*ID fields to chatView struct

This commit is contained in:
ayn2op 2025-11-24 14:58:14 -05:00
parent 4142c30790
commit ac1436b4d4
5 changed files with 26 additions and 24 deletions

View file

@ -7,6 +7,7 @@ import (
"github.com/ayn2op/discordo/internal/keyring"
"github.com/ayn2op/discordo/internal/ui"
"github.com/ayn2op/tview"
"github.com/diamondburned/arikawa/v3/discord"
"github.com/gdamore/tcell/v2"
)
@ -27,6 +28,9 @@ type chatView struct {
messagesList *messagesList
messageInput *messageInput
selectedGuildID discord.GuildID
selectedChannelID discord.ChannelID
app *tview.Application
cfg *config.Config
}

View file

@ -18,9 +18,7 @@ import (
type guildsTree struct {
*tview.TreeView
cfg *config.Config
selectedChannelID discord.ChannelID
selectedGuildID discord.GuildID
cfg *config.Config
}
func newGuildsTree(cfg *config.Config) *guildsTree {
@ -199,8 +197,8 @@ func (gt *guildsTree) onSelected(node *tview.TreeNode) {
app.SetFocus(app.chatView.messageInput)
}
gt.selectedChannelID = channel.ID
gt.selectedGuildID = channel.GuildID
app.chatView.selectedChannelID = channel.ID
app.chatView.selectedGuildID = channel.GuildID
case nil: // Direct messages
channels, err := discordState.PrivateChannels()
if err != nil {

View file

@ -155,7 +155,7 @@ func (mi *messageInput) paste() {
}
func (mi *messageInput) send() {
if !app.chatView.guildsTree.selectedChannelID.IsValid() {
if !app.chatView.selectedChannelID.IsValid() {
return
}
@ -164,7 +164,7 @@ func (mi *messageInput) send() {
return
}
text = processText(app.chatView.guildsTree.selectedChannelID, []byte(text))
text = processText(app.chatView.selectedChannelID, []byte(text))
if text == "" {
return
}
@ -185,8 +185,8 @@ func (mi *messageInput) send() {
} else {
data := mi.sendMessageData
data.Content = text
if _, err := discordState.SendMessageComplex(app.chatView.guildsTree.selectedChannelID, *data); err != nil {
slog.Error("failed to send message in channel", "channel_id", app.chatView.guildsTree.selectedChannelID, "err", err)
if _, err := discordState.SendMessageComplex(app.chatView.selectedChannelID, *data); err != nil {
slog.Error("failed to send message in channel", "channel_id", app.chatView.selectedChannelID, "err", err)
}
}
@ -236,7 +236,7 @@ func expandMentions(cID discord.ChannelID, src []byte) []byte {
return mentionRegex.ReplaceAllFunc(src, func(input []byte) []byte {
output := input
name := string(input[1:])
discordState.MemberStore.Each(app.chatView.guildsTree.selectedGuildID, func(m *discord.Member) bool {
discordState.MemberStore.Each(app.chatView.selectedGuildID, func(m *discord.Member) bool {
if strings.EqualFold(m.User.Username, name) && channelHasUser(cID, m.User.ID) {
output = []byte(m.User.ID.Mention())
return true
@ -259,8 +259,8 @@ func (mi *messageInput) tabComplete(isAuto bool) {
}
pos := posEnd - (len(name) + 1)
gID := app.chatView.guildsTree.selectedGuildID
cID := app.chatView.guildsTree.selectedChannelID
gID := app.chatView.selectedGuildID
cID := app.chatView.selectedChannelID
if !isAuto {
if mi.cfg.AutocompleteLimit == 0 {
@ -504,7 +504,7 @@ func (mi *messageInput) addTitle(s string) {
}
func (mi *messageInput) openFilePicker() {
if !app.chatView.guildsTree.selectedChannelID.IsValid() {
if !app.chatView.selectedChannelID.IsValid() {
return
}

View file

@ -240,7 +240,7 @@ func (ml *messagesList) selectedMessage() (*discord.Message, error) {
return nil, errors.New("no message is currently selected")
}
m, err := discordState.Cabinet.Message(app.chatView.guildsTree.selectedChannelID, ml.selectedMessageID)
m, err := discordState.Cabinet.Message(app.chatView.selectedChannelID, ml.selectedMessageID)
if err != nil {
return nil, fmt.Errorf("could not retrieve selected message: %w", err)
}
@ -249,7 +249,7 @@ func (ml *messagesList) selectedMessage() (*discord.Message, error) {
}
func (ml *messagesList) selectedMessageIndex() (int, error) {
ms, err := discordState.Cabinet.Messages(app.chatView.guildsTree.selectedChannelID)
ms, err := discordState.Cabinet.Messages(app.chatView.selectedChannelID)
if err != nil {
return -1, err
}
@ -295,9 +295,9 @@ func (ml *messagesList) onInputCapture(event *tcell.EventKey) *tcell.EventKey {
}
func (ml *messagesList) _select(name string) {
ms, err := discordState.Cabinet.Messages(app.chatView.guildsTree.selectedChannelID)
ms, err := discordState.Cabinet.Messages(app.chatView.selectedChannelID)
if err != nil {
slog.Error("failed to get messages", "err", err, "channel_id", app.chatView.guildsTree.selectedChannelID)
slog.Error("failed to get messages", "err", err, "channel_id", app.chatView.selectedChannelID)
return
}
@ -622,16 +622,16 @@ func (ml *messagesList) delete() {
}
}
if err := discordState.DeleteMessage(app.chatView.guildsTree.selectedChannelID, msg.ID, ""); err != nil {
slog.Error("failed to delete message", "channel_id", app.chatView.guildsTree.selectedChannelID, "message_id", msg.ID, "err", err)
if err := discordState.DeleteMessage(app.chatView.selectedChannelID, msg.ID, ""); err != nil {
slog.Error("failed to delete message", "channel_id", app.chatView.selectedChannelID, "message_id", msg.ID, "err", err)
return
}
ml.selectedMessageID = 0
ml.Highlight()
if err := discordState.MessageRemove(app.chatView.guildsTree.selectedChannelID, msg.ID); err != nil {
slog.Error("failed to delete message", "channel_id", app.chatView.guildsTree.selectedChannelID, "message_id", msg.ID, "err", err)
if err := discordState.MessageRemove(app.chatView.selectedChannelID, msg.ID); err != nil {
slog.Error("failed to delete message", "channel_id", app.chatView.selectedChannelID, "message_id", msg.ID, "err", err)
return
}

View file

@ -146,7 +146,7 @@ func onReady(r *gateway.ReadyEvent) {
}
func onMessageCreate(message *gateway.MessageCreateEvent) {
if app.chatView.guildsTree.selectedChannelID == message.ChannelID {
if app.chatView.selectedChannelID == message.ChannelID {
app.chatView.messagesList.drawMessage(app.chatView.messagesList, message.Message)
app.Draw()
}
@ -157,13 +157,13 @@ func onMessageCreate(message *gateway.MessageCreateEvent) {
}
func onMessageUpdate(message *gateway.MessageUpdateEvent) {
if app.chatView.guildsTree.selectedChannelID == message.ChannelID {
if app.chatView.selectedChannelID == message.ChannelID {
onMessageDelete(&gateway.MessageDeleteEvent{ID: message.ID, ChannelID: message.ChannelID, GuildID: message.GuildID})
}
}
func onMessageDelete(message *gateway.MessageDeleteEvent) {
if app.chatView.guildsTree.selectedChannelID == message.ChannelID {
if app.chatView.selectedChannelID == message.ChannelID {
messages, err := discordState.Cabinet.Messages(message.ChannelID)
if err != nil {
slog.Error("failed to get messages from state", "err", err, "channel_id", message.ChannelID)