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 }) = ...@@ -54,7 +54,8 @@ const ChatWrapper: FC<Props> = React.memo(({ presuppositionQuestion, inputs }) =
handleFeedback, handleFeedback,
// appInfoLoading, // appInfoLoading,
handleNewConversation, handleNewConversation,
isManuallyStopped, // 添加这个状态 isManuallyStopped,
taskId,
} = useChat(inputs) } = useChat(inputs)
useEffect(() => { useEffect(() => {
if (autoSendQuestion && config.initial_message !== undefined && isAutoSend) { if (autoSendQuestion && config.initial_message !== undefined && isAutoSend) {
...@@ -110,7 +111,7 @@ const ChatWrapper: FC<Props> = React.memo(({ presuppositionQuestion, inputs }) = ...@@ -110,7 +111,7 @@ const ChatWrapper: FC<Props> = React.memo(({ presuppositionQuestion, inputs }) =
onRestart={onRestart} onRestart={onRestart}
onFeedback={handleFeedback} onFeedback={handleFeedback}
onStopResponding={handleStop}> onStopResponding={handleStop}>
<ChatRespondingProvider isResponding={isResponding}> <ChatRespondingProvider isResponding={isResponding} taskId={taskId}>
<ChatListProvider chatList={chatList} isManuallyStopped={isManuallyStopped}> <ChatListProvider chatList={chatList} isManuallyStopped={isManuallyStopped}>
<Chat /> <Chat />
</ChatListProvider> </ChatListProvider>
......
...@@ -23,6 +23,7 @@ const ChatContext = createContext<ChatContextValue>({ ...@@ -23,6 +23,7 @@ const ChatContext = createContext<ChatContextValue>({
onRestart: noop, onRestart: noop,
onSend: noop, onSend: noop,
onFeedback: noop, onFeedback: noop,
onStopResponding: noop,
}) })
type ChatContextProviderProps = { type ChatContextProviderProps = {
...@@ -45,7 +46,7 @@ export const ChatContextProvider = ({ ...@@ -45,7 +46,7 @@ export const ChatContextProvider = ({
onFeedback, onFeedback,
onStopResponding, onStopResponding,
}), }),
[], [onSend, onRegenerate, onRestart, onFeedback, onStopResponding],
) )
return <ChatContext.Provider value={value}>{children}</ChatContext.Provider> return <ChatContext.Provider value={value}>{children}</ChatContext.Provider>
} }
......
...@@ -7,10 +7,12 @@ import { usePostMessage } from '../hooks' ...@@ -7,10 +7,12 @@ import { usePostMessage } from '../hooks'
type RespondingContext = { type RespondingContext = {
isResponding?: boolean isResponding?: boolean
taskId?: string
} }
const ChatRespondingContext = createContext<RespondingContext>({ const ChatRespondingContext = createContext<RespondingContext>({
isResponding: false, isResponding: false,
taskId: '',
}) })
export const useChatRespondingContext = () => useContext(ChatRespondingContext) export const useChatRespondingContext = () => useContext(ChatRespondingContext)
...@@ -19,9 +21,10 @@ type Props = { ...@@ -19,9 +21,10 @@ type Props = {
children: React.ReactNode children: React.ReactNode
} & RespondingContext } & RespondingContext
const postMessage = usePostMessage() const postMessage = usePostMessage()
export const ChatRespondingProvider = ({ children, isResponding }: Props) => { export const ChatRespondingProvider = ({ children, isResponding, taskId }: Props) => {
const __isResponding = useMemo(() => isResponding, [isResponding]) const __isResponding = useMemo(() => isResponding, [isResponding])
const value = useMemo(() => ({ isResponding: __isResponding }), [__isResponding]) const __taskId = useMemo(() => taskId, [taskId])
useEffect(() => postMessage(value), [__isResponding]) const value = useMemo(() => ({ isResponding: __isResponding, taskId: __taskId }), [__isResponding, __taskId])
useEffect(() => postMessage(value), [__isResponding, __taskId])
return <ChatRespondingContext.Provider value={value}>{children}</ChatRespondingContext.Provider> return <ChatRespondingContext.Provider value={value}>{children}</ChatRespondingContext.Provider>
} }
...@@ -167,11 +167,13 @@ export const useChat = (inputs: Inputs = {}) => { ...@@ -167,11 +167,13 @@ export const useChat = (inputs: Inputs = {}) => {
// 清除最后一个空回答节点 // 清除最后一个空回答节点
const lastAnswer = getLastAnswer(refs.current.chatTree) const lastAnswer = getLastAnswer(refs.current.chatTree)
refs.current.taskId = '' // 清空 taskId
if (lastAnswer && !lastAnswer.content.trim()) { if (lastAnswer && !lastAnswer.content.trim()) {
setState(prev => ({ setState(prev => ({
...prev, ...prev,
chatTree: prev.chatTree.filter(item => item.id !== lastAnswer.id), chatTree: prev.chatTree.filter(item => item.id !== lastAnswer.id),
})) }))
} }
}, 50) }, 50)
}, [stopChat]) }, [stopChat])
...@@ -682,6 +684,7 @@ export const useChat = (inputs: Inputs = {}) => { ...@@ -682,6 +684,7 @@ export const useChat = (inputs: Inputs = {}) => {
suggestedQuestions, suggestedQuestions,
handleNewConversation, handleNewConversation,
isManuallyStopped: state.isManuallyStopped, // 添加手动停止状态 isManuallyStopped: state.isManuallyStopped, // 添加手动停止状态
taskId: refs.current.taskId,
}), }),
[config, chatList, isResponding, suggestedQuestions, state.isManuallyStopped], [config, chatList, isResponding, suggestedQuestions, state.isManuallyStopped],
) )
......
...@@ -5,13 +5,19 @@ import { AI_DATA_SEND, AI_DATA_STOP } from '@/app/components/base/chat/constants ...@@ -5,13 +5,19 @@ import { AI_DATA_SEND, AI_DATA_STOP } from '@/app/components/base/chat/constants
const Send: FC = () => { const Send: FC = () => {
const { handleSend } = useMessageContext() const { handleSend } = useMessageContext()
const { isResponding } = useChatRespondingContext() const { isResponding, taskId } = useChatRespondingContext()
const { onStopResponding: stopResponding } = useChatContext() const { onStopResponding: stopResponding } = useChatContext()
return ( return (
<> <>
<div className="send-icon-box"> <div className="send-icon-box">
{isResponding && ( {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" />} {!isResponding && <img src={AI_DATA_SEND} onClick={handleSend} alt="Send" title="Send" className="send-icon" />}
</div> </div>
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!