Skip to content

Commit

Permalink
feat: Can go back to choose SDK page from show SDK instructions (#120)
Browse files Browse the repository at this point in the history
Can go back to choose SDK page from show SDK instructions with esc
  • Loading branch information
dbolson authored Apr 3, 2024
1 parent 286428e commit 6900bc6
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 29 deletions.
19 changes: 12 additions & 7 deletions internal/quickstart/choose_sdk.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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) {
Expand All @@ -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
Expand All @@ -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
}
Expand Down
57 changes: 43 additions & 14 deletions internal/quickstart/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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
Expand Down Expand Up @@ -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"),
Expand Down
20 changes: 12 additions & 8 deletions internal/quickstart/messages.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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,
}
}
}
Expand Down Expand Up @@ -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}
}
}

0 comments on commit 6900bc6

Please sign in to comment.