2026-05-07 02:41:01 +08:00
|
|
|
import { cn } from '@/lib/utils';
|
2026-05-07 03:22:15 +08:00
|
|
|
import { renderMarkdown } from '@/lib/markdown';
|
2026-05-07 04:14:16 +08:00
|
|
|
import { RefreshCw, ArrowRight, Quote, Bot, User } from 'lucide-react';
|
2026-05-07 02:41:01 +08:00
|
|
|
import type { Message } from '@/types';
|
|
|
|
|
|
2026-05-07 04:09:00 +08:00
|
|
|
interface Props {
|
|
|
|
|
message: Message;
|
|
|
|
|
onRegenerate?: (msgId: string) => void;
|
|
|
|
|
onContinue?: (msgId: string) => void;
|
|
|
|
|
onQuote?: (content: string) => void;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export function ChatMessage({ message, onRegenerate, onContinue, onQuote }: Props) {
|
2026-05-07 02:41:01 +08:00
|
|
|
const isUser = message.role === 'user';
|
2026-05-07 03:22:15 +08:00
|
|
|
const isEmpty = !message.content;
|
2026-05-07 02:41:01 +08:00
|
|
|
|
|
|
|
|
return (
|
2026-05-07 04:14:16 +08:00
|
|
|
<div className={cn('flex gap-3 mb-6', isUser ? 'flex-row-reverse' : 'flex-row')}>
|
|
|
|
|
{/* Avatar */}
|
|
|
|
|
<div className={cn(
|
|
|
|
|
'w-8 h-8 rounded-full flex items-center justify-center flex-shrink-0',
|
|
|
|
|
isUser ? 'bg-indigo-600 text-white' : 'bg-zinc-100 text-zinc-500'
|
|
|
|
|
)}>
|
|
|
|
|
{isUser ? <User size={15} /> : <Bot size={15} />}
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
{/* Content */}
|
|
|
|
|
<div className={cn('flex-1 max-w-[75%]', isUser && 'flex flex-col items-end')}>
|
|
|
|
|
{/* Role label */}
|
|
|
|
|
<div className={cn('text-[10px] font-medium mb-1 px-1', isUser ? 'text-right text-indigo-500' : 'text-zinc-400')}>
|
|
|
|
|
{isUser ? '你' : '美图 Agent'}
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
{/* Bubble */}
|
|
|
|
|
<div className={cn(
|
|
|
|
|
'rounded-2xl px-4 py-3 text-sm leading-relaxed',
|
|
|
|
|
isUser
|
|
|
|
|
? 'bg-indigo-600 text-white rounded-tr-md'
|
|
|
|
|
: 'bg-white text-zinc-700 border border-zinc-200/60 shadow-sm rounded-tl-md'
|
|
|
|
|
)}>
|
2026-05-07 04:09:00 +08:00
|
|
|
{isEmpty ? (
|
2026-05-07 04:14:16 +08:00
|
|
|
<div className="flex items-center gap-1 text-zinc-400">
|
|
|
|
|
<span className="inline-block w-1.5 h-1.5 bg-zinc-300 rounded-full animate-pulse" />
|
|
|
|
|
<span className="inline-block w-1.5 h-1.5 bg-zinc-300 rounded-full animate-pulse delay-100" />
|
|
|
|
|
<span className="inline-block w-1.5 h-1.5 bg-zinc-300 rounded-full animate-pulse delay-200" />
|
|
|
|
|
</div>
|
2026-05-07 04:09:00 +08:00
|
|
|
) : isUser ? (
|
2026-05-07 04:14:16 +08:00
|
|
|
<p className="whitespace-pre-wrap">{message.content}</p>
|
2026-05-07 04:09:00 +08:00
|
|
|
) : (
|
2026-05-07 04:14:16 +08:00
|
|
|
<div className="markdown-body" dangerouslySetInnerHTML={{ __html: renderMarkdown(message.content) }} />
|
2026-05-07 04:09:00 +08:00
|
|
|
)}
|
|
|
|
|
</div>
|
|
|
|
|
|
2026-05-07 04:14:16 +08:00
|
|
|
{/* Actions - only for assistant messages */}
|
2026-05-07 04:09:00 +08:00
|
|
|
{!isUser && !isEmpty && (
|
2026-05-07 04:14:16 +08:00
|
|
|
<div className="flex gap-0.5 mt-1 opacity-0 hover:opacity-100 transition-opacity px-1">
|
|
|
|
|
<button onClick={() => onQuote?.(message.content)}
|
|
|
|
|
className="flex items-center gap-1 px-2 py-0.5 rounded text-[10px] text-zinc-400 hover:text-zinc-600 hover:bg-zinc-100 transition-colors">
|
|
|
|
|
<Quote size={10} />引用
|
|
|
|
|
</button>
|
|
|
|
|
<button onClick={() => onRegenerate?.(message.id)}
|
|
|
|
|
className="flex items-center gap-1 px-2 py-0.5 rounded text-[10px] text-zinc-400 hover:text-zinc-600 hover:bg-zinc-100 transition-colors">
|
|
|
|
|
<RefreshCw size={10} />重新生成
|
|
|
|
|
</button>
|
|
|
|
|
<button onClick={() => onContinue?.(message.id)}
|
|
|
|
|
className="flex items-center gap-1 px-2 py-0.5 rounded text-[10px] text-zinc-400 hover:text-zinc-600 hover:bg-zinc-100 transition-colors">
|
|
|
|
|
<ArrowRight size={10} />继续
|
|
|
|
|
</button>
|
2026-05-07 04:09:00 +08:00
|
|
|
</div>
|
2026-05-07 03:22:15 +08:00
|
|
|
)}
|
2026-05-07 02:41:01 +08:00
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|