Skip to content

Commit

Permalink
feat: modal
Browse files Browse the repository at this point in the history
  • Loading branch information
HashMapsData2Value committed Oct 30, 2024
1 parent 0f0dc2c commit 501c034
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 11 deletions.
8 changes: 8 additions & 0 deletions ui/pages/keys/cmds.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,20 @@ import (

type DeleteKey *api.ParticipationKey

type DeleteFinished string

func EmitDeleteKey(key *api.ParticipationKey) tea.Cmd {
return func() tea.Msg {
return DeleteKey(key)
}
}

func EmitKeyDeleted() tea.Cmd {
return func() tea.Msg {
return DeleteFinished("Key deleted")
}
}

// EmitKeySelected waits for and retrieves a new set of table rows from a given channel.
func EmitKeySelected(key *api.ParticipationKey) tea.Cmd {
return func() tea.Msg {
Expand Down
22 changes: 20 additions & 2 deletions ui/pages/keys/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,33 @@ func (m ViewModel) HandleMessage(msg tea.Msg) (ViewModel, tea.Cmd) {
case internal.Account:
m.Address = msg.Address
m.table.SetRows(m.makeRows(m.Data))
case DeleteFinished:
m.SelectedKeyToDelete = nil
m.DeleteLoading = false
case tea.KeyMsg:
switch msg.String() {
case "enter":
return m, EmitKeySelected(m.SelectedKey())
case "g":
// TODO: navigation

case "d":
return m, EmitDeleteKey(m.SelectedKey())
if m.SelectedKeyToDelete == nil {
m.SelectedKeyToDelete = m.SelectedKey()
} else {
m.SelectedKeyToDelete = nil
}
return m, nil
case "y": // "Yes do delete" option in the delete confirmation modal
if m.SelectedKeyToDelete != nil {
m.DeleteLoading = true // show loading spinner
return m, EmitDeleteKey(m.SelectedKeyToDelete)
}
return m, nil
case "n": // "do NOT delete" option in the delete confirmation modal
if m.SelectedKeyToDelete != nil {
m.SelectedKeyToDelete = nil
}
return m, nil
case "ctrl+c":
return m, tea.Quit
}
Expand Down
4 changes: 4 additions & 0 deletions ui/pages/keys/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ type ViewModel struct {
Width int
Height int

SelectedKeyToDelete *api.ParticipationKey

DeleteLoading bool

table table.Model
controls controls.Model
}
Expand Down
37 changes: 36 additions & 1 deletion ui/pages/keys/view.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,45 @@
package keys

import (
"fmt"

"github.com/algorandfoundation/hack-tui/api"
"github.com/algorandfoundation/hack-tui/ui/pages"
"github.com/charmbracelet/bubbles/spinner"
"github.com/charmbracelet/lipgloss"
)

func (m ViewModel) View() string {
return lipgloss.JoinVertical(lipgloss.Center, pages.Padding1(m.table.View()), m.controls.View())
if m.SelectedKeyToDelete != nil {
return lipgloss.JoinVertical(
lipgloss.Center,
renderDeleteConfirmationModal(m.SelectedKeyToDelete, m.DeleteLoading),
m.controls.View(),
)
}
return lipgloss.JoinVertical(
lipgloss.Center,
pages.Padding1(m.table.View()),
m.controls.View(),
)
}

func renderDeleteConfirmationModal(partKey *api.ParticipationKey, deleteLoading bool) string {
modalStyle := lipgloss.NewStyle().
Width(60).
Height(7).
Align(lipgloss.Center).
Border(lipgloss.RoundedBorder()).
Padding(1, 2)

var modalContent string
if deleteLoading {
s := spinner.New()
s.Spinner = spinner.Dot
modalContent = fmt.Sprintf("Deleting key...\n%s", s.View())
} else {
modalContent = fmt.Sprintf("Are you sure you want to delete this key from your node?\nParticipation Key: %v\nAccount Address: %v\nPress either y (yes) or n (no).", partKey.Id, partKey.Address)
}

return modalStyle.Render(modalContent)
}
14 changes: 6 additions & 8 deletions ui/viewport.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,13 @@ type ViewportViewModel struct {
errorPage ErrorViewModel
}

type DeleteFinished string

func DeleteKey(client *api.ClientWithResponses, key keys.DeleteKey) tea.Cmd {
return func() tea.Msg {
err := internal.DeletePartKey(context.Background(), client, key.Id)
if err != nil {
return DeleteFinished(err.Error())
return keys.DeleteFinished(err.Error())
}
return DeleteFinished("Key deleted")
return keys.DeleteFinished("Key deleted")
}
}

Expand Down Expand Up @@ -96,8 +94,8 @@ func (m ViewportViewModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
m.page = KeysPage
case keys.DeleteKey:
return m, DeleteKey(m.client, msg)
case DeleteFinished:
// TODO
case keys.DeleteFinished:
return m, keys.EmitKeyDeleted()
case tea.KeyMsg:
switch msg.String() {
// Tab Backwards
Expand Down Expand Up @@ -128,12 +126,12 @@ func (m ViewportViewModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
// Navigate to the transaction page
return m, keys.EmitKeySelected(m.keysPage.SelectedKey())
}
case "a":
m.page = AccountsPage
case "g":
m.generatePage.Inputs[0].SetValue(m.accountsPage.SelectedAccount().Address)
m.page = GeneratePage
return m, nil
case "a":
m.page = AccountsPage
case "k":
m.page = KeysPage
return m, accounts.EmitAccountSelected(m.accountsPage.SelectedAccount())
Expand Down

0 comments on commit 501c034

Please sign in to comment.