Files
forumlify/components/admin/AdminCustomCss.js
T
lezi-fun 4ee38f6486 feat: align with upstream — pin posts, edit marker, custom CSS, UX fixes
- Pin/unpin posts (PUT /api/posts/:id/pin, admin) with notification to author
- is_pinned/pinned_at in schema; 📌 badge on feed + detail; admin pin button
- Edited marker (已编辑) on feed + detail; edit button + modal for authors
- Custom CSS: upload/delete API + admin tab + global loader (style.css override)
- Fix: duplicate post title on detail page (remove card title, keep header)
- Fix: forum name flicker on load (localStorage cache before fetch)
- Fix: dropdown menu closes on outside click (document listener)
- Fix: document.title + <title> sync on forum rename
2026-08-10 12:32:06 +08:00

83 lines
3.3 KiB
JavaScript

'use client';
// 管理后台 - 自定义 CSS(上传 style.css 覆盖默认样式)
import { useState, useRef } from 'react';
import { API } from '@/lib/api';
export default function AdminCustomCss() {
const [status, setStatus] = useState(null);
const [dragOver, setDragOver] = useState(false);
const fileInputRef = useRef(null);
const handleFile = async (file) => {
if (!file) return;
if (file.name !== 'style.css') {
setStatus({ ok: false, msg: '❌ 文件名必须是 style.css' });
return;
}
if (file.size > 1024 * 1024) {
setStatus({ ok: false, msg: '❌ CSS 文件不能超过 1MB' });
return;
}
try {
await API.uploadCustomCss(file);
setStatus({ ok: true, msg: '✅ 自定义 CSS 已生效(刷新页面后应用)' });
// 强制刷新样式
const link = document.getElementById('custom-css-link');
if (link) link.href = '/api/custom-css?t=' + Date.now();
} catch (err) {
setStatus({ ok: false, msg: '❌ ' + err.message });
}
};
const handleDelete = async () => {
if (!confirm('确定要删除自定义 CSS 吗?')) return;
try {
await API.deleteCustomCss();
setStatus({ ok: true, msg: '✅ 已删除自定义 CSS' });
const link = document.getElementById('custom-css-link');
if (link) link.remove();
} catch (err) {
setStatus({ ok: false, msg: '❌ ' + err.message });
}
};
return (
<>
<h3 style={{ marginBottom: 16 }}>🎨 自定义 CSS</h3>
<p style={{ fontSize: 13, color: 'var(--text-secondary)', marginBottom: 12 }}>
上传一个 <code>style.css</code> 文件覆盖默认样式(支持 CSS 变量、暗色模式等)。
</p>
<div
style={{
border: '2px dashed ' + (dragOver ? 'var(--primary)' : 'var(--border)'),
borderRadius: 8, padding: 32, textAlign: 'center', cursor: 'pointer',
transition: 'all 0.3s', background: dragOver ? 'var(--primary-bg)' : 'var(--bg)',
}}
onClick={() => fileInputRef.current?.click()}
onDragOver={(e) => { e.preventDefault(); setDragOver(true); }}
onDragLeave={() => setDragOver(false)}
onDrop={(e) => { e.preventDefault(); setDragOver(false); if (e.dataTransfer.files?.[0]) handleFile(e.dataTransfer.files[0]); }}
>
<span style={{ fontSize: 13, color: 'var(--text-secondary)' }}>
{dragOver ? '松开上传' : '📄 点击或拖拽上传 style.css'}
</span>
</div>
<input
type="file"
ref={fileInputRef}
accept=".css"
style={{ display: 'none' }}
onChange={(e) => { if (e.target.files?.[0]) handleFile(e.target.files[0]); }}
/>
{status && (
<div style={{ fontSize: 13, marginTop: 8, color: status.ok ? '#22c55e' : '#ef4444' }}>{status.msg}</div>
)}
<div style={{ display: 'flex', gap: 8, marginTop: 16 }}>
<button className="btn-primary" style={{ padding: '8px 20px' }} onClick={() => fileInputRef.current?.click()}>💾 保存 CSS</button>
<button className="btn-secondary" style={{ padding: '8px 20px', border: '1px solid var(--border)', borderRadius: 4, background: 'var(--surface)', cursor: 'pointer', color: 'var(--text)' }} onClick={handleDelete}>🗑️ 删除自定义 CSS</button>
</div>
</>
);
}