themes-coffee/hours_strip.go
Alex Dunmow 11c6c8c63e initial: theme plugin coffee
Bootstrapped during the 2026-06-06 BlockNinja consolidation. Was previously
an unversioned directory inside ~/src/blockninja-themes/coffee.

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-06-06 14:11:22 +08:00

74 lines
1.6 KiB
Go

package main
import (
"bytes"
"context"
"time"
"git.dev.alexdunmow.com/block/core/blocks"
)
// HoursStripBlockMeta defines metadata for the hours_strip block.
var HoursStripBlockMeta = blocks.BlockMeta{
Key: "hours_strip",
Title: "Hours Strip",
Description: "Weekly hours strip with today's row server-side highlighted",
Source: "coffee",
}
// HoursStripBlock renders a weekly hours strip.
// Content shape:
//
// {
// "todayLabel": "Today",
// "hours": [
// {"day": "Mon", "open": "7:00", "close": "15:00"}, ...
// ]
// }
//
// The "today" row is detected server-side by comparing each row's day to the
// current local weekday — no JS required.
func HoursStripBlock(ctx context.Context, content map[string]any) string {
todayLabel := getString(content, "todayLabel")
if todayLabel == "" {
todayLabel = "Today"
}
today := shortDayName(time.Now())
rawHours := getSlice(content, "hours")
var rows []HoursRow
for _, h := range rawHours {
day := normaliseDay(getString(h, "day"))
rows = append(rows, HoursRow{
Day: day,
Open: getString(h, "open"),
Close: getString(h, "close"),
IsToday: day != "" && day == today,
})
}
data := HoursStripData{
TodayLabel: todayLabel,
Rows: rows,
}
var buf bytes.Buffer
_ = hoursStripComponent(data).Render(ctx, &buf)
return buf.String()
}
// HoursStripData contains data for the hours strip component.
type HoursStripData struct {
TodayLabel string
Rows []HoursRow
}
// HoursRow represents one weekday's hours.
type HoursRow struct {
Day string
Open string
Close string
IsToday bool
}