From a2f16a4d721780cd88fbe3c4f001dae9a9037dbb Mon Sep 17 00:00:00 2001 From: Dmytro Maistruk <75750764+JACTyt@users.noreply.github.com> Date: Sat, 27 Sep 2025 03:38:57 +0200 Subject: [PATCH] feat: add confirmation modal for delete action (#575) (#605) Co-authored-by: Ayyan --- cmd/application.go | 22 ++++++++++++++++++++++ cmd/messages_list.go | 17 ++++++++++++++++- internal/config/config.toml | 3 ++- internal/config/keys.go | 7 ++++--- 4 files changed, 44 insertions(+), 5 deletions(-) diff --git a/cmd/application.go b/cmd/application.go index e2bcd8b..9775db9 100644 --- a/cmd/application.go +++ b/cmd/application.go @@ -6,6 +6,7 @@ import ( "github.com/ayn2op/discordo/internal/config" "github.com/ayn2op/discordo/internal/consts" "github.com/ayn2op/discordo/internal/login" + "github.com/ayn2op/discordo/internal/ui" "github.com/ayn2op/tview" "github.com/gdamore/tcell/v2" "github.com/zalando/go-keyring" @@ -15,6 +16,7 @@ const ( flexPageName = "flex" mentionsListPageName = "mentionsList" attachmentsListPageName = "attachmentsList" + confirmModalPageName = "confirmModal" ) type application struct { @@ -151,3 +153,23 @@ func (a *application) onFlexInputCapture(event *tcell.EventKey) *tcell.EventKey return event } + +func (a *application) showConfirmModal(prompt string, buttons []string, onDone func(label string)) { + previousFocus := a.GetFocus() + + modal := tview.NewModal(). + SetText(prompt). + AddButtons(buttons). + SetDoneFunc(func(_ int, buttonLabel string) { + a.pages.RemovePage(confirmModalPageName).SwitchToPage(flexPageName) + a.SetFocus(previousFocus) + + if onDone != nil { + onDone(buttonLabel) + } + }) + + a.pages. + AddAndSwitchToPage(confirmModalPageName, ui.Centered(modal, 40, 10), true). + ShowPage(flexPageName) +} diff --git a/cmd/messages_list.go b/cmd/messages_list.go index c15da83..3217292 100644 --- a/cmd/messages_list.go +++ b/cmd/messages_list.go @@ -282,8 +282,9 @@ func (ml *messagesList) onInputCapture(event *tcell.EventKey) *tcell.EventKey { ml.reply(true) case ml.cfg.Keys.MessagesList.Delete: ml.delete() + case ml.cfg.Keys.MessagesList.DeleteConfirm: + ml.confirmDelete() } - return nil } @@ -557,6 +558,20 @@ func (ml *messagesList) reply(mention bool) { app.SetFocus(app.messageInput) } +func (ml *messagesList) confirmDelete() { + onChoice := func(choice string) { + if choice == "Yes" { + ml.delete() + } + } + + app.showConfirmModal( + "Are you sure you want to delete this message?", + []string{"Yes", "No"}, + onChoice, + ) +} + func (ml *messagesList) delete() { msg, err := ml.selectedMsg() if err != nil { diff --git a/internal/config/config.toml b/internal/config/config.toml index 9012276..845f93f 100644 --- a/internal/config/config.toml +++ b/internal/config/config.toml @@ -69,7 +69,8 @@ reply = "Rune[r]" # Reply (with mention) to the selected message. reply_mention = "Rune[R]" cancel = "Esc" -delete = "Rune[d]" +delete = "Rune[D]" +delete_confirm = "Rune[d]" # Open the selected message's attachments or hyperlinks in the message # using the default browser application. open = "Rune[o]" diff --git a/internal/config/keys.go b/internal/config/keys.go index 6480ad1..d7eaac6 100644 --- a/internal/config/keys.go +++ b/internal/config/keys.go @@ -40,9 +40,10 @@ type ( Reply string `toml:"reply"` ReplyMention string `toml:"reply_mention"` - Cancel string `toml:"cancel"` - Delete string `toml:"delete"` - Open string `toml:"open"` + Cancel string `toml:"cancel"` + Delete string `toml:"delete"` + DeleteConfirm string `toml:"delete_confirm"` + Open string `toml:"open"` YankContent string `toml:"yank_content"` YankURL string `toml:"yank_url"`