Skip to content

Commit 240c7b7

Browse files
authored
💄 style: Enable toggling search on/off via search button click & historyCount button (lobehub#9173)
* ✨ feat: Enhance Search Action with mobile support and update agent chat config * ✨ feat: Update History component to support mobile interactions and enhance Controls with form handling
1 parent b73d097 commit 240c7b7

File tree

4 files changed

+54
-5
lines changed

4 files changed

+54
-5
lines changed

‎src/features/ChatInput/ActionBar/History/Controls.tsx‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Form, type FormItemProps, SliderWithInput } from '@lobehub/ui';
2-
import { Switch } from 'antd';
2+
import { Switch, Form as AntdForm } from 'antd';
33
import { debounce } from 'lodash-es';
4-
import { memo } from 'react';
4+
import { memo, useEffect } from 'react';
55
import { useTranslation } from 'react-i18next';
66

77
import { useAgentStore } from '@/store/agent';
@@ -13,6 +13,7 @@ interface ControlsProps {
1313
}
1414
const Controls = memo<ControlsProps>(({ updating, setUpdating }) => {
1515
const { t } = useTranslation('setting');
16+
const [form] = AntdForm.useForm();
1617

1718
const [historyCount, enableHistoryCount, updateAgentConfig] = useAgentStore((s) => {
1819
return [
@@ -22,6 +23,14 @@ const Controls = memo<ControlsProps>(({ updating, setUpdating }) => {
2223
];
2324
});
2425

26+
// Sync external store updates to the form without remounting to keep Switch animation
27+
useEffect(() => {
28+
form?.setFieldsValue({
29+
enableHistoryCount,
30+
historyCount,
31+
});
32+
}, [enableHistoryCount, historyCount, form]);
33+
2534
let items: FormItemProps[] = [
2635
{
2736
children: <Switch loading={updating} size={'small'} />,
@@ -55,6 +64,7 @@ const Controls = memo<ControlsProps>(({ updating, setUpdating }) => {
5564

5665
return (
5766
<Form
67+
form={form}
5868
initialValues={{
5969
enableHistoryCount,
6070
historyCount,

‎src/features/ChatInput/ActionBar/History/index.tsx‎

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,20 @@ import { useTranslation } from 'react-i18next';
44

55
import { useAgentStore } from '@/store/agent';
66
import { agentChatConfigSelectors, agentSelectors } from '@/store/agent/selectors';
7+
import { useIsMobile } from '@/hooks/useIsMobile';
78

89
import Action from '../components/Action';
910
import Controls from './Controls';
1011

1112
const History = memo(() => {
12-
const [isLoading] = useAgentStore((s) => [agentSelectors.isAgentConfigLoading(s)]);
13+
const [isLoading, chatConfig, updateAgentChatConfig] = useAgentStore((s) => [
14+
agentSelectors.isAgentConfigLoading(s),
15+
agentChatConfigSelectors.currentChatConfig(s),
16+
s.updateAgentChatConfig,
17+
]);
1318
const [updating, setUpdating] = useState(false);
1419
const { t } = useTranslation('setting');
20+
const isMobile = useIsMobile();
1521

1622
const [historyCount, enableHistoryCount] = useAgentStore((s) => {
1723
return [
@@ -33,9 +39,20 @@ const History = memo(() => {
3339
<Action
3440
icon={enableHistoryCount ? Timer : TimerOff}
3541
loading={updating}
42+
onClick={
43+
isMobile
44+
? undefined
45+
: async (e) => {
46+
e?.preventDefault?.();
47+
e?.stopPropagation?.();
48+
const next = !Boolean(chatConfig.enableHistoryCount);
49+
await updateAgentChatConfig({ enableHistoryCount: next });
50+
}
51+
}
3652
popover={{
3753
content: <Controls setUpdating={setUpdating} updating={updating} />,
3854
minWidth: 240,
55+
trigger: isMobile ? ['click'] : ['hover'],
3956
}}
4057
showTooltip={false}
4158
title={title}

‎src/features/ChatInput/ActionBar/Search/index.tsx‎

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,24 @@ import { useTranslation } from 'react-i18next';
66

77
import { isDeprecatedEdition } from '@/const/version';
88
import { useAgentEnableSearch } from '@/hooks/useAgentEnableSearch';
9+
import { useIsMobile } from '@/hooks/useIsMobile';
910
import { useAgentStore } from '@/store/agent';
1011
import { agentSelectors } from '@/store/agent/selectors';
12+
import { agentChatConfigSelectors } from '@/store/agent/slices/chat';
1113

1214
import Action from '../components/Action';
1315
import Controls from './Controls';
1416

1517
const Search = memo(() => {
1618
const { t } = useTranslation('chat');
17-
const [isLoading] = useAgentStore((s) => [agentSelectors.isAgentConfigLoading(s)]);
19+
const [isLoading, mode, updateAgentChatConfig] = useAgentStore((s) => [
20+
agentSelectors.isAgentConfigLoading(s),
21+
agentChatConfigSelectors.agentSearchMode(s),
22+
s.updateAgentChatConfig,
23+
]);
1824
const isAgentEnableSearch = useAgentEnableSearch();
1925
const theme = useTheme();
26+
const isMobile = useIsMobile();
2027

2128
if (isDeprecatedEdition) return null;
2229
if (isLoading) return <Action disabled icon={GlobeOffIcon} />;
@@ -25,6 +32,16 @@ const Search = memo(() => {
2532
<Action
2633
color={isAgentEnableSearch ? theme.colorInfo : undefined}
2734
icon={isAgentEnableSearch ? Globe : GlobeOffIcon}
35+
onClick={
36+
isMobile
37+
? undefined
38+
: async (e) => {
39+
e?.preventDefault?.();
40+
e?.stopPropagation?.();
41+
const next = mode === 'off' ? 'auto' : 'off';
42+
await updateAgentChatConfig({ searchMode: next });
43+
}
44+
}
2845
popover={{
2946
content: <Controls />,
3047
maxWidth: 320,
@@ -35,6 +52,7 @@ const Search = memo(() => {
3552
padding: 4,
3653
},
3754
},
55+
trigger: isMobile ? ['click'] : ['hover'],
3856
}}
3957
showTooltip={false}
4058
title={t('search.title')}

‎src/features/ChatInput/ActionBar/components/Action.tsx‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ const Action = memo<ActionProps>(
3131
onOpenChange,
3232
trigger,
3333
disabled,
34+
onClick,
3435
...rest
3536
}) => {
3637
const [show, setShow] = useMergeState(false, {
@@ -43,7 +44,10 @@ const Action = memo<ActionProps>(
4344
disabled={disabled}
4445
icon={icon}
4546
loading={loading}
46-
onClick={() => setShow(true)}
47+
onClick={(e) => {
48+
if (onClick) return onClick(e);
49+
setShow(true);
50+
}}
4751
title={
4852
isUndefined(showTooltip) ? (mobile ? undefined : title) : showTooltip ? title : undefined
4953
}

0 commit comments

Comments
 (0)