Files
games/ui/gamelist.go
Ryan Hamamura 5f452914f8 Add game deletion with authorization check
- Add Delete method to GameStore and Persister interface
- Add delete button to game list on home page
- Verify user owns game before allowing deletion
- Use status constants instead of magic numbers
- Remove unused variable in persister
2026-01-14 17:44:09 -10:00

111 lines
2.3 KiB
Go

package ui
import (
"fmt"
"time"
"github.com/ryanhamamura/c4/game"
"github.com/ryanhamamura/via/h"
)
type GameListItem struct {
ID string
Status int
OpponentName string
IsMyTurn bool
LastPlayed time.Time
}
func GameList(games []GameListItem, deleteClick func(id string) h.H) h.H {
if len(games) == 0 {
return nil
}
var items []h.H
for _, g := range games {
items = append(items, gameListEntry(g, deleteClick))
}
listItems := []h.H{h.Class("game-list-items")}
listItems = append(listItems, items...)
return h.Div(h.Class("game-list"),
h.H3(h.Text("Your Games")),
h.Div(listItems...),
)
}
func gameListEntry(g GameListItem, deleteClick func(id string) h.H) h.H {
statusText, statusClass := getStatusDisplay(g)
return h.Div(h.Class("game-entry"),
h.A(
h.Href("/game/"+g.ID),
h.Class("game-entry-link"),
h.Div(h.Class("game-entry-main"),
h.Span(h.Class("opponent-name"), h.Text(getOpponentDisplay(g))),
h.Span(h.Class("game-status "+statusClass), h.Text(statusText)),
),
h.Div(h.Class("game-entry-meta"),
h.Span(h.Class("time-ago"), h.Text(formatTimeAgo(g.LastPlayed))),
),
),
h.Button(
h.Type("button"),
h.Class("game-delete-btn"),
h.Text("\u00d7"),
deleteClick(g.ID),
),
)
}
func getStatusDisplay(g GameListItem) (string, string) {
switch game.GameStatus(g.Status) {
case game.StatusWaitingForPlayer:
return "Waiting for opponent", "waiting"
case game.StatusInProgress:
if g.IsMyTurn {
return "Your turn!", "your-turn"
}
return "Opponent's turn", "opponent-turn"
}
return "", ""
}
func getOpponentDisplay(g GameListItem) string {
if g.OpponentName == "" {
return "Waiting for opponent..."
}
return "vs " + g.OpponentName
}
func formatTimeAgo(t time.Time) string {
if t.IsZero() {
return ""
}
duration := time.Since(t)
if duration < time.Minute {
return "just now"
}
if duration < time.Hour {
mins := int(duration.Minutes())
if mins == 1 {
return "1 minute ago"
}
return fmt.Sprintf("%d minutes ago", mins)
}
if duration < 24*time.Hour {
hours := int(duration.Hours())
if hours == 1 {
return "1 hour ago"
}
return fmt.Sprintf("%d hours ago", hours)
}
days := int(duration.Hours() / 24)
if days == 1 {
return "yesterday"
}
return fmt.Sprintf("%d days ago", days)
}