|
| 1 | +package driver |
| 2 | + |
| 3 | +import ( |
| 4 | + "github.com/charmbracelet/bubbles/table" |
| 5 | + tea "github.com/charmbracelet/bubbletea" |
| 6 | + |
| 7 | + "github.com/acifani/formula1-go/internal/ui" |
| 8 | + "github.com/acifani/formula1-go/internal/ui/page" |
| 9 | + "github.com/acifani/formula1-go/pkg/api" |
| 10 | +) |
| 11 | + |
| 12 | +type model struct { |
| 13 | + data *api.RaceTable |
| 14 | + table table.Model |
| 15 | + styles ui.Styles |
| 16 | + err error |
| 17 | +} |
| 18 | + |
| 19 | +type LoadDone struct { |
| 20 | + err error |
| 21 | + data *api.RaceTable |
| 22 | +} |
| 23 | + |
| 24 | +type BackMsg struct{} |
| 25 | + |
| 26 | +func New(styles ui.Styles) page.Model { |
| 27 | + columns := []table.Column{ |
| 28 | + {Title: "#", Width: 4}, |
| 29 | + {Title: "Race", Width: 25}, |
| 30 | + {Title: "Pos", Width: 4}, |
| 31 | + {Title: "Pts", Width: 4}, |
| 32 | + {Title: "Status", Width: 15}, |
| 33 | + } |
| 34 | + t := table.New(table.WithColumns(columns)) |
| 35 | + t.SetStyles(styles.Table) |
| 36 | + |
| 37 | + return &model{table: t, styles: styles} |
| 38 | +} |
| 39 | + |
| 40 | +func (m model) GetPageTitle() string { |
| 41 | + if m.data != nil { |
| 42 | + driver := m.data.Races[0].Results[0].Driver |
| 43 | + team := m.data.Races[0].Results[0].Constructor |
| 44 | + return driver.PermanentNumber + " " + driver.GivenName + " " + driver.FamilyName + " - " + team.Name |
| 45 | + } |
| 46 | + return "" |
| 47 | +} |
| 48 | + |
| 49 | +func (m model) Init() tea.Cmd { |
| 50 | + return nil |
| 51 | +} |
| 52 | + |
| 53 | +func (m model) Update(msg tea.Msg) (page.Model, tea.Cmd) { |
| 54 | + var cmd tea.Cmd |
| 55 | + |
| 56 | + switch msg := msg.(type) { |
| 57 | + case tea.KeyMsg: |
| 58 | + switch msg.String() { |
| 59 | + case "esc": |
| 60 | + return m, backMsg |
| 61 | + } |
| 62 | + case LoadDone: |
| 63 | + if msg.err != nil { |
| 64 | + m.err = msg.err |
| 65 | + } else { |
| 66 | + m.data = msg.data |
| 67 | + rows := generateRows(msg.data) |
| 68 | + m.table.SetHeight(len(rows)) |
| 69 | + m.table.SetRows(rows) |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + m.table, cmd = m.table.Update(msg) |
| 74 | + |
| 75 | + return m, cmd |
| 76 | +} |
| 77 | + |
| 78 | +func (m model) View() string { |
| 79 | + if m.err != nil { |
| 80 | + return m.styles.Paragraph.Render(m.err.Error()) |
| 81 | + } |
| 82 | + |
| 83 | + return m.table.View() |
| 84 | +} |
| 85 | + |
| 86 | +func LoadResults(year, driverID string) tea.Cmd { |
| 87 | + return func() tea.Msg { |
| 88 | + results, err := api.GetDriverRaceResults(year, driverID) |
| 89 | + return LoadDone{data: results, err: err} |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +func generateRows(data *api.RaceTable) []table.Row { |
| 94 | + rows := make([]table.Row, len(data.Races)) |
| 95 | + for i, race := range data.Races { |
| 96 | + rows[i] = table.Row{ |
| 97 | + race.Round, |
| 98 | + race.RaceName, |
| 99 | + race.Results[0].PositionText, |
| 100 | + race.Results[0].Points, |
| 101 | + race.Results[0].Status, |
| 102 | + } |
| 103 | + } |
| 104 | + return rows |
| 105 | +} |
| 106 | + |
| 107 | +func backMsg() tea.Msg { |
| 108 | + return BackMsg{} |
| 109 | +} |
0 commit comments