Skip to content

Commit d82535c

Browse files
committed
add loading state for run l1 node
1 parent b42857d commit d82535c

File tree

2 files changed

+87
-47
lines changed

2 files changed

+87
-47
lines changed

models/weaveinit/run_l1_node.go

+86-40
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"os"
66
"os/exec"
77
"path/filepath"
8+
"time"
89

910
tea "github.com/charmbracelet/bubbletea"
1011

@@ -171,7 +172,8 @@ func (m *RunL1NodeMonikerInput) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
171172
if done {
172173
m.state.moniker = input.Text
173174
m.state.weave.PreviousResponse += styles.RenderPreviousResponse(styles.DotsSeparator, m.GetQuestion(), []string{"moniker"}, input.Text)
174-
return NewExistingAppChecker(m.state), utils.DoTick()
175+
model := NewExistingAppChecker(m.state)
176+
return model, model.Init()
175177
}
176178
m.TextInput = input
177179
return m, cmd
@@ -182,45 +184,58 @@ func (m *RunL1NodeMonikerInput) View() string {
182184
}
183185

184186
type ExistingAppChecker struct {
185-
state *RunL1NodeState
187+
state *RunL1NodeState
188+
loading utils.Loading
186189
}
187190

188191
func NewExistingAppChecker(state *RunL1NodeState) *ExistingAppChecker {
189192
return &ExistingAppChecker{
190-
state: state,
193+
state: state,
194+
loading: utils.NewLoading("Checking for an existing Initia app...", WaitExistingAppChecker(state)),
191195
}
192196
}
193197

194198
func (m *ExistingAppChecker) Init() tea.Cmd {
195-
return utils.DoTick()
199+
return m.loading.Init()
196200
}
197201

198-
func (m *ExistingAppChecker) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
199-
switch msg.(type) {
200-
case utils.TickMsg:
202+
func WaitExistingAppChecker(state *RunL1NodeState) tea.Cmd {
203+
return func() tea.Msg {
201204
homeDir, err := os.UserHomeDir()
202205
if err != nil {
203-
fmt.Printf("[error] Failed to get user home directory: %v\n", err)
204-
return m, tea.Quit
206+
return utils.ErrorLoading{Err: err}
205207
}
206208

207209
initiaConfigPath := filepath.Join(homeDir, utils.InitiaConfigDirectory)
208210
appTomlPath := filepath.Join(initiaConfigPath, "app.toml")
209211
configTomlPath := filepath.Join(initiaConfigPath, "config.toml")
212+
time.Sleep(1500 * time.Millisecond)
210213
if !utils.FileOrFolderExists(configTomlPath) || !utils.FileOrFolderExists(appTomlPath) {
211-
m.state.existingApp = false
214+
state.existingApp = false
215+
return utils.EndLoading{}
216+
} else {
217+
state.existingApp = true
218+
return utils.EndLoading{}
219+
}
220+
}
221+
}
222+
223+
func (m *ExistingAppChecker) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
224+
loader, cmd := m.loading.Update(msg)
225+
m.loading = loader
226+
if m.loading.Completing {
227+
228+
if !m.state.existingApp {
212229
return NewMinGasPriceInput(m.state), nil
213230
} else {
214-
m.state.existingApp = true
215231
return NewExistingAppReplaceSelect(m.state), nil
216232
}
217-
default:
218-
return m, nil
219233
}
234+
return m, cmd
220235
}
221236

222237
func (m *ExistingAppChecker) View() string {
223-
return m.state.weave.PreviousResponse + "Checking for an existing Initia app..."
238+
return m.state.weave.PreviousResponse + "\n" + m.loading.View()
224239
}
225240

