package components import "github.com/tigorlazuardi/redmage/models" import "fmt" import "github.com/tigorlazuardi/redmage/views/utils" type ImageCardOption uint func (o ImageCardOption) Has(opt ImageCardOption) bool { return o&opt != 0 } const ( HideTitle ImageCardOption = 1 << iota HideSubreddit HidePoster HideDevice ) templ ImageCard(data *models.Image, opts ImageCardOption) {
{
if !opts.Has(HideTitle) { { truncateTitle(data.PostTitle) } } { data.PostAuthor }
@utils.RelativeTimeNode(fmt.Sprintf("relative-time-%s", data.PostName), data.CreatedAt, "text-sm")

{ fmt.Sprintf("%d \u00d7 %d", data.ImageWidth, data.ImageHeight) } px

{ formatByteSize(data.ImageSize) }

if data.R.Device != nil && !opts.Has(HideDevice) {
{ data.R.Device.Name }
}
} func formatByteSize(size int64) string { if size < 1024 { return fmt.Sprintf("%d B", size) } if size < 1024*1024 { return fmt.Sprintf("%.2f KiB", float64(size)/1024) } if size < 1024*1024*1024 { return fmt.Sprintf("%.2f MiB", float64(size)/(1024*1024)) } return fmt.Sprintf("%.2f GiB", float64(size)/(1024*1024*1024)) } func truncateTitle(title string) string { if len(title) > 52 { return title[:50] + "..." } return title }