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:09:00 +08:00
|
|
|
import { RefreshCw, ArrowRight, Quote } 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:09:00 +08:00
|
|
|
<div className={cn('mb-4 flex group', isUser ? 'justify-end' : 'justify-start')}>
|
|
|
|
|
<div className="max-w-[80%]">
|
|
|
|
|
<div
|
|
|
|
|
className={cn(
|
|
|
|
|
'rounded-xl px-4 py-2.5',
|
|
|
|
|
isUser
|
|
|
|
|
? 'bg-indigo-600 text-white'
|
|
|
|
|
: 'bg-white text-zinc-700 border border-zinc-200 shadow-sm'
|
|
|
|
|
)}
|
|
|
|
|
>
|
|
|
|
|
{isEmpty ? (
|
|
|
|
|
<span className="text-zinc-400 italic text-xs">...</span>
|
|
|
|
|
) : isUser ? (
|
|
|
|
|
<p className="text-sm leading-relaxed whitespace-pre-wrap">{message.content}</p>
|
|
|
|
|
) : (
|
|
|
|
|
<div
|
|
|
|
|
className="markdown-body text-sm leading-relaxed"
|
|
|
|
|
dangerouslySetInnerHTML={{ __html: renderMarkdown(message.content) }}
|
|
|
|
|
/>
|
|
|
|
|
)}
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
{/* Action buttons - only for assistant messages */}
|
|
|
|
|
{!isUser && !isEmpty && (
|
|
|
|
|
<div className="flex gap-1 mt-1 opacity-0 group-hover:opacity-100 transition-opacity">
|
|
|
|
|
{onQuote && (
|
|
|
|
|
<button
|
|
|
|
|
onClick={() => onQuote(message.content)}
|
|
|
|
|
className="flex items-center gap-0.5 px-1.5 py-0.5 rounded text-[10px] text-zinc-400 hover:text-zinc-600 hover:bg-zinc-100 transition-colors"
|
|
|
|
|
title="引用"
|
|
|
|
|
>
|
|
|
|
|
<Quote size={10} />
|
|
|
|
|
引用
|
|
|
|
|
</button>
|
|
|
|
|
)}
|
|
|
|
|
{onRegenerate && (
|
|
|
|
|
<button
|
|
|
|
|
onClick={() => onRegenerate(message.id)}
|
|
|
|
|
className="flex items-center gap-0.5 px-1.5 py-0.5 rounded text-[10px] text-zinc-400 hover:text-zinc-600 hover:bg-zinc-100 transition-colors"
|
|
|
|
|
title="重新生成"
|
|
|
|
|
>
|
|
|
|
|
<RefreshCw size={10} />
|
|
|
|
|
重新生成
|
|
|
|
|
</button>
|
|
|
|
|
)}
|
|
|
|
|
{onContinue && (
|
|
|
|
|
<button
|
|
|
|
|
onClick={() => onContinue(message.id)}
|
|
|
|
|
className="flex items-center gap-0.5 px-1.5 py-0.5 rounded text-[10px] text-zinc-400 hover:text-zinc-600 hover:bg-zinc-100 transition-colors"
|
|
|
|
|
title="继续"
|
|
|
|
|
>
|
|
|
|
|
<ArrowRight size={10} />
|
|
|
|
|
继续
|
|
|
|
|
</button>
|
|
|
|
|
)}
|
|
|
|
|
</div>
|
2026-05-07 03:22:15 +08:00
|
|
|
)}
|
2026-05-07 02:41:01 +08:00
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
}
|