"use client"; import { ExternalLinkIcon, Globe, ImageIcon, LinkIcon } from "lucide-react"; import Image from "next/image"; import { Component, type ReactNode } from "react"; import { z } from "zod"; import { Badge } from "@/components/ui/badge"; import { Button } from "@/components/ui/button"; import { Card, CardContent } from "@/components/ui/card"; import { Spinner } from "@/components/ui/spinner"; import { Tooltip, TooltipContent, TooltipProvider, TooltipTrigger } from "@/components/ui/tooltip"; import { cn } from "@/lib/utils"; /** * Zod schemas for runtime validation */ const AspectRatioSchema = z.enum(["1:1", "4:3", "16:9", "9:16", "21:9", "auto"]); const MediaCardKindSchema = z.enum(["link", "image", "video", "audio"]); const ResponseActionSchema = z.object({ id: z.string(), label: z.string(), variant: z.enum(["default", "secondary", "outline", "destructive", "ghost"]).nullish(), confirmLabel: z.string().nullish(), }); const SerializableMediaCardSchema = z.object({ id: z.string(), assetId: z.string(), kind: MediaCardKindSchema, href: z.string().nullish(), src: z.string().nullish(), title: z.string(), description: z.string().nullish(), thumb: z.string().nullish(), ratio: AspectRatioSchema.nullish(), domain: z.string().nullish(), }); /** * Types derived from Zod schemas */ type AspectRatio = z.infer; type MediaCardKind = z.infer; type ResponseAction = z.infer; export type SerializableMediaCard = z.infer; /** * Props for the MediaCard component */ export interface MediaCardProps { id: string; assetId: string; kind: MediaCardKind; href?: string; src?: string; title: string; description?: string; thumb?: string; ratio?: AspectRatio; domain?: string; maxWidth?: string; alt?: string; className?: string; responseActions?: ResponseAction[]; onResponseAction?: (id: string) => void; } /** * Parse and validate serializable media card from tool result */ export function parseSerializableMediaCard(result: unknown): SerializableMediaCard { const parsed = SerializableMediaCardSchema.safeParse(result); if (!parsed.success) { console.warn("Invalid media card data:", parsed.error.issues); throw new Error(`Invalid media card: ${parsed.error.issues.map((i) => i.message).join(", ")}`); } return parsed.data; } /** * Get aspect ratio class based on ratio prop */ function getAspectRatioClass(ratio?: AspectRatio): string { switch (ratio) { case "1:1": return "aspect-square"; case "4:3": return "aspect-[4/3]"; case "16:9": return "aspect-video"; case "9:16": return "aspect-[9/16]"; case "21:9": return "aspect-[21/9]"; case "auto": default: return "aspect-[2/1]"; } } /** * Get icon based on media card kind */ function getKindIcon(kind: MediaCardKind) { switch (kind) { case "link": return ; case "image": return ; case "video": case "audio": return ; default: return ; } } /** * Error boundary for MediaCard */ interface MediaCardErrorBoundaryState { hasError: boolean; error?: Error; } export class MediaCardErrorBoundary extends Component< { children: ReactNode }, MediaCardErrorBoundaryState > { constructor(props: { children: ReactNode }) { super(props); this.state = { hasError: false }; } static getDerivedStateFromError(error: Error): MediaCardErrorBoundaryState { return { hasError: true, error }; } render() { if (this.state.hasError) { return (

Failed to load preview

{this.state.error?.message || "An error occurred"}

); } return this.props.children; } } /** * Loading skeleton for MediaCard */ export function MediaCardSkeleton({ maxWidth = "420px" }: { maxWidth?: string }) { return (
); } /** * MediaCard Component * * A rich media card for displaying link previews, images, and other media * in AI chat applications. Supports thumbnails, descriptions, and actions. */ export function MediaCard({ id, kind, href, title, description, thumb, ratio = "auto", domain, maxWidth = "420px", alt, className, responseActions, onResponseAction, }: MediaCardProps) { const aspectRatioClass = getAspectRatioClass(ratio); const displayDomain = domain || (href ? new URL(href).hostname.replace("www.", "") : undefined); const handleCardClick = () => { if (href) { window.open(href, "_blank", "noopener,noreferrer"); } }; return ( { if (href && (e.key === "Enter" || e.key === " ")) { e.preventDefault(); handleCardClick(); } }} > {/* Thumbnail */} {thumb && (
{alt { // Hide broken images e.currentTarget.style.display = "none"; }} /> {/* Gradient overlay */}
)} {/* Fallback when no thumbnail */} {!thumb && (
{getKindIcon(kind)} {kind === "link" ? "Link Preview" : kind}
)} {/* Content */}
{/* Domain favicon placeholder */}
{/* Domain badge */} {displayDomain && (
{displayDomain} {href && ( )}
)} {/* Title */}

{title}

{/* Description */} {description && (

{description}

)}
{/* Response Actions */} {responseActions && responseActions.length > 0 && (
{responseActions.map((action) => ( {action.confirmLabel && (

{action.confirmLabel}

)}
))}
)}
); } /** * MediaCard Loading State */ export function MediaCardLoading({ title = "Loading preview..." }: { title?: string }) { return (

{title}

); }