Files
hapi/web/src/components/AssistantChat/messages/SystemMessage.tsx
T
weishu 05c2cbb724 perf(web): narrow message state subscriptions
- web/src/components/AssistantChat/messages/UserMessage.tsx now subscribes only to role, text, status,
  and localId instead of the entire message object to avoid re-renders caused by unrelated message state
  changes (hover/isLast/etc.).
- web/src/components/AssistantChat/messages/SystemMessage.tsx now subscribes only to role, text, and icon
  for the same reason.
- This reduces unnecessary renders observed in React DevTools without changing message UI or behavior.
2025-12-19 21:48:03 +08:00

31 lines
1.2 KiB
TypeScript

import { useAssistantState } from '@assistant-ui/react'
import { getEventPresentation } from '@/chat/presentation'
import type { HappyChatMessageMetadata } from '@/lib/assistant-runtime'
export function HappySystemMessage() {
const role = useAssistantState(({ message }) => message.role)
const text = useAssistantState(({ message }) => {
if (message.role !== 'system') return ''
return message.content[0]?.type === 'text' ? message.content[0].text : ''
})
const icon = useAssistantState(({ message }) => {
if (message.role !== 'system') return null
const custom = message.metadata.custom as Partial<HappyChatMessageMetadata> | undefined
const event = custom?.kind === 'event' ? custom.event : undefined
return event ? getEventPresentation(event).icon : null
})
if (role !== 'system') return null
return (
<div className="py-1">
<div className="mx-auto w-fit max-w-[92%] px-2 text-center text-xs text-[var(--app-hint)] opacity-80">
<span className="inline-flex items-center gap-1">
{icon ? <span aria-hidden="true">{icon}</span> : null}
<span>{text}</span>
</span>
</div>
</div>
)
}