wacrm/src/components/inbox/message-bubble.tsx
Arnas Donauskas 339b1fe94f
feat(inbox): reactions, reply-to, and copy on chat messages
Adds three WhatsApp-standard affordances to the inbox: per-message
emoji reactions, reply-with-quote, and copy-text. Bubbles stay pure
presenters — actions layer on by composition (MessageActions wraps
MessageBubble). Hover toolbar on desktop, long-press (onContextMenu)
on touch.

Data model (migration 009):
- messages.reply_to_message_id (self-FK, ON DELETE SET NULL) so replies
  survive a deleted parent
- message_reactions table — one row per (message, actor) with a unique
  constraint, conversation_id denormalised so Supabase Realtime can
  filter without a join
- RLS scoped via conversation ownership; service-role bypass for the
  webhook

API:
- /api/whatsapp/send accepts reply_to_message_id, resolves the parent's
  Meta message_id and forwards it as context.message_id
- /api/whatsapp/react (new) sends Meta reaction payloads and mirrors
  the upsert/delete into message_reactions
- Webhook stops storing inbound reactions as fake text messages and
  instead upserts/deletes message_reactions; inbound replies (context.id)
  resolve to internal UUID and populate reply_to_message_id

UI:
- MessageActions hover toolbar with inline 6-emoji Popover (static set;
  no emoji-picker library)
- ReplyQuote shared between the in-bubble quote and the composer chip
- MessageReactions pill row with click-to-toggle
- Thread subscribes to message_reactions filtered by conversation_id;
  optimistic upsert with rollback on failure

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-19 16:17:26 +03:00

277 lines
7.5 KiB
TypeScript

