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
@@ -2,11 +2,13 @@
|
||||
|
||||
// 账户安全:修改密码 + 修改邮箱
|
||||
import { useState } from 'react';
|
||||
import { useTranslation } from 'react-i18next';
|
||||
import { API } from '@/lib/api';
|
||||
import { useApp } from './AppProvider';
|
||||
|
||||
export default function AccountSecurity() {
|
||||
const { currentUser } = useApp();
|
||||
const { t } = useTranslation();
|
||||
const [oldPassword, setOldPassword] = useState('');
|
||||
const [newPassword, setNewPassword] = useState('');
|
||||
const [pwStatus, setPwStatus] = useState(null);
|
||||
@@ -15,11 +17,11 @@ export default function AccountSecurity() {
|
||||
const [emailStatus, setEmailStatus] = useState(null);
|
||||
|
||||
const changePassword = async () => {
|
||||
if (!oldPassword || !newPassword) { alert('请填写完整信息'); return; }
|
||||
if (newPassword.length < 6) { alert('新密码至少6位'); return; }
|
||||
if (!oldPassword || !newPassword) { alert(t('security.fillAll')); return; }
|
||||
if (newPassword.length < 6) { alert(t('security.pwMin')); return; }
|
||||
try {
|
||||
await API.updatePassword(currentUser.id, oldPassword, newPassword);
|
||||
setPwStatus({ ok: true, msg: '✅ 密码修改成功' });
|
||||
setPwStatus({ ok: true, msg: t('security.pwChanged') });
|
||||
setOldPassword('');
|
||||
setNewPassword('');
|
||||
} catch (err) {
|
||||
@@ -28,10 +30,10 @@ export default function AccountSecurity() {
|
||||
};
|
||||
|
||||
const changeEmail = async () => {
|
||||
if (!emailPassword || !newEmail) { alert('请填写完整信息'); return; }
|
||||
if (!emailPassword || !newEmail) { alert(t('security.fillAll')); return; }
|
||||
try {
|
||||
await API.updateEmail(currentUser.id, emailPassword, newEmail);
|
||||
setEmailStatus({ ok: true, msg: '✅ 邮箱修改成功' });
|
||||
setEmailStatus({ ok: true, msg: t('security.emailChanged') });
|
||||
setEmailPassword('');
|
||||
setNewEmail('');
|
||||
} catch (err) {
|
||||
@@ -41,30 +43,30 @@ export default function AccountSecurity() {
|
||||
|
||||
return (
|
||||
<div style={{ marginTop: 16, background: 'var(--surface)', border: '1px solid var(--border)', borderRadius: 8, padding: 20 }}>
|
||||
<h3 style={{ fontSize: 16, marginBottom: 12 }}>🔒 账户安全</h3>
|
||||
<h3 style={{ fontSize: 16, marginBottom: 12 }}>🔒 {t('security.title')}</h3>
|
||||
|
||||
<div style={{ marginBottom: 12 }}>
|
||||
<label style={{ fontWeight: 600, fontSize: 13, display: 'block', marginBottom: 4 }}>当前密码</label>
|
||||
<label style={{ fontWeight: 600, fontSize: 13, display: 'block', marginBottom: 4 }}>{t('security.currentPw')}</label>
|
||||
<input type="password" value={oldPassword} style={{ width: '100%', padding: '8px 12px', border: '1px solid var(--border)', borderRadius: 4, fontSize: 14, background: 'var(--bg)', color: 'var(--text)' }} onChange={(e) => setOldPassword(e.target.value)} />
|
||||
</div>
|
||||
<div style={{ marginBottom: 12 }}>
|
||||
<label style={{ fontWeight: 600, fontSize: 13, display: 'block', marginBottom: 4 }}>新密码</label>
|
||||
<label style={{ fontWeight: 600, fontSize: 13, display: 'block', marginBottom: 4 }}>{t('security.newPw')}</label>
|
||||
<input type="password" value={newPassword} style={{ width: '100%', padding: '8px 12px', border: '1px solid var(--border)', borderRadius: 4, fontSize: 14, background: 'var(--bg)', color: 'var(--text)' }} onChange={(e) => setNewPassword(e.target.value)} />
|
||||
</div>
|
||||
<button className="btn-secondary" style={{ padding: '8px 16px', border: '1px solid var(--border)', borderRadius: 4, background: 'var(--surface)', cursor: 'pointer', color: 'var(--text)' }} onClick={changePassword}>修改密码</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={changePassword}>{t('security.changePw')}</button>
|
||||
{pwStatus && <div style={{ fontSize: 13, marginTop: 6, color: pwStatus.ok ? '#22c55e' : '#ef4444' }}>{pwStatus.msg}</div>}
|
||||
|
||||
<div style={{ borderTop: '1px solid var(--border)', margin: '16px 0' }}></div>
|
||||
|
||||
<div style={{ marginBottom: 12 }}>
|
||||
<label style={{ fontWeight: 600, fontSize: 13, display: 'block', marginBottom: 4 }}>当前密码(验证身份)</label>
|
||||
<label style={{ fontWeight: 600, fontSize: 13, display: 'block', marginBottom: 4 }}>{t('security.currentPwVerify')}</label>
|
||||
<input type="password" value={emailPassword} style={{ width: '100%', padding: '8px 12px', border: '1px solid var(--border)', borderRadius: 4, fontSize: 14, background: 'var(--bg)', color: 'var(--text)' }} onChange={(e) => setEmailPassword(e.target.value)} />
|
||||
</div>
|
||||
<div style={{ marginBottom: 12 }}>
|
||||
<label style={{ fontWeight: 600, fontSize: 13, display: 'block', marginBottom: 4 }}>新邮箱</label>
|
||||
<label style={{ fontWeight: 600, fontSize: 13, display: 'block', marginBottom: 4 }}>{t('security.newEmail')}</label>
|
||||
<input type="email" value={newEmail} style={{ width: '100%', padding: '8px 12px', border: '1px solid var(--border)', borderRadius: 4, fontSize: 14, background: 'var(--bg)', color: 'var(--text)' }} onChange={(e) => setNewEmail(e.target.value)} />
|
||||
</div>
|
||||
<button className="btn-secondary" style={{ padding: '8px 16px', border: '1px solid var(--border)', borderRadius: 4, background: 'var(--surface)', cursor: 'pointer', color: 'var(--text)' }} onClick={changeEmail}>修改邮箱</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={changeEmail}>{t('security.changeEmail')}</button>
|
||||
{emailStatus && <div style={{ fontSize: 13, marginTop: 6, color: emailStatus.ok ? '#22c55e' : '#ef4444' }}>{emailStatus.msg}</div>}
|
||||
</div>
|
||||
);
|
||||
|
||||
@@ -10,11 +10,10 @@ export function useApp() {
|
||||
return useContext(AppContext);
|
||||
}
|
||||
|
||||
export default function AppProvider({ children }) {
|
||||
export default function AppProvider({ children, cachedName = '' }) {
|
||||
const [currentUser, setCurrentUser] = useState(null);
|
||||
// 固定初始值(SSR/客户端一致,避免 hydration mismatch 导致加载圈重启);
|
||||
// 缓存论坛名由 useEffect 读取,首帧显示靠内联脚本(body 首个子元素)
|
||||
const [forumName, setForumName] = useState('Forumlify');
|
||||
// 论坛名初始值来自 cookie(SSR 首帧即输出,SSR/客户端一致 → 无 hydration mismatch)
|
||||
const [forumName, setForumName] = useState(cachedName || 'Forumlify');
|
||||
const [theme, setThemeState] = useState('light');
|
||||
const [view, setView] = useState('feed'); // feed | post | new | admin | settings | messages | user | custom
|
||||
const [currentPostId, setCurrentPostId] = useState(null);
|
||||
@@ -23,12 +22,19 @@ export default function AppProvider({ children }) {
|
||||
const [sort, setSort] = useState('latest');
|
||||
const [refreshKey, setRefreshKey] = useState(0);
|
||||
const [ready, setReady] = useState(false); // 初始加载完成标记(控制加载页/淡入)
|
||||
// 固定 false(避免 hydration mismatch);有缓存时由 useEffect 立即置 true
|
||||
const [forumNameLoaded, setForumNameLoaded] = useState(false);
|
||||
// 有 cookie 缓存名即视为已加载(SSR/客户端一致)
|
||||
const [forumNameLoaded, setForumNameLoaded] = useState(!!cachedName);
|
||||
const loadedRef = useRef(false);
|
||||
|
||||
const refresh = useCallback(() => setRefreshKey((k) => k + 1), []);
|
||||
|
||||
// 论坛名写 cookie(SSR 可读 → 首帧输出,无需内联脚本注入 DOM)
|
||||
const syncForumNameCookie = useCallback((name) => {
|
||||
try {
|
||||
document.cookie = 'forumlify-name=' + encodeURIComponent(name) + '; path=/; max-age=31536000';
|
||||
} catch (e) {}
|
||||
}, []);
|
||||
|
||||
// 主题
|
||||
useEffect(() => {
|
||||
const t = getTheme();
|
||||
@@ -67,12 +73,13 @@ export default function AppProvider({ children }) {
|
||||
|
||||
// 初始化:读取缓存论坛名(立即显示),恢复登录、加载服务器论坛名
|
||||
useEffect(() => {
|
||||
// 缓存论坛名(hydrate 后立即生效,首帧显示由内联脚本完成)
|
||||
// 缓存论坛名(hydrate 后立即生效;SSR 首帧已由 cookie 输出)
|
||||
try {
|
||||
const cached = localStorage.getItem('forumlify-forum-name');
|
||||
if (cached) {
|
||||
setForumName(cached);
|
||||
setForumNameLoaded(true);
|
||||
syncForumNameCookie(cached);
|
||||
}
|
||||
} catch (e) {}
|
||||
|
||||
@@ -95,6 +102,7 @@ export default function AppProvider({ children }) {
|
||||
if (s.forum_name) {
|
||||
setForumName(s.forum_name);
|
||||
localStorage.setItem('forumlify-forum-name', s.forum_name);
|
||||
syncForumNameCookie(s.forum_name);
|
||||
}
|
||||
// 先标记论坛名已收到(加载页原地淡入名字),稍后切换主页
|
||||
setForumNameLoaded(true);
|
||||
@@ -246,7 +254,8 @@ export default function AppProvider({ children }) {
|
||||
const updateForumName = useCallback((name) => {
|
||||
setForumName(name);
|
||||
localStorage.setItem('forumlify-forum-name', name);
|
||||
}, []);
|
||||
syncForumNameCookie(name);
|
||||
}, [syncForumNameCookie]);
|
||||
|
||||
const value = {
|
||||
currentUser, setCurrentUser,
|
||||
|
||||
+18
-16
@@ -2,6 +2,7 @@
|
||||
|
||||
// 信息流:帖子列表 + 排序 tab + 发布新帖按钮
|
||||
import { useEffect, useState, useCallback, useRef } from 'react';
|
||||
import { useTranslation } from 'react-i18next';
|
||||
import { API } from '@/lib/api';
|
||||
import { useApp } from './AppProvider';
|
||||
import { Icon } from './Icons';
|
||||
@@ -15,6 +16,7 @@ function avatar(username) {
|
||||
|
||||
export default function Feed({ onOpenModal, onReport }) {
|
||||
const { currentUser, sort, setSort, openPost, openUser, navigate, refreshKey } = useApp();
|
||||
const { t } = useTranslation();
|
||||
const [posts, setPosts] = useState(null);
|
||||
const [error, setError] = useState(null);
|
||||
const [loading, setLoading] = useState(true);
|
||||
@@ -57,12 +59,12 @@ export default function Feed({ onOpenModal, onReport }) {
|
||||
useEffect(() => { setPage(1); }, [sort]);
|
||||
|
||||
const handleDelete = async (postId) => {
|
||||
if (!confirm('确定要删除这条帖子吗?')) return;
|
||||
if (!confirm(t('feed.confirmDelete'))) return;
|
||||
try {
|
||||
await API.deletePost(postId);
|
||||
load(page);
|
||||
} catch (err) {
|
||||
alert('删除失败:' + err.message);
|
||||
alert(t('feed.deleteFailed', { msg: err.message }));
|
||||
}
|
||||
};
|
||||
|
||||
@@ -70,26 +72,26 @@ export default function Feed({ onOpenModal, onReport }) {
|
||||
<main id="feed">
|
||||
<div className="feed-header">
|
||||
<div className="feed-header-left">
|
||||
<button className={'tab' + (sort === 'latest' ? ' active' : '')} onClick={() => setSort('latest')}>最新发布</button>
|
||||
<button className={'tab' + (sort === 'hot' ? ' active' : '')} onClick={() => setSort('hot')}>最新回复</button>
|
||||
<button className={'tab' + (sort === 'latest' ? ' active' : '')} onClick={() => setSort('latest')}>{t('feed.latest')}</button>
|
||||
<button className={'tab' + (sort === 'hot' ? ' active' : '')} onClick={() => setSort('hot')}>{t('feed.hot')}</button>
|
||||
</div>
|
||||
<button
|
||||
className="btn-primary fab-btn"
|
||||
onClick={() => {
|
||||
if (!currentUser) { alert('请先登录'); return; }
|
||||
if (!currentUser) { alert(t('newPost.pleaseLogin')); return; }
|
||||
navigate('new');
|
||||
}}
|
||||
>
|
||||
<Icon name="plus" /> 发布新帖
|
||||
<Icon name="plus" /> {t('feed.newPost')}
|
||||
</button>
|
||||
</div>
|
||||
<div id="postList" ref={postListRef}>
|
||||
{posts === null ? (
|
||||
<div style={{ textAlign: 'center', color: '#94a3b8', padding: '40px 0' }}><span className="spinner-sm" />加载中...</div>
|
||||
<div style={{ textAlign: 'center', color: '#94a3b8', padding: '40px 0' }}><span className="spinner-sm" />{t('feed.loading')}</div>
|
||||
) : error ? (
|
||||
<div style={{ textAlign: 'center', color: '#ef4444', padding: '40px 0' }}>加载失败:{error}</div>
|
||||
<div style={{ textAlign: 'center', color: '#ef4444', padding: '40px 0' }}>{t('feed.loadFailed', { msg: error })}</div>
|
||||
) : posts.length === 0 ? (
|
||||
<div style={{ textAlign: 'center', color: '#94a3b8', padding: '60px 0' }}>✨ 还没有帖子,快来发布第一条吧!</div>
|
||||
<div style={{ textAlign: 'center', color: '#94a3b8', padding: '60px 0' }}>{t('feed.empty')}</div>
|
||||
) : (
|
||||
posts.map((p) => {
|
||||
const time = p.created_at ? new Date(p.created_at).toLocaleString('zh-CN') : '';
|
||||
@@ -97,7 +99,7 @@ export default function Feed({ onOpenModal, onReport }) {
|
||||
<div key={p.id} className="post-card" style={{ cursor: 'pointer' }}
|
||||
onClick={() => openPost(p.id)}>
|
||||
{p.is_pinned && (
|
||||
<div style={{ fontSize: 12, color: 'var(--primary)', fontWeight: 600, marginBottom: 4 }}>📌 置顶</div>
|
||||
<div style={{ fontSize: 12, color: 'var(--primary)', fontWeight: 600, marginBottom: 4 }}>{t('feed.pinned')}</div>
|
||||
)}
|
||||
<div className="post-header">
|
||||
<img src={p.avatar_url || avatar(p.username)} className="post-avatar" alt="" />
|
||||
@@ -106,14 +108,14 @@ export default function Feed({ onOpenModal, onReport }) {
|
||||
style={{ cursor: 'pointer', color: 'var(--primary)' }}
|
||||
onClick={(e) => { e.stopPropagation(); openUser(p.username); }}
|
||||
>
|
||||
{p.username || '匿名用户'}
|
||||
{p.username || t('feed.anonymous')}
|
||||
</span>
|
||||
<span className="post-time">{time}</span>
|
||||
{p.edited_at && (
|
||||
<span style={{ fontSize: 11, color: 'var(--text-light)', marginLeft: 6 }}>(已编辑)</span>
|
||||
<span style={{ fontSize: 11, color: 'var(--text-light)', marginLeft: 6 }}>{t('feed.deleted')}</span>
|
||||
)}
|
||||
</div>
|
||||
<div className="post-title">{p.title || '无标题'}</div>
|
||||
<div className="post-title">{p.title || t('feed.noTitle')}</div>
|
||||
<div className="post-content" dangerouslySetInnerHTML={{ __html: renderMarkdown(p.content) }} />
|
||||
{p.images && p.images.length > 0 && (
|
||||
<div className="post-images">
|
||||
@@ -128,17 +130,17 @@ export default function Feed({ onOpenModal, onReport }) {
|
||||
</span>
|
||||
<button className="action-report" onClick={(e) => {
|
||||
e.stopPropagation();
|
||||
if (!currentUser) { alert('请先登录'); return; }
|
||||
if (!currentUser) { alert(t('newPost.pleaseLogin')); return; }
|
||||
onReport(p.id);
|
||||
}}>
|
||||
<Icon name="flag" size={14} /> 举报
|
||||
<Icon name="flag" size={14} /> {t('feed.report')}
|
||||
</button>
|
||||
{currentUser && (currentUser.id === p.user_id || currentUser.role === 'admin') && (
|
||||
<button className="action-delete" onClick={(e) => {
|
||||
e.stopPropagation();
|
||||
handleDelete(p.id);
|
||||
}}>
|
||||
<Icon name="trash" size={14} /> 删除
|
||||
<Icon name="trash" size={14} /> {t('feed.delete')}
|
||||
</button>
|
||||
)}
|
||||
</div>
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
'use client';
|
||||
|
||||
// i18n 客户端初始化(挂在 layout,hydrate 后执行)
|
||||
import { useEffect } from 'react';
|
||||
import { initI18n } from '@/lib/i18n';
|
||||
|
||||
export default function I18nInit() {
|
||||
useEffect(() => {
|
||||
initI18n();
|
||||
}, []);
|
||||
return null;
|
||||
}
|
||||
@@ -1,15 +1,17 @@
|
||||
'use client';
|
||||
|
||||
// 全屏加载页:初始化未完成时显示转圈,加载完成后淡出
|
||||
// - 名字容器用 dangerouslySetInnerHTML(空):React 完全不管理其文本内容,
|
||||
// 内联脚本(body 首个子元素,DOMContentLoaded)填入缓存名后不会被 React
|
||||
// 检测/重写 → 无 hydration mismatch,加载圈不会重启,名字不会二次出现
|
||||
export default function LoadingScreen() {
|
||||
// 论坛名:容器由 React 渲染(SSR 即有),内联脚本在 DOMContentLoaded 时
|
||||
// 填充缓存名 → 首帧显示;suppressHydrationWarning 让 React 跳过该元素
|
||||
// 的文本检查(保留脚本填入的内容),hydrate 后由 AppProvider 状态接管。
|
||||
export default function LoadingScreen({ forumName, forumNameLoaded }) {
|
||||
return (
|
||||
<div className="loading-screen">
|
||||
<div className="loading-inner">
|
||||
<div className="loading-spinner" />
|
||||
<div className="loading-title loading-title-appear" id="loadingTitle" dangerouslySetInnerHTML={{ __html: '' }} />
|
||||
<div className="loading-title loading-title-appear" id="loadingTitle" suppressHydrationWarning>
|
||||
{forumNameLoaded && forumName}
|
||||
</div>
|
||||
<div className="loading-sub">加载中...</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
+20
-18
@@ -2,6 +2,7 @@
|
||||
|
||||
// 登录/注册/举报 模态框
|
||||
import { useState, useEffect } from 'react';
|
||||
import { useTranslation } from 'react-i18next';
|
||||
import { API, generateCaptcha } from '@/lib/api';
|
||||
import { useApp } from './AppProvider';
|
||||
import { Icon } from './Icons';
|
||||
@@ -9,6 +10,7 @@ import CaptchaImage from './CaptchaImage';
|
||||
|
||||
export default function Modals({ modal, onClose, reportPostId }) {
|
||||
const { login, register, currentUser, refresh } = useApp();
|
||||
const { t } = useTranslation();
|
||||
|
||||
// 登录表单
|
||||
const [loginEmail, setLoginEmail] = useState('');
|
||||
@@ -90,11 +92,11 @@ export default function Modals({ modal, onClose, reportPostId }) {
|
||||
<div className="modal active" onClick={close}>
|
||||
<div className="modal-content">
|
||||
<span className="close" onClick={onClose}><Icon name="close" size={20} /></span>
|
||||
<h2 style={{ marginBottom: 16 }}>登录</h2>
|
||||
<input type="email" placeholder="邮箱" value={loginEmail} onChange={(e) => setLoginEmail(e.target.value)} />
|
||||
<input type="password" placeholder="密码" value={loginPassword} onChange={(e) => setLoginPassword(e.target.value)}
|
||||
<h2 style={{ marginBottom: 16 }}>{t('auth.login')}</h2>
|
||||
<input type="email" placeholder={t('auth.email')} value={loginEmail} onChange={(e) => setLoginEmail(e.target.value)} />
|
||||
<input type="password" placeholder={t('auth.password')} value={loginPassword} onChange={(e) => setLoginPassword(e.target.value)}
|
||||
onKeyDown={(e) => { if (e.key === 'Enter') handleLogin(); }} />
|
||||
<button className="btn-primary" style={{ width: '100%' }} onClick={handleLogin}>登录</button>
|
||||
<button className="btn-primary" style={{ width: '100%' }} onClick={handleLogin}>{t('auth.login')}</button>
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
@@ -103,19 +105,19 @@ export default function Modals({ modal, onClose, reportPostId }) {
|
||||
<div className="modal active" onClick={close}>
|
||||
<div className="modal-content">
|
||||
<span className="close" onClick={onClose}><Icon name="close" size={20} /></span>
|
||||
<h2 style={{ marginBottom: 16 }}>注册</h2>
|
||||
<input type="text" placeholder="用户名" value={regUsername} onChange={(e) => setRegUsername(e.target.value)} />
|
||||
<input type="email" placeholder="邮箱" value={regEmail} onChange={(e) => setRegEmail(e.target.value)} />
|
||||
<input type="password" placeholder="密码(至少6位)" value={regPassword} onChange={(e) => setRegPassword(e.target.value)} />
|
||||
<h2 style={{ marginBottom: 16 }}>{t('auth.register')}</h2>
|
||||
<input type="text" placeholder={t('auth.username')} value={regUsername} onChange={(e) => setRegUsername(e.target.value)} />
|
||||
<input type="email" placeholder={t('auth.email')} value={regEmail} onChange={(e) => setRegEmail(e.target.value)} />
|
||||
<input type="password" placeholder={t('auth.passwordHint')} value={regPassword} onChange={(e) => setRegPassword(e.target.value)} />
|
||||
<div className="captcha-row" style={{ display: 'flex', alignItems: 'center', gap: 10, margin: '12px 0' }}>
|
||||
{regCaptcha ? (
|
||||
<CaptchaImage captcha={regCaptcha} onRefresh={() => { setRegCaptcha(generateCaptcha()); setRegCaptchaInput(''); }} />
|
||||
) : (
|
||||
<span style={{ color: 'var(--text-light)' }}>验证码加载中...</span>
|
||||
<span style={{ color: 'var(--text-light)' }}>{t('newPost.captchaLoading')}</span>
|
||||
)}
|
||||
<input type="text" placeholder="答案" style={{ width: 80, background: 'var(--bg)', color: 'var(--text)', border: '1px solid var(--border)', padding: '8px 12px', borderRadius: 4 }} value={regCaptchaInput} onChange={(e) => setRegCaptchaInput(e.target.value)} />
|
||||
</div>
|
||||
<button className="btn-primary" style={{ width: '100%' }} onClick={handleRegister}>注册</button>
|
||||
<button className="btn-primary" style={{ width: '100%' }} onClick={handleRegister}>{t('auth.register')}</button>
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
@@ -124,20 +126,20 @@ export default function Modals({ modal, onClose, reportPostId }) {
|
||||
<div className="modal active" onClick={close}>
|
||||
<div className="modal-content">
|
||||
<span className="close" onClick={onClose}><Icon name="close" size={20} /></span>
|
||||
<h2 style={{ marginBottom: 16 }}><Icon name="shieldAlert" size={20} /> 举报帖子</h2>
|
||||
<p style={{ fontSize: 14, color: '#64748b', marginBottom: 12 }}>请选择举报原因:</p>
|
||||
<h2 style={{ marginBottom: 16 }}><Icon name="shieldAlert" size={20} /> {t('post.reportTitle')}</h2>
|
||||
<p style={{ fontSize: 14, color: '#64748b', marginBottom: 12 }}>{t('post.reportReason')}</p>
|
||||
<select
|
||||
style={{ width: '100%', padding: '8px 12px', border: '1px solid var(--border)', borderRadius: 4, marginBottom: 16, background: 'var(--bg)', color: 'var(--text)' }}
|
||||
value={reportReason}
|
||||
onChange={(e) => setReportReason(e.target.value)}
|
||||
>
|
||||
<option value="spam">垃圾广告</option>
|
||||
<option value="abuse">人身攻击</option>
|
||||
<option value="illegal">违法内容</option>
|
||||
<option value="nsfw">不适当内容</option>
|
||||
<option value="other">其他</option>
|
||||
<option value="spam">{t('report.spam')}</option>
|
||||
<option value="abuse">{t('report.abuse')}</option>
|
||||
<option value="illegal">{t('report.illegal')}</option>
|
||||
<option value="nsfw">{t('report.nsfw')}</option>
|
||||
<option value="other">{t('report.other')}</option>
|
||||
</select>
|
||||
<button className="btn-primary" style={{ width: '100%' }} onClick={handleReport}>提交举报</button>
|
||||
<button className="btn-primary" style={{ width: '100%' }} onClick={handleReport}>{t('post.submitReport')}</button>
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
|
||||
+10
-8
@@ -2,6 +2,7 @@
|
||||
|
||||
// 导航栏:论坛名、自定义页面链接、登录/注册、私信、用户菜单、主题切换
|
||||
import { useState, useEffect } from 'react';
|
||||
import { useTranslation } from 'react-i18next';
|
||||
import { useApp } from './AppProvider';
|
||||
import { Icon } from './Icons';
|
||||
import { API } from '@/lib/api';
|
||||
@@ -9,6 +10,7 @@ import { DMButton } from './chat/ChatManager';
|
||||
|
||||
export default function Navbar({ onOpenModal }) {
|
||||
const { currentUser, forumName, theme, toggleTheme, navigate, openCustomPage, logout } = useApp();
|
||||
const { t } = useTranslation();
|
||||
const [menuOpen, setMenuOpen] = useState(false);
|
||||
const [customPages, setCustomPages] = useState([]);
|
||||
|
||||
@@ -77,8 +79,8 @@ export default function Navbar({ onOpenModal }) {
|
||||
<div id="userMenu">
|
||||
{!currentUser ? (
|
||||
<div id="authButtons">
|
||||
<button className="btn-ghost" onClick={() => onOpenModal('login')}>登录</button>
|
||||
<button className="btn-primary" onClick={() => onOpenModal('register')}>注册</button>
|
||||
<button className="btn-ghost" onClick={() => onOpenModal('login')}>{t('nav.login')}</button>
|
||||
<button className="btn-primary" onClick={() => onOpenModal('register')}>{t('nav.register')}</button>
|
||||
</div>
|
||||
) : (
|
||||
<div id="userDropdown" style={{ display: 'block' }}>
|
||||
@@ -91,14 +93,14 @@ export default function Navbar({ onOpenModal }) {
|
||||
{menuOpen && (
|
||||
<div className="dropdown-menu show" onClick={(e) => e.stopPropagation()}>
|
||||
<a href="#" onClick={openMessages}>
|
||||
<Icon name="message" /> 消息
|
||||
<Icon name="message" /> {t('nav.messages')}
|
||||
</a>
|
||||
<a href="#" onClick={(e) => { e.preventDefault(); goPage('settings'); }}>
|
||||
<Icon name="settings" /> 设置
|
||||
<Icon name="settings" /> {t('nav.settings')}
|
||||
</a>
|
||||
{currentUser.role === 'admin' && (
|
||||
<a href="#" onClick={(e) => { e.preventDefault(); goPage('admin'); }}>
|
||||
<Icon name="shieldAlert" /> 管理后台
|
||||
<Icon name="shieldAlert" /> {t('nav.admin')}
|
||||
</a>
|
||||
)}
|
||||
<hr />
|
||||
@@ -110,20 +112,20 @@ export default function Navbar({ onOpenModal }) {
|
||||
setMenuOpen(false);
|
||||
}}
|
||||
>
|
||||
<Icon name={theme === 'dark' ? 'sun' : 'moon'} /> {theme === 'dark' ? '亮色模式' : '暗色模式'}
|
||||
<Icon name={theme === 'dark' ? 'sun' : 'moon'} /> {theme === 'dark' ? t('nav.lightMode') : t('nav.darkMode')}
|
||||
</a>
|
||||
<hr />
|
||||
<a
|
||||
href="#"
|
||||
onClick={(e) => {
|
||||
e.preventDefault();
|
||||
if (confirm('确定要退出吗?')) {
|
||||
if (confirm(t('nav.logoutConfirm'))) {
|
||||
setMenuOpen(false);
|
||||
logout();
|
||||
}
|
||||
}}
|
||||
>
|
||||
<Icon name="logout" /> 退出
|
||||
<Icon name="logout" /> {t('nav.logout')}
|
||||
</a>
|
||||
</div>
|
||||
)}
|
||||
|
||||
+10
-8
@@ -1,7 +1,8 @@
|
||||
'use client';
|
||||
|
||||
// 发布新帖页
|
||||
// 发帖页
|
||||
import { useState, useEffect, useRef } from 'react';
|
||||
import { useTranslation } from 'react-i18next';
|
||||
import { API, generateCaptcha, uploadImage } from '@/lib/api';
|
||||
import { useApp } from './AppProvider';
|
||||
import { Icon } from './Icons';
|
||||
@@ -10,6 +11,7 @@ import CaptchaImage from './CaptchaImage';
|
||||
|
||||
export default function NewPost() {
|
||||
const { currentUser, navigate, refresh } = useApp();
|
||||
const { t } = useTranslation();
|
||||
const { toast } = useToast();
|
||||
const [title, setTitle] = useState('');
|
||||
const [content, setContent] = useState('');
|
||||
@@ -85,19 +87,19 @@ export default function NewPost() {
|
||||
return (
|
||||
<div className="page-slide active">
|
||||
<div className="page-header" style={{ maxWidth: 600, margin: '0 auto', width: '100%' }}>
|
||||
<h2><Icon name="plus" size={20} /> 发布新帖</h2>
|
||||
<h2><Icon name="plus" size={20} /> {t('newPost.title')}</h2>
|
||||
</div>
|
||||
<div style={{ maxWidth: 600, margin: '0 auto', width: '100%' }}>
|
||||
<input
|
||||
type="text"
|
||||
placeholder="标题"
|
||||
placeholder={t('newPost.title')}
|
||||
style={{ width: '100%', padding: '10px 14px', border: '1.5px solid var(--border)', borderRadius: 6, fontSize: 15, fontWeight: 600, marginBottom: 12, fontFamily: 'inherit', background: 'var(--bg)', color: 'var(--text)' }}
|
||||
value={title}
|
||||
onChange={(e) => setTitle(e.target.value)}
|
||||
/>
|
||||
<textarea
|
||||
rows={6}
|
||||
placeholder="说点什么..."
|
||||
placeholder={t('newPost.content')}
|
||||
style={{ width: '100%', padding: 12, border: '1.5px solid var(--border)', borderRadius: 6, fontSize: 15, fontFamily: 'inherit', resize: 'vertical', background: 'var(--bg)', color: 'var(--text)' }}
|
||||
value={content}
|
||||
onChange={(e) => setContent(e.target.value)}
|
||||
@@ -116,7 +118,7 @@ export default function NewPost() {
|
||||
onDrop={(e) => { e.preventDefault(); setDragOver(false); if (e.dataTransfer.files) handleFiles(Array.from(e.dataTransfer.files)); }}
|
||||
>
|
||||
<div style={{ fontSize: 13, color: 'var(--text-secondary)' }}>
|
||||
{uploading ? '上传中...' : (dragOver ? '松开上传' : '📷 点击或拖拽上传图片')}
|
||||
{uploading ? t('newPost.uploading') : (dragOver ? t('newPost.drop') : '📷 ' + t('newPost.dropHint'))}
|
||||
</div>
|
||||
</div>
|
||||
<input
|
||||
@@ -132,18 +134,18 @@ export default function NewPost() {
|
||||
{captcha ? (
|
||||
<CaptchaImage captcha={captcha} onRefresh={() => { setCaptcha(generateCaptcha()); setCaptchaInput(''); }} />
|
||||
) : (
|
||||
<span style={{ color: 'var(--text-light)' }}>验证码加载中...</span>
|
||||
<span style={{ color: 'var(--text-light)' }}>{t('newPost.captchaLoading')}</span>
|
||||
)}
|
||||
<input
|
||||
type="text"
|
||||
placeholder="答案"
|
||||
placeholder={t('newPost.answer')}
|
||||
style={{ width: 80, padding: '8px 12px', border: '1px solid var(--border)', borderRadius: 4, background: 'var(--bg)', color: 'var(--text)', outline: 'none' }}
|
||||
value={captchaInput}
|
||||
onChange={(e) => setCaptchaInput(e.target.value)}
|
||||
/>
|
||||
</div>
|
||||
<button className="btn-primary" style={{ padding: '10px 32px', fontSize: 15 }} onClick={handleSubmit}>
|
||||
{uploading ? '上传中...' : '发布帖子'}
|
||||
{uploading ? t('newPost.uploading') : t('newPost.publish')}
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
+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>
|
||||
);
|
||||
|
||||
@@ -4,18 +4,20 @@
|
||||
// - 三个面板纵向排列在滚动容器里,scroll-snap 每次滑动对齐一个面板
|
||||
// - 点侧边栏导航 → 平滑滚动到对应面板;滚动结束 → 自动更新导航高亮
|
||||
import { useState, useRef, useCallback } from 'react';
|
||||
import { useTranslation } from 'react-i18next';
|
||||
import { Icon } from './Icons';
|
||||
import SettingsProfile from './SettingsProfile';
|
||||
import AccountSecurity from './AccountSecurity';
|
||||
import RecoveryCodes from './RecoveryCodes';
|
||||
|
||||
const NAV = [
|
||||
{ key: 'profile', label: '个人资料', icon: 'user' },
|
||||
{ key: 'security', label: '安全设置', icon: 'shieldAlert' },
|
||||
{ key: 'recovery', label: '恢复码', icon: 'lock' },
|
||||
{ key: 'profile', labelKey: 'settings.profile', icon: 'user' },
|
||||
{ key: 'security', labelKey: 'settings.security', icon: 'shieldAlert' },
|
||||
{ key: 'recovery', labelKey: 'settings.recovery', icon: 'lock' },
|
||||
];
|
||||
|
||||
export default function SettingsPage() {
|
||||
const { t } = useTranslation();
|
||||
const [tab, setTab] = useState('profile');
|
||||
const scrollRef = useRef(null);
|
||||
const scrollTicking = useRef(false);
|
||||
@@ -46,7 +48,7 @@ export default function SettingsPage() {
|
||||
return (
|
||||
<div className="page-slide active">
|
||||
<div className="page-header" style={{ maxWidth: 1200, margin: '0 auto', width: '100%' }}>
|
||||
<h2><Icon name="settings" size={20} /> 设置</h2>
|
||||
<h2><Icon name="settings" size={20} /> {t('settings.title')}</h2>
|
||||
</div>
|
||||
<div className="settings-layout" style={{ maxWidth: 1200, margin: '0 auto', width: '100%', display: 'flex', gap: 28, alignItems: 'flex-start' }}>
|
||||
<aside className="settings-sidebar" style={{ width: 200, flexShrink: 0 }}>
|
||||
@@ -60,7 +62,7 @@ export default function SettingsPage() {
|
||||
onClick={(e) => goTo(n.key, e)}
|
||||
>
|
||||
<span className="nav-icon"><Icon name={n.icon} size={18} /></span>
|
||||
{n.label}
|
||||
{t(n.labelKey)}
|
||||
</a>
|
||||
))}
|
||||
</nav>
|
||||
|
||||
@@ -1,12 +1,15 @@
|
||||
'use client';
|
||||
|
||||
// 设置 - 个人资料(头像 + 用户名/简介/签名)
|
||||
// 设置 - 个人资料(头像 + 用户名/简介/签名 + 语言)
|
||||
import { useState, useRef } from 'react';
|
||||
import { useTranslation } from 'react-i18next';
|
||||
import { API, uploadImage } from '@/lib/api';
|
||||
import { useApp } from './AppProvider';
|
||||
import { setAppLanguage } from '@/lib/i18n';
|
||||
|
||||
export default function SettingsProfile() {
|
||||
const { currentUser, setCurrentUser } = useApp();
|
||||
const { t, i18n } = useTranslation();
|
||||
const [username, setUsername] = useState(currentUser?.username || '');
|
||||
const [bio, setBio] = useState(currentUser?.bio || '');
|
||||
const [signature, setSignature] = useState(currentUser?.signature || '');
|
||||
@@ -82,6 +85,27 @@ export default function SettingsProfile() {
|
||||
</div>
|
||||
<button className="btn-primary" style={{ width: '100%', padding: 10 }} onClick={handleSave}>保存设置</button>
|
||||
</div>
|
||||
|
||||
{/* 语言设置 */}
|
||||
<div style={{ background: 'var(--surface)', border: '1px solid var(--border)', borderRadius: 8, padding: 20, marginTop: 16 }}>
|
||||
<label style={{ fontWeight: 600, fontSize: 14, display: 'block', marginBottom: 8 }}>{t('settings.language')}</label>
|
||||
<div style={{ display: 'flex', gap: 10 }}>
|
||||
<button
|
||||
className={'btn' + (i18n.language === 'zh' ? ' btn-primary' : '')}
|
||||
style={{ padding: '8px 20px', borderRadius: 6, cursor: 'pointer', border: '1px solid var(--border)', background: i18n.language === 'zh' ? 'var(--primary)' : 'transparent', color: i18n.language === 'zh' ? '#fff' : 'var(--text)' }}
|
||||
onClick={() => setAppLanguage('zh')}
|
||||
>
|
||||
{t('settings.languageZh')}
|
||||
</button>
|
||||
<button
|
||||
className={'btn' + (i18n.language === 'en' ? ' btn-primary' : '')}
|
||||
style={{ padding: '8px 20px', borderRadius: 6, cursor: 'pointer', border: '1px solid var(--border)', background: i18n.language === 'en' ? 'var(--primary)' : 'transparent', color: i18n.language === 'en' ? '#fff' : 'var(--text)' }}
|
||||
onClick={() => setAppLanguage('en')}
|
||||
>
|
||||
{t('settings.languageEn')}
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -3,6 +3,7 @@
|
||||
// 私信 - 会话列表
|
||||
import { useEffect, useState } from 'react';
|
||||
import { API } from '@/lib/api';
|
||||
import { Icon } from '../Icons';
|
||||
|
||||
function avatar(username) {
|
||||
return 'https://ui-avatars.com/api/?name=' + encodeURIComponent(username || 'U') +
|
||||
@@ -24,7 +25,7 @@ export default function ConversationList({ onOpenChat, onClose }) {
|
||||
<div className="modal active" onClick={(e) => { if (e.target === e.currentTarget) onClose(); }}>
|
||||
<div className="modal-content" style={{ display: 'flex', flexDirection: 'column', height: '70vh', maxHeight: '70vh', padding: 0, overflow: 'hidden' }}>
|
||||
<div style={{ display: 'flex', justifyContent: 'space-between', alignItems: 'center', padding: '16px 20px', borderBottom: '1px solid var(--border)' }}>
|
||||
<h2 style={{ fontSize: 18, margin: 0 }}>✉️ 私信</h2>
|
||||
<h2 style={{ fontSize: 18, margin: 0 }}><Icon name="message" size={18} style={{ verticalAlign: -3, marginRight: 6 }} /> 私信</h2>
|
||||
<span className="close" onClick={onClose} style={{ fontSize: 24, cursor: 'pointer', color: 'var(--text-light)', lineHeight: 1 }}>×</span>
|
||||
</div>
|
||||
<div id="messageListContent" style={{ flex: 1, overflowY: 'auto', padding: 0 }}>
|
||||
|
||||
Reference in new issue
Block a user