Commit 6b41da3f by liimin

refactor(chat): 优化反馈组件和操作位置的计算逻辑

- 移除了 FeedbackActive 和 Feedback 组件中的 useMemo,改为直接计算条件
- 调整了 OperationWrapper 组件中的位置计算逻辑,确保操作按钮正确显示
- 移除了 OperationProvider 中的 positionRight 计算,简化了上下文值
- 注释掉了 ChatBodyProvider 中的宽度计算相关代码,减少了不必要的计算
1 parent 6893e298
...@@ -11,11 +11,11 @@ const FeedbackActive: FC = () => { ...@@ -11,11 +11,11 @@ const FeedbackActive: FC = () => {
const { supportFeedback } = useChatConfigContext() const { supportFeedback } = useChatConfigContext()
const { handleFeedback, localFeedback, isOpeningStatement } = useOperationContext() const { handleFeedback, localFeedback, isOpeningStatement } = useOperationContext()
// 判断是否已点赞 // 判断是否已点赞
const feedbackActive = useMemo( // const feedbackActive = useMemo(
() => supportFeedback && localFeedback?.rating && !isOpeningStatement, // () => supportFeedback && localFeedback?.rating && !isOpeningStatement,
[supportFeedback, localFeedback?.rating, onFeedback, isOpeningStatement], // [supportFeedback, localFeedback?.rating, onFeedback, isOpeningStatement],
) // )
if (!feedbackActive) return null if (!localFeedback?.rating||!supportFeedback||isOpeningStatement) return null
return ( return (
<div className="ml-1 flex items-center gap-0.5 rounded-[10px] border-[0.5px] border-components-actionbar-border bg-components-actionbar-bg p-0.5 shadow-md backdrop-blur-sm"> <div className="ml-1 flex items-center gap-0.5 rounded-[10px] border-[0.5px] border-components-actionbar-border bg-components-actionbar-bg p-0.5 shadow-md backdrop-blur-sm">
{localFeedback?.rating === 'like' && ( {localFeedback?.rating === 'like' && (
......
...@@ -11,11 +11,11 @@ const Feedback: FC = () => { ...@@ -11,11 +11,11 @@ const Feedback: FC = () => {
const { supportFeedback } = useChatConfigContext() const { supportFeedback } = useChatConfigContext()
const { handleFeedback, localFeedback, isOpeningStatement } = useOperationContext() const { handleFeedback, localFeedback, isOpeningStatement } = useOperationContext()
// 判断反馈按钮是否启用 // 判断反馈按钮是否启用
const feedbackEnabled = useMemo( // const feedbackEnabled = useMemo(
() => supportFeedback && !localFeedback?.rating && !isOpeningStatement, // () => supportFeedback && !localFeedback?.rating && !isOpeningStatement,
[supportFeedback, localFeedback?.rating, onFeedback, isOpeningStatement], // [supportFeedback, localFeedback?.rating, onFeedback, isOpeningStatement],
) // )
if (!feedbackEnabled) return null if (localFeedback?.rating || !supportFeedback || isOpeningStatement) return null
return ( return (
<div className="ml-1 hidden items-center gap-0.5 rounded-[10px] border-[0.5px] border-components-actionbar-border bg-components-actionbar-bg p-0.5 shadow-md backdrop-blur-sm group-hover:flex"> <div className="ml-1 hidden items-center gap-0.5 rounded-[10px] border-[0.5px] border-components-actionbar-border bg-components-actionbar-bg p-0.5 shadow-md backdrop-blur-sm group-hover:flex">
<ActionButton onClick={() => handleFeedback('like')}> <ActionButton onClick={() => handleFeedback('like')}>
......
import type { FC } from 'react' import type { FC } from 'react'
import { memo } from 'react' import { memo, useMemo } from 'react'
import cn from '@/utils/classnames' import cn from '@/utils/classnames'
import { useOperationContext } from '../../context/operation' import { useOperationContext } from '../../context/operation'
import { useChatBodyContext } from '../../../body/context' import { useChatBodyContext } from '../../../body/context'
...@@ -9,16 +9,20 @@ type OperationWrapperProps = { ...@@ -9,16 +9,20 @@ type OperationWrapperProps = {
} }
const OperationWrapper: FC<OperationWrapperProps> = ({ children }) => { const OperationWrapper: FC<OperationWrapperProps> = ({ children }) => {
const { positionRight } = useOperationContext() const { operationWidth } = useOperationContext()
const { contentWidth, workflowProcess } = useChatBodyContext() const { workflowProcess, contentRef, containerRef } = useChatBodyContext()
const hasWorkflowProcess = !!workflowProcess const hasWorkflowProcess = !!workflowProcess
const contentWidth = contentRef.current?.clientWidth || 0
const containerWidth = containerRef.current?.clientWidth || 0
const maxSize = containerWidth - contentWidth - 4
const positionRight = operationWidth < maxSize
return ( return (
<div <div
className={cn( className={cn(
'absolute flex justify-end gap-1', 'absolute flex justify-end gap-1',
// hasWorkflowProcess && '-bottom-4 right-2', // hasWorkflowProcess && '-bottom-4 right-2',
!positionRight && '-bottom-4 right-2', !positionRight && '-bottom-4 right-2',
!hasWorkflowProcess && positionRight && '!top-[9px]', !hasWorkflowProcess && positionRight && '-bottom-[-2px]', //'!top-[9px]',
!hasWorkflowProcess && positionRight ? 'left-auto' : '', !hasWorkflowProcess && positionRight ? 'left-auto' : '',
)} )}
style={!hasWorkflowProcess && positionRight ? { left: contentWidth + 8 } : {}}> style={!hasWorkflowProcess && positionRight ? { left: contentWidth + 8 } : {}}>
......
...@@ -12,7 +12,7 @@ interface OperationContextType { ...@@ -12,7 +12,7 @@ interface OperationContextType {
content: string content: string
handleFeedback: (rating: 'like' | 'dislike' | null) => Promise<void> handleFeedback: (rating: 'like' | 'dislike' | null) => Promise<void>
operationWidth: number operationWidth: number
positionRight: boolean // positionRight: boolean
isLast: boolean isLast: boolean
isOpeningStatement: boolean isOpeningStatement: boolean
localFeedback: { rating: 'like' | 'dislike' | null } localFeedback: { rating: 'like' | 'dislike' | null }
...@@ -39,10 +39,10 @@ export const OperationProvider: FC<OperationProviderProps> = ({ ...@@ -39,10 +39,10 @@ export const OperationProvider: FC<OperationProviderProps> = ({
const {onFeedback,onRegenerate } = useChatContext() const {onFeedback,onRegenerate } = useChatContext()
const {supportFeedback,text_to_speech,annotation_reply,supportAnnotation} = useChatConfigContext() const {supportFeedback,text_to_speech,annotation_reply,supportAnnotation} = useChatConfigContext()
const [showLike, setShowLike] = useState(false) const [showLike, setShowLike] = useState(false)
const { id, isOpeningStatement, content: messageContent, isLast,noChatInput,annotation,parentMessageId,containerWidth,contentWidth, showPromptLog,feedback, adminFeedback, agent_thoughts, } = useChatBodyContext() const { id, isOpeningStatement, content: messageContent, isLast,noChatInput,annotation,parentMessageId, showPromptLog,feedback, adminFeedback, agent_thoughts, } = useChatBodyContext()
const [localFeedback, setLocalFeedback] = useState(supportAnnotation ? adminFeedback : feedback) const [localFeedback, setLocalFeedback] = useState(supportAnnotation ? adminFeedback : feedback)
const [isShowReplyModal, setIsShowReplyModal] = useState(false) const [isShowReplyModal, setIsShowReplyModal] = useState(false)
const maxSize = containerWidth - contentWidth - 4 // const maxSize = containerWidth - contentWidth - 4
// 计算内容 // 计算内容
const content = useMemo(() => { const content = useMemo(() => {
if (agent_thoughts?.length) return agent_thoughts.reduce((acc, cur) => acc + cur.thought, '') if (agent_thoughts?.length) return agent_thoughts.reduce((acc, cur) => acc + cur.thought, '')
...@@ -91,14 +91,14 @@ export const OperationProvider: FC<OperationProviderProps> = ({ ...@@ -91,14 +91,14 @@ export const OperationProvider: FC<OperationProviderProps> = ({
]) ])
// 判断位置是否靠左 // 判断位置是否靠左
const positionRight = useMemo(() => operationWidth < maxSize, [operationWidth, maxSize]) // const positionRight = useMemo(() => operationWidth < maxSize, [operationWidth, maxSize])
const value = useMemo( const value = useMemo(
() => ({ () => ({
content, content,
handleFeedback, handleFeedback,
operationWidth, operationWidth,
positionRight, // positionRight,
showLike, showLike,
isLast: !!isLast, isLast: !!isLast,
isOpeningStatement: !!isOpeningStatement, isOpeningStatement: !!isOpeningStatement,
...@@ -108,7 +108,7 @@ export const OperationProvider: FC<OperationProviderProps> = ({ ...@@ -108,7 +108,7 @@ export const OperationProvider: FC<OperationProviderProps> = ({
setIsShowReplyModal, setIsShowReplyModal,
handleRegenerate handleRegenerate
}), }),
[content, handleFeedback, operationWidth, positionRight, showLike, isLast], [content, handleFeedback, operationWidth, showLike, isLast],
) )
return <OperationContext.Provider value={value}>{children}</OperationContext.Provider> return <OperationContext.Provider value={value}>{children}</OperationContext.Provider>
......
...@@ -4,8 +4,8 @@ import { ChatItem } from '../../types' ...@@ -4,8 +4,8 @@ import { ChatItem } from '../../types'
import { useChatListContext, useChatRespondingContext } from '@/app/components/base/chat/chat-with-history/context' import { useChatListContext, useChatRespondingContext } from '@/app/components/base/chat/chat-with-history/context'
// 定义 AnswerContext 的类型 // 定义 AnswerContext 的类型
interface ChatBodyContextType { interface ChatBodyContextType {
containerWidth: number // containerWidth: number
contentWidth: number // contentWidth: number
responding: boolean responding: boolean
hasAgentThoughts: boolean hasAgentThoughts: boolean
citation: ChatItem['citation'] citation: ChatItem['citation']
...@@ -84,48 +84,49 @@ export const ChatBodyProvider: React.FC<ChatBodyProviderProps> = ({ ...@@ -84,48 +84,49 @@ export const ChatBodyProvider: React.FC<ChatBodyProviderProps> = ({
parentMessageId, parentMessageId,
} = item } = item
const hasAgentThoughts = !!agent_thoughts?.length const hasAgentThoughts = !!agent_thoughts?.length
const [containerWidth, setContainerWidth] = useState(0) // const [containerWidth, setContainerWidth] = useState(0)
const [contentWidth, setContentWidth] = useState(0) // const [contentWidth, setContentWidth] = useState(0)
const containerRef = useRef<HTMLDivElement>(null) const containerRef = useRef<HTMLDivElement>(null)
const contentRef = useRef<HTMLDivElement>(null) const contentRef = useRef<HTMLDivElement>(null)
const { isResponding } = useChatRespondingContext() const { isResponding } = useChatRespondingContext()
const { chatList } = useChatListContext() const { chatList } = useChatListContext()
const isLast = isAnswer && item.id === chatList[chatList.length - 1]?.id const isLast = useMemo(() => isAnswer && item.id === chatList[chatList.length - 1]?.id, [chatList.length, isAnswer])
const responding = isLast && isResponding const responding = isLast && isResponding
const getContainerWidth = useCallback(() => { // const getContainerWidth = useCallback(() => {
if (containerRef.current) setContainerWidth(containerRef.current?.clientWidth + 16) // if (containerRef.current) setContainerWidth(containerRef.current?.clientWidth + 16)
}, [containerRef, setContainerWidth]) // }, [containerRef, setContainerWidth])
useEffect(() => { // useEffect(() => {
getContainerWidth() // getContainerWidth()
}, []) // }, [])
const getContentWidth = useCallback(() => { // const getContentWidth = useCallback(() => {
if (contentRef.current) setContentWidth(contentRef.current?.clientWidth) // if (contentRef.current) setContentWidth(contentRef.current?.clientWidth)
}, [contentRef, setContentWidth]) // console.log('getContentWidth',contentRef.current?.clientWidth)
// }, [contentRef, setContentWidth])
useEffect(() => { // useEffect(() => {
if (!responding) getContentWidth() // if (!responding) getContentWidth()
}, [responding]) // }, [responding])
// Recalculate contentWidth when content changes (e.g., SVG preview/source toggle) // Recalculate contentWidth when content changes (e.g., SVG preview/source toggle)
useEffect(() => { // useEffect(() => {
if (!containerRef.current) return // if (!containerRef.current) return
const resizeObserver = new ResizeObserver(() => { // const resizeObserver = new ResizeObserver(() => {
getContentWidth() // getContentWidth()
}) // })
resizeObserver.observe(containerRef.current) // resizeObserver.observe(containerRef.current)
return () => { // return () => {
resizeObserver.disconnect() // resizeObserver.disconnect()
} // }
}, []) // }, [])
const value = useMemo( const value = useMemo(
() => ({ () => ({
id, id,
isOpeningStatement: !!isOpeningStatement, isOpeningStatement: !!isOpeningStatement,
containerWidth, // containerWidth,
contentWidth, // contentWidth,
responding: !!responding, responding: !!responding,
hasAgentThoughts, hasAgentThoughts,
citation, citation,
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!