2026-04-24 14:34:21 +08:00
|
|
|
"use client"
|
|
|
|
|
|
|
|
|
|
import {
|
|
|
|
|
forwardRef,
|
|
|
|
|
memo,
|
|
|
|
|
useCallback,
|
|
|
|
|
useEffect,
|
|
|
|
|
useImperativeHandle,
|
|
|
|
|
useRef,
|
|
|
|
|
} from "react"
|
|
|
|
|
|
2026-07-29 10:09:17 +08:00
|
|
|
import { ConversationMessageBubble } from "@/components/chat/conversation-message-bubble"
|
|
|
|
|
import { ConversationMessageRow } from "@/components/chat/conversation-message-row"
|
|
|
|
|
import {
|
|
|
|
|
ConversationMessageScroller,
|
|
|
|
|
ConversationMessageScrollerItem,
|
|
|
|
|
type ConversationMessageScrollerHandle,
|
|
|
|
|
} from "@/components/chat/conversation-message-scroller"
|
2026-04-24 14:34:21 +08:00
|
|
|
import { ImMessageHTML } from "@/components/im-message-html"
|
|
|
|
|
import { useImageLightbox } from "@/components/image-lightbox"
|
2026-04-25 11:26:53 +08:00
|
|
|
import { Avatar, AvatarFallback, AvatarImage } from "@/components/ui/avatar"
|
|
|
|
|
import { Badge } from "@/components/ui/badge"
|
|
|
|
|
import { Button } from "@/components/ui/button"
|
2026-04-24 14:34:21 +08:00
|
|
|
import type { ImMessage } from "@/lib/api/im"
|
|
|
|
|
import { renderIMMessageHTML } from "@/lib/im-message"
|
|
|
|
|
import { cn, formatDateTime } from "@/lib/utils"
|
2026-05-25 12:06:15 +08:00
|
|
|
import { useI18n } from "@/i18n/provider"
|
2026-04-24 14:34:21 +08:00
|
|
|
|
2026-05-30 14:00:23 +08:00
|
|
|
type SupportChatMessageListProps = {
|
2026-04-24 19:17:07 +08:00
|
|
|
messages?: ImMessage[] | null
|
2026-04-24 14:34:21 +08:00
|
|
|
onNearBottomVisible?: () => void
|
|
|
|
|
hasMoreOlder?: boolean
|
|
|
|
|
loadingOlder?: boolean
|
|
|
|
|
onLoadOlder?: () => Promise<void>
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-30 14:00:23 +08:00
|
|
|
export type SupportChatMessageListHandle = {
|
2026-04-24 14:34:21 +08:00
|
|
|
scrollToBottom: () => void
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function getDayKey(value?: string) {
|
|
|
|
|
if (!value) {
|
|
|
|
|
return "unknown"
|
|
|
|
|
}
|
|
|
|
|
const date = new Date(value)
|
|
|
|
|
if (Number.isNaN(date.getTime())) {
|
|
|
|
|
return value.slice(0, 10)
|
|
|
|
|
}
|
|
|
|
|
return `${date.getFullYear()}-${String(date.getMonth() + 1).padStart(2, "0")}-${String(
|
|
|
|
|
date.getDate()
|
|
|
|
|
).padStart(2, "0")}`
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-25 12:06:15 +08:00
|
|
|
function getTimelineLabel(
|
|
|
|
|
value: string | undefined,
|
|
|
|
|
t: (key: string, values?: Record<string, string | number>) => string
|
|
|
|
|
) {
|
2026-04-24 14:34:21 +08:00
|
|
|
if (!value) {
|
2026-05-30 14:00:23 +08:00
|
|
|
return t("supportChat.justNow")
|
2026-04-24 14:34:21 +08:00
|
|
|
}
|
|
|
|
|
const date = new Date(value)
|
|
|
|
|
if (Number.isNaN(date.getTime())) {
|
|
|
|
|
return value
|
|
|
|
|
}
|
|
|
|
|
const currentDayKey = getDayKey(value)
|
|
|
|
|
const todayDayKey = getDayKey(new Date().toISOString())
|
|
|
|
|
const timeText = `${String(date.getHours()).padStart(2, "0")}:${String(
|
|
|
|
|
date.getMinutes()
|
|
|
|
|
).padStart(2, "0")}`
|
|
|
|
|
if (currentDayKey === todayDayKey) {
|
2026-05-30 14:00:23 +08:00
|
|
|
return t("supportChat.todayAt", { time: timeText })
|
2026-04-24 14:34:21 +08:00
|
|
|
}
|
|
|
|
|
return `${currentDayKey} ${timeText}`
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-30 14:00:23 +08:00
|
|
|
export const SupportChatMessageList = forwardRef<SupportChatMessageListHandle, SupportChatMessageListProps>(
|
|
|
|
|
function SupportChatMessageList(
|
2026-04-24 14:34:21 +08:00
|
|
|
{
|
|
|
|
|
messages,
|
|
|
|
|
onNearBottomVisible,
|
|
|
|
|
hasMoreOlder = false,
|
|
|
|
|
loadingOlder = false,
|
|
|
|
|
onLoadOlder,
|
|
|
|
|
},
|
|
|
|
|
ref
|
|
|
|
|
) {
|
2026-05-25 12:06:15 +08:00
|
|
|
const t = useI18n()
|
2026-07-29 10:09:17 +08:00
|
|
|
const scrollerRef = useRef<ConversationMessageScrollerHandle | null>(null)
|
2026-04-24 14:34:21 +08:00
|
|
|
const shouldStickToBottomRef = useRef(true)
|
2026-04-25 12:26:45 +08:00
|
|
|
const onNearBottomVisibleRef = useRef(onNearBottomVisible)
|
2026-04-24 19:17:07 +08:00
|
|
|
const safeMessages = Array.isArray(messages) ? messages : []
|
2026-04-24 14:34:21 +08:00
|
|
|
|
2026-04-25 12:26:45 +08:00
|
|
|
useEffect(() => {
|
|
|
|
|
onNearBottomVisibleRef.current = onNearBottomVisible
|
|
|
|
|
}, [onNearBottomVisible])
|
|
|
|
|
|
2026-04-24 14:34:21 +08:00
|
|
|
const scrollToBottom = useCallback(() => {
|
2026-07-29 10:09:17 +08:00
|
|
|
scrollerRef.current?.scrollToBottom()
|
2026-04-24 14:34:21 +08:00
|
|
|
}, [])
|
|
|
|
|
|
|
|
|
|
const handleImageSettled = useCallback(() => {
|
|
|
|
|
if (shouldStickToBottomRef.current) {
|
2026-07-29 10:09:17 +08:00
|
|
|
scrollToBottom()
|
2026-04-25 12:26:45 +08:00
|
|
|
onNearBottomVisibleRef.current?.()
|
2026-04-24 14:34:21 +08:00
|
|
|
}
|
2026-07-29 10:09:17 +08:00
|
|
|
}, [scrollToBottom])
|
2026-04-24 14:34:21 +08:00
|
|
|
|
|
|
|
|
useImperativeHandle(ref, () => ({
|
|
|
|
|
scrollToBottom,
|
|
|
|
|
}))
|
|
|
|
|
|
|
|
|
|
const handleLoadOlder = useCallback(async () => {
|
|
|
|
|
if (!onLoadOlder || loadingOlder || !hasMoreOlder) {
|
|
|
|
|
return
|
|
|
|
|
}
|
2026-07-29 10:09:17 +08:00
|
|
|
await onLoadOlder()
|
2026-04-24 14:34:21 +08:00
|
|
|
}, [hasMoreOlder, loadingOlder, onLoadOlder])
|
|
|
|
|
|
|
|
|
|
return (
|
2026-07-29 10:09:17 +08:00
|
|
|
<ConversationMessageScroller
|
|
|
|
|
ref={scrollerRef}
|
|
|
|
|
className="flex min-h-0 flex-1"
|
|
|
|
|
viewportClassName="bg-transparent"
|
|
|
|
|
contentClassName="gap-4 px-4 py-4"
|
|
|
|
|
hasMoreOlder={hasMoreOlder}
|
|
|
|
|
loadingOlder={loadingOlder}
|
|
|
|
|
onLoadOlder={onLoadOlder}
|
|
|
|
|
onNearBottomChange={(nearBottom) => {
|
|
|
|
|
shouldStickToBottomRef.current = nearBottom
|
|
|
|
|
}}
|
|
|
|
|
onNearBottomVisible={onNearBottomVisible}
|
|
|
|
|
topSlot={
|
|
|
|
|
hasMoreOlder && onLoadOlder ? (
|
2026-04-24 14:34:21 +08:00
|
|
|
<div className="flex justify-center py-1">
|
2026-04-25 11:26:53 +08:00
|
|
|
<Button
|
2026-04-24 14:34:21 +08:00
|
|
|
type="button"
|
2026-04-25 11:26:53 +08:00
|
|
|
variant="outline"
|
|
|
|
|
size="sm"
|
2026-04-24 14:34:21 +08:00
|
|
|
disabled={loadingOlder}
|
|
|
|
|
onClick={() => void handleLoadOlder()}
|
2026-04-25 11:31:16 +08:00
|
|
|
className="h-7 rounded-full bg-background/90 text-xs text-muted-foreground shadow-sm hover:bg-background hover:text-sky-700 dark:hover:text-sky-400"
|
2026-04-24 14:34:21 +08:00
|
|
|
>
|
2026-05-30 14:00:23 +08:00
|
|
|
{loadingOlder ? t("supportChat.loadingOlder") : t("supportChat.loadOlder")}
|
2026-04-25 11:26:53 +08:00
|
|
|
</Button>
|
2026-04-24 14:34:21 +08:00
|
|
|
</div>
|
2026-07-29 10:09:17 +08:00
|
|
|
) : null
|
|
|
|
|
}
|
|
|
|
|
>
|
|
|
|
|
{safeMessages.length === 0 ? (
|
|
|
|
|
<div className="flex min-h-32 items-center justify-center px-3 py-6 text-center text-sm leading-6 text-muted-foreground">
|
|
|
|
|
{t("supportChat.emptyPrompt")}
|
|
|
|
|
</div>
|
|
|
|
|
) : null}
|
2026-05-02 12:30:27 +08:00
|
|
|
|
2026-07-29 10:09:17 +08:00
|
|
|
{safeMessages.map((message, index) => {
|
|
|
|
|
const previousMessage = index > 0 ? safeMessages[index - 1] : null
|
|
|
|
|
const showTimeline =
|
|
|
|
|
index === 0 ||
|
|
|
|
|
getDayKey(previousMessage?.sentAt) !== getDayKey(message.sentAt)
|
2026-04-24 14:34:21 +08:00
|
|
|
|
2026-07-29 10:09:17 +08:00
|
|
|
return (
|
|
|
|
|
<ConversationMessageScrollerItem
|
|
|
|
|
key={message.id}
|
|
|
|
|
messageId={`${message.id}`}
|
|
|
|
|
>
|
2026-04-24 14:34:21 +08:00
|
|
|
<MessageItem
|
|
|
|
|
message={message}
|
|
|
|
|
showTimeline={showTimeline}
|
|
|
|
|
onImageSettled={handleImageSettled}
|
2026-05-25 12:06:15 +08:00
|
|
|
timelineLabel={getTimelineLabel(message.sentAt, t)}
|
2026-04-24 14:34:21 +08:00
|
|
|
/>
|
2026-07-29 10:09:17 +08:00
|
|
|
</ConversationMessageScrollerItem>
|
|
|
|
|
)
|
|
|
|
|
})}
|
|
|
|
|
</ConversationMessageScroller>
|
2026-04-24 14:34:21 +08:00
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type MessageItemProps = {
|
|
|
|
|
message: ImMessage
|
|
|
|
|
showTimeline: boolean
|
|
|
|
|
onImageSettled: () => void
|
2026-05-25 12:06:15 +08:00
|
|
|
timelineLabel: string
|
2026-04-24 14:34:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const MessageItem = memo(
|
2026-05-25 12:06:15 +08:00
|
|
|
function MessageItem({ message, showTimeline, onImageSettled, timelineLabel }: MessageItemProps) {
|
|
|
|
|
const t = useI18n()
|
2026-04-24 14:34:21 +08:00
|
|
|
const { open } = useImageLightbox()
|
|
|
|
|
const isCustomer = message.senderType === "customer"
|
2026-05-30 14:00:23 +08:00
|
|
|
const senderName = isCustomer ? t("supportChat.customerSelf") : message.senderName?.trim() || t("supportChat.agentLabel")
|
2026-04-24 14:34:21 +08:00
|
|
|
const avatarSrc =
|
|
|
|
|
!isCustomer && message.senderAvatar?.trim() ? message.senderAvatar.trim() : undefined
|
|
|
|
|
const htmlContent = renderIMMessageHTML(message)
|
2026-04-25 11:26:53 +08:00
|
|
|
const fallbackName = senderName.slice(0, 1).toUpperCase()
|
2026-04-24 14:34:21 +08:00
|
|
|
|
|
|
|
|
return (
|
2026-04-25 11:26:53 +08:00
|
|
|
<div>
|
2026-04-24 14:34:21 +08:00
|
|
|
{showTimeline ? (
|
|
|
|
|
<div className="mb-3 flex items-center justify-center">
|
2026-04-25 11:26:53 +08:00
|
|
|
<Badge
|
|
|
|
|
variant="outline"
|
2026-04-25 11:31:16 +08:00
|
|
|
className="border-border bg-background/85 text-[11px] font-medium text-muted-foreground shadow-sm"
|
2026-04-25 11:26:53 +08:00
|
|
|
>
|
2026-05-25 12:06:15 +08:00
|
|
|
{timelineLabel}
|
2026-04-25 11:26:53 +08:00
|
|
|
</Badge>
|
2026-04-24 14:34:21 +08:00
|
|
|
</div>
|
|
|
|
|
) : null}
|
|
|
|
|
|
2026-07-29 10:09:17 +08:00
|
|
|
<ConversationMessageRow
|
|
|
|
|
align={isCustomer ? "end" : "start"}
|
|
|
|
|
avatar={
|
|
|
|
|
!isCustomer ? (
|
|
|
|
|
<Avatar>
|
|
|
|
|
{avatarSrc ? <AvatarImage src={avatarSrc} alt="" /> : null}
|
|
|
|
|
<AvatarFallback className="bg-muted text-muted-foreground">
|
|
|
|
|
{fallbackName || t("supportChat.customerFallback")}
|
|
|
|
|
</AvatarFallback>
|
|
|
|
|
</Avatar>
|
|
|
|
|
) : null
|
|
|
|
|
}
|
|
|
|
|
contentClassName="max-w-[86%] gap-1.5"
|
|
|
|
|
headerClassName="flex-wrap gap-x-2 gap-y-1 px-1 text-[11px]"
|
|
|
|
|
header={
|
|
|
|
|
<>
|
2026-04-24 14:34:21 +08:00
|
|
|
<span className="font-medium">{senderName}</span>
|
|
|
|
|
<span>{formatDateTime(message.sentAt)}</span>
|
|
|
|
|
{isCustomer ? (
|
2026-05-30 14:00:23 +08:00
|
|
|
<span>{message.agentRead ? t("supportChat.agentRead") : t("supportChat.agentUnread")}</span>
|
2026-04-24 14:34:21 +08:00
|
|
|
) : null}
|
2026-07-29 10:09:17 +08:00
|
|
|
</>
|
|
|
|
|
}
|
|
|
|
|
>
|
|
|
|
|
<ConversationMessageBubble
|
|
|
|
|
variant={isCustomer ? "customer" : "system"}
|
|
|
|
|
className={cn(
|
|
|
|
|
"rounded-lg border-0 px-3 py-2 text-sm leading-normal shadow-[0_10px_22px_rgba(15,23,42,0.06)]",
|
|
|
|
|
isCustomer
|
2026-07-29 19:13:53 +08:00
|
|
|
? "!bg-[#a9ea7a] !text-[#161616] dark:!bg-emerald-500 dark:!text-emerald-950"
|
|
|
|
|
: "!border-border !bg-card !text-card-foreground dark:!bg-background"
|
2026-07-29 10:09:17 +08:00
|
|
|
)}
|
|
|
|
|
>
|
|
|
|
|
<ImMessageHTML
|
|
|
|
|
html={htmlContent}
|
2026-04-24 14:34:21 +08:00
|
|
|
className={cn(
|
|
|
|
|
isCustomer
|
2026-07-29 10:09:17 +08:00
|
|
|
? "[&_p]:text-[#161616] dark:[&_p]:text-emerald-950 [&_a]:text-[#161616] dark:[&_a]:text-emerald-950 [&_a]:underline [&_img]:cursor-zoom-in"
|
|
|
|
|
: "[&_a]:text-card-foreground [&_a]:underline [&_img]:cursor-zoom-in"
|
2026-04-24 14:34:21 +08:00
|
|
|
)}
|
2026-07-29 10:09:17 +08:00
|
|
|
onImageSettled={onImageSettled}
|
|
|
|
|
onImageClick={open}
|
|
|
|
|
/>
|
|
|
|
|
</ConversationMessageBubble>
|
|
|
|
|
</ConversationMessageRow>
|
2026-04-24 14:34:21 +08:00
|
|
|
</div>
|
|
|
|
|
)
|
|
|
|
|
},
|
|
|
|
|
(prevProps, nextProps) =>
|
2026-04-25 12:26:45 +08:00
|
|
|
isSameMessageItemRender(prevProps.message, nextProps.message) &&
|
2026-04-24 14:34:21 +08:00
|
|
|
prevProps.showTimeline === nextProps.showTimeline &&
|
2026-05-25 12:06:15 +08:00
|
|
|
prevProps.timelineLabel === nextProps.timelineLabel &&
|
2026-04-24 14:34:21 +08:00
|
|
|
prevProps.onImageSettled === nextProps.onImageSettled
|
|
|
|
|
)
|
2026-04-25 12:26:45 +08:00
|
|
|
|
|
|
|
|
function isSameMessageItemRender(prev: ImMessage, next: ImMessage) {
|
|
|
|
|
return (
|
|
|
|
|
prev.id === next.id &&
|
|
|
|
|
prev.senderType === next.senderType &&
|
|
|
|
|
prev.senderName === next.senderName &&
|
|
|
|
|
prev.senderAvatar === next.senderAvatar &&
|
|
|
|
|
prev.messageType === next.messageType &&
|
|
|
|
|
prev.content === next.content &&
|
|
|
|
|
prev.payload === next.payload &&
|
|
|
|
|
prev.sentAt === next.sentAt &&
|
|
|
|
|
prev.agentRead === next.agentRead
|
|
|
|
|
)
|
|
|
|
|
}
|