Settings.tsx

4.1 KB v1.0
// 设置页:左侧菜单 + 右侧配置面板
import { useState } from 'react';
import { useI18n, type I18nKey } from '../i18n';
import { Icon, type IconName } from '../components/Icon';
import { useToast } from '../components/Toast';
import type { AppSettings, SettingsSection, User } from '../types';
import { cx } from '../utils';
import { AppearancePanel } from './settings/AppearancePanel';
import { AccountsPanel } from './settings/AccountsPanel';
import { BackupPanel } from './settings/BackupPanel';
import { EditorPanel } from './settings/EditorPanel';
import { AccountPanel } from './settings/AccountPanel';
import { SyncPanel } from './settings/SyncPanel';
import { NotificationsPanel } from './settings/NotificationsPanel';
import { AboutPanel } from './settings/AboutPanel';

// 菜单顺序固定:外观、邮箱账号管理、备份设置、编辑器、账户、同步、关于
const NAV: Array<{ id: SettingsSection; labelKey: I18nKey; icon: IconName }> = [
  { id: 'appearance', labelKey: 'navAppearance', icon: 'palette' },
  { id: 'accounts', labelKey: 'navAccounts', icon: 'server' },
  { id: 'backup', labelKey: 'navBackup', icon: 'cloud' },
  { id: 'editor', labelKey: 'navEditor', icon: 'compose' },
  { id: 'account', labelKey: 'navAccount', icon: 'user' },
  { id: 'sync', labelKey: 'navSync', icon: 'refresh' },
  { id: 'notifications', labelKey: 'navNotifications', icon: 'bell' },
  { id: 'about', labelKey: 'navAbout', icon: 'shield' },
];

export function SettingsPage({
  user,
  settings,
  updateSettings,
  flushSettings,
  onBack,
  onLogout,
  initialSection,
}: {
  user: User;
  settings: AppSettings;
  updateSettings: (patch: unknown) => void;
  flushSettings: () => Promise<void>;
  onBack: () => void;
  onLogout: () => void;
  initialSection?: SettingsSection;
}) {
  const { t } = useI18n();
  const { show, view } = useToast();
  const [section, setSection] = useState<SettingsSection>(initialSection || 'appearance');

  const panelProps = { user, settings, updateSettings, flushSettings, showToast: show };

  return (
    <div className="settings-page">
      <aside className="settings-sidebar">
        <div className="settings-sidebar-head">
          <button type="button" className="btn btn-icon btn-ghost" onClick={onBack} title={t('back')}>
            <Icon name="chevronLeft" size={18} />
          </button>
          <div className="brand-logo">
            <Icon name="mail" size={16} />
          </div>
          <div className="settings-sidebar-title">{t('settingsTitle')}</div>
        </div>

        <nav className="settings-nav">
          {NAV.map((item) => (
            <button
              type="button"
              key={item.id}
              className={cx('settings-nav-item', section === item.id && 'active')}
              onClick={() => setSection(item.id)}
            >
              <Icon name={item.icon} size={16} />
              <span>{t(item.labelKey)}</span>
            </button>
          ))}
        </nav>

        <div className="settings-sidebar-foot">
          <div className="settings-user">
            <span className="settings-user-avatar">{user.username.slice(0, 1).toUpperCase()}</span>
            <span className="settings-user-name">{user.username}</span>
          </div>
          <button type="button" className="settings-nav-item" onClick={() => void onLogout()}>
            <Icon name="logout" size={16} />
            <span>{t('logout')}</span>
          </button>
        </div>
      </aside>

      <main className="settings-main">
        <div className="settings-content">
          {section === 'appearance' && <AppearancePanel {...panelProps} />}
          {section === 'accounts' && <AccountsPanel {...panelProps} />}
          {section === 'backup' && <BackupPanel {...panelProps} />}
          {section === 'editor' && <EditorPanel {...panelProps} />}
          {section === 'account' && <AccountPanel {...panelProps} />}
          {section === 'sync' && <SyncPanel {...panelProps} />}
          {section === 'notifications' && <NotificationsPanel {...panelProps} />}
          {section === 'about' && <AboutPanel {...panelProps} />}
        </div>
      </main>

      {view}
    </div>
  );
}