feat: follow lite forum updates
This commit is contained in:
1 parent
f59739e36f
commit
b42944b687
38 files changed
+1433
-71
No files matched your search
@@ -6,6 +6,7 @@ import { API } from '@/lib/api';
|
||||
import { useApp } from '../AppProvider';
|
||||
import { Icon } from '../Icons';
|
||||
import { useToast } from '../Toast';
|
||||
import { useTranslation } from 'react-i18next';
|
||||
|
||||
function avatar(username) {
|
||||
return 'https://ui-avatars.com/api/?name=' + encodeURIComponent(username || 'U') +
|
||||
@@ -15,9 +16,12 @@ function avatar(username) {
|
||||
export default function ChatWindow({ conversationId, otherUserId, otherUsername, onClose, onRefreshList }) {
|
||||
const { currentUser } = useApp();
|
||||
const { toast } = useToast();
|
||||
const { t, i18n } = useTranslation();
|
||||
const [messages, setMessages] = useState(null);
|
||||
const [content, setContent] = useState('');
|
||||
const containerRef = useRef(null);
|
||||
const waitingForReply = messages && !messages.some((message) => message.sender_id === otherUserId)
|
||||
&& messages.filter((message) => message.sender_id === currentUser?.id).length >= 3;
|
||||
|
||||
const load = useCallback(async (silent = false) => {
|
||||
if (!silent) setMessages(null);
|
||||
@@ -43,14 +47,15 @@ export default function ChatWindow({ conversationId, otherUserId, otherUsername,
|
||||
}, [messages]);
|
||||
|
||||
const send = async () => {
|
||||
if (!content.trim()) return;
|
||||
if (!content.trim() || waitingForReply) return;
|
||||
try {
|
||||
await API.sendMessage(conversationId, content.trim());
|
||||
setContent('');
|
||||
load();
|
||||
onRefreshList();
|
||||
} catch (err) {
|
||||
toast('发送失败:' + err.message, 'error');
|
||||
toast(err.status === 429 ? t('chat.limitReached') : err.message, 'error');
|
||||
if (err.status === 429) load(true);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -69,7 +74,7 @@ export default function ChatWindow({ conversationId, otherUserId, otherUsername,
|
||||
) : (
|
||||
messages.map((m) => {
|
||||
const isMine = m.sender_id === currentUser?.id;
|
||||
const time = m.created_at ? new Date(m.created_at).toLocaleString('zh-CN') : '';
|
||||
const time = m.created_at ? new Date(m.created_at).toLocaleString(i18n.language === 'en' ? 'en-US' : 'zh-CN') : '';
|
||||
return (
|
||||
<div key={m.id} style={{ display: 'flex', justifyContent: isMine ? 'flex-end' : 'flex-start', marginBottom: 12 }}>
|
||||
{!isMine && (
|
||||
@@ -94,6 +99,7 @@ export default function ChatWindow({ conversationId, otherUserId, otherUsername,
|
||||
})
|
||||
)}
|
||||
</div>
|
||||
{waitingForReply && <div className="chat-limit-hint">{t('chat.limitReached')}</div>}
|
||||
<div style={{ display: 'flex', gap: 8, padding: '12px 16px', borderTop: '1px solid var(--border)', flexShrink: 0 }}>
|
||||
<input
|
||||
type="text"
|
||||
@@ -101,10 +107,11 @@ export default function ChatWindow({ conversationId, otherUserId, otherUsername,
|
||||
placeholder="输入消息..."
|
||||
style={{ flex: 1, padding: '8px 12px', border: '1px solid var(--border)', borderRadius: 6, fontSize: 14, background: 'var(--bg)', color: 'var(--text)', outline: 'none' }}
|
||||
value={content}
|
||||
disabled={waitingForReply}
|
||||
onChange={(e) => setContent(e.target.value)}
|
||||
onKeyDown={(e) => { if (e.key === 'Enter') send(); }}
|
||||
/>
|
||||
<button className="btn-primary" style={{ padding: '8px 16px' }} onClick={send}>发送</button>
|
||||
<button className="btn-primary" style={{ padding: '8px 16px' }} disabled={waitingForReply} onClick={send}>发送</button>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@@ -4,6 +4,8 @@
|
||||
import { useEffect, useState } from 'react';
|
||||
import { API } from '@/lib/api';
|
||||
import { Icon } from '../Icons';
|
||||
import { useTranslation } from 'react-i18next';
|
||||
import { useToast } from '../Toast';
|
||||
|
||||
function avatar(username) {
|
||||
return 'https://ui-avatars.com/api/?name=' + encodeURIComponent(username || 'U') +
|
||||
@@ -11,7 +13,12 @@ function avatar(username) {
|
||||
}
|
||||
|
||||
export default function ConversationList({ onOpenChat, onClose }) {
|
||||
const { t, i18n } = useTranslation();
|
||||
const { toast } = useToast();
|
||||
const [conversations, setConversations] = useState(null);
|
||||
const [search, setSearch] = useState('');
|
||||
const [matches, setMatches] = useState([]);
|
||||
const [searching, setSearching] = useState(false);
|
||||
|
||||
const load = () => {
|
||||
API.getConversations()
|
||||
@@ -21,23 +28,67 @@ export default function ConversationList({ onOpenChat, onClose }) {
|
||||
|
||||
useEffect(load, []);
|
||||
|
||||
useEffect(() => {
|
||||
const keyword = search.trim();
|
||||
if (!keyword) { setMatches([]); setSearching(false); return; }
|
||||
let canceled = false;
|
||||
const timer = setTimeout(async () => {
|
||||
setSearching(true);
|
||||
try {
|
||||
const result = await API.searchUsers(keyword);
|
||||
if (!canceled) setMatches(result.data || []);
|
||||
} catch (error) {
|
||||
if (!canceled) toast(t('chat.openFailed', { msg: error.message }), 'error');
|
||||
} finally {
|
||||
if (!canceled) setSearching(false);
|
||||
}
|
||||
}, 300);
|
||||
return () => { canceled = true; clearTimeout(timer); };
|
||||
}, [search, t, toast]);
|
||||
|
||||
const startChat = async (user) => {
|
||||
try {
|
||||
const conversation = await API.getOrCreateConversation(user.id);
|
||||
onOpenChat(conversation.id, user.id, user.username);
|
||||
} catch (error) {
|
||||
toast(t('chat.openFailed', { msg: error.message }), 'error');
|
||||
}
|
||||
};
|
||||
|
||||
return (
|
||||
<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 }}><Icon name="message" size={18} style={{ verticalAlign: -3, marginRight: 6 }} /> 私信</h2>
|
||||
<h2 style={{ fontSize: 18, margin: 0 }}><Icon name="message" size={18} style={{ verticalAlign: -3, marginRight: 6 }} /> {t('chat.title')}</h2>
|
||||
<span className="close" onClick={onClose} style={{ cursor: 'pointer', color: 'var(--text-light)', lineHeight: 1 }}><Icon name="close" size={20} /></span>
|
||||
</div>
|
||||
<div className="chat-user-search">
|
||||
<Icon name="search" size={17} aria-hidden="true" />
|
||||
<input type="search" name="forumlify-user-search" autoComplete="off" maxLength={50}
|
||||
aria-label={t('chat.searchPlaceholder')} placeholder={t('chat.searchPlaceholder')}
|
||||
value={search} onChange={(event) => setSearch(event.target.value)} />
|
||||
</div>
|
||||
<div id="messageListContent" style={{ flex: 1, overflowY: 'auto', padding: 0 }}>
|
||||
{search.trim() ? (
|
||||
searching ? <div className="chat-search-state">{t('chat.loading')}</div>
|
||||
: matches.length === 0 ? <div className="chat-search-state">{t('chat.searchEmpty')}</div>
|
||||
: matches.map((user) => (
|
||||
<button key={user.id} type="button" className="chat-user-result" onClick={() => startChat(user)}>
|
||||
<img src={user.avatar_url || avatar(user.username)} alt="" />
|
||||
<span>{user.username}</span>
|
||||
</button>
|
||||
))
|
||||
) : (
|
||||
<>
|
||||
{conversations === 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('chat.loading')}</div>
|
||||
) : conversations.length === 0 ? (
|
||||
<div style={{ textAlign: 'center', color: '#94a3b8', padding: '40px 0' }}>暂无私信</div>
|
||||
<div style={{ textAlign: 'center', color: '#94a3b8', padding: '40px 0' }}>{t('chat.empty')}</div>
|
||||
) : (
|
||||
conversations.map((c) => {
|
||||
const unread = c.unread_count || 0;
|
||||
const lastMsg = c.last_message || '暂无消息';
|
||||
const time = c.last_message_time ? new Date(c.last_message_time).toLocaleString('zh-CN') : '';
|
||||
const lastMsg = c.last_message || t('chat.noMessage');
|
||||
const time = c.last_message_time ? new Date(c.last_message_time).toLocaleString(i18n.language === 'en' ? 'en-US' : 'zh-CN') : '';
|
||||
return (
|
||||
<div
|
||||
key={c.id}
|
||||
@@ -62,6 +113,8 @@ export default function ConversationList({ onOpenChat, onClose }) {
|
||||
);
|
||||
})
|
||||
)}
|
||||
</>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
Reference in new issue
Block a user