All files / components/chat message-bubble.tsx

100% Statements 6/6
86.95% Branches 20/23
100% Functions 2/2
100% Lines 6/6

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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                                      5x 1x                         4x                                                                               4x 2x                       2x    
import type { ReactNode } from "react";
import { Avatar } from "@/components/ui/avatar";
import { cn } from "@/utils/class-name";
 
export type MessageBubbleProps = {
  isUser: boolean;
  text?: string;
  placeholder?: string;
  fullWidth?: boolean;
  children?: ReactNode;
};
 
export function MessageBubble({
  isUser,
  text,
  placeholder,
  fullWidth = false,
  children,
}: MessageBubbleProps) {
  if (isUser) {
    return (
      <div
        className={cn(
          "ml-auto max-w-bubble-user rounded-bubble-user border px-3.75 py-2.5 text-sm leading-relaxed wrap-break-word shadow-bubble-user backdrop-blur-lg",
          "border-violet-300/28 bg-bubble-user text-white/90",
          "light:border-stone-600/20 light:text-white/95 light:backdrop-blur-none",
        )}
      >
        <span className="whitespace-pre-wrap">{text ?? placeholder}</span>
      </div>
    );
  }
 
  return (
    <div
      className={cn(
        "rounded-2xl border px-4 py-2.5 text-sm leading-relaxed shadow-bubble-assistant backdrop-blur-md",
        "border-white/11 bg-bubble-assistant text-white/88",
        "light:border-app-border-10 light:text-app-fg light:backdrop-blur-none",
        fullWidth ? "max-w-full" : "max-w-bubble-assistant",
      )}
    >
      {text ? <span className="whitespace-pre-wrap">{text}</span> : null}
      {!text && placeholder ? (
        <span className="text-white/40 light:text-app-muted-50">
          {placeholder}
        </span>
      ) : null}
      {children ? (
        <div className={cn(text || placeholder ? "mt-3" : undefined)}>
          {children}
        </div>
      ) : null}
    </div>
  );
}
 
export type MessageAvatarProps = {
  initials: string;
  isUser: boolean;
  avatarUrl?: string;
  avatarLabel?: string;
  size?: "sm" | "md" | "lg";
  children?: ReactNode;
};
 
export function MessageAvatar({
  initials,
  isUser,
  avatarUrl,
  avatarLabel = "User avatar",
  size = "sm",
}: MessageAvatarProps) {
  if (isUser) {
    return (
      <Avatar
        variant="user"
        src={avatarUrl}
        alt={avatarLabel}
        initials={initials}
        size={size}
        className="mt-1"
      />
    );
  }
 
  return <Avatar variant="assistant" size="sm" className="mt-1" />;
}