Skip to content

🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

💄 style: interactive improvement of model search (#14192)

sxjeru29235dc1ed1d
diff --git a/src/features/ChatInput/ActionBar/Model/index.tsx b/src/features/ChatInput/ActionBar/Model/index.tsx
index 583987ceb4e..d9688861c65 100644
--- a/src/features/ChatInput/ActionBar/Model/index.tsx
+++ b/src/features/ChatInput/ActionBar/Model/index.tsx
@@ -116,7 +116,11 @@ const ModelSwitch = memo(() => {
</Flexbox>
}
>
- <ModelDetailPanel model={model} provider={provider} />
+ <ModelDetailPanel
+ defaultExpandedKeys={['pricing', 'config']}
+ model={model}
+ provider={provider}
+ />
</Suspense>
),
maxWidth: 400,
diff --git a/src/features/ModelSwitchPanel/components/ModelDetailPanel.tsx b/src/features/ModelSwitchPanel/components/ModelDetailPanel.tsx
index d1a1b7cbaba..2855c096910 100644
--- a/src/features/ModelSwitchPanel/components/ModelDetailPanel.tsx
+++ b/src/features/ModelSwitchPanel/components/ModelDetailPanel.tsx
@@ -220,6 +220,7 @@ const ABILITY_CONFIG: AbilityItem[] = [
export type PricingMode = 'image' | 'video';
interface ModelDetailPanelProps {
+ defaultExpandedKeys?: string[];
enabledList?: EnabledProviderWithModels[];
model?: string;
pricingMode?: PricingMode;
@@ -227,7 +228,13 @@ interface ModelDetailPanelProps {
}
const ModelDetailPanel: FC<ModelDetailPanelProps> = memo(
- ({ model: modelId, provider, enabledList: enabledListProp, pricingMode }) => {
+ ({
+ defaultExpandedKeys = ['pricing'],
+ model: modelId,
+ provider,
+ enabledList: enabledListProp,
+ pricingMode,
+ }) => {
const { t } = useTranslation('components');
const enabledListFromHook = useEnabledChatModels();
@@ -242,7 +249,7 @@ const ModelDetailPanel: FC<ModelDetailPanelProps> = memo(
aiModelSelectors.isModelHasExtendParams(modelId ?? '', provider ?? ''),
);
- const [expandedKeys, setExpandedKeys] = useState<string[]>(['pricing']);
+ const [expandedKeys, setExpandedKeys] = useState<string[]>(defaultExpandedKeys);
const hasPricing = !!model?.pricing;
const formatPrice = hasPricing ? getPrice(model!.pricing!) : null;
diff --git a/src/routes/(main)/settings/provider/ProviderMenu/index.tsx b/src/routes/(main)/settings/provider/ProviderMenu/index.tsx
index 09a6fcbd6a8..34c776c32e7 100644
--- a/src/routes/(main)/settings/provider/ProviderMenu/index.tsx
+++ b/src/routes/(main)/settings/provider/ProviderMenu/index.tsx
@@ -58,9 +58,9 @@ const Layout = memo(({ children, mobile }: ProviderMenuProps) => {
>
<SearchBar
allowClear
- defaultValue={providerSearchKeyword}
placeholder={t('menu.searchProviders')}
style={{ width: '100%' }}
+ value={providerSearchKeyword}
variant={'borderless'}
prefix={
<Icon
@@ -77,9 +77,8 @@ const Layout = memo(({ children, mobile }: ProviderMenuProps) => {
paddingLeft: 6,
},
}}
- onSearch={(v) => useAiInfraStore.setState({ providerSearchKeyword: v })}
onInputChange={(v) => {
- if (!v) useAiInfraStore.setState({ providerSearchKeyword: '' });
+ useAiInfraStore.setState({ providerSearchKeyword: v });
}}
/>
<AddNew />
diff --git a/src/routes/(main)/settings/provider/features/ModelList/ModelTitle/Search.test.tsx b/src/routes/(main)/settings/provider/features/ModelList/ModelTitle/Search.test.tsx
new file mode 100644
index 00000000000..eea0e35d34c
--- /dev/null
+++ b/src/routes/(main)/settings/provider/features/ModelList/ModelTitle/Search.test.tsx
@@ -0,0 +1,53 @@
+import { act, fireEvent, render, screen } from '@testing-library/react';
+import { beforeEach, describe, expect, it, vi } from 'vitest';
+
+import Search from './Search';
+
+vi.mock('react-i18next', () => ({
+ useTranslation: () => ({
+ t: (key: string) => key,
+ }),
+}));
+
+vi.mock('@lobehub/ui', () => ({
+ SearchBar: ({ onInputChange, value, ...props }: any) => (
+ <input
+ {...props}
+ data-testid="search-input"
+ value={value}
+ onChange={(event) => onInputChange(event.target.value)}
+ />
+ ),
+}));
+
+describe('ModelList Search', () => {
+ beforeEach(() => {
+ vi.useFakeTimers();
+ vi.clearAllMocks();
+ });
+
+ it('should clear immediately without waiting for debounce', () => {
+ const onChange = vi.fn();
+
+ render(<Search value="abc" onChange={onChange} />);
+
+ fireEvent.change(screen.getByTestId('search-input'), { target: { value: '' } });
+
+ expect(onChange).toHaveBeenCalledWith('');
+ });
+
+ it('should not replay stale debounced value after external reset', async () => {
+ const onChange = vi.fn();
+ const { rerender } = render(<Search value="abc" onChange={onChange} />);
+
+ fireEvent.change(screen.getByTestId('search-input'), { target: { value: 'abcd' } });
+
+ rerender(<Search value="" onChange={onChange} />);
+
+ await act(async () => {
+ await vi.advanceTimersByTimeAsync(200);
+ });
+
+ expect(onChange).not.toHaveBeenCalledWith('abcd');
+ });
+});
diff --git a/src/routes/(main)/settings/provider/features/ModelList/ModelTitle/Search.tsx b/src/routes/(main)/settings/provider/features/ModelList/ModelTitle/Search.tsx
index 625441257cc..aec0c8b826c 100644
--- a/src/routes/(main)/settings/provider/features/ModelList/ModelTitle/Search.tsx
+++ b/src/routes/(main)/settings/provider/features/ModelList/ModelTitle/Search.tsx
@@ -1,6 +1,7 @@
import { type InputProps } from '@lobehub/ui';
import { SearchBar } from '@lobehub/ui';
-import { memo } from 'react';
+import { useDebounce } from 'ahooks';
+import { memo, useEffect, useRef, useState } from 'react';
import { useTranslation } from 'react-i18next';
interface SearchProps {
@@ -11,15 +12,41 @@ interface SearchProps {
const Search = memo<SearchProps>(({ value, onChange, variant }) => {
const { t } = useTranslation('modelProvider');
+ const [localValue, setLocalValue] = useState(value);
+ const debouncedValue = useDebounce(localValue, { wait: 200 });
+ const skipDebouncedEmitRef = useRef(false);
+
+ useEffect(() => {
+ skipDebouncedEmitRef.current = true;
+ setLocalValue(value);
+ }, [value]);
+
+ useEffect(() => {
+ if (skipDebouncedEmitRef.current) {
+ skipDebouncedEmitRef.current = false;
+
+ return;
+ }
+
+ if (!localValue) {
+ if (value) onChange('');
+
+ return;
+ }
+
+ if (debouncedValue === value) return;
+
+ onChange(debouncedValue);
+ }, [debouncedValue, localValue, onChange, value]);
return (
<SearchBar
allowClear
- defaultValue={value}
placeholder={t('providerModels.list.search')}
size={'small'}
+ value={localValue}
variant={variant}
- onSearch={(keyword) => onChange(keyword)}
+ onInputChange={setLocalValue}
/>
);
});
diff --git a/src/routes/(main)/settings/provider/features/ModelList/ModelTitle/index.tsx b/src/routes/(main)/settings/provider/features/ModelList/ModelTitle/index.tsx
index 194d8265835..beb002df71d 100644
--- a/src/routes/(main)/settings/provider/features/ModelList/ModelTitle/index.tsx
+++ b/src/routes/(main)/settings/provider/features/ModelList/ModelTitle/index.tsx
@@ -2,7 +2,7 @@ import { ActionIcon, Button, DropdownMenu, Flexbox, Skeleton, Text } from '@lobe
import { App, Space } from 'antd';
import { cssVar } from 'antd-style';
import { CircleX, EllipsisVertical, LucideRefreshCcwDot, PlusIcon } from 'lucide-react';
-import { memo, useState } from 'react';
+import { memo, useEffect, useState } from 'react';
import { useTranslation } from 'react-i18next';
import { useIsMobile } from '@/hooks/useIsMobile';
@@ -50,6 +50,10 @@ const ModelTitle = memo<ModelFetcherProps>(
const mobile = useIsMobile();
+ useEffect(() => {
+ useAiInfraStore.setState({ modelSearchKeyword: '' });
+ }, [provider]);
+
return (
<Flexbox
gap={12}