226241
type ExistingAppReplaceSelect struct {
@@ -443,7 +458,8 @@ func (m *PersistentPeersInput) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
443458
if done {
444459
m.state.persistentPeers = input.Text
445460
m.state.weave.PreviousResponse += styles.RenderPreviousResponse(styles.DotsSeparator, m.GetQuestion(), []string{"persistent_peers"}, input.Text)
446-
return NewExistingGenesisChecker(m.state), utils.DoTick()
461+
model := NewExistingGenesisChecker(m.state)
462+
return model, model.Init()
447463
}
448464
m.TextInput = input
449465
return m, cmd
@@ -454,48 +470,62 @@ func (m *PersistentPeersInput) View() string {
454470
}
455471

456472
type ExistingGenesisChecker struct {
457-
state *RunL1NodeState
473+
state *RunL1NodeState
474+
loading utils.Loading
458475
}
459476

460477
func NewExistingGenesisChecker(state *RunL1NodeState) *ExistingGenesisChecker {
461478
return &ExistingGenesisChecker{
462-
state: state,
479+
state: state,
480+
loading: utils.NewLoading("Checking for an existing Initia genesis file...", WaitExistingGenesisChecker(state)),
463481
}
464482
}
465483

466484
func (m *ExistingGenesisChecker) Init() tea.Cmd {
467-
return utils.DoTick()
485+
return m.loading.Init()
468486
}
469487

470-
func (m *ExistingGenesisChecker) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
471-
switch msg.(type) {
472-
case utils.TickMsg:
488+
func WaitExistingGenesisChecker(state *RunL1NodeState) tea.Cmd {
489+
return func() tea.Msg {
473490
homeDir, err := os.UserHomeDir()
474491
if err != nil {
475492
fmt.Printf("[error] Failed to get user home directory: %v\n", err)
476-
return m, tea.Quit
493+
return utils.ErrorLoading{Err: err}
477494
}
478-
479495
initiaConfigPath := filepath.Join(homeDir, utils.InitiaConfigDirectory)
480496
genesisFilePath := filepath.Join(initiaConfigPath, "genesis.json")
497+
498+
time.Sleep(1500 * time.Millisecond)
499+
481500
if !utils.FileOrFolderExists(genesisFilePath) {
482-
m.state.existingGenesis = false
501+
state.existingGenesis = false
502+
return utils.EndLoading{}
503+
} else {
504+
state.existingGenesis = true
505+
return utils.EndLoading{}
506+
}
507+
}
508+
}
509+
510+
func (m *ExistingGenesisChecker) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
511+
loader, cmd := m.loading.Update(msg)
512+
m.loading = loader
513+
if m.loading.Completing {
514+
if !m.state.existingGenesis {
483515
if m.state.network == string(Local) {
484516
newLoader := NewInitializingAppLoading(m.state)
485517
return newLoader, newLoader.Init()
486518
}
487519
return NewGenesisEndpointInput(m.state), nil
488520
} else {
489-
m.state.existingGenesis = true
490521
return NewExistingGenesisReplaceSelect(m.state), nil
491522
}
492-
default:
493-
return m, nil
494523
}
524+
return m, cmd
495525
}
496526

497527
func (m *ExistingGenesisChecker) View() string {
498-
return m.state.weave.PreviousResponse + "Checking for an existing Initia genesis file..."
528+
return m.state.weave.PreviousResponse + "\n" + m.loading.View()
499529
}
500530

501531
type ExistingGenesisReplaceSelect struct {
@@ -676,7 +706,8 @@ func (m *SyncMethodSelect) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
676706
if selected != nil {
677707
m.state.syncMethod = string(*selected)
678708
m.state.weave.PreviousResponse += styles.RenderPreviousResponse(styles.ArrowSeparator, m.GetQuestion(), []string{""}, string(*selected))
679-
return NewExistingDataChecker(m.state), utils.DoTick()
709+
model := NewExistingDataChecker(m.state)
710+
return model, model.Init()
680711
}
681712

682713
return m, cmd
@@ -691,31 +722,47 @@ func (m *SyncMethodSelect) View() string {
691722
}
692723

693724
type ExistingDataChecker struct {
694-
state *RunL1NodeState
725+
state *RunL1NodeState
726+
loading utils.Loading
695727
}
696728

697729
func NewExistingDataChecker(state *RunL1NodeState) *ExistingDataChecker {
698730
return &ExistingDataChecker{
699-
state: state,
731+
state: state,
732+
loading: utils.NewLoading("Checking for an existing Initia data...", WaitExistingDataChecker(state)),
700733
}
701734
}
702735

703736
func (m *ExistingDataChecker) Init() tea.Cmd {
704-
return utils.DoTick()
737+
return m.loading.Init()
705738
}
706739

707-
func (m *ExistingDataChecker) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
708-
switch msg.(type) {
709-
case utils.TickMsg:
740+
func WaitExistingDataChecker(state *RunL1NodeState) tea.Cmd {
741+
return func() tea.Msg {
710742
homeDir, err := os.UserHomeDir()
711743
if err != nil {
712744
fmt.Printf("[error] Failed to get user home directory: %v\n", err)
713-
return m, tea.Quit
745+
return utils.ErrorLoading{Err: err}
714746
}
715747

716748
initiaDataPath := filepath.Join(homeDir, utils.InitiaDataDirectory)
749+
time.Sleep(1500 * time.Millisecond)
750+
717751
if !utils.FileOrFolderExists(initiaDataPath) {
718-
m.state.existingData = false
752+
state.existingData = false
753+
return utils.EndLoading{}
754+
} else {
755+
state.existingData = true
756+
return utils.EndLoading{}
757+
}
758+
}
759+
}
760+
761+
func (m *ExistingDataChecker) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
762+
loader, cmd := m.loading.Update(msg)
763+
m.loading = loader
764+
if m.loading.Completing {
765+
if !m.state.existingData {
719766
switch m.state.syncMethod {
720767
case string(Snapshot):
721768
return NewSnapshotEndpointInput(m.state), nil
@@ -727,13 +774,12 @@ func (m *ExistingDataChecker) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
727774
m.state.existingData = true
728775
return NewExistingDataReplaceSelect(m.state), nil
729776
}
730-
default:
731-
return m, nil
732777
}
778+
return m, cmd
733779
}
734780

735781
func (m *ExistingDataChecker) View() string {
736-
return m.state.weave.PreviousResponse + "Checking for an existing Initia data..."
782+
return m.state.weave.PreviousResponse + "\n" + m.loading.View()
737783
}
738784

739785
type ExistingDataReplaceSelect struct {

styles/text.go

+1-7
Original file line numberDiff line numberDiff line change
@@ -111,13 +111,7 @@ func RenderPrompt(text string, highlights []string, status PromptStatus) string
111111

112112
for _, highlight := range highlights {
113113
if strings.Contains(text, highlight) {
114-
coloredHighlight := ""
115-
if status == Information {
116-
coloredHighlight = BoldText(highlight, Ivory)
117-
} else {
118-
coloredHighlight = BoldText(highlight, Cyan)
119-
}
120-
text = strings.ReplaceAll(text, highlight, coloredHighlight)
114+
text = strings.ReplaceAll(text, highlight, BoldText(highlight, Cyan))
121115
}
122116
}
123117

0 commit comments

Comments
 (0)