[feat] log
This commit is contained in:
14 files changed
+305
-210
No files matched your search
@@ -1,5 +1,5 @@
|
||||
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
||||
import { useEffect, useState } from 'react';
|
||||
import { useEffect, useRef, useState } from 'react';
|
||||
import { StatusCard } from './StatusCard.js';
|
||||
const table = {
|
||||
width: '100%',
|
||||
@@ -8,7 +8,7 @@ const table = {
|
||||
};
|
||||
const cell = {
|
||||
borderBottom: '1px solid var(--dsh-border, #e5e7eb)',
|
||||
padding: '8px 10px',
|
||||
padding: '6px 8px',
|
||||
verticalAlign: 'middle',
|
||||
textAlign: 'left',
|
||||
};
|
||||
@@ -38,8 +38,22 @@ const primary = {
|
||||
color: '#fff',
|
||||
};
|
||||
const muted = { color: '#6b7280', fontSize: 12 };
|
||||
const section = { display: 'grid', gap: 16, maxWidth: 760 };
|
||||
const section = { display: 'grid', gap: 16, maxWidth: 640 };
|
||||
const row = { display: 'flex', gap: 8, alignItems: 'center' };
|
||||
const logBox = {
|
||||
margin: 0,
|
||||
padding: '8px 10px',
|
||||
maxHeight: 220,
|
||||
overflow: 'auto',
|
||||
border: '1px solid var(--dsh-border, #e5e7eb)',
|
||||
borderRadius: 6,
|
||||
background: 'var(--dsh-code-bg, rgba(127,127,127,0.08))',
|
||||
fontSize: 12,
|
||||
lineHeight: 1.6,
|
||||
fontFamily: 'ui-monospace, SFMono-Regular, Consolas, monospace',
|
||||
whiteSpace: 'pre-wrap',
|
||||
wordBreak: 'break-all',
|
||||
};
|
||||
export function MailGatewaySection({ api }) {
|
||||
const [snapshot, setSnapshot] = useState();
|
||||
const [mailbox, setMailbox] = useState();
|
||||
@@ -49,6 +63,38 @@ export function MailGatewaySection({ api }) {
|
||||
const [test, setTest] = useState();
|
||||
const [busy, setBusy] = useState(null);
|
||||
const [notice, setNotice] = useState();
|
||||
const logRef = useRef(null);
|
||||
// 首次读取填充表单;之后每 3 秒只刷新状态与日志,不覆盖正在编辑的表单。
|
||||
useEffect(() => {
|
||||
let alive = true;
|
||||
const load = async (initial) => {
|
||||
const result = await api.get();
|
||||
if (!alive)
|
||||
return;
|
||||
if (!result.ok) {
|
||||
if (initial)
|
||||
setNotice({ kind: 'error', text: result.error.message });
|
||||
return;
|
||||
}
|
||||
setSnapshot(result.value);
|
||||
if (initial) {
|
||||
setMailbox({ ...result.value.mailbox });
|
||||
setAllowed([...result.value.allowedAddresses]);
|
||||
setPassword('');
|
||||
}
|
||||
};
|
||||
void load(true);
|
||||
const timer = setInterval(() => void load(false), 3000);
|
||||
return () => {
|
||||
alive = false;
|
||||
clearInterval(timer);
|
||||
};
|
||||
}, [api]);
|
||||
useEffect(() => {
|
||||
const box = logRef.current;
|
||||
if (box)
|
||||
box.scrollTop = box.scrollHeight;
|
||||
}, [snapshot?.logs]);
|
||||
const applySnapshot = (next, resetMailbox) => {
|
||||
setSnapshot(next);
|
||||
setAllowed([...next.allowedAddresses]);
|
||||
@@ -57,20 +103,6 @@ export function MailGatewaySection({ api }) {
|
||||
setPassword('');
|
||||
}
|
||||
};
|
||||
useEffect(() => {
|
||||
let alive = true;
|
||||
void api.get().then(result => {
|
||||
if (!alive)
|
||||
return;
|
||||
if (result.ok)
|
||||
applySnapshot(result.value, true);
|
||||
else
|
||||
setNotice({ kind: 'error', text: result.error.message });
|
||||
});
|
||||
return () => {
|
||||
alive = false;
|
||||
};
|
||||
}, [api]);
|
||||
const setField = (key, value) => {
|
||||
setMailbox(current => (current ? { ...current, [key]: value } : current));
|
||||
};
|
||||
@@ -88,7 +120,7 @@ export function MailGatewaySection({ api }) {
|
||||
if (result.ok) {
|
||||
applySnapshot(result.value, true);
|
||||
setTest(undefined);
|
||||
setNotice({ kind: 'ok', text: '邮箱绑定已保存,连接已按新配置重建。' });
|
||||
setNotice({ kind: 'ok', text: '已保存,按新配置重连。' });
|
||||
}
|
||||
else {
|
||||
setNotice({ kind: 'error', text: result.error.message });
|
||||
@@ -131,7 +163,7 @@ export function MailGatewaySection({ api }) {
|
||||
setBusy(null);
|
||||
if (result.ok) {
|
||||
applySnapshot(result.value, false);
|
||||
setNotice({ kind: 'ok', text: '白名单已保存,收信策略立即生效,连接保持不变。' });
|
||||
setNotice({ kind: 'ok', text: '白名单已保存。' });
|
||||
}
|
||||
else {
|
||||
setNotice({ kind: 'error', text: result.error.message });
|
||||
@@ -146,5 +178,8 @@ export function MailGatewaySection({ api }) {
|
||||
const boolField = (key) => (event) => {
|
||||
setField(key, event.target.checked);
|
||||
};
|
||||
return (_jsxs("div", { style: section, children: [_jsx(StatusCard, { status: snapshot?.status, allowedCount: allowed.length, test: test }), _jsxs("div", { children: [_jsx("h3", { style: { margin: '0 0 8px', fontSize: 15 }, children: "\u90AE\u7BB1\u7ED1\u5B9A" }), _jsxs("table", { style: table, children: [_jsx("thead", { children: _jsxs("tr", { children: [_jsx("th", { style: { ...cell, ...muted, width: 140 }, children: "\u5B57\u6BB5" }), _jsx("th", { style: { ...cell, ...muted }, children: "\u503C" }), _jsx("th", { style: { ...cell, ...muted, width: 240 }, children: "\u8BF4\u660E" })] }) }), _jsxs("tbody", { children: [_jsxs("tr", { children: [_jsx("td", { style: cell, children: "\u90AE\u7BB1\u5730\u5740" }), _jsx("td", { style: cell, children: _jsx("input", { style: input, value: mailbox?.email ?? '', onChange: textField('email') }) }), _jsx("td", { style: { ...cell, ...muted }, children: "\u63A5\u6536\u90AE\u4EF6\u4E0E\u56DE\u4FE1\u4F7F\u7528\u7684\u5730\u5740\u3002" })] }), _jsxs("tr", { children: [_jsx("td", { style: cell, children: "\u663E\u793A\u540D" }), _jsx("td", { style: cell, children: _jsx("input", { style: input, value: mailbox?.displayName ?? '', onChange: textField('displayName') }) }), _jsx("td", { style: { ...cell, ...muted }, children: "\u56DE\u4FE1\u65F6\u7684\u53D1\u4EF6\u4EBA\u540D\u79F0\uFF0C\u53EF\u7559\u7A7A\u3002" })] }), _jsxs("tr", { children: [_jsx("td", { style: cell, children: "IMAP \u4E3B\u673A" }), _jsx("td", { style: cell, children: _jsx("input", { style: input, value: mailbox?.imapHost ?? '', onChange: textField('imapHost') }) }), _jsx("td", { style: { ...cell, ...muted }, children: "\u6807\u51C6 IMAP \u670D\u52A1\u5668\u5730\u5740\u3002" })] }), _jsxs("tr", { children: [_jsx("td", { style: cell, children: "IMAP \u7AEF\u53E3" }), _jsx("td", { style: cell, children: _jsx("input", { style: input, value: mailbox?.imapPort ?? 993, onChange: numberField('imapPort') }) }), _jsx("td", { style: { ...cell, ...muted }, children: "SSL \u4E00\u822C\u4E3A 993\u3002" })] }), _jsxs("tr", { children: [_jsx("td", { style: cell, children: "IMAP TLS" }), _jsx("td", { style: cell, children: _jsx("input", { type: "checkbox", checked: mailbox?.imapSecure ?? true, onChange: boolField('imapSecure') }) }), _jsx("td", { style: { ...cell, ...muted }, children: "\u52FE\u9009\u8868\u793A\u4F7F\u7528 SSL/TLS\u3002" })] }), _jsxs("tr", { children: [_jsx("td", { style: cell, children: "SMTP \u4E3B\u673A" }), _jsx("td", { style: cell, children: _jsx("input", { style: input, value: mailbox?.smtpHost ?? '', onChange: textField('smtpHost') }) }), _jsx("td", { style: { ...cell, ...muted }, children: "\u6807\u51C6 SMTP \u670D\u52A1\u5668\u5730\u5740\u3002" })] }), _jsxs("tr", { children: [_jsx("td", { style: cell, children: "SMTP \u7AEF\u53E3" }), _jsx("td", { style: cell, children: _jsx("input", { style: input, value: mailbox?.smtpPort ?? 465, onChange: numberField('smtpPort') }) }), _jsx("td", { style: { ...cell, ...muted }, children: "SSL \u4E00\u822C\u4E3A 465\u3002" })] }), _jsxs("tr", { children: [_jsx("td", { style: cell, children: "SMTP TLS" }), _jsx("td", { style: cell, children: _jsx("input", { type: "checkbox", checked: mailbox?.smtpSecure ?? true, onChange: boolField('smtpSecure') }) }), _jsx("td", { style: { ...cell, ...muted }, children: "\u52FE\u9009\u8868\u793A\u4F7F\u7528 SSL/TLS\u3002" })] }), _jsxs("tr", { children: [_jsx("td", { style: cell, children: "\u767B\u5F55\u7528\u6237\u540D" }), _jsx("td", { style: cell, children: _jsx("input", { style: input, value: mailbox?.username ?? '', onChange: textField('username') }) }), _jsx("td", { style: { ...cell, ...muted }, children: "\u7559\u7A7A\u65F6\u4F7F\u7528\u90AE\u7BB1\u5730\u5740\u3002" })] }), _jsxs("tr", { children: [_jsx("td", { style: cell, children: "\u5BC6\u7801" }), _jsx("td", { style: cell, children: _jsx("input", { style: input, type: "password", value: password, placeholder: snapshot?.passwordSet ? '已设置,留空表示不修改' : '未设置', onChange: event => setPassword(event.target.value) }) }), _jsx("td", { style: { ...cell, ...muted }, children: "\u4FDD\u5B58\u540E\u53EA\u663E\u793A\u201C\u5DF2\u8BBE\u7F6E\u201D\uFF0C\u4E0D\u4F1A\u56DE\u663E\u660E\u6587\u3002" })] })] })] }), _jsxs("div", { style: { ...row, marginTop: 10 }, children: [_jsx("button", { style: primary, disabled: busy !== null, onClick: () => void saveMailbox(), children: busy === 'mailbox' ? '保存中…' : '保存邮箱绑定' }), _jsx("button", { style: button, disabled: busy !== null, onClick: () => void testConnection(), children: busy === 'test' ? '测试中…' : '测试连接' }), _jsx("span", { style: muted, children: "\u4FDD\u5B58\u90AE\u7BB1\u7ED1\u5B9A\u65F6\u4F1A\u6309\u65B0\u914D\u7F6E\u91CD\u5EFA\u8FDE\u63A5\u3002" })] })] }), _jsxs("div", { children: [_jsx("h3", { style: { margin: '0 0 8px', fontSize: 15 }, children: "\u767D\u540D\u5355" }), _jsxs("div", { style: { ...row, marginBottom: 8 }, children: [_jsx("input", { style: { ...input,Line truncated
|
||||
const textRow = (label, key) => (_jsxs("tr", { children: [_jsx("td", { style: { ...cell, width: 110 }, children: label }), _jsx("td", { style: cell, children: _jsx("input", { style: input, value: mailbox?.[key] ?? '', onChange: textField(key) }) })] }));
|
||||
const portRow = (label, key) => (_jsxs("tr", { children: [_jsx("td", { style: { ...cell, width: 110 }, children: label }), _jsx("td", { style: cell, children: _jsx("input", { style: input, value: mailbox?.[key] ?? 0, onChange: numberField(key) }) })] }));
|
||||
const tlsRow = (label, key) => (_jsxs("tr", { children: [_jsx("td", { style: { ...cell, width: 110 }, children: label }), _jsx("td", { style: cell, children: _jsx("input", { type: "checkbox", checked: mailbox?.[key] ?? true, onChange: boolField(key) }) })] }));
|
||||
return (_jsxs("div", { style: section, children: [_jsx(StatusCard, { status: snapshot?.status, allowedCount: allowed.length, test: test }), _jsxs("div", { children: [_jsx("h3", { style: { margin: '0 0 8px', fontSize: 15 }, children: "\u90AE\u7BB1\u7ED1\u5B9A" }), _jsx("table", { style: table, children: _jsxs("tbody", { children: [textRow('邮箱地址', 'email'), textRow('显示名', 'displayName'), textRow('IMAP 主机', 'imapHost'), portRow('IMAP 端口', 'imapPort'), tlsRow('IMAP TLS', 'imapSecure'), textRow('SMTP 主机', 'smtpHost'), portRow('SMTP 端口', 'smtpPort'), tlsRow('SMTP TLS', 'smtpSecure'), textRow('登录用户名', 'username'), _jsxs("tr", { children: [_jsx("td", { style: { ...cell, width: 110 }, children: "\u5BC6\u7801" }), _jsx("td", { style: cell, children: _jsx("input", { style: input, type: "password", value: password, placeholder: snapshot?.passwordSet ? '已设置,留空表示不修改' : '未设置', onChange: event => setPassword(event.target.value) }) })] })] }) }), _jsxs("div", { style: { ...row, marginTop: 10 }, children: [_jsx("button", { style: primary, disabled: busy !== null, onClick: () => void saveMailbox(), children: busy === 'mailbox' ? '保存中…' : '保存邮箱绑定' }), _jsx("button", { style: button, disabled: busy !== null, onClick: () => void testConnection(), children: busy === 'test' ? '测试中…' : '测试连接' })] })] }), _jsxs("div", { children: [_jsx("h3", { style: { margin: '0 0 8px', fontSize: 15 }, children: "\u767D\u540D\u5355" }), _jsxs("div", { style: { ...row, marginBottom: 8 }, children: [_jsx("input", { style: { ...input, maxWidth: 320 }, value: entry, placeholder: "\u8F93\u5165\u90AE\u7BB1\u5730\u5740\u540E\u56DE\u8F66\u6DFB\u52A0", onChange: event => setEntry(event.target.value), onKeyDown: onEntryKey }), _jsx("button", { style: button, onClick: addAddress, children: "\u6DFB\u52A0" })] }), _jsx("table", { style: table, children: _jsx("tbody", { children: allowed.length === 0 ? (_jsx("tr", { children: _jsx("td", { style: { ...cell, ...muted }, children: "\u6682\u65E0\u5730\u5740\uFF0C\u767D\u540D\u5355\u4E3A\u7A7A\u65F6\u4E0D\u4F1A\u63A5\u6536\u4EFB\u4F55\u90AE\u4EF6\u3002" }) })) : (allowed.map(address => (_jsxs("tr", { children: [_jsx("td", { style: cell, children: address }), _jsx("td", { style: { ...cell, width: 80, textAlign: 'right' }, children: _jsx("button", { style: button, onClick: () => removeAddress(address), children: "\u5220\u9664" }) })] }, address)))) }) }), _jsx("div", { style: { ...row, marginTop: 10 }, children: _jsx("button", { style: primary, disabled: busy !== null, onClick: () => void saveWhitelist(), children: busy === 'whitelist' ? '保存中…' : '保存白名单' }) })] }), _jsxs("div", { children: [_jsx("h3", { style: { margin: '0 0 8px', fontSize: 15 }, children: "\u8FD0\u884C\u65E5\u5FD7" }), _jsx("pre", { ref: logRef, style: logBox, children: snapshot?.logs?.length ? snapshot.logs.join('\n') : '暂无日志' })] }), notice ? (_jsx("div", { style: { color: notice.kind === 'ok' ? '#047857' : '#b91c1c', fontSize: 13 }, children: notice.text })) : null] }));
|
||||
}
|
||||
Reference in new issue
Block a user