---
import type { WordpressPost } from "../lib/content/types";
import StandardCard from "./StandardCard.astro";

type Props = {
  post: WordpressPost;
  placement: string;
  targetId: string;
};

const { post, placement, targetId } = Astro.props;

function truncate(s: string, n: number) {
  const t = (s || "").trim();
  if (!t) return "";
  if (t.length <= n) return t;
  return `${t.slice(0, Math.max(0, n - 1)).trimEnd()}…`;
}

const d = new Date(post.publishedAt);
const dateLabel = Number.isFinite(d.valueOf())
  ? d.toLocaleDateString(undefined, { year: "numeric", month: "short", day: "numeric" })
  : "";
---

<StandardCard
  href={`/blog/post/${post.slug}`}
  title={post.title}
  summary={post.excerpt}
  imageUrl={post.featuredImageUrl}
  dateLabel={dateLabel}
  viewsLabel={undefined}
  sourceLabel="blog"
  isExternal={false}
  linkAttrs={{
    "data-umami-event": "click",
    "data-umami-event-target_id": targetId,
    "data-umami-event-placement": placement,
    "data-umami-event-target_url": `/blog/post/${post.slug}`,
    "data-umami-event-title": truncate(post.title, 160),
    "data-umami-event-type": "blog_post",
  }}
/>

