feat(qa): 周报邮件区分主送抄送并附统计看板图片
build / build (push) Successful in 1m21s

设置新增抄送列表,MIME 增加 Cc 头、envelope 同时投递主送与抄送,同一地址不得同时出现在两边。
确认后可保存统计看板 PNG(multipart/related + cid),发送时放在正文末尾内联显示;正文改动使确认失效后不再使用旧图。
This commit is contained in:
hermes
2026-10-26 15:59:06 +08:00
parent 600e9a2dc2
commit 822fb652ff
13 changed files with 325 additions and 25 deletions
+95 -5
View File
@@ -1,5 +1,9 @@
import { useCallback, useEffect, useState } from 'react'
import { useOptionalAuth } from './auth'
import { QADashboardView } from './QADashboard'
import { fetchQADashboard } from './qaDashboardData'
import { createDashboardPNG } from './qaDashboardExport'
import { qaDateTimeToISO, qaWeekQueryRange } from './qaWeeks'
import {
confirmWeeklyMail,
fetchWeeklyMailConfirmation,
@@ -7,6 +11,7 @@ import {
fetchWeeklyMailSettings,
saveWeeklyMailSettings,
sendWeeklyMailTest,
uploadWeeklyMailImage,
weeklyMailWeekdayLabels,
} from './qaWeeklyMail'
import type { WeeklyMailConfirmation, WeeklyMailSend, WeeklyMailSettings } from './qaWeeklyMail'
@@ -25,6 +30,8 @@ export function QAWeeklyMail({ week, busy }: { week: string; busy: boolean }) {
const [working, setWorking] = useState(false)
const [editing, setEditing] = useState(false)
const [testing, setTesting] = useState(false)
const [capture, setCapture] = useState(false)
const [attaching, setAttaching] = useState(false)
const reload = useCallback(async () => {
setError('')
@@ -77,6 +84,30 @@ export function QAWeeklyMail({ week, busy }: { week: string; busy: boolean }) {
setTesting(false)
}
}
const attachDashboard = () => {
if (attaching || !confirmation) return
setError('')
setNotice('')
setAttaching(true)
setCapture(true)
}
const uploadCaptured = async (png: Blob) => {
try {
const bytes = await uploadWeeklyMailImage(week, png)
setNotice(`已附上统计看板图片(${Math.round(bytes / 1024)} KB),发送时放在正文末尾`)
await reload()
} catch (reason) {
setError(reason instanceof Error ? reason.message : '附图上传失败')
} finally {
setCapture(false)
setAttaching(false)
}
}
const failCaptured = (reason: unknown) => {
setError(reason instanceof Error ? reason.message : '看板图片生成失败')
setCapture(false)
setAttaching(false)
}
const stale = confirmation !== null && !confirmation.current
return (
<section className="qa-weekly-mail" aria-label="周报自动发送">
@@ -104,6 +135,9 @@ export function QAWeeklyMail({ week, busy }: { week: string; busy: boolean }) {
<button type="button" disabled={working || testing} onClick={() => void testSend()}>
{testing ? '正在发送测试…' : '发送测试邮件'}
</button>
<button type="button" disabled={working || attaching || !confirmation || stale} onClick={attachDashboard}>
{attaching ? '正在生成图片…' : confirmation?.has_image ? '重新附看板图片' : '附上统计看板图片'}
</button>
<button type="button" disabled={working} onClick={() => setEditing(!editing)}>
{editing ? '收起发送设置' : '发送设置'}
</button>
@@ -136,6 +170,7 @@ export function QAWeeklyMail({ week, busy }: { week: string; busy: boolean }) {
}}
/>
) : null}
{capture ? <DashboardCapture week={week} onDone={uploadCaptured} onError={failCaptured} /> : null}
{sends.length ? (
<ul className="qa-weekly-mail-sends">
{sends.map((send) => (
@@ -165,6 +200,7 @@ function SettingsForm({
const [clearPassword, setClearPassword] = useState(false)
const [from, setFrom] = useState(settings.from)
const [recipients, setRecipients] = useState(settings.recipients.join('\n'))
const [cc, setCc] = useState(settings.cc_recipients.join('\n'))
const [weekday, setWeekday] = useState(settings.weekday)
const [hour, setHour] = useState(String(settings.hour))
const [enabled, setEnabled] = useState(settings.enabled)
@@ -182,10 +218,8 @@ function SettingsForm({
password,
clear_password: clearPassword,
from: from.trim(),
recipients: recipients
.split(/[\n,,;;]/)
.map((item) => item.trim())
.filter(Boolean),
recipients: splitAddresses(recipients),
cc_recipients: splitAddresses(cc),
weekday,
hour: Number(hour),
enabled,
@@ -237,9 +271,13 @@ function SettingsForm({
<input value={from} onChange={(e) => setFrom(e.target.value)} placeholder="kb@example.com" />
</label>
<label className="qa-weekly-mail-wide">
收件人(每行一个)
主送(每行一个)
<textarea rows={3} value={recipients} onChange={(e) => setRecipients(e.target.value)} />
</label>
<label className="qa-weekly-mail-wide">
抄送(每行一个,可留空)
<textarea rows={2} value={cc} onChange={(e) => setCc(e.target.value)} />
</label>
<label>
发送日
<select value={weekday} onChange={(e) => setWeekday(Number(e.target.value))}>
@@ -272,3 +310,55 @@ function SettingsForm({
</div>
)
}
const splitAddresses = (value: string) =>
value
.split(/[\n,,;;]/)
.map((item) => item.trim())
.filter(Boolean)
// The chart is rendered off-screen from the same read-only dashboard data and
// attached to the confirmation, so the mail always shows what the operator saw.
function DashboardCapture({
week,
onDone,
onError,
}: {
week: string
onDone: (png: Blob) => void
onError: (reason: unknown) => void
}) {
const [data, setData] = useState<Awaited<ReturnType<typeof fetchQADashboard>> | null>(null)
const [error, setError] = useState('')
useEffect(() => {
const range = qaWeekQueryRange(Number(week.slice(0, 4)), Number(week.slice(6)))
void fetchQADashboard({
start: qaDateTimeToISO(range.start),
end: qaDateTimeToISO(range.end),
keyword: '',
cursor: '',
})
.then(setData)
.catch((reason) => {
setError(reason instanceof Error ? reason.message : '看板读取失败')
onError(reason)
})
}, [week, onError])
useEffect(() => {
if (!data) return
const timer = window.setTimeout(() => {
const svg = document.querySelector<SVGSVGElement>('#qa-dashboard-capture svg')
if (!svg) {
onError(new Error('看板尚未渲染完成,请重试'))
return
}
void createDashboardPNG(svg).then(onDone).catch(onError)
}, 600)
return () => window.clearTimeout(timer)
}, [data, onDone, onError])
return (
<div id="qa-dashboard-capture" aria-hidden="true" style={{ position: 'fixed', left: -10000, top: 0, width: 1280 }}>
<QADashboardView data={data} loading={!data && !error} error={error} onRetry={() => {}} />
</div>
)
}
+1
View File
@@ -9,6 +9,7 @@ const settings = {
username: 'kb@example.com',
from: 'kb@example.com',
recipients: ['a@example.com'],
cc_recipients: [],
weekday: 1,
hour: 9,
enabled: true,
+22
View File
@@ -6,6 +6,7 @@ export interface WeeklyMailSettings {
username: string
from: string
recipients: string[]
cc_recipients: string[]
weekday: number
hour: number
enabled: boolean
@@ -17,6 +18,7 @@ export interface WeeklyMailSettings {
export interface WeeklyMailConfirmation {
week: string
has_image: boolean
draft_revision: number
body_sha256: string
confirmed_by: string
@@ -45,6 +47,8 @@ export function parseWeeklyMailSettings(value: unknown): WeeklyMailSettings {
const v = obj(value)
const recipients = v.recipients === null ? [] : v.recipients
if (!Array.isArray(recipients) || recipients.some((item) => typeof item !== 'string')) return bad()
const cc = v.cc_recipients === null || v.cc_recipients === undefined ? [] : v.cc_recipients
if (!Array.isArray(cc) || cc.some((item) => typeof item !== 'string')) return bad()
if (typeof v.enabled !== 'boolean' || typeof v.has_password !== 'boolean') return bad()
const weekday = num(v.weekday),
hour = num(v.hour),
@@ -56,6 +60,7 @@ export function parseWeeklyMailSettings(value: unknown): WeeklyMailSettings {
username: str(v.username),
from: str(v.from),
recipients: recipients as string[],
cc_recipients: cc as string[],
weekday,
hour,
enabled: v.enabled,
@@ -77,6 +82,7 @@ export function parseWeeklyMailConfirmation(value: unknown, week: string): Weekl
confirmed_by: str(v.confirmed_by),
confirmed_at: str(v.confirmed_at),
current: v.current,
has_image: v.has_image === true,
}
}
@@ -123,6 +129,7 @@ export async function saveWeeklyMailSettings(
clear_password: form.clear_password,
from: form.from,
recipients: form.recipients,
cc_recipients: form.cc_recipients,
weekday: form.weekday,
hour: form.hour,
enabled: form.enabled,
@@ -162,3 +169,18 @@ export async function sendWeeklyMailTest(week: string): Promise<string> {
if (typeof payload.message_id !== 'string' || !payload.message_id) bad()
return payload.message_id
}
export async function uploadWeeklyMailImage(week: string, png: Blob): Promise<number> {
const buffer = await png.arrayBuffer()
let binary = ''
const bytes = new Uint8Array(buffer)
for (let index = 0; index < bytes.length; index += 0x8000)
binary += String.fromCharCode(...bytes.subarray(index, index + 0x8000))
const payload = (await getPayload(endpoint('confirmations', week) + '/image', undefined, {
method: 'POST',
headers,
body: JSON.stringify({ png_base64: btoa(binary) }),
})) as { bytes: number }
if (typeof payload.bytes !== 'number' || payload.bytes <= 0) bad()
return payload.bytes
}