-
Notifications
You must be signed in to change notification settings - Fork 12
/
start_onboarding.go
166 lines (137 loc) · 3.79 KB
/
start_onboarding.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package main
import (
"fmt"
"strings"
"time"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
"github.com/companyzero/bisonrelay/client"
"github.com/companyzero/bisonrelay/client/clientintf"
)
type startOnboardScreen struct {
initless
as *appState
form formHelper
attemptingStart bool
onboardErr error
}
func (os startOnboardScreen) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
var cmd tea.Cmd
// Early check for a quit msg to put us into the shutdown state (to
// shutdown DB, etc).
if ss, cmd := maybeShutdown(os.as, msg); ss != nil {
return ss, cmd
}
// Handle generic messages.
switch msg := msg.(type) {
case tea.WindowSizeMsg: // resize window
os.as.winW = msg.Width
os.as.winH = msg.Height
return os, nil
}
// Handle messages when inputing form data.
switch msg := msg.(type) {
case msgCancelForm:
// Send to screen to receive funds.
return newLNFundWalletWindow(os.as)
case msgSubmitForm:
// Start onboarding attempt.
os.attemptingStart = true
key := os.form.inputs[0].(*textInputHelper).Value()
return os, cmdAttemptStartOnboard(os.as.c, key)
case msgStartOnboardErr:
os.onboardErr = msg
os.attemptingStart = false
return os, nil
case msgOnboardStateChanged:
// Initial notification that onboarding started.
return newOnboardScreen(os.as)
case tea.KeyMsg:
os.form, cmd = os.form.Update(msg)
return os, cmd
}
return os, cmd
}
func (os startOnboardScreen) headerView(styles *theme) string {
msg := "Onboarding Bison Relay Client"
headerMsg := styles.header.Render(msg)
spaces := styles.header.Render(strings.Repeat(" ",
max(0, os.as.winW-lipgloss.Width(headerMsg))))
return headerMsg + spaces
}
func (os startOnboardScreen) footerView(styles *theme) string {
footerMsg := fmt.Sprintf(
" [%s] ",
time.Now().Format("15:04"),
)
fs := styles.footer
spaces := fs.Render(strings.Repeat(" ",
max(0, os.as.winW-lipgloss.Width(footerMsg))))
return fs.Render(footerMsg + spaces)
}
func (os startOnboardScreen) View() string {
styles := os.as.styles.Load()
var b strings.Builder
b.WriteString(os.headerView(styles))
b.WriteString("\n\n")
b.WriteString("Automatic onboarding is supported by having an existing\n")
b.WriteString("BR user send you a Paid Invite Key with funds for setting\n")
b.WriteString("up the required LN channels.\n")
b.WriteString("\n")
nbLines := 7
if os.attemptingStart {
b.WriteString("Attempting to start onboarding...\n")
nbLines += 1
} else {
b.WriteString(os.form.View())
nbLines += os.form.lineCount()
}
if os.onboardErr != nil {
b.WriteString("\n")
b.WriteString(styles.err.Render(os.onboardErr.Error()))
b.WriteString("\n")
nbLines += 2
}
b.WriteString(blankLines(os.as.winH - nbLines - 1))
b.WriteString(os.footerView(styles))
return b.String()
}
func cmdAttemptStartOnboard(c *client.Client, key string) func() tea.Msg {
return func() tea.Msg {
if key == "" {
return msgStartOnboardErr(fmt.Errorf("key is empty"))
}
pik, err := clientintf.DecodePaidInviteKey(key)
if err != nil {
return msgStartOnboardErr(fmt.Errorf("invalid key: %v", err))
}
err = c.StartOnboarding(pik)
if err != nil {
return msgStartOnboardErr(fmt.Errorf("error when attempting to start onboard: %v", err))
}
return nil
}
}
func newStartOnboardScreen(as *appState) (startOnboardScreen, tea.Cmd) {
styles := as.styles.Load()
form := newFormHelper(styles,
newTextInputHelper(styles,
tihWithPrompt("Key: "),
),
newButtonHelper(styles,
btnWithLabel(" [ Start Onboarding ]"),
btnWithTrailing(" "),
btnWithFixedMsgAction(msgSubmitForm{}),
),
newButtonHelper(styles,
btnWithLabel(" [ Skip Onboarding ]"),
btnWithTrailing("\n"),
btnWithFixedMsgAction(msgCancelForm{}),
),
)
os := startOnboardScreen{
as: as,
form: form,
}
return os, nil
}