Skip to content

Commit

Permalink
split txn modals finished; responsive vertical spacing in QR view
Browse files Browse the repository at this point in the history
  • Loading branch information
Tasos Bitsios authored and d13co committed Jan 22, 2025
1 parent 0706d63 commit 6ada694
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 44 deletions.
3 changes: 1 addition & 2 deletions ui/modals/transaction/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,7 @@ func (m *ViewModel) Account() *algod.Account {
}

func (m *ViewModel) IsIncentiveProtocol() bool {
return true // NOCOMMIT
// return m.State.Status.LastProtocolVersion == "https://github.com/algorandfoundation/specs/tree/236dcc18c9c507d794813ab768e467ea42d1b4d9"
return m.State.Status.LastProtocolVersion == "https://github.com/algorandfoundation/specs/tree/236dcc18c9c507d794813ab768e467ea42d1b4d9"
}

// Whether the 2A incentive fee should be added
Expand Down
3 changes: 1 addition & 2 deletions ui/modals/transaction/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,7 @@ func (m ViewModel) FormatedAddress() string {
}

func (m ViewModel) IsQREnabled() bool {
return true // TODO
// return m.State.Status.Network == "testnet-v1.0" || m.State.Status.Network == "mainnet-v1.0"
return m.State.Status.Network == "testnet-v1.0" || m.State.Status.Network == "mainnet-v1.0"
}

// New creates and instance of the ViewModel with a default controls.Model
Expand Down
121 changes: 81 additions & 40 deletions ui/modals/transaction/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,66 @@ import (
"github.com/charmbracelet/x/ansi"
)

var textHeader = "Sign this transaction to register your account as %s"
var textOpenURLInBrowser = "Open this URL in your browser:"
var textPeraUserPressSForQr = style.Italics("Pera wallet user? Press S to scan a QR instead.")
var text2AFeeWarning = style.Red.Render(style.Bold(("⚠ Transaction fee set to 2 ALGO (opting in to rewards)")))
var textScanQrOrPressS = style.Green.Render("Scan the QR code with Pera") + " or " + style.Yellow.Render("press S to show a link instead")
var textOffline320Note1 = style.Bold("Note: this will take effect after 320 rounds (~15 min.)")
var textOffline320Note2 = "Please keep your node running during this cooldown period."

func (m ViewModel) isOffline() bool {
return m.ATxn.AUrlTxnKeyreg.VotePK == nil
}

func (m ViewModel) renderQRModal(renderIn string, txn string, spacing int) (render string) {
remainingSpaces := spacing
render = renderIn
if m.ShouldAddIncentivesFee() {
if remainingSpaces > 0 {
render = lipgloss.JoinVertical(lipgloss.Center, render, "")
remainingSpaces -= 1
}
render = lipgloss.JoinVertical(
lipgloss.Center,
render,
text2AFeeWarning,
)
}
if m.isOffline() {
if remainingSpaces > 0 {
render = lipgloss.JoinVertical(lipgloss.Center, render, "")
remainingSpaces -= 1
}
render = lipgloss.JoinVertical(
lipgloss.Center,
render,
textOffline320Note1,
textOffline320Note2,
)
}
// if we did not emit a space already but we can
if remainingSpaces > 0 {
render = lipgloss.JoinVertical(lipgloss.Center, render, "")
remainingSpaces -= 1
}
render = lipgloss.JoinVertical(
lipgloss.Center,
render,
textScanQrOrPressS,
)
if remainingSpaces > 0 {
render = lipgloss.JoinVertical(lipgloss.Center, render, "")
remainingSpaces -= 1
}
render = lipgloss.JoinVertical(
lipgloss.Center,
render,
qrStyle.Render(txn),
)
return
}

func (m ViewModel) View() string {
if m.Participation == nil {
return "No key selected"
Expand All @@ -20,29 +80,23 @@ func (m ViewModel) View() string {
}

var adj string
isOffline := m.ATxn.AUrlTxnKeyreg.VotePK == nil
if isOffline {
if m.isOffline() {
adj = "offline"
} else {
adj = "online"
}

textHeader := "Sign this transaction to register your account as %s"
textOpenURLInBrowser := "Open this URL in your browser:"
textPeraUserPressSForQr := style.Italics("Pera wallet user? Press S to scan a QR instead.")
text2AFeeWarning := style.Yellow.Render(style.Bold(("⚠ Transaction fee set to 2 ALGO (opting in to rewards)")))
textScanQrOrPressS := style.Green.Render("Scan the QR code with Pera") + " or " + style.Yellow.Render("press S to show a link instead")
textOffline320Note := style.Bold("Note: this will take effect after 320 rounds (~15 min.)\n") + "Please keep your node running during this cooldown period."

intro := fmt.Sprintf(textHeader, adj)
render := intro
render := ""
qrSpacing := 3
qrText, qrErr := m.ATxn.ProduceQRCode()

if m.ShowLink {
link := participation.ToShortLink(*m.Link, m.ShouldAddIncentivesFee())

render = lipgloss.JoinVertical(
lipgloss.Center,
render,
intro,
"",
textOpenURLInBrowser,
"",
Expand All @@ -56,12 +110,13 @@ func (m ViewModel) View() string {
text2AFeeWarning,
)
}
if isOffline {
if m.isOffline() {
render = lipgloss.JoinVertical(
lipgloss.Center,
render,
"",
textOffline320Note,
textOffline320Note1,
textOffline320Note2,
)
}
if m.IsQREnabled() {
Expand All @@ -75,41 +130,27 @@ func (m ViewModel) View() string {

} else {
// TODO: Refactor ATxn to Interface
txn, err := m.ATxn.ProduceQRCode()

// TODO responsive vertical spaces? calculate on modal/screen height diff
if m.ShouldAddIncentivesFee() {
render = lipgloss.JoinVertical(
if qrErr != nil {
return lipgloss.JoinVertical(
lipgloss.Center,
render,
text2AFeeWarning,
"Something went wrong while generating the QR code",
"Press S to display a link",
)
}
if isOffline {
render = lipgloss.JoinVertical(
lipgloss.Center,
render,
textOffline320Note,
)
}
render = lipgloss.JoinVertical(
lipgloss.Center,
render,
textScanQrOrPressS,
)
if err != nil {
return "Something went wrong"
}
render = lipgloss.JoinVertical(
lipgloss.Center,
render,
qrStyle.Render(txn),
)

render = m.renderQRModal(intro, qrText, qrSpacing)
}

width := lipgloss.Width(render)
height := lipgloss.Height(render)

for qrSpacing > 0 && !m.ShowLink && (width > m.Width || height > m.Height) {
qrSpacing -= 1
render = m.renderQRModal(intro, qrText, qrSpacing)
width = lipgloss.Width(render)
height = lipgloss.Height(render)
}

if !m.ShowLink && (width > m.Width || height > m.Height) {
return lipgloss.JoinVertical(
lipgloss.Center,
Expand Down

0 comments on commit 6ada694

Please sign in to comment.