Redmage/views/homeview/recently_added_image.templ

74 lines
2.2 KiB
Plaintext
Raw Normal View History

package homeview
import "github.com/tigorlazuardi/redmage/models"
import "fmt"
import "github.com/tigorlazuardi/redmage/views/utils"
import "strconv"
import "github.com/alecthomas/units"
type ImageCardOption uint
func (o ImageCardOption) Has(opt ImageCardOption) bool {
return o&opt != 0
}
const (
HideTitle ImageCardOption = 1 << iota
HideDescription
HideSubreddit
HidePoster
)
templ RecentlyAddedImageCard(data *models.Image, opts ImageCardOption) {
<div class="not-prose card card-bordered bg-base-100 hover:bg-base-200 shadow-xl min-w-[256px] rounded-xl top-0 hover:-top-1 hover:drop-shadow-2xl transition-all">
<figure>
<a
href={ templ.URL(fmt.Sprintf("/img/%s", data.ImageRelativePath)) }
target="_blank"
>
<img
class="object-contain max-w-[256px] max-h-[256px]"
src={ fmt.Sprintf("/img/%s", data.ThumbnailRelativePath) }
2024-04-30 14:12:33 +07:00
alt={ data.PostTitle }
/>
</a>
</figure>
<div class="card-body">
if !opts.Has(HideTitle) {
<a
href={ templ.URL(data.PostURL) }
2024-04-29 13:31:09 +07:00
class="card-title font-bold underline text-base text-primary"
2024-04-30 14:12:33 +07:00
>{ truncateTitle(data.PostTitle) }</a>
}
2024-04-30 14:12:33 +07:00
<a class="text-primary underline" href={ templ.URL(data.PostAuthorURL) }>{ data.PostAuthor }</a>
<div class="flex-1"></div>
<div class="flex w-full justify-end">
@utils.RelativeTimeNode(fmt.Sprintf("relative-time-%s", data.PostName), data.CreatedAt, "text-sm")
</div>
<div class="grid grid-cols-2 gap-x-4">
<p class="text-xs">Width</p>
<p class="text-xs text-end">{ strconv.Itoa(int(data.ImageWidth)) } px</p>
<p class="text-xs">Height</p>
<p class="text-xs text-end">{ strconv.Itoa(int(data.ImageHeight)) } px</p>
<p class="text-xs">Size</p>
<p class="text-xs text-end">{ units.MetricBytes(data.ImageSize).Round(1).String() }</p>
</div>
</div>
</div>
}
func truncateTitle(title string) string {
if len(title) > 50 {
return title[:50] + "..."
}
return title
}
templ RecentlyAddedImageList(images models.ImageSlice, opts ImageCardOption) {
<div class="overflow-x-auto flex gap-4 p-6 shadow-inner bg-base-300 rounded-2xl w-[85vw] md:w-full">
for _, data := range images {
@RecentlyAddedImageCard(data, opts)
}
</div>
}