Redmage/views/homeview/recently_added_image.templ

83 lines
2.3 KiB
Plaintext
Raw Normal View History

package homeview
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
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>
2024-05-01 14:39:34 +07:00
<div class="flex-1"></div>
<div class="card-body">
if !opts.Has(HideTitle) {
<a
href={ templ.URL(data.PostURL) }
2024-05-01 14:39:34 +07:00
class="card-title font-bold underline text-sm text-primary"
2024-04-30 14:12:33 +07:00
>{ truncateTitle(data.PostTitle) }</a>
}
2024-05-01 14:39:34 +07:00
<a class="text-primary text-sm underline" href={ templ.URL(data.PostAuthorURL) }>{ data.PostAuthor }</a>
<div class="flex-1"></div>
2024-05-01 14:39:34 +07:00
<div class="flex">
@utils.RelativeTimeNode(fmt.Sprintf("relative-time-%s", data.PostName), data.CreatedAt, "text-sm")
</div>
2024-05-01 14:27:09 +07:00
<div class="grid grid-cols-2">
<p class="text-xs">{ fmt.Sprintf("%d \u00d7 %d", data.ImageWidth, data.ImageHeight) } px</p>
<p class="text-xs text-end">{ formatByteSize(data.ImageSize) }</p>
</div>
</div>
</div>
}
2024-05-01 14:27:09 +07:00
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) > 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>
}