Show user's active games on home page after login

This commit is contained in:
Ryan Hamamura
2026-01-14 17:11:27 -10:00
parent 2153b6ad75
commit d96f7dcc29
5 changed files with 250 additions and 1 deletions

79
main.go
View File

@@ -43,6 +43,24 @@ func main() {
username := c.Session().GetString("username")
isLoggedIn := userID != ""
var userGames []ui.GameListItem
if isLoggedIn {
ctx := context.Background()
games, err := queries.GetUserActiveGames(ctx, sql.NullString{String: userID, Valid: true})
if err == nil {
for _, g := range games {
isMyTurn := g.Status == 1 && g.CurrentTurn == g.MyColor
userGames = append(userGames, ui.GameListItem{
ID: g.ID,
Status: int(g.Status),
OpponentName: g.OpponentNickname.String,
IsMyTurn: isMyTurn,
LastPlayed: g.UpdatedAt.Time,
})
}
}
}
nickname := c.Signal("")
if isLoggedIn {
nickname = c.Signal(username)
@@ -72,6 +90,7 @@ func main() {
isLoggedIn,
username,
logout.OnClick(),
userGames,
)
})
})
@@ -489,4 +508,64 @@ const gameCSS = `
border-radius: 8px;
margin-bottom: 1rem;
}
.game-list {
margin-top: 2rem;
text-align: left;
}
.game-list h3 {
margin-bottom: 1rem;
text-align: center;
}
.game-list-items {
display: flex;
flex-direction: column;
gap: 0.5rem;
}
.game-entry {
display: flex;
justify-content: space-between;
align-items: center;
padding: 0.75rem 1rem;
background: var(--pico-muted-background);
border-radius: 8px;
text-decoration: none;
color: inherit;
transition: background 0.2s;
}
.game-entry:hover {
background: var(--pico-secondary-background);
}
.game-entry-main {
display: flex;
flex-direction: column;
gap: 0.25rem;
}
.opponent-name {
font-weight: bold;
}
.game-status {
font-size: 0.875rem;
}
.game-status.your-turn {
color: #22c55e;
font-weight: bold;
}
.game-status.waiting {
color: var(--pico-muted-color);
}
.time-ago {
font-size: 0.75rem;
color: var(--pico-muted-color);
}
`