From 6900bc6301bf668830ca029867f6d9de4c555d85 Mon Sep 17 00:00:00 2001 From: Danny Olson Date: Wed, 3 Apr 2024 15:40:50 -0700 Subject: [PATCH] feat: Can go back to choose SDK page from show SDK instructions (#120) Can go back to choose SDK page from show SDK instructions with esc --- internal/quickstart/choose_sdk.go | 19 +++++++---- internal/quickstart/container.go | 57 +++++++++++++++++++++++-------- internal/quickstart/messages.go | 20 ++++++----- 3 files changed, 67 insertions(+), 29 deletions(-) diff --git a/internal/quickstart/choose_sdk.go b/internal/quickstart/choose_sdk.go index 72f0d908..69344f9b 100644 --- a/internal/quickstart/choose_sdk.go +++ b/internal/quickstart/choose_sdk.go @@ -22,11 +22,12 @@ const ( ) type chooseSDKModel struct { - list list.Model - selectedSDK sdkDetail + list list.Model + selectedIndex int + selectedSDK sdkDetail } -func NewChooseSDKModel() tea.Model { +func NewChooseSDKModel(selectedIndex int) tea.Model { l := list.New(sdksToItems(), sdkDelegate{}, 30, 14) l.Title = "Select your SDK:\n\n" // extra newlines to show pagination // reset title styles @@ -38,12 +39,13 @@ func NewChooseSDKModel() tea.Model { l.Paginator.PerPage = 5 return chooseSDKModel{ - list: l, + list: l, + selectedIndex: selectedIndex, } } func (m chooseSDKModel) Init() tea.Cmd { - return nil + return sendSelectedSDKMsg(m.selectedIndex) } func (m chooseSDKModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) { @@ -55,12 +57,14 @@ func (m chooseSDKModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) { i, ok := m.list.SelectedItem().(sdkDetail) if ok { m.selectedSDK = i + m.selectedSDK.index = m.list.Index() + cmd = sendChoseSDKMsg(m.selectedSDK) } - - return m, sendChoseSDKMsg(i) default: m.list, cmd = m.list.Update(msg) } + case selectedSDKMsg: + m.list.Select(msg.index) } return m, cmd @@ -73,6 +77,7 @@ func (m chooseSDKModel) View() string { type sdkDetail struct { canonicalName string displayName string + index int kind string url string // custom URL if it differs from the other SDKs } diff --git a/internal/quickstart/container.go b/internal/quickstart/container.go index 58275576..753d3d72 100644 --- a/internal/quickstart/container.go +++ b/internal/quickstart/container.go @@ -19,17 +19,16 @@ const ( // ContainerModel is a high level container model that controls the nested models wher each // represents a step in the quick-start flow. type ContainerModel struct { - err error - flagKey string - flagsClient flags.Client - quitMsg string // TODO: set this? - quitting bool - accessToken string baseUri string currentModel tea.Model currentStep int - sdkKind string + err error + flagKey string + flagsClient flags.Client + quitMsg string // TODO: set this? + quitting bool + sdk sdkDetail totalSteps int } @@ -56,32 +55,57 @@ func (m ContainerModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) { case key.Matches(msg, keys.Quit): m.quitting = true cmd = tea.Quit + case key.Matches(msg, keys.Back): + // if showing SDK instructions, let the user go back to choose a different SDK + if m.currentStep == 2 { + m.currentStep -= 1 + m.currentModel = NewChooseSDKModel(m.sdk.index) + cmd = m.currentModel.Init() + } default: // delegate all other input to the current model m.currentModel, cmd = m.currentModel.Update(msg) } case choseSDKMsg: - m.currentModel = NewShowSDKInstructionsModel(m.accessToken, m.baseUri, msg.canonicalName, msg.url, m.flagKey) + m.currentModel = NewShowSDKInstructionsModel( + m.accessToken, + m.baseUri, + msg.sdk.canonicalName, + msg.sdk.url, + m.flagKey, + ) cmd = m.currentModel.Init() - m.sdkKind = msg.sdkKind + m.sdk = msg.sdk m.currentStep += 1 case createdFlagMsg: - m.currentModel = NewChooseSDKModel() + m.currentModel = NewChooseSDKModel(0) m.flagKey = msg.flagKey // TODO: figure out if we maintain state here or pass in another message m.currentStep += 1 case errMsg: m.err = msg.err case noInstructionsMsg: // skip the ShowSDKInstructionsModel and move along to toggling the flag - m.currentModel = NewToggleFlagModel(m.flagsClient, m.accessToken, m.baseUri, m.flagKey, m.sdkKind) + m.currentModel = NewToggleFlagModel( + m.flagsClient, + m.accessToken, + m.baseUri, + m.flagKey, + m.sdk.kind, + ) m.currentStep += 1 - case fetchedSDKInstructions, fetchedEnv, toggledFlagMsg: + case fetchedSDKInstructions, fetchedEnv, selectedSDKMsg, toggledFlagMsg: m.currentModel, cmd = m.currentModel.Update(msg) case showToggleFlagMsg: - m.currentModel = NewToggleFlagModel(m.flagsClient, m.accessToken, m.baseUri, m.flagKey, m.sdkKind) + m.currentModel = NewToggleFlagModel( + m.flagsClient, + m.accessToken, + m.baseUri, + m.flagKey, + m.sdk.kind, + ) m.currentStep += 1 default: - log.Println("container default - bad", msg) + log.Printf("container default: %T\n", msg) } return m, cmd @@ -114,12 +138,17 @@ func (m ContainerModel) View() string { } type keyMap struct { + Back key.Binding Enter key.Binding Quit key.Binding Tab key.Binding } var keys = keyMap{ + Back: key.NewBinding( + key.WithKeys("esc"), + key.WithHelp("esc", "go back"), + ), Enter: key.NewBinding( key.WithKeys("enter"), key.WithHelp("enter", "select"), diff --git a/internal/quickstart/messages.go b/internal/quickstart/messages.go index 68d0b5db..d44653c3 100644 --- a/internal/quickstart/messages.go +++ b/internal/quickstart/messages.go @@ -71,10 +71,7 @@ type fetchedSDKInstructions struct { } type choseSDKMsg struct { - canonicalName string - displayName string - sdkKind string - url string + sdk sdkDetail } func sendChoseSDKMsg(sdk sdkDetail) tea.Cmd { @@ -84,10 +81,7 @@ func sendChoseSDKMsg(sdk sdkDetail) tea.Cmd { } return choseSDKMsg{ - canonicalName: sdk.canonicalName, - displayName: sdk.displayName, - url: sdk.url, - sdkKind: sdk.kind, + sdk: sdk, } } } @@ -147,3 +141,13 @@ func sendFetchEnv(accessToken string, baseUri string, key string, projKey string // noInstructionsMsg is sent when we can't find the SDK instructions repository for the given SDK. type noInstructionsMsg struct{} + +type selectedSDKMsg struct { + index int +} + +func sendSelectedSDKMsg(index int) tea.Cmd { + return func() tea.Msg { + return selectedSDKMsg{index: index} + } +}