{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "tweet-card",
  "title": "Tweet Card",
  "description": "A post from X in your theme, with no embed script: a card, a server component that fetches by id, and a client one.",
  "dependencies": [
    "react-tweet@^3.3.1",
    "lucide-react"
  ],
  "registryDependencies": [
    "@corsair-ui/utils"
  ],
  "files": [
    {
      "path": "registry/default/ui/tweet-card.tsx",
      "content": "\"use client\";\n\nimport { BadgeCheckIcon, CheckIcon, HeartIcon, LinkIcon } from \"lucide-react\";\nimport { useEffect, useRef, useState, type ComponentProps } from \"react\";\nimport { enrichTweet, useTweet, type EnrichedTweet } from \"react-tweet\";\nimport type { Tweet, TweetEntities } from \"react-tweet/api\";\n\nimport { cn } from \"@/registry/default/lib/utils\";\n\n// The X logo.\nconst X_LOGO =\n  \"M14.234 10.162 22.977 0h-2.072l-7.591 8.824L7.251 0H.258l9.168 13.343L.258 24H2.33l8.016-9.318L16.749 24h6.993zm-2.837 3.299-.929-1.329L3.076 1.56h3.182l5.965 8.532.929 1.329 7.754 11.09h-3.182z\";\n\nconst ENTITIES: Record<string, string> = {\n  amp: \"&\",\n  lt: \"<\",\n  gt: \">\",\n  quot: '\"',\n  apos: \"'\",\n  nbsp: \"\\u00a0\",\n};\n\n/** X sends tweet text HTML-escaped. Decode it as text, never as markup. */\nfunction decode(text: string) {\n  return text.replace(/&(#x[\\da-f]+|#\\d+|[a-z]+);/gi, (match, entity: string) => {\n    if (entity[0] === \"#\") {\n      const code =\n        entity[1] === \"x\" || entity[1] === \"X\"\n          ? parseInt(entity.slice(2), 16)\n          : parseInt(entity.slice(1), 10);\n      return Number.isFinite(code) && code > 0 && code < 0x110000\n        ? String.fromCodePoint(code)\n        : match;\n    }\n    return ENTITIES[entity.toLowerCase()] ?? match;\n  });\n}\n\nfunction compact(count: number) {\n  return new Intl.NumberFormat(\"en-US\", { notation: \"compact\", maximumFractionDigits: 1 }).format(\n    count\n  );\n}\n\n/**\n * The syndication API leaves out empty entity lists, and older versions of\n * the enricher assumed they were there. Fill them in, and fall back to\n * nothing rather than throwing on a tweet shape nobody expected.\n */\nconst ENTITY_LISTS = [\"hashtags\", \"urls\", \"user_mentions\", \"symbols\"] as const;\n\nfunction enrich(tweet: Tweet): EnrichedTweet | null {\n  const entities: Partial<TweetEntities> = { ...tweet.entities };\n  for (const list of ENTITY_LISTS) entities[list] ??= [];\n  try {\n    return enrichTweet({ ...tweet, entities: entities as TweetEntities });\n  } catch {\n    return null;\n  }\n}\n\nconst frame = \"w-full max-w-xl rounded-xl border bg-card p-4 text-card-foreground shadow\";\n\n/** The card's size and shape while a tweet loads. */\nfunction TweetCardSkeleton({ className, ...props }: ComponentProps<\"div\">) {\n  return (\n    <div\n      role=\"status\"\n      aria-label=\"Loading post\"\n      data-slot=\"tweet-card-skeleton\"\n      className={cn(frame, className)}\n      {...props}\n    >\n      <div className=\"flex items-center gap-2.5\">\n        <div className=\"bg-muted size-10 shrink-0 animate-pulse rounded-full\" />\n        <div className=\"grid gap-1.5\">\n          <div className=\"bg-muted h-3.5 w-28 animate-pulse rounded\" />\n          <div className=\"bg-muted h-3 w-20 animate-pulse rounded\" />\n        </div>\n      </div>\n      <div className=\"mt-4 grid gap-2\">\n        <div className=\"bg-muted h-3.5 w-full animate-pulse rounded\" />\n        <div className=\"bg-muted h-3.5 w-3/4 animate-pulse rounded\" />\n      </div>\n    </div>\n  );\n}\n\n/** What shows when a tweet is deleted, private or could not be fetched. */\nfunction TweetCardUnavailable({ className, ...props }: ComponentProps<\"div\">) {\n  return (\n    <div\n      data-slot=\"tweet-card-unavailable\"\n      className={cn(frame, \"text-muted-foreground py-6 text-center text-sm\", className)}\n      {...props}\n    >\n      This post is unavailable.\n    </div>\n  );\n}\n\nfunction CopyLink({ url }: { url: string }) {\n  const [copied, setCopied] = useState(false);\n  const timer = useRef<ReturnType<typeof setTimeout>>(undefined);\n  useEffect(() => () => clearTimeout(timer.current), []);\n\n  const copy = async () => {\n    try {\n      await navigator.clipboard.writeText(url);\n    } catch {\n      return;\n    }\n    setCopied(true);\n    clearTimeout(timer.current);\n    timer.current = setTimeout(() => setCopied(false), 1600);\n  };\n\n  return (\n    <button\n      type=\"button\"\n      onClick={copy}\n      className=\"text-muted-foreground hover:text-foreground focus-visible:ring-ring/50 inline-flex cursor-pointer items-center gap-1.5 rounded-md transition-colors outline-none focus-visible:ring-[3px]\"\n    >\n      {copied ? (\n        <CheckIcon aria-hidden=\"true\" className=\"text-success size-4\" />\n      ) : (\n        <LinkIcon aria-hidden=\"true\" className=\"size-4\" />\n      )}\n      <span aria-live=\"polite\">{copied ? \"Copied\" : \"Copy link\"}</span>\n    </button>\n  );\n}\n\ninterface TweetCardProps extends Omit<ComponentProps<\"article\">, \"children\"> {\n  /** The tweet as the syndication API returns it: from `getTweet`, `useTweet` or your own fetch. */\n  tweet: Tweet;\n  showDate?: boolean;\n  showLikes?: boolean;\n  showCopyLink?: boolean;\n  /** Locale for the date. A fixed default keeps the server and the browser in agreement. */\n  locale?: string;\n  /** Time zone for the date. */\n  timeZone?: string;\n}\n\n/**\n * A post from X, drawn with your theme instead of an embed: no iframe, no\n * third-party script, and text you can select. Mentions, hashtags and links\n * are real links; the text is decoded as text, never injected as HTML.\n * Photos carry their alt text and videos have controls rather than\n * autoplaying. For server components, `Tweet` fetches by id and renders\n * this card.\n *\n * @example\n * const { data } = useTweet(id);\n * return data ? <TweetCard tweet={data} /> : <TweetCardSkeleton />;\n */\nfunction TweetCard({\n  tweet: raw,\n  showDate = true,\n  showLikes = true,\n  showCopyLink = true,\n  locale = \"en-US\",\n  timeZone = \"UTC\",\n  className,\n  ...props\n}: TweetCardProps) {\n  const tweet = enrich(raw);\n  if (!tweet) return <TweetCardUnavailable className={className} />;\n\n  const date = new Date(tweet.created_at);\n  const photos = tweet.mediaDetails?.filter((media) => media.type === \"photo\") ?? [];\n  const video = tweet.video;\n  const source =\n    video?.variants.find((variant) => variant.type === \"video/mp4\") ?? video?.variants[0];\n\n  return (\n    <article\n      data-slot=\"tweet-card\"\n      aria-label={`Post by ${tweet.user.name}`}\n      className={cn(frame, className)}\n      {...props}\n    >\n      <header className=\"flex items-start justify-between gap-3\">\n        <a\n          href={tweet.user.url}\n          target=\"_blank\"\n          rel=\"noopener noreferrer\"\n          className=\"focus-visible:ring-ring/50 flex min-w-0 items-center gap-2.5 rounded-md outline-none focus-visible:ring-[3px]\"\n        >\n          <img\n            src={tweet.user.profile_image_url_https}\n            alt=\"\"\n            width={40}\n            height={40}\n            loading=\"lazy\"\n            className={cn(\n              \"size-10 shrink-0 object-cover\",\n              tweet.user.profile_image_shape === \"Square\" ? \"rounded-md\" : \"rounded-full\"\n            )}\n          />\n          <span className=\"grid min-w-0\">\n            <span className=\"flex items-center gap-1 truncate text-sm font-semibold\">\n              {tweet.user.name}\n              {tweet.user.verified || tweet.user.is_blue_verified ? (\n                <BadgeCheckIcon aria-label=\"Verified\" className=\"size-4 shrink-0 text-sky-500\" />\n              ) : null}\n            </span>\n            <span className=\"text-muted-foreground truncate text-sm\">\n              @{tweet.user.screen_name}\n            </span>\n          </span>\n        </a>\n        <a\n          href={tweet.url}\n          target=\"_blank\"\n          rel=\"noopener noreferrer\"\n          aria-label=\"View on X\"\n          className=\"text-foreground focus-visible:ring-ring/50 shrink-0 rounded-md outline-none hover:opacity-70 focus-visible:ring-[3px]\"\n        >\n          <svg viewBox=\"0 0 24 24\" aria-hidden=\"true\" className=\"size-4 fill-current\">\n            <path d={X_LOGO} />\n          </svg>\n        </a>\n      </header>\n\n      <p className=\"mt-3 text-[15px] leading-6 break-words whitespace-pre-wrap\">\n        {tweet.entities.map((entity, index) =>\n          entity.type === \"text\" ? (\n            <span key={index}>{decode(entity.text)}</span>\n          ) : entity.type === \"media\" ? null : (\n            <a\n              key={index}\n              href={entity.href}\n              target=\"_blank\"\n              rel=\"noopener noreferrer\"\n              className=\"text-sky-600 hover:underline dark:text-sky-400\"\n            >\n              {decode(entity.text)}\n            </a>\n          )\n        )}\n      </p>\n\n      {photos.length > 0 ? (\n        <div\n          className={cn(\n            \"mt-3 grid gap-1 overflow-hidden rounded-lg\",\n            photos.length > 1 && \"grid-cols-2\"\n          )}\n        >\n          {photos.map((photo, index) => (\n            <img\n              key={photo.media_url_https}\n              src={photo.media_url_https}\n              alt={photo.type === \"photo\" ? (photo.ext_alt_text ?? \"\") : \"\"}\n              width={photo.original_info.width}\n              height={photo.original_info.height}\n              loading=\"lazy\"\n              className={cn(\n                \"size-full object-cover\",\n                photos.length === 3 && index === 0 && \"row-span-2\"\n              )}\n            />\n          ))}\n        </div>\n      ) : null}\n\n      {video && source ? (\n        <video\n          poster={video.poster}\n          controls\n          muted\n          playsInline\n          preload=\"metadata\"\n          className=\"mt-3 w-full rounded-lg\"\n        >\n          <source src={source.src} type={source.type} />\n        </video>\n      ) : null}\n\n      {showDate ? (\n        <time dateTime={tweet.created_at} className=\"text-muted-foreground mt-3 block text-sm\">\n          {new Intl.DateTimeFormat(locale, {\n            hour: \"numeric\",\n            minute: \"2-digit\",\n            month: \"short\",\n            day: \"numeric\",\n            year: \"numeric\",\n            timeZone,\n          }).format(date)}\n        </time>\n      ) : null}\n\n      {showLikes || showCopyLink ? (\n        <footer className=\"mt-3 flex items-center gap-5 border-t pt-3 text-sm\">\n          {showLikes ? (\n            <a\n              href={tweet.like_url}\n              target=\"_blank\"\n              rel=\"noopener noreferrer\"\n              className=\"group/like text-muted-foreground hover:text-foreground focus-visible:ring-ring/50 inline-flex items-center gap-1.5 rounded-md outline-none focus-visible:ring-[3px]\"\n            >\n              <HeartIcon\n                aria-hidden=\"true\"\n                className=\"size-4 transition-colors group-hover/like:fill-rose-500 group-hover/like:text-rose-500\"\n              />\n              <span>\n                {compact(tweet.favorite_count)}\n                <span className=\"sr-only\"> likes, like on X</span>\n              </span>\n            </a>\n          ) : null}\n          {showCopyLink ? <CopyLink url={tweet.url} /> : null}\n        </footer>\n      ) : null}\n    </article>\n  );\n}\n\n/**\n * Fetches a tweet in the browser and renders it. It uses react-tweet's\n * `useTweet`, which by default asks react-tweet's public endpoint; pass\n * `apiUrl` to go through your own route instead.\n */\nfunction ClientTweet({\n  id,\n  apiUrl,\n  ...props\n}: Omit<TweetCardProps, \"tweet\"> & { id: string; apiUrl?: string }) {\n  const { data, isLoading } = useTweet(id, apiUrl);\n  if (isLoading) return <TweetCardSkeleton className={props.className} />;\n  if (!data) return <TweetCardUnavailable className={props.className} />;\n  return <TweetCard tweet={data} {...props} />;\n}\n\nexport { ClientTweet, TweetCard, TweetCardSkeleton, TweetCardUnavailable, type TweetCardProps };\n",
      "type": "registry:ui"
    },
    {
      "path": "registry/default/ui/tweet.tsx",
      "content": "import { getTweet } from \"react-tweet/api\";\n\nimport {\n  TweetCard,\n  TweetCardUnavailable,\n  type TweetCardProps,\n} from \"@/registry/default/ui/tweet-card\";\n\ninterface TweetProps extends Omit<TweetCardProps, \"tweet\"> {\n  /** The id at the end of the post's URL. */\n  id: string;\n  /** Passed to `fetch`, e.g. `{ next: { revalidate: 3600 } }` to cache it in Next.js. */\n  fetchOptions?: RequestInit;\n}\n\n/**\n * A server component that fetches a post from X's syndication API while\n * the page renders and shows it with TweetCard: nothing is fetched in the\n * browser and no third-party script loads. Wrap it in `<Suspense>` with a\n * `TweetCardSkeleton` fallback to stream the rest of the page first. In\n * client components, use `ClientTweet` instead.\n *\n * @example\n * <Suspense fallback={<TweetCardSkeleton />}>\n *   <Tweet id=\"1628832338187636740\" />\n * </Suspense>\n */\nasync function Tweet({ id, fetchOptions, ...props }: TweetProps) {\n  const tweet = await getTweet(id, fetchOptions).catch(() => undefined);\n  if (!tweet) return <TweetCardUnavailable className={props.className} />;\n  return <TweetCard tweet={tweet} {...props} />;\n}\n\nexport { Tweet, type TweetProps };\n",
      "type": "registry:ui"
    }
  ],
  "type": "registry:ui"
}