Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: showing a loading message #364

Merged
merged 6 commits into from
Jun 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions ui/components/issuessection/issuessection.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ func (m Model) Update(msg tea.Msg) (section.Section, tea.Cmd) {
currIssue.Assignees.Nodes = removeAssignees(currIssue.Assignees.Nodes, msg.RemovedAssignees.Nodes)
}
m.Issues[i] = currIssue
m.Table.SetIsLoading(false)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If L122 and L136 is not added, issues can not be shown because Loading... is showed always.

m.Table.SetRows(m.BuildRows())
break
}
Expand All @@ -132,6 +133,7 @@ func (m Model) Update(msg tea.Msg) (section.Section, tea.Cmd) {
m.Issues = msg.Issues
}
m.TotalCount = msg.TotalCount
m.Table.SetIsLoading(false)
m.PageInfo = &msg.PageInfo
m.Table.SetRows(m.BuildRows())
m.UpdateLastUpdated(time.Now())
Expand All @@ -144,7 +146,11 @@ func (m Model) Update(msg tea.Msg) (section.Section, tea.Cmd) {

prompt, promptCmd := m.PromptConfirmationBox.Update(msg)
m.PromptConfirmationBox = prompt
return &m, tea.Batch(cmd, searchCmd, promptCmd)

table, tableCmd := m.Table.Update(msg)
m.Table = table

return &m, tea.Batch(cmd, searchCmd, promptCmd, tableCmd)
}

func GetSectionColumns(
Expand Down Expand Up @@ -301,7 +307,6 @@ func (m *Model) FetchNextPageSectionRows() []tea.Cmd {
TaskId: taskId,
},
}

}
cmds = append(cmds, fetchCmd)

Expand Down
35 changes: 23 additions & 12 deletions ui/components/prssection/prssection.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,16 @@ func NewModel(
lastUpdated time.Time,
) Model {
m := Model{}
m.Model =
section.NewModel(
id,
ctx,
cfg.ToSectionConfig(),
SectionType,
GetSectionColumns(cfg, ctx),
m.GetItemSingularForm(),
m.GetItemPluralForm(),
lastUpdated,
)
m.Model = section.NewModel(
id,
ctx,
cfg.ToSectionConfig(),
SectionType,
GetSectionColumns(cfg, ctx),
m.GetItemSingularForm(),
m.GetItemPluralForm(),
lastUpdated,
)
m.Prs = []data.PullRequestData{}

return m
Expand Down Expand Up @@ -150,6 +149,7 @@ func (m Model) Update(msg tea.Msg) (section.Section, tea.Cmd) {
currPr.Mergeable = ""
}
m.Prs[i] = currPr
m.Table.SetIsLoading(false)
m.Table.SetRows(m.BuildRows())
break
}
Expand All @@ -164,6 +164,7 @@ func (m Model) Update(msg tea.Msg) (section.Section, tea.Cmd) {
}
m.TotalCount = msg.TotalCount
m.PageInfo = &msg.PageInfo
m.Table.SetIsLoading(false)
m.Table.SetRows(m.BuildRows())
m.UpdateLastUpdated(time.Now())
m.UpdateTotalItemsCount(m.TotalCount)
Expand All @@ -176,7 +177,11 @@ func (m Model) Update(msg tea.Msg) (section.Section, tea.Cmd) {

prompt, promptCmd := m.PromptConfirmationBox.Update(msg)
m.PromptConfirmationBox = prompt
return &m, tea.Batch(cmd, searchCmd, promptCmd)

table, tableCmd := m.Table.Update(msg)
m.Table = table

return &m, tea.Batch(cmd, searchCmd, promptCmd, tableCmd)
}

func GetSectionColumns(
Expand Down Expand Up @@ -357,6 +362,12 @@ func (m *Model) FetchNextPageSectionRows() []tea.Cmd {
}
cmds = append(cmds, fetchCmd)

if m.PageInfo == nil {
m.Table.SetIsLoading(true)
cmds = append(cmds, m.Table.StartLoadingSpinner())

}

return cmds
}

Expand Down
2 changes: 2 additions & 0 deletions ui/components/section/section.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ func NewModel(
m.PluralForm,
),
)),
"Loading...",
true,
)
return m
}
Expand Down
63 changes: 51 additions & 12 deletions ui/components/table/table.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
package table

import (
"fmt"
"time"

"github.com/charmbracelet/bubbles/spinner"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"

"github.com/dlvhdr/gh-dash/ui/common"
Expand All @@ -12,12 +15,15 @@ import (
)

type Model struct {
ctx context.ProgramContext
Columns []Column
Rows []Row
EmptyState *string
dimensions constants.Dimensions
rowsViewport listviewport.Model
ctx context.ProgramContext
Columns []Column
Rows []Row
EmptyState *string
loadingMessage string
isLoading bool
loadingSpinner spinner.Model
dimensions constants.Dimensions
rowsViewport listviewport.Model
}

type Column struct {
Expand All @@ -37,17 +43,27 @@ func NewModel(
rows []Row,
itemTypeLabel string,
emptyState *string,
loadingMessage string,
isLoading bool,
) Model {
itemHeight := 1
if ctx.Config.Theme.Ui.Table.ShowSeparator {
itemHeight = 2
}

loadingSpinner := spinner.New()
loadingSpinner.Spinner = spinner.Dot
loadingSpinner.Style = lipgloss.NewStyle().Foreground(ctx.Theme.SecondaryText)

return Model{
ctx: ctx,
Columns: columns,
Rows: rows,
EmptyState: emptyState,
dimensions: dimensions,
ctx: ctx,
Columns: columns,
Rows: rows,
EmptyState: emptyState,
loadingMessage: loadingMessage,
isLoading: isLoading,
loadingSpinner: loadingSpinner,
dimensions: dimensions,
rowsViewport: listviewport.NewModel(
ctx,
dimensions,
Expand All @@ -59,13 +75,27 @@ func NewModel(
}
}

func (m Model) Update(msg tea.Msg) (Model, tea.Cmd) {
var cmd tea.Cmd
m.loadingSpinner, cmd = m.loadingSpinner.Update(msg)
return m, cmd
}

func (m Model) StartLoadingSpinner() tea.Cmd {
return m.loadingSpinner.Tick
}

func (m Model) View() string {
header := m.renderHeader()
body := m.renderBody()

return lipgloss.JoinVertical(lipgloss.Left, header, body)
}

func (m *Model) SetIsLoading(isLoading bool) {
m.isLoading = isLoading
}

func (m *Model) SetDimensions(dimensions constants.Dimensions) {
m.dimensions = dimensions
m.rowsViewport.SetDimensions(constants.Dimensions{
Expand Down Expand Up @@ -209,6 +239,16 @@ func (m *Model) renderBody() string {
Height(m.dimensions.Height).
MaxWidth(m.dimensions.Width)

if m.isLoading {
return lipgloss.Place(
m.dimensions.Width,
m.dimensions.Height,
lipgloss.Center,
lipgloss.Center,
fmt.Sprintf("%s%s", m.loadingSpinner.View(), m.loadingMessage),
)
}

if len(m.Rows) == 0 && m.EmptyState != nil {
return bodyStyle.Render(*m.EmptyState)
}
Expand Down Expand Up @@ -249,7 +289,6 @@ func (m *Model) renderRow(rowId int, headerColumns []string) string {
BorderBottom(m.ctx.Config.Theme.Ui.Table.ShowSeparator).
MaxWidth(m.dimensions.Width).
Render(lipgloss.JoinHorizontal(lipgloss.Top, renderedColumns...))

}

func (m *Model) UpdateProgramContext(ctx *context.ProgramContext) {
Expand Down
Loading