Files
terminalShop/pkg/tui/header.go
marcusDenslow 93b257a612 fix: store coffee prices as int cents to prevent rounding errors
prices stored as float64 could lead to rounding errors when converting to cents for stripe (19.99 * 100 = 1998.99... truncating to 1998). canged Coffee.Price to int, storing cents directly. Updated the seed data, fallback products, API handlers, and all TUI display code to be in sync with this change.
2026-03-26 09:09:20 +01:00

93 lines
2.8 KiB
Go

package tui
import (
"fmt"
"github.com/charmbracelet/lipgloss"
)
func (m Model) BuildHeader() string {
// Calculate cart total and item count
total := 0
itemCount := 0
for _, item := range m.Cart {
total += item.Quantity * item.Coffee.Price
itemCount += item.Quantity
}
// Define colors
grayStyle := lipgloss.NewStyle().Foreground(lipgloss.Color("#AAAAAA"))
boldWhiteStyle := lipgloss.NewStyle().Foreground(lipgloss.Color("#FFFFFF")).Bold(true)
boldGrayStyle := lipgloss.NewStyle().Foreground(lipgloss.Color("#AAAAAA")).Bold(true)
// Build cart tab (used in all sizes)
itemCountText := grayStyle.Render(fmt.Sprintf("[%d]", itemCount))
var cartKeybind, cartName, cartInfo string
if m.inCartFlow() {
// Active: bold white keybind and name
cartKeybind = boldWhiteStyle.Render("c")
cartName = boldWhiteStyle.Render("cart")
cartInfo = fmt.Sprintf(" %s %s",
boldWhiteStyle.Render(fmt.Sprintf("$%.2f", float64(total)/100)),
itemCountText)
} else {
// Inactive: bold gray keybind, gray name
cartKeybind = boldGrayStyle.Render("c")
cartName = grayStyle.Render("cart")
cartInfo = fmt.Sprintf(" %s %s",
boldWhiteStyle.Render(fmt.Sprintf("$%.2f", float64(total)/100)),
itemCountText)
}
cartTab := cartKeybind + " " + cartName + cartInfo
var tabsContent string
switch m.size {
case small:
// Small: just a mark + Cart
mark := boldWhiteStyle.Render("t")
tabsContent = mark + " " + cartTab
case medium:
menuTab := boldGrayStyle.Render("m") + " " + grayStyle.Render("menu")
logo := boldWhiteStyle.Render("terminal coffee")
separator := lipgloss.NewStyle().Foreground(lipgloss.Color("#FFFFFF")).Render("|")
tabsContent = menuTab + " " + separator + " " + logo + " " + separator + " " + cartTab
default:
separator := lipgloss.NewStyle().Foreground(lipgloss.Color("#FFFFFF")).Render("|")
var shopKeybind, shopName string
if m.currentPage == shopPage {
shopKeybind = boldWhiteStyle.Render("s")
shopName = boldWhiteStyle.Render("shop")
} else {
shopKeybind = boldGrayStyle.Render("s")
shopName = grayStyle.Render("shop")
}
var accountKeybind, accountName string
if m.currentPage == accountPage {
accountKeybind = boldWhiteStyle.Render("a")
accountName = boldWhiteStyle.Render("account")
} else {
accountKeybind = boldGrayStyle.Render("a")
accountName = grayStyle.Render("account")
}
tabsContent = fmt.Sprintf("%s %s %s %s %s %s %s", shopKeybind, shopName, separator, accountKeybind, accountName, separator, cartTab)
}
// Add box with padding (no vertical padding, just horizontal)
tabBox := lipgloss.NewStyle().
Border(lipgloss.NormalBorder()).
Padding(0, 2).
MarginBottom(1)
// Center the whole thing within the container
centered := lipgloss.NewStyle().
Width(m.widthContainer).
Align(lipgloss.Center)
return centered.Render(tabBox.Render(tabsContent))
}