Commit 6a029a0e by Liu

fix:点击工具时新建会话

parent 187e8a25
...@@ -6,8 +6,9 @@ import { LoginModal } from '../LoginModal' ...@@ -6,8 +6,9 @@ import { LoginModal } from '../LoginModal'
import type { RootState } from '@/store' import type { RootState } from '@/store'
import SendIcon from '@/assets/svg/send.svg?react' import SendIcon from '@/assets/svg/send.svg?react'
import { type WithAuthProps, withAuth } from '@/auth/withAuth' import { type WithAuthProps, withAuth } from '@/auth/withAuth'
import { useAppSelector } from '@/store/hook' import { useAppDispatch, useAppSelector } from '@/store/hook'
import { fetchToolList } from '@/api/home' import { fetchToolList } from '@/api/home'
import { createConversation } from '@/store/conversationSlice'
interface ChatEditorProps { interface ChatEditorProps {
onChange?: (value: string) => void onChange?: (value: string) => void
...@@ -22,6 +23,7 @@ interface ChatEditorProps { ...@@ -22,6 +23,7 @@ interface ChatEditorProps {
const ChatEditorBase: React.FC<ChatEditorProps & WithAuthProps> = ({ checkAuth, onChange, onFocus, onSubmit, onToolClick, placeholders, showContentTips = false, initialValue = '' }) => { const ChatEditorBase: React.FC<ChatEditorProps & WithAuthProps> = ({ checkAuth, onChange, onFocus, onSubmit, onToolClick, placeholders, showContentTips = false, initialValue = '' }) => {
// const dispatch = useAppDispatch() // const dispatch = useAppDispatch()
const [content, setContent] = useState(initialValue) const [content, setContent] = useState(initialValue)
const dispatch = useAppDispatch()
const editorRef = useRef<HTMLDivElement>(null) const editorRef = useRef<HTMLDivElement>(null)
const [currentPlaceholder, setCurrentPlaceholder] = useState(0) const [currentPlaceholder, setCurrentPlaceholder] = useState(0)
const intervalRef = useRef<NodeJS.Timeout | null>(null) const intervalRef = useRef<NodeJS.Timeout | null>(null)
...@@ -118,21 +120,42 @@ const ChatEditorBase: React.FC<ChatEditorProps & WithAuthProps> = ({ checkAuth, ...@@ -118,21 +120,42 @@ const ChatEditorBase: React.FC<ChatEditorProps & WithAuthProps> = ({ checkAuth,
document.execCommand('insertText', false, text) document.execCommand('insertText', false, text)
} }
// 处理通用模式按钮点击 // 处理通用模式按钮点击:先创建新会话
const handleGeneralClick = () => { const handleGeneralClick = async () => {
// 切换到通用模式(true 表示通用模式,恢复默认不切换左侧) if (!checkAuth())
setIsToolBtnActive(true) return
// 清除选中的工具ID try {
setSelectedToolId(null) await dispatch(createConversation({
// 调用父组件的回调函数,传递通用模式状态和工具ID,以及是否需要改变样式 conversationData: {},
onToolClick?.(true, undefined, false) shouldNavigate: true,
shouldSendQuestion: '',
})).unwrap()
setIsToolBtnActive(true)
setSelectedToolId(null)
onToolClick?.(true, undefined, false)
}
catch (error) {
console.error('创建会话失败:', error)
}
} }
// 处理工具按钮点击 // 处理工具按钮点击:先创建新会话,再切换工具
const handleToolClick = (tool: any) => { const handleToolClick = async (tool: any) => {
setSelectedToolId(tool.toolId) if (!checkAuth())
setIsToolBtnActive(false) return
onToolClick?.(false, tool.toolId, true) try {
await dispatch(createConversation({
conversationData: {},
shouldNavigate: true,
shouldSendQuestion: '',
})).unwrap()
setSelectedToolId(tool.toolId)
setIsToolBtnActive(false)
onToolClick?.(false, tool.toolId, true)
}
catch (error) {
console.error('创建会话失败:', error)
}
} }
useEffect(() => { useEffect(() => {
...@@ -240,8 +263,17 @@ const ChatEditorBase: React.FC<ChatEditorProps & WithAuthProps> = ({ checkAuth, ...@@ -240,8 +263,17 @@ const ChatEditorBase: React.FC<ChatEditorProps & WithAuthProps> = ({ checkAuth,
{toolList && toolList.length > 0 && ( {toolList && toolList.length > 0 && (
<div className="absolute left-4 bottom-2 flex items-center gap-3 pointer-events-auto pl-[16px]"> <div className="absolute left-4 bottom-2 flex items-center gap-3 pointer-events-auto pl-[16px]">
{toolList.map((tool: any, index: number) => { {toolList.map((tool: any, index: number) => {
// index === 2 的按钮(通用模式)在默认状态下高亮 // index === 1 的按钮(通用模式)在默认状态下高亮
const isSelected = (selectedToolId === tool.toolId && !isToolBtnActive) || (index === 2 && isToolBtnActive) const isSelected = (selectedToolId === tool.toolId && !isToolBtnActive) || (index === 1 && isToolBtnActive)
const handleButtonPress = async () => {
// 高亮状态直接返回,避免重复触发
if (isSelected)
return
if (index === 1)
await handleGeneralClick()
else
await handleToolClick(tool)
}
return ( return (
<Button <Button
key={tool.toolId || `tool-${index}`} key={tool.toolId || `tool-${index}`}
...@@ -252,7 +284,7 @@ const ChatEditorBase: React.FC<ChatEditorProps & WithAuthProps> = ({ checkAuth, ...@@ -252,7 +284,7 @@ const ChatEditorBase: React.FC<ChatEditorProps & WithAuthProps> = ({ checkAuth,
}`} }`}
radius="full" radius="full"
variant="bordered" variant="bordered"
onPress={() => index === 2 ? handleGeneralClick() : handleToolClick(tool)} onPress={handleButtonPress}
> >
{tool.toolIcon && ( {tool.toolIcon && (
<img <img
......
...@@ -13,7 +13,7 @@ export const ChatWelcome: React.FC<ChatWelcomeProps> = ({ isEfficiencyMode = fal ...@@ -13,7 +13,7 @@ export const ChatWelcome: React.FC<ChatWelcomeProps> = ({ isEfficiencyMode = fal
// 根据模式显示不同的提示语 // 根据模式显示不同的提示语
const getWelcomeText = () => { const getWelcomeText = () => {
if (isEfficiencyMode) { if (isEfficiencyMode) {
return 'HI~ 我是您的提质增效助手,有什么可以帮您?' return 'HI~我是您的数据助手,可以帮你查询业务数据哦'
} }
return '您好,有什么我可以帮您的吗?' return '您好,有什么我可以帮您的吗?'
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment