Skip to content

Commit

Permalink
improving error handling flow, and adding basic dashboard responsiven…
Browse files Browse the repository at this point in the history
…ess for horizontal layouts
  • Loading branch information
k1nho committed Sep 3, 2023
1 parent 21ae5eb commit 3b5a92e
Show file tree
Hide file tree
Showing 7 changed files with 269 additions and 185 deletions.
104 changes: 69 additions & 35 deletions cmd/show/contributors.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package show

import (
"context"
"errors"
"fmt"
"sync"

Expand All @@ -12,42 +13,36 @@ import (

// ContributorModel holds all the information related to a contributor
type ContributorModel struct {
username string
userInfo *client.DbUser
userPrs []client.DbPullRequest
}

// BackMsg: message to signal main model that we are back to dashboard when backspace is pressed
type BackMsg struct{}
type (
// BackMsg: message to signal main model that we are back to dashboard when backspace is pressed
BackMsg struct{}

// ContributorErrMsg: message to signal that an error ocurred when fetching contributor information
ContributorErrMsg struct {
name string
err error
}
)

// InitContributor: initializes the contributorModel
func InitContributor(contributorName string) (ContributorModel, tea.Cmd) {
func InitContributor(contributorName string) (ContributorModel, error) {
var contributorModel ContributorModel
var wg sync.WaitGroup

wg.Add(2)
go func() {
defer wg.Done()
// TODO: handle error
userInfo, _ := fetchContributorInfo(contributorName)
contributorModel.userInfo = userInfo
contributorModel.username = contributorName

}()

go func() {
defer wg.Done()
// TODO: handle error
userPRs, _ := fetchContributorPRs(contributorName)
contributorModel.userPrs = userPRs
}()

wg.Wait()
err := contributorModel.fetchUser()
if err != nil {
return contributorModel, err
}

return contributorModel, func() tea.Msg { return SuccessMsg{} }
return contributorModel, nil
}

func (m ContributorModel) Init() tea.Cmd {
return nil
}
func (m ContributorModel) Init() tea.Cmd { return nil }

func (m ContributorModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
var cmd tea.Cmd
Expand All @@ -56,18 +51,57 @@ func (m ContributorModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg.String() {
case "backspace":
return m, func() tea.Msg { return BackMsg{} }
case "q", "esc":
case "q", "esc", "ctrl+c", "ctrl+d":
return m, tea.Quit
}
}
return m, cmd
}

func (m ContributorModel) View() string {
if m.userInfo != nil {
return m.drawContributorView()
return m.drawContributorView()
}

// fetchUser: fetches all the user information (general info, and pull requests)
func (model *ContributorModel) fetchUser() error {
var (
wg sync.WaitGroup
errChan = make(chan error, 2)
)

wg.Add(2)
go func() {
defer wg.Done()
userInfo, err := fetchContributorInfo(model.username)
if err != nil {
errChan <- err
return
}
model.userInfo = userInfo

}()

go func() {
defer wg.Done()
userPRs, err := fetchContributorPRs(model.username)
if err != nil {
errChan <- err
return
}
model.userPrs = userPRs
}()

wg.Wait()
close(errChan)
if len(errChan) > 0 {
var allErrors error
for err := range errChan {
allErrors = errors.Join(allErrors, err)
}
return allErrors
}
return "🚧 Not found"

return nil
}

// fetchContributorInfo: fetches the contributor info
Expand Down Expand Up @@ -108,7 +142,7 @@ func fetchContributorPRs(name string) ([]client.DbPullRequest, error) {

// drawContributorView: view of the contributor model
func (m *ContributorModel) drawContributorView() string {
return viewport().Render(lipgloss.JoinVertical(lipgloss.Center, m.drawContributorInfo(), m.drawPullRequests()))
return Viewport.Copy().Render(lipgloss.JoinVertical(lipgloss.Center, m.drawContributorInfo(), m.drawPullRequests()))
}

// drawContributorInfo: view of the contributor info (open issues, pr velocity, pr count, maintainer)
Expand All @@ -118,13 +152,13 @@ func (m *ContributorModel) drawContributorInfo() string {
prVelocity := fmt.Sprintf("🔥 PR Velocity (30d): %d%%", m.userInfo.RecentPullRequestVelocityCount)
prCount := fmt.Sprintf("🚀 PR Count (30d): %d", m.userInfo.RecentPullRequestsCount)

prStats := lipgloss.JoinVertical(lipgloss.Left, textContainer().Render(prVelocity), textContainer().Render(prCount))
issuesAndMaintainer := lipgloss.JoinVertical(lipgloss.Center, textContainer().Render(userOpenIssues), textContainer().Render(isUserMaintainer))
prStats := lipgloss.JoinVertical(lipgloss.Left, TextContainer.Render(prVelocity), TextContainer.Render(prCount))
issuesAndMaintainer := lipgloss.JoinVertical(lipgloss.Center, TextContainer.Render(userOpenIssues), TextContainer.Render(isUserMaintainer))

contributorInfo := lipgloss.JoinHorizontal(lipgloss.Center, prStats, issuesAndMaintainer)
contributorView := lipgloss.JoinVertical(lipgloss.Center, m.userInfo.Login, contributorInfo)

return squareBorder().Render(contributorView)
return SquareBorder.Render(contributorView)
}

// drawPullRequests: view of the contributor pull requests (draws the last 5 pull requests)
Expand All @@ -143,7 +177,7 @@ func (m *ContributorModel) drawPullRequests() string {
}

for i := 0; i < numberOfPrs; i++ {
prContainer := textContainer().Render(fmt.Sprintf("#%d %s\n%s\n(%s)", m.userPrs[i].Number, m.userPrs[i].GetFullName(),
prContainer := TextContainer.Render(fmt.Sprintf("#%d %s\n%s\n(%s)", m.userPrs[i].Number, m.userPrs[i].GetFullName(),
m.userPrs[i].Title, m.userPrs[i].State))
pullRequests = append(pullRequests, prContainer)
}
Expand All @@ -152,5 +186,5 @@ func (m *ContributorModel) drawPullRequests() string {
title := lipgloss.NewStyle().AlignHorizontal(lipgloss.Center).Render("✨ Latest Pull Requests")

pullRequestView := lipgloss.JoinVertical(lipgloss.Center, title, formattedPrs)
return widgetContainer().Render(pullRequestView)
return WidgetContainer.Render(pullRequestView)
}
Loading

0 comments on commit 3b5a92e

Please sign in to comment.