feat: i18n (i18next + react-i18next) zh/en + hydration fixes
- i18next + react-i18next: zh/en locales, language detection (localStorage > navigator > zh), settings language switcher - Components i18n'd: Navbar, Feed, NewPost, Modals, SettingsPage, SettingsProfile, AccountSecurity, RecoveryCodes - Fix hydration mismatch (spinner restart): forum name via cookie (Server Component reads cookie, SSR outputs name, no inline script DOM injection); remove createPortal in RecoveryCodes modal; i18n sync init (initImmediate) so SSR/CSR text matches - LoadingScreen: React-state name rendering (no script-injected nodes) - Fix double spinner in Feed; DM icon emoji → SVG in ConversationList
This commit is contained in:
1 parent
82bc2eb885
commit
0562dd735a
20 files changed
+599
-168
No files matched your search
+15
-15
@@ -2,10 +2,11 @@
|
||||
|
||||
// 恢复码管理:查看 + 重新生成
|
||||
import { useState, useEffect } from 'react';
|
||||
import { createPortal } from 'react-dom';
|
||||
import { useTranslation } from 'react-i18next';
|
||||
import { API } from '@/lib/api';
|
||||
|
||||
export default function RecoveryCodes() {
|
||||
const { t } = useTranslation();
|
||||
const [count, setCount] = useState(0);
|
||||
const [modal, setModal] = useState(null); // null | {codes, isNew}
|
||||
|
||||
@@ -15,13 +16,13 @@ export default function RecoveryCodes() {
|
||||
useEffect(loadCount, []);
|
||||
|
||||
const regenerate = async () => {
|
||||
if (!confirm('重新生成将使所有旧恢复码失效,确定继续吗?')) return;
|
||||
if (!confirm(t('recovery.confirmRegenerate'))) return;
|
||||
try {
|
||||
const d = await API.generateRecoveryCodes();
|
||||
setModal({ codes: d.codes || [], isNew: true });
|
||||
loadCount();
|
||||
} catch (err) {
|
||||
alert('生成失败:' + err.message);
|
||||
alert(t('recovery.genFailed', { msg: err.message }));
|
||||
}
|
||||
};
|
||||
|
||||
@@ -32,35 +33,34 @@ export default function RecoveryCodes() {
|
||||
setModal({ codes: d.codes || [], isNew: false });
|
||||
loadCount();
|
||||
} catch (err) {
|
||||
alert('获取失败:' + err.message);
|
||||
alert(t('recovery.fetchFailed', { msg: err.message }));
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
<div style={{ marginTop: 16, background: 'var(--surface)', border: '1px solid var(--border)', borderRadius: 8, padding: 20 }}>
|
||||
<h3 style={{ fontSize: 16, marginBottom: 8 }}>🔑 恢复码</h3>
|
||||
<p style={{ fontSize: 13, color: 'var(--text-secondary)', marginBottom: 12 }}>用于忘记密码时重置账户。每个恢复码只能使用一次。</p>
|
||||
<h3 style={{ fontSize: 16, marginBottom: 8 }}>🔑 {t('recovery.title')}</h3>
|
||||
<p style={{ fontSize: 13, color: 'var(--text-secondary)', marginBottom: 12 }}>{t('recovery.desc')}</p>
|
||||
<div style={{ display: 'flex', gap: 8, flexWrap: 'wrap' }}>
|
||||
<button className="btn-secondary" style={{ padding: '8px 16px', border: '1px solid var(--border)', borderRadius: 4, background: 'var(--surface)', cursor: 'pointer', color: 'var(--text)' }} onClick={view}>📋 查看恢复码</button>
|
||||
<button className="btn-secondary" style={{ padding: '8px 16px', border: '1px solid var(--border)', borderRadius: 4, background: 'var(--surface)', cursor: 'pointer', color: 'var(--text)' }} onClick={regenerate}>🔄 重新生成</button>
|
||||
<button className="btn-secondary" style={{ padding: '8px 16px', border: '1px solid var(--border)', borderRadius: 4, background: 'var(--surface)', cursor: 'pointer', color: 'var(--text)' }} onClick={view}>📋 {t('recovery.view')}</button>
|
||||
<button className="btn-secondary" style={{ padding: '8px 16px', border: '1px solid var(--border)', borderRadius: 4, background: 'var(--surface)', cursor: 'pointer', color: 'var(--text)' }} onClick={regenerate}>🔄 {t('recovery.regenerate')}</button>
|
||||
</div>
|
||||
<div id="recoveryCodesStatus" style={{ fontSize: 13, color: 'var(--text-light)', marginTop: 8 }}>剩余 {count} 个可用恢复码</div>
|
||||
<div id="recoveryCodesStatus" style={{ fontSize: 13, color: 'var(--text-light)', marginTop: 8 }}>{t('recovery.remaining', { count })}</div>
|
||||
|
||||
{modal && typeof document !== 'undefined' && createPortal(
|
||||
{modal && (
|
||||
<div className="modal active" style={{ display: 'flex' }} onClick={(e) => { if (e.target === e.currentTarget) setModal(null); }}>
|
||||
<div className="modal-content" style={{ maxWidth: 480, position: 'relative' }}>
|
||||
<span className="close" style={{ position: 'absolute', top: 12, right: 16, fontSize: 24, cursor: 'pointer', color: 'var(--text-light)' }} onClick={() => setModal(null)}>×</span>
|
||||
<h2 style={{ marginBottom: 12 }}>{modal.isNew ? '🆕 新恢复码' : '🔑 恢复码'}</h2>
|
||||
<p style={{ fontSize: 13, color: '#ef4444', marginBottom: 12 }}>⚠️ 请妥善保存这些恢复码,关闭窗口后无法再次查看。</p>
|
||||
<h2 style={{ marginBottom: 12 }}>{modal.isNew ? t('recovery.newCodes') : t('recovery.title')}</h2>
|
||||
<p style={{ fontSize: 13, color: '#ef4444', marginBottom: 12 }}>{t('recovery.saveWarning')}</p>
|
||||
<div style={{ display: 'grid', gridTemplateColumns: '1fr 1fr', gap: 8 }}>
|
||||
{modal.codes.map((c, i) => (
|
||||
<code key={i} style={{ background: 'var(--bg)', padding: '8px 12px', borderRadius: 6, fontSize: 13, textAlign: 'center', userSelect: 'all' }}>{c}</code>
|
||||
))}
|
||||
</div>
|
||||
<button className="btn-primary" style={{ width: '100%', padding: 10, marginTop: 16 }} onClick={() => setModal(null)}>我已保存</button>
|
||||
<button className="btn-primary" style={{ width: '100%', padding: 10, marginTop: 16 }} onClick={() => setModal(null)}>{t('recovery.saved')}</button>
|
||||
</div>
|
||||
</div>,
|
||||
document.body
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
|
||||
Reference in new issue
Block a user