"use client";
import { useState, useEffect, useCallback } from "react";
import { cn } from "@/lib/utils";
import type { Message, MessageReaction } from "@/types";
import {
Clock,
Check,
CheckCheck,
XCircle,
FileText,
MapPin,
LayoutTemplate,
ImageOff,
} from "lucide-react";
import { format } from "date-fns";
import { ReplyQuote } from "./reply-quote";
import { MessageReactions } from "./message-reactions";
interface MessageBubbleProps {
message: Message;
/** Pre-computed quote info for messages that reply to another. */
reply?: { authorLabel: string; preview: string } | null;
reactions?: MessageReaction[];
currentUserId?: string;
onToggleReaction?: (emoji: string) => void;
}
function StatusIcon({ status }: { status: Message["status"] }) {
switch (status) {
case "sending":
return <Clock className="h-3 w-3 text-slate-400" />;
case "sent":
return <Check className="h-3 w-3 text-slate-400" />;
case "delivered":
return <CheckCheck className="h-3 w-3 text-slate-400" />;
case "read":
return <CheckCheck className="h-3 w-3 text-blue-400" />;
case "failed":
return <XCircle className="h-3 w-3 text-red-400" />;
default:
return null;
}
}
function MediaUnavailable({ label }: { label: string }) {
return (
<div className="flex items-center gap-2 rounded-lg bg-slate-700/40 px-3 py-2 text-xs text-slate-300">
<ImageOff className="h-4 w-4 shrink-0 text-slate-500" />
<span>{label} unavailable</span>
</div>
);
}
function MediaImage({ url, alt }: { url: string; alt: string }) {
const [src, setSrc] = useState<string | null>(null);
const [error, setError] = useState(false);
const [loading, setLoading] = useState(true);
const loadImage = useCallback(async () => {
if (!url) return;
// Proxy URLs need auth fetch to create blob URL
if (url.startsWith("/api/whatsapp/media/")) {
try {
const res = await fetch(url);
if (!res.ok) throw new Error("Failed to load media");
const blob = await res.blob();
const blobUrl = URL.createObjectURL(blob);
setSrc(blobUrl);
} catch {
setError(true);
} finally {
setLoading(false);
}
} else {
setSrc(url);
setLoading(false);
}
}, [url]);
useEffect(() => {
loadImage();
return () => {
if (src?.startsWith("blob:")) {
URL.revokeObjectURL(src);
}
};
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [loadImage]);
if (error) {
return (
<div className="flex h-40 w-60 items-center justify-center rounded-lg bg-slate-700">
<ImageOff className="h-8 w-8 text-slate-500" />
</div>
);
}
if (loading) {
return (
<div className="flex h-40 w-60 items-center justify-center rounded-lg bg-slate-700">
<div className="h-5 w-5 animate-spin rounded-full border-2 border-violet-500 border-t-transparent" />
</div>
);
}
return (
<img
src={src ?? ""}
alt={alt}
className="max-h-64 max-w-60 rounded-lg object-cover"
onError={() => setError(true)}
/>
);
}
function MessageContent({ message }: { message: Message }) {
switch (message.content_type) {
case "text":
return (
<p className="whitespace-pre-wrap break-words text-sm">
{message.content_text}
</p>
);
case "image":
return (
<div>
{message.media_url ? (
<MediaImage url={message.media_url} alt="Shared image" />
) : (
<MediaUnavailable label="Image" />
)}
{message.content_text && (
<p className="mt-1 whitespace-pre-wrap break-words text-sm">
{message.content_text}
</p>
)}
</div>
);
case "video":
return (
<div>
{message.media_url ? (
<video
src={message.media_url}
controls
className="max-h-64 max-w-60 rounded-lg"
/>
) : (
<MediaUnavailable label="Video" />
)}
{message.content_text && (
<p className="mt-1 whitespace-pre-wrap break-words text-sm">
{message.content_text}
</p>
)}
</div>
);
case "audio":
return (
<div>
{message.media_url ? (
<audio src={message.media_url} controls className="max-w-60" />
) : (
<MediaUnavailable label="Audio" />
)}
</div>
);
case "document":
if (!message.media_url) {
return <MediaUnavailable label={message.content_text || "Document"} />;
}
return (
<a
href={message.media_url}
target="_blank"
rel="noopener noreferrer"
className="flex items-center gap-2 rounded-lg bg-slate-700/50 px-3 py-2 text-sm hover:bg-slate-700"
>
<FileText className="h-5 w-5 shrink-0 text-slate-400" />
<span className="truncate">
{message.content_text || "Document"}
</span>
</a>
);
case "template":
return (
<div>
<span className="mb-1 inline-flex items-center gap-1 rounded bg-violet-500/20 px-1.5 py-0.5 text-[10px] font-medium text-violet-400">
<LayoutTemplate className="h-3 w-3" />
Template
</span>
{message.content_text && (
<p className="mt-1 whitespace-pre-wrap break-words text-sm">
{message.content_text}
</p>
)}
</div>
);
case "location":
return (
<div className="flex items-center gap-2 text-sm">
<MapPin className="h-4 w-4 shrink-0 text-slate-400" />
<span>{message.content_text || "Location shared"}</span>
</div>
);
default:
return (
<p className="whitespace-pre-wrap break-words text-sm">
{message.content_text || "[Unsupported message type]"}
</p>
);
}
}
export function MessageBubble({
message,
reply,
reactions,
currentUserId,
onToggleReaction,
}: MessageBubbleProps) {
const isAgent = message.sender_type === "agent" || message.sender_type === "bot";
const time = format(new Date(message.created_at), "HH:mm");
return (
<div
className={cn("flex w-full", isAgent ? "justify-end" : "justify-start")}
>
<div
className={cn(
"flex max-w-[75%] flex-col",
isAgent ? "items-end" : "items-start",
)}
>
<div
className={cn(
"relative rounded-2xl px-3 py-2",
isAgent
? "rounded-br-md bg-violet-600 text-white"
: "rounded-bl-md bg-slate-800 text-slate-100",
)}
>
{reply && (
<ReplyQuote authorLabel={reply.authorLabel} preview={reply.preview} />
)}
<MessageContent message={message} />
<div
className={cn(
"mt-1 flex items-center gap-1",
isAgent ? "justify-end" : "justify-start",
)}
>
<span className="text-[10px] text-white/60">{time}</span>
{isAgent && <StatusIcon status={message.status} />}
</div>
</div>
{reactions && reactions.length > 0 && onToggleReaction && (
<MessageReactions
reactions={reactions}
currentUserId={currentUserId}
onToggle={onToggleReaction}
/>
)}
</div>
</div>
);
}