Adding Cache

This commit is contained in:
2026-03-03 01:16:04 -05:00
parent f0641262bd
commit cb77df0eb6
7 changed files with 160 additions and 19 deletions

View File

@@ -26,6 +26,7 @@ type AppModel struct {
client *api.OpenF1Client
activeTab tabIndex
year int
width int
height int
@@ -37,11 +38,13 @@ type AppModel struct {
// NewAppModel creates the root model and wires sub-models.
func NewAppModel(client *api.OpenF1Client) AppModel {
year := 2025
return AppModel{
client: client,
activeTab: tabStandings,
standings: NewStandingsModel(client),
calendar: NewCalendarModel(client),
year: year,
standings: NewStandingsModel(client, year),
calendar: NewCalendarModel(client, year),
raceDetail: NewRaceDetailModel(client),
driver: NewDriverModel(client),
}
@@ -95,6 +98,23 @@ func (m AppModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
m.activeTab = tabCalendar
return m, nil
}
case "y":
// Cycle years: 2025 -> 2023 -> 2024 -> 2025
if m.year == 2025 {
m.year = 2023
} else {
m.year++
}
// Update sub-models and re-trigger fetches
m.calendar.year = m.year
m.calendar.loading = true
m.standings.year = m.year
m.standings.loading = true
return m, tea.Batch(
m.calendar.Init(),
m.standings.Init(),
)
}
case meetingSelectedMsg:

View File

@@ -19,11 +19,12 @@ type CalendarModel struct {
err error
spinner spinner.Model
cursor int
year int
width int
height int
}
func NewCalendarModel(client *api.OpenF1Client) CalendarModel {
func NewCalendarModel(client *api.OpenF1Client, year int) CalendarModel {
s := spinner.New()
s.Spinner = spinner.MiniDot
s.Style = lipgloss.NewStyle().Foreground(lipgloss.Color(colorF1Red))
@@ -31,6 +32,7 @@ func NewCalendarModel(client *api.OpenF1Client) CalendarModel {
client: client,
loading: true,
spinner: s,
year: year,
}
}
@@ -44,7 +46,7 @@ func fetchMeetings(client *api.OpenF1Client, year int) tea.Cmd {
func (m CalendarModel) Init() tea.Cmd {
return tea.Batch(
m.spinner.Tick,
fetchMeetings(m.client, 2025),
fetchMeetings(m.client, m.year),
)
}
@@ -108,13 +110,13 @@ func (m CalendarModel) findNextRaceIndex() int {
func (m CalendarModel) View() string {
if m.loading {
return fmt.Sprintf("\n %s Loading 2025 calendar…", m.spinner.View())
return fmt.Sprintf("\n %s Loading %d calendar…", m.spinner.View(), m.year)
}
if m.err != nil {
return styleError.Render(fmt.Sprintf("\n Error: %v", m.err))
}
if len(m.meetings) == 0 {
return styleMuted.Render("\n No meetings found for 2025.")
return styleMuted.Render(fmt.Sprintf("\n No meetings found for %d.", m.year))
}
const (
@@ -179,9 +181,10 @@ func (m CalendarModel) View() string {
}
var sb strings.Builder
sb.WriteString(styleBold.Render(fmt.Sprintf(" Season: %d", m.year)) + "\n\n")
sb.WriteString(strings.Join(rows, "\n"))
sb.WriteString("\n\n")
sb.WriteString(helpBar("j/k navigate", "enter select race", "q quit"))
sb.WriteString(helpBar("y season", "j/k navigate", "enter select race", "q quit"))
return sb.String()
}

View File

@@ -13,6 +13,7 @@ type GlobalKeyMap struct {
Down key.Binding
Enter key.Binding
Back key.Binding
Year key.Binding
}
// GlobalKeys is the singleton global key map.
@@ -53,6 +54,10 @@ var GlobalKeys = GlobalKeyMap{
key.WithKeys("b"),
key.WithHelp("b", "back"),
),
Year: key.NewBinding(
key.WithKeys("y"),
key.WithHelp("y", "switch year"),
),
}
// StandingsKeyMap holds standing-specific keybindings.

View File

@@ -30,12 +30,13 @@ type StandingsModel struct {
err error
spinner spinner.Model
year int
cursor int
width int
height int
}
func NewStandingsModel(client *api.OpenF1Client) StandingsModel {
func NewStandingsModel(client *api.OpenF1Client, year int) StandingsModel {
s := spinner.New()
s.Spinner = spinner.MiniDot
s.Style = lipgloss.NewStyle().Foreground(lipgloss.Color(colorF1Red))
@@ -45,27 +46,28 @@ func NewStandingsModel(client *api.OpenF1Client) StandingsModel {
loading: true,
spinner: s,
drivers: make(map[int]models.Driver),
year: year,
}
}
func (m StandingsModel) Init() tea.Cmd {
return tea.Batch(
m.spinner.Tick,
fetchDriverChampionship(m.client),
fetchTeamChampionship(m.client),
fetchDriverChampionship(m.client, m.year),
fetchTeamChampionship(m.client, m.year),
)
}
func fetchDriverChampionship(client *api.OpenF1Client) tea.Cmd {
func fetchDriverChampionship(client *api.OpenF1Client, year int) tea.Cmd {
return func() tea.Msg {
standings, err := client.GetLatestDriverChampionship()
standings, err := client.GetDriverChampionshipForYear(year)
return driverChampionshipLoadedMsg{standings: standings, err: err}
}
}
func fetchTeamChampionship(client *api.OpenF1Client) tea.Cmd {
func fetchTeamChampionship(client *api.OpenF1Client, year int) tea.Cmd {
return func() tea.Msg {
standings, err := client.GetLatestTeamChampionship()
standings, err := client.GetTeamChampionshipForYear(year)
return teamChampionshipLoadedMsg{standings: standings, err: err}
}
}
@@ -138,7 +140,7 @@ func (m StandingsModel) Update(msg tea.Msg) (StandingsModel, tea.Cmd) {
func (m StandingsModel) View() string {
if m.loading {
return fmt.Sprintf("\n %s Loading championship standings…", m.spinner.View())
return fmt.Sprintf("\n %s Loading %d championship standings…", m.spinner.View(), m.year)
}
if m.err != nil {
return styleError.Render(fmt.Sprintf("\n Error: %v", m.err))
@@ -146,6 +148,9 @@ func (m StandingsModel) View() string {
var sb strings.Builder
// Year indicator
sb.WriteString(styleBold.Render(fmt.Sprintf(" Season: %d", m.year)) + "\n\n")
// Toggle bar
dStyle, cStyle := styleInactiveTab, styleInactiveTab
if m.view == standingsViewDriver {
@@ -166,7 +171,7 @@ func (m StandingsModel) View() string {
}
sb.WriteString("\n")
sb.WriteString(helpBar("d drivers", "c constructors", "j/k navigate", "q quit"))
sb.WriteString(helpBar("y season", "d drivers", "c constructors", "j/k navigate", "q quit"))
return sb.String()
}