mirror of
https://github.com/CherryHQ/cherry-studio.git
synced 2026-01-05 12:29:44 +08:00
refactor(WebSearchButton): simplify web search button logic and improve tooltip behavior
- Removed unused imports and streamlined the logic for enabling web search. - Updated the tooltip title to reflect the current state of web search functionality. - Enhanced the handling of quick panel opening based on the assistant's web search settings.
This commit is contained in:
parent
c7ab71f01f
commit
8c6684cbdf
@ -65,7 +65,7 @@ const KnowledgeBaseButton: FC<Props> = ({ ref, selectedBases, onSelect, disabled
|
||||
title: t('chat.input.knowledge_base'),
|
||||
list: baseItems,
|
||||
symbol: '#',
|
||||
multiple: true,
|
||||
multiple: false,
|
||||
afterAction({ item }) {
|
||||
item.isSelected = !item.isSelected
|
||||
}
|
||||
|
||||
@ -6,10 +6,9 @@ import WebSearchService from '@renderer/services/WebSearchService'
|
||||
import { Assistant, WebSearchProvider } from '@renderer/types'
|
||||
import { hasObjectKey } from '@renderer/utils'
|
||||
import { Tooltip } from 'antd'
|
||||
import { CircleX, Globe, Settings } from 'lucide-react'
|
||||
import { Globe } from 'lucide-react'
|
||||
import { FC, memo, useCallback, useImperativeHandle, useMemo } from 'react'
|
||||
import { useTranslation } from 'react-i18next'
|
||||
import { useNavigate } from 'react-router-dom'
|
||||
|
||||
export interface WebSearchButtonRef {
|
||||
openQuickPanel: () => void
|
||||
@ -23,11 +22,12 @@ interface Props {
|
||||
|
||||
const WebSearchButton: FC<Props> = ({ ref, assistant, ToolbarButton }) => {
|
||||
const { t } = useTranslation()
|
||||
const navigate = useNavigate()
|
||||
const quickPanel = useQuickPanel()
|
||||
const { providers } = useWebSearchProviders()
|
||||
const { updateAssistant } = useAssistant(assistant.id)
|
||||
|
||||
const enableWebSearch = assistant?.webSearchProviderId || assistant.enableWebSearch
|
||||
|
||||
const updateSelectedWebSearchProvider = useCallback(
|
||||
(providerId?: WebSearchProvider['id']) => {
|
||||
// TODO: updateAssistant有性能问题,会导致关闭快捷面板卡顿
|
||||
@ -78,42 +78,41 @@ const WebSearchButton: FC<Props> = ({ ref, assistant, ToolbarButton }) => {
|
||||
})
|
||||
}
|
||||
|
||||
items.push({
|
||||
label: t('chat.input.web_search.settings'),
|
||||
icon: <Settings />,
|
||||
action: () => navigate('/settings/tool/websearch')
|
||||
})
|
||||
|
||||
items.unshift({
|
||||
label: t('common.close'),
|
||||
description: t('chat.input.web_search.no_web_search.description'),
|
||||
icon: <CircleX />,
|
||||
isSelected: !assistant.enableWebSearch && !assistant.webSearchProviderId,
|
||||
action: () => {
|
||||
updateSelectedWebSearchProvider(undefined)
|
||||
}
|
||||
})
|
||||
|
||||
return items
|
||||
}, [
|
||||
assistant.model,
|
||||
assistant.enableWebSearch,
|
||||
assistant.webSearchProviderId,
|
||||
assistant.model,
|
||||
assistant?.webSearchProviderId,
|
||||
providers,
|
||||
t,
|
||||
updateSelectedWebSearchProvider,
|
||||
updateSelectedWebSearchBuiltin,
|
||||
navigate
|
||||
updateSelectedWebSearchProvider
|
||||
])
|
||||
|
||||
const openQuickPanel = useCallback(() => {
|
||||
if (assistant.webSearchProviderId) {
|
||||
return updateSelectedWebSearchProvider(undefined)
|
||||
}
|
||||
|
||||
if (assistant.enableWebSearch) {
|
||||
return updateSelectedWebSearchBuiltin()
|
||||
}
|
||||
|
||||
quickPanel.open({
|
||||
title: t('chat.input.web_search'),
|
||||
list: providerItems,
|
||||
symbol: '?',
|
||||
pageSize: 9
|
||||
})
|
||||
}, [quickPanel, providerItems, t])
|
||||
}, [
|
||||
assistant.webSearchProviderId,
|
||||
assistant.enableWebSearch,
|
||||
quickPanel,
|
||||
t,
|
||||
providerItems,
|
||||
updateSelectedWebSearchProvider,
|
||||
updateSelectedWebSearchBuiltin
|
||||
])
|
||||
|
||||
const handleOpenQuickPanel = useCallback(() => {
|
||||
if (quickPanel.isVisible && quickPanel.symbol === '?') {
|
||||
@ -128,13 +127,12 @@ const WebSearchButton: FC<Props> = ({ ref, assistant, ToolbarButton }) => {
|
||||
}))
|
||||
|
||||
return (
|
||||
<Tooltip placement="top" title={t('chat.input.web_search')} arrow>
|
||||
<Tooltip placement="top" title={enableWebSearch ? t('common.close') : t('chat.input.web_search')} arrow>
|
||||
<ToolbarButton type="text" onClick={handleOpenQuickPanel}>
|
||||
<Globe
|
||||
size={18}
|
||||
style={{
|
||||
color:
|
||||
assistant?.webSearchProviderId || assistant.enableWebSearch ? 'var(--color-link)' : 'var(--color-icon)'
|
||||
color: enableWebSearch ? 'var(--color-link)' : 'var(--color-icon)'
|
||||
}}
|
||||
/>
|
||||
</ToolbarButton>
|
||||
|
||||
Loading…
Reference in New Issue
Block a user