Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 93aa7b1

Browse files
sideshowbarkerdlvhdr
authored andcommitted
feat: show confirm-cancel prompt for comments and approvals
1 parent 9b7cca6 commit 93aa7b1

File tree

2 files changed

+78
-19
lines changed

2 files changed

+78
-19
lines changed

‎ui/components/issuesidebar/issuesidebar.go‎

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,9 @@ import (
1818
)
1919

2020
var (
21-
htmlCommentRegex = regexp.MustCompile("(?U)<!--(.|[[:space:]])*-->")
22-
lineCleanupRegex = regexp.MustCompile(`((\n)+|^)([^\r\n]*\|[^\r\n]*(\n)?)+`)
21+
htmlCommentRegex = regexp.MustCompile("(?U)<!--(.|[[:space:]])*-->")
22+
lineCleanupRegex = regexp.MustCompile(`((\n)+|^)([^\r\n]*\|[^\r\n]*(\n)?)+`)
23+
commentPrompt = "Leave a comment..."
2324
)
2425

2526
type Model struct {
@@ -28,6 +29,7 @@ type Model struct {
2829
sectionId int
2930
width int
3031

32+
ShowConfirmCancel bool
3133
isCommenting bool
3234
isAssigning bool
3335
isUnassigning bool
@@ -71,9 +73,22 @@ func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) {
7173
return m, cmd
7274

7375
case tea.KeyEsc, tea.KeyCtrlC:
74-
m.inputBox.Blur()
75-
m.isCommenting = false
76-
return m, nil
76+
if !m.ShowConfirmCancel {
77+
m.shouldCancelComment()
78+
}
79+
default:
80+
if msg.String() == "Y" || msg.String() == "y" {
81+
if m.shouldCancelComment() {
82+
return m, nil
83+
}
84+
}
85+
if m.ShowConfirmCancel && (msg.String() == "N" || msg.String() == "n") {
86+
m.inputBox.SetPrompt(commentPrompt)
87+
m.ShowConfirmCancel = false
88+
return m, nil
89+
}
90+
m.inputBox.SetPrompt(commentPrompt)
91+
m.ShowConfirmCancel = false
7792
}
7893

7994
m.inputBox, taCmd = m.inputBox.Update(msg)
@@ -243,6 +258,18 @@ func (m *Model) GetIsCommenting() bool {
243258
return m.isCommenting
244259
}
245260

261+
func (m *Model) shouldCancelComment() bool {
262+
if !m.ShowConfirmCancel {
263+
m.inputBox.SetPrompt(lipgloss.NewStyle().Foreground(m.ctx.Theme.ErrorText).Render("Discard comment? (y/N)"))
264+
m.ShowConfirmCancel = true
265+
return false
266+
}
267+
m.inputBox.Blur()
268+
m.isCommenting = false
269+
m.ShowConfirmCancel = false
270+
return true
271+
}
272+
246273
func (m *Model) SetIsCommenting(isCommenting bool) tea.Cmd {
247274
if !m.isCommenting && isCommenting {
248275
m.inputBox.Reset()

‎ui/components/prsidebar/prsidebar.go‎

Lines changed: 46 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,10 @@ import (
1818
)
1919

2020
var (
21-
htmlCommentRegex = regexp.MustCompile("(?U)<!--(.|[[:space:]])*-->")
22-
lineCleanupRegex = regexp.MustCompile(`((\n)+|^)([^\r\n]*\|[^\r\n]*(\n)?)+`)
21+
htmlCommentRegex = regexp.MustCompile("(?U)<!--(.|[[:space:]])*-->")
22+
lineCleanupRegex = regexp.MustCompile(`((\n)+|^)([^\r\n]*\|[^\r\n]*(\n)?)+`)
23+
commentPrompt = "Leave a comment..."
24+
approvalPrompt = "Approve with comment..."
2325
)
2426

2527
type Model struct {
@@ -28,10 +30,11 @@ type Model struct {
2830
pr *pr.PullRequest
2931
width int
3032

31-
isCommenting bool
32-
isApproving bool
33-
isAssigning bool
34-
isUnassigning bool
33+
ShowConfirmCancel bool
34+
isCommenting bool
35+
isApproving bool
36+
isAssigning bool
37+
isUnassigning bool
3538

3639
inputBox inputbox.Model
3740
}
@@ -73,9 +76,22 @@ func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) {
7376
return m, cmd
7477

7578
case tea.KeyEsc, tea.KeyCtrlC:
76-
m.inputBox.Blur()
77-
m.isCommenting = false
78-
return m, nil
79+
if !m.ShowConfirmCancel {
80+
m.shouldCancelComment()
81+
}
82+
default:
83+
if msg.String() == "Y" || msg.String() == "y" {
84+
if m.shouldCancelComment() {
85+
return m, nil
86+
}
87+
}
88+
if m.ShowConfirmCancel && (msg.String() == "N" || msg.String() == "n") {
89+
m.inputBox.SetPrompt(commentPrompt)
90+
m.ShowConfirmCancel = false
91+
return m, nil
92+
}
93+
m.inputBox.SetPrompt(commentPrompt)
94+
m.ShowConfirmCancel = false
7995
}
8096

8197
m.inputBox, taCmd = m.inputBox.Update(msg)
@@ -94,9 +110,12 @@ func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) {
94110
return m, cmd
95111

96112
case tea.KeyEsc, tea.KeyCtrlC:
97-
m.inputBox.Blur()
98-
m.isApproving = false
99-
return m, nil
113+
if m.shouldCancelComment() {
114+
return m, nil
115+
}
116+
default:
117+
m.inputBox.SetPrompt(approvalPrompt)
118+
m.ShowConfirmCancel = false
100119
}
101120

102121
m.inputBox, taCmd = m.inputBox.Update(msg)
@@ -345,12 +364,25 @@ func (m *Model) UpdateProgramContext(ctx *context.ProgramContext) {
345364
m.inputBox.UpdateProgramContext(ctx)
346365
}
347366

367+
func (m *Model) shouldCancelComment() bool {
368+
if !m.ShowConfirmCancel {
369+
m.inputBox.SetPrompt(lipgloss.NewStyle().Foreground(m.ctx.Theme.ErrorText).Render("Discard comment? (y/N)"))
370+
m.ShowConfirmCancel = true
371+
return false
372+
}
373+
m.inputBox.Blur()
374+
m.isCommenting = false
375+
m.isApproving = false
376+
m.ShowConfirmCancel = false
377+
return true
378+
}
379+
348380
func (m *Model) SetIsCommenting(isCommenting bool) tea.Cmd {
349381
if !m.isCommenting && isCommenting {
350382
m.inputBox.Reset()
351383
}
352384
m.isCommenting = isCommenting
353-
m.inputBox.SetPrompt("Leave a comment...")
385+
m.inputBox.SetPrompt(commentPrompt)
354386

355387
if isCommenting {
356388
return tea.Sequence(textarea.Blink, m.inputBox.Focus())
@@ -371,7 +403,7 @@ func (m *Model) SetIsApproving(isApproving bool) tea.Cmd {
371403
m.inputBox.Reset()
372404
}
373405
m.isApproving = isApproving
374-
m.inputBox.SetPrompt("Approve with comment...")
406+
m.inputBox.SetPrompt(approvalPrompt)
375407
m.inputBox.SetValue("LGTM")
376408

377409
if isApproving {

0 commit comments

Comments
 (0)