Skip to content

Commit 5eafec4

Browse files
committed
fix(settings): 重置设置页切换滚动位置
1 parent 4cc6f91 commit 5eafec4

2 files changed

Lines changed: 80 additions & 1 deletion

File tree

src/pages/SettingsPage.tsx

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { useLayoutEffect, useRef } from 'react';
12
import { theme } from 'antd';
23
import { useUIStore } from '@/stores';
34
import {
@@ -38,6 +39,15 @@ export function SettingsPage() {
3839
const { token } = theme.useToken();
3940
const settingsSection = useUIStore((s) => s.settingsSection);
4041
const ContentComponent = SECTION_COMPONENTS[settingsSection];
42+
const contentScrollRef = useRef<HTMLDivElement>(null);
43+
44+
useLayoutEffect(() => {
45+
const contentScroller = contentScrollRef.current;
46+
if (!contentScroller) return;
47+
48+
contentScroller.scrollTop = 0;
49+
contentScroller.scrollLeft = 0;
50+
}, [settingsSection]);
4151

4252
return (
4353
<div className="flex h-full">
@@ -47,7 +57,11 @@ export function SettingsPage() {
4757
>
4858
<SettingsSidebar />
4959
</div>
50-
<div className="min-w-0 flex-1 overflow-y-auto" style={{ backgroundColor: token.colorBgElevated }}>
60+
<div
61+
ref={contentScrollRef}
62+
className="min-w-0 flex-1 overflow-y-auto"
63+
style={{ backgroundColor: token.colorBgElevated }}
64+
>
5165
<ContentComponent />
5266
</div>
5367
</div>
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import { act, render, screen } from '@testing-library/react';
2+
import { beforeEach, describe, expect, it, vi } from 'vitest';
3+
import { useUIStore } from '@/stores';
4+
import { SettingsPage } from '../SettingsPage';
5+
6+
vi.mock('antd', () => ({
7+
theme: {
8+
useToken: () => ({
9+
token: {
10+
colorBgContainer: '#ffffff',
11+
colorBgElevated: '#f8f8f8',
12+
},
13+
}),
14+
},
15+
}));
16+
17+
vi.mock('@/components/settings', () => ({
18+
SettingsSidebar: () => <nav>settings-sidebar</nav>,
19+
ProviderSettings: () => <div>providers-content</div>,
20+
GeneralSettings: () => <div>general-content</div>,
21+
DisplaySettings: () => <div>display-content</div>,
22+
ProxySettings: () => <div>proxy-content</div>,
23+
ShortcutSettings: () => <div>shortcuts-content</div>,
24+
DataManager: () => <div>data-content</div>,
25+
AboutPage: () => <div>about-content</div>,
26+
SearchProviderSettings: () => <div>search-providers-content</div>,
27+
McpServerSettings: () => <div>mcp-servers-content</div>,
28+
BackupCenter: () => <div>backup-content</div>,
29+
StorageSpaceManager: () => <div>storage-content</div>,
30+
}));
31+
32+
vi.mock('@/components/settings/DefaultModelSettings', () => ({
33+
DefaultModelSettings: () => <div>default-model-content</div>,
34+
}));
35+
36+
vi.mock('@/components/settings/ConversationSettings', () => ({
37+
ConversationSettings: () => <div>conversation-settings-content</div>,
38+
}));
39+
40+
describe('SettingsPage', () => {
41+
beforeEach(() => {
42+
act(() => {
43+
useUIStore.setState({
44+
activePage: 'settings',
45+
previousPage: 'chat',
46+
settingsSection: 'general',
47+
selectedProviderId: null,
48+
});
49+
});
50+
});
51+
52+
it('resets the content scroll position when switching settings sections', () => {
53+
render(<SettingsPage />);
54+
55+
const contentScroller = screen.getByText('general-content').parentElement as HTMLElement;
56+
contentScroller.scrollTop = 480;
57+
58+
act(() => {
59+
useUIStore.getState().setSettingsSection('display');
60+
});
61+
62+
expect(screen.getByText('display-content')).toBeInTheDocument();
63+
expect(contentScroller.scrollTop).toBe(0);
64+
});
65+
});

0 commit comments

Comments
 (0)