aboutsummaryrefslogtreecommitdiffstats
path: root/src/components/BlogCard.astro
blob: 1a74025f5a1295ab9e271fd3c43fbf9ccb6ea961 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
---
import AvatarComponent from '@/components/ui/avatar'
import { Badge } from '@/components/ui/badge'
import { Separator } from '@/components/ui/separator'
import {
  getCombinedReadingTime,
  getSubpostCount,
  isSubpost,
  parseAuthors,
} from '@/lib/data-utils'
import { formatDate } from '@/lib/utils'
import { Icon } from 'astro-icon/components'
import { Image } from 'astro:assets'
import type { CollectionEntry } from 'astro:content'
import Link from './Link.astro'

interface Props {
  entry: CollectionEntry<'blog'>
}

const { entry } = Astro.props
const formattedDate = formatDate(entry.data.date)
const readTime = await getCombinedReadingTime(entry.id)
const authors = await parseAuthors(entry.data.authors ?? [])
const subpostCount = !isSubpost(entry.id) ? await getSubpostCount(entry.id) : 0
---

<div
  class="hover:bg-muted/50 rounded-xl border p-4 transition-colors duration-300 ease-in-out"
>
  <Link
    href={`/${entry.collection}/${entry.id}`}
    class="flex flex-col gap-4 sm:flex-row"
  >
    {
      entry.data.image && (
        <div class="w-full sm:max-w-3xs sm:shrink-0">
          <Image
            src={entry.data.image}
            alt={entry.data.title}
            width={1200}
            height={630}
            class="object-cover"
          />
        </div>
      )
    }

    <div class="grow">
      <h3 class="mb-1 text-lg font-medium">{entry.data.title}</h3>
      <p class="text-muted-foreground mb-2 text-sm">{entry.data.description}</p>

      <div
        class="text-muted-foreground mb-2 flex flex-wrap items-center gap-x-2 text-xs"
      >
        {
          authors.length > 0 && (
            <>
              {authors.map((author) => (
                <div class="flex items-center gap-x-1.5">
                  <AvatarComponent
                    client:load
                    src={author.avatar}
                    alt={author.name}
                    fallback={author.name[0]}
                    className="size-5 rounded-full"
                  />
                  <span>{author.name}</span>
                </div>
              ))}
              <Separator orientation="vertical" className="h-4!" />
            </>
          )
        }
        <span>{formattedDate}</span>
        <Separator orientation="vertical" className="h-4!" />
        <span>{readTime}</span>
        {
          subpostCount > 0 && (
            <>
              <Separator orientation="vertical" className="h-4!" />
              <span class="flex items-center gap-1">
                <Icon name="lucide:file-text" class="size-3" />
                {subpostCount} subpost{subpostCount === 1 ? '' : 's'}
              </span>
            </>
          )
        }
      </div>

      {
        entry.data.tags && (
          <div class="flex flex-wrap gap-2">
            {entry.data.tags.map((tag) => (
              <Badge variant="muted" className="flex items-center gap-x-1">
                <Icon name="lucide:hash" class="size-3" />
                {tag}
              </Badge>
            ))}
          </div>
        )
      }
    </div>
  </Link>
</div>