Commit 76f1d3a2 by 李伟康

fix(chat): 解决聊天停止按钮在无任务ID时的禁用状态

- 在Send组件中添加taskId检查,当无任务ID时禁用停止按钮
- 为停止按钮添加cursor-not-allowed和opacity-50样式类
- 在ChatRespondingContext中添加taskId属性支持
- 在useChat hook中返回taskId到组件状态
- 修复ChatContextProvider的useMemo依赖数组问题
1 parent 85696125
......@@ -54,7 +54,8 @@ const ChatWrapper: FC<Props> = React.memo(({ presuppositionQuestion, inputs }) =
handleFeedback,
// appInfoLoading,
handleNewConversation,
isManuallyStopped, // 添加这个状态
isManuallyStopped,
taskId,
} = useChat(inputs)
useEffect(() => {
if (autoSendQuestion && config.initial_message !== undefined && isAutoSend) {
......@@ -110,7 +111,7 @@ const ChatWrapper: FC<Props> = React.memo(({ presuppositionQuestion, inputs }) =
onRestart={onRestart}
onFeedback={handleFeedback}
onStopResponding={handleStop}>
<ChatRespondingProvider isResponding={isResponding}>
<ChatRespondingProvider isResponding={isResponding} taskId={taskId}>
<ChatListProvider chatList={chatList} isManuallyStopped={isManuallyStopped}>
<Chat />
</ChatListProvider>
......
......@@ -23,6 +23,7 @@ const ChatContext = createContext<ChatContextValue>({
onRestart: noop,
onSend: noop,
onFeedback: noop,
onStopResponding: noop,
})
type ChatContextProviderProps = {
......@@ -45,7 +46,7 @@ export const ChatContextProvider = ({
onFeedback,
onStopResponding,
}),
[],
[onSend, onRegenerate, onRestart, onFeedback, onStopResponding],
)
return <ChatContext.Provider value={value}>{children}</ChatContext.Provider>
}
......
......@@ -7,10 +7,12 @@ import { usePostMessage } from '../hooks'
type RespondingContext = {
isResponding?: boolean
taskId?: string
}
const ChatRespondingContext = createContext<RespondingContext>({
isResponding: false,
taskId: '',
})
export const useChatRespondingContext = () => useContext(ChatRespondingContext)
......@@ -19,9 +21,10 @@ type Props = {
children: React.ReactNode
} & RespondingContext
const postMessage = usePostMessage()
export const ChatRespondingProvider = ({ children, isResponding }: Props) => {
export const ChatRespondingProvider = ({ children, isResponding, taskId }: Props) => {
const __isResponding = useMemo(() => isResponding, [isResponding])
const value = useMemo(() => ({ isResponding: __isResponding }), [__isResponding])
useEffect(() => postMessage(value), [__isResponding])
const __taskId = useMemo(() => taskId, [taskId])
const value = useMemo(() => ({ isResponding: __isResponding, taskId: __taskId }), [__isResponding, __taskId])
useEffect(() => postMessage(value), [__isResponding, __taskId])
return <ChatRespondingContext.Provider value={value}>{children}</ChatRespondingContext.Provider>
}
......@@ -167,11 +167,13 @@ export const useChat = (inputs: Inputs = {}) => {
// 清除最后一个空回答节点
const lastAnswer = getLastAnswer(refs.current.chatTree)
refs.current.taskId = '' // 清空 taskId
if (lastAnswer && !lastAnswer.content.trim()) {
setState(prev => ({
...prev,
chatTree: prev.chatTree.filter(item => item.id !== lastAnswer.id),
}))
}
}, 50)
}, [stopChat])
......@@ -682,6 +684,7 @@ export const useChat = (inputs: Inputs = {}) => {
suggestedQuestions,
handleNewConversation,
isManuallyStopped: state.isManuallyStopped, // 添加手动停止状态
taskId: refs.current.taskId,
}),
[config, chatList, isResponding, suggestedQuestions, state.isManuallyStopped],
)
......
......@@ -5,13 +5,19 @@ import { AI_DATA_SEND, AI_DATA_STOP } from '@/app/components/base/chat/constants
const Send: FC = () => {
const { handleSend } = useMessageContext()
const { isResponding } = useChatRespondingContext()
const { isResponding, taskId } = useChatRespondingContext()
const { onStopResponding: stopResponding } = useChatContext()
return (
<>
<div className="send-icon-box">
{isResponding && (
<img src={AI_DATA_STOP} onClick={stopResponding} alt="Stop" title="Stop" className="send-icon" />
<img
src={AI_DATA_STOP}
onClick={taskId ? stopResponding : undefined}
alt="Stop"
title="Stop"
className={`send-icon ${!taskId ? 'cursor-not-allowed opacity-50' : ''}`}
/>
)}
{!isResponding && <img src={AI_DATA_SEND} onClick={handleSend} alt="Send" title="Send" className="send-icon" />}
</div>
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!