Commit 6b41da3f by liimin

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

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