Skip to content

Commit

Permalink
feat: show confirm-cancel prompt for comments and approvals
Browse files Browse the repository at this point in the history
  • Loading branch information
sideshowbarker committed Feb 2, 2025
1 parent 9b7cca6 commit 1731d8c
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 19 deletions.
37 changes: 32 additions & 5 deletions ui/components/issuesidebar/issuesidebar.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ import (
)

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

type Model struct {
Expand All @@ -28,6 +29,7 @@ type Model struct {
sectionId int
width int

ShowConfirmCancel bool
isCommenting bool
isAssigning bool
isUnassigning bool
Expand Down Expand Up @@ -71,9 +73,22 @@ func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) {
return m, cmd

case tea.KeyEsc, tea.KeyCtrlC:
m.inputBox.Blur()
m.isCommenting = false
return m, nil
if !m.ShowConfirmCancel {
m.shouldCancelComment()
}
default:
if msg.String() == "Y" || msg.String() == "y" {
if m.shouldCancelComment() {
return m, nil
}
}
if m.ShowConfirmCancel && (msg.String() == "N" || msg.String() == "n") {
m.inputBox.SetPrompt(commentPrompt)
m.ShowConfirmCancel = false
return m, nil
}
m.inputBox.SetPrompt(commentPrompt)
m.ShowConfirmCancel = false
}

m.inputBox, taCmd = m.inputBox.Update(msg)
Expand Down Expand Up @@ -243,6 +258,18 @@ func (m *Model) GetIsCommenting() bool {
return m.isCommenting
}

func (m *Model) shouldCancelComment() bool {
if !m.ShowConfirmCancel {
m.inputBox.SetPrompt(lipgloss.NewStyle().Foreground(m.ctx.Theme.ErrorText).Render("Discard comment? (y/N)"))
m.ShowConfirmCancel = true
return false
}
m.inputBox.Blur()
m.isCommenting = false
m.ShowConfirmCancel = false
return true
}

func (m *Model) SetIsCommenting(isCommenting bool) tea.Cmd {
if !m.isCommenting && isCommenting {
m.inputBox.Reset()
Expand Down
60 changes: 46 additions & 14 deletions ui/components/prsidebar/prsidebar.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ import (
)

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

type Model struct {
Expand All @@ -28,10 +30,11 @@ type Model struct {
pr *pr.PullRequest
width int

isCommenting bool
isApproving bool
isAssigning bool
isUnassigning bool
ShowConfirmCancel bool
isCommenting bool
isApproving bool
isAssigning bool
isUnassigning bool

inputBox inputbox.Model
}
Expand Down Expand Up @@ -73,9 +76,22 @@ func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) {
return m, cmd

case tea.KeyEsc, tea.KeyCtrlC:
m.inputBox.Blur()
m.isCommenting = false
return m, nil
if !m.ShowConfirmCancel {
m.shouldCancelComment()
}
default:
if msg.String() == "Y" || msg.String() == "y" {
if m.shouldCancelComment() {
return m, nil
}
}
if m.ShowConfirmCancel && (msg.String() == "N" || msg.String() == "n") {
m.inputBox.SetPrompt(commentPrompt)
m.ShowConfirmCancel = false
return m, nil
}
m.inputBox.SetPrompt(commentPrompt)
m.ShowConfirmCancel = false
}

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

case tea.KeyEsc, tea.KeyCtrlC:
m.inputBox.Blur()
m.isApproving = false
return m, nil
if m.shouldCancelComment() {
return m, nil
}
default:
m.inputBox.SetPrompt(approvalPrompt)
m.ShowConfirmCancel = false
}

m.inputBox, taCmd = m.inputBox.Update(msg)
Expand Down Expand Up @@ -345,12 +364,25 @@ func (m *Model) UpdateProgramContext(ctx *context.ProgramContext) {
m.inputBox.UpdateProgramContext(ctx)
}

func (m *Model) shouldCancelComment() bool {
if !m.ShowConfirmCancel {
m.inputBox.SetPrompt(lipgloss.NewStyle().Foreground(m.ctx.Theme.ErrorText).Render("Discard comment? (y/N)"))
m.ShowConfirmCancel = true
return false
}
m.inputBox.Blur()
m.isCommenting = false
m.isApproving = false
m.ShowConfirmCancel = false
return true
}

func (m *Model) SetIsCommenting(isCommenting bool) tea.Cmd {
if !m.isCommenting && isCommenting {
m.inputBox.Reset()
}
m.isCommenting = isCommenting
m.inputBox.SetPrompt("Leave a comment...")
m.inputBox.SetPrompt(commentPrompt)

if isCommenting {
return tea.Sequence(textarea.Blink, m.inputBox.Focus())
Expand All @@ -371,7 +403,7 @@ func (m *Model) SetIsApproving(isApproving bool) tea.Cmd {
m.inputBox.Reset()
}
m.isApproving = isApproving
m.inputBox.SetPrompt("Approve with comment...")
m.inputBox.SetPrompt(approvalPrompt)
m.inputBox.SetValue("LGTM")

if isApproving {
Expand Down

0 comments on commit 1731d8c

Please sign in to comment.