add proxy switch demo

This commit is contained in:
go0p
2025-03-04 16:07:14 +08:00
parent 8c30607514
commit 1e25561fae
6 changed files with 299 additions and 32 deletions
+48 -6
View File
@@ -103,17 +103,21 @@ export const OptionsPage: React.FC = () => {
}, []);
useEffect(() => {
// 加载日志
const loadLogs = async () => {
const result = await chrome.storage.local.get('proxyLogs');
setProxyLogs(result.proxyLogs || []);
};
loadLogs();
// 监听存储变化
const handleStorageChange = (changes: StorageChanges) => {
if (changes.proxyLogs) {
setProxyLogs(changes.proxyLogs.newValue);
setProxyLogs(changes.proxyLogs.newValue || []);
}
};
chrome.storage.onChanged.addListener(handleStorageChange);
return () => chrome.storage.onChanged.removeListener(handleStorageChange);
}, []);
@@ -235,6 +239,22 @@ export const OptionsPage: React.FC = () => {
}
};
const handleClearLogs = () => {
chrome.runtime.sendMessage({
action: ProxyActionType.CLEAR_PROXY_LOGS
}, (response) => {
if (chrome.runtime.lastError) {
message.error(chrome.runtime.lastError.message || '清除日志失败');
return;
}
if (response?.success) {
message.success('日志已清除');
} else {
message.error(response?.error || '清除日志失败');
}
});
};
return (
<Layout className="options-page">
<Content style={contentStyle}>
@@ -370,11 +390,33 @@ export const OptionsPage: React.FC = () => {
key: 'logs',
label: '代理日志',
children: (
<Table
dataSource={proxyLogs}
columns={columns}
pagination={{ pageSize: 50 }}
/>
<>
<div style={{
marginBottom: 16,
display: 'flex',
justifyContent: 'flex-end'
}}>
<Button
danger
onClick={handleClearLogs}
icon={<DeleteOutlined />}
>
清除日志
</Button>
</div>
<Table
dataSource={proxyLogs}
columns={columns}
pagination={{
pageSize: 10,
showSizeChanger: true,
showQuickJumper: true,
showTotal: (total) => `共 ${total} 条`,
pageSizeOptions: ['10', '20', '50', '100']
}}
rowKey="id"
/>
</>
)
}
]}