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, fetchWeeklyMailSends, fetchWeeklyMailSettings, saveWeeklyMailSettings, sendWeeklyMailTest, uploadWeeklyMailImage, weeklyMailWeekdayLabels, } from './qaWeeklyMail' import type { WeeklyMailConfirmation, WeeklyMailSend, WeeklyMailSettings } from './qaWeeklyMail' import './qaWeeklyMail.css' const stamp = (value: string) => (value ? new Date(value).toLocaleString('zh-CN', { hour12: false }) : '—') export function QAWeeklyMail({ week, busy }: { week: string; busy: boolean }) { const auth = useOptionalAuth() const admin = auth?.identity?.role === 'admin' const [confirmation, setConfirmation] = useState(null) const [settings, setSettings] = useState(null) const [sends, setSends] = useState([]) const [error, setError] = useState('') const [notice, setNotice] = useState('') 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('') try { const [nextConfirmation, nextSends, nextSettings] = await Promise.all([ fetchWeeklyMailConfirmation(week), fetchWeeklyMailSends(week), admin ? fetchWeeklyMailSettings() : Promise.resolve(null), ]) setConfirmation(nextConfirmation) setSends(nextSends) if (nextSettings) setSettings(nextSettings) } catch (reason) { setError(reason instanceof Error ? reason.message : '周报邮件状态读取失败') } }, [week, admin]) useEffect(() => { void reload() }, [reload]) const toggleConfirm = async (confirm: boolean) => { if (working) return setWorking(true) setError('') setNotice('') try { const next = await confirmWeeklyMail(week, confirm) setConfirmation(next) setNotice(confirm ? '已确认本周周报;到设定时间会自动发送,正文改动后需重新确认' : '已取消确认,不会自动发送') setSends(await fetchWeeklyMailSends(week)) // The chart can only be rendered by the browser, so it is generated and attached // the moment the operator confirms — no separate step for the common path. if (confirm && next?.current) { setNotice('已确认本周周报;正在生成并附上统计看板图片…') setAttaching(true) setCapture(true) } } catch (reason) { setError(reason instanceof Error ? reason.message : '确认失败') } finally { setWorking(false) } } const testSend = async () => { if (testing) return setTesting(true) setError('') setNotice('') try { const id = await sendWeeklyMailTest(week) setNotice(`测试邮件已发出(${id});成功后不影响正式发送记录`) } catch (reason) { setError(reason instanceof Error ? reason.message : '测试发送失败') } finally { 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 (

自动发送

{!confirmation ? '未确认' : stale ? `已过期(确认于第${confirmation.draft_revision}版,正文已改,需重新确认)` : `已确认 · 第${confirmation.draft_revision}版 · ${stamp(confirmation.confirmed_at)} · ${confirmation.confirmed_by}`}
{confirmation ? ( ) : ( )} {admin ? ( <> {confirmation?.has_image && !stale ? ( ) : null} ) : null}
{error ? (

{error}

) : null} {notice ?

{notice}

: null} {settings ? (

{settings.enabled ? '自动发送已启用' : '自动发送未启用'} ·{' '} {settings.host ? `${settings.host}:${settings.port}` : '未配置邮件服务器'} ·{' '} {settings.recipients.length ? `${settings.recipients.length} 位收件人` : '未配置收件人'} ·{' '} {weeklyMailWeekdayLabels[settings.weekday - 1] ?? '—'} {String(settings.hour).padStart(2, '0')}:00(北京时间) {settings.has_password ? '' : ' · 未保存授权码'}

) : null} {editing && settings ? ( { setSettings(next) setEditing(false) setNotice('发送设置已保存') }} /> ) : null} {capture ? : null} {sends.length ? (
    {sends.map((send) => (
  • {send.week} · 第{send.draft_revision}版 ·{' '} {{ sending: '发送中', sent: '已发送', failed: '发送失败' }[send.status]} · {stamp(send.updated_at)} {send.last_error ? ` · ${send.last_error}` : ''}
  • ))}
) : null}
) } function SettingsForm({ settings, onSaved, }: { settings: WeeklyMailSettings onSaved: (next: WeeklyMailSettings) => void }) { const [host, setHost] = useState(settings.host) const [port, setPort] = useState(String(settings.port)) const [username, setUsername] = useState(settings.username) const [password, setPassword] = useState('') 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 [review, setReview] = useState(settings.review_address) const [weekday, setWeekday] = useState(settings.weekday) const [hour, setHour] = useState(String(settings.hour)) const [enabled, setEnabled] = useState(settings.enabled) const [error, setError] = useState('') const [saving, setSaving] = useState(false) const submit = async () => { setSaving(true) setError('') try { const next = await saveWeeklyMailSettings(settings.revision, { host: host.trim(), port: Number(port), username: username.trim(), password, clear_password: clearPassword, from: from.trim(), recipients: splitAddresses(recipients), cc_recipients: splitAddresses(cc), review_address: review.trim(), weekday, hour: Number(hour), enabled, }) setPassword('') onSaved(next) } catch (reason) { setError(reason instanceof Error ? reason.message : '保存失败') } finally { setSaving(false) } } return (
{settings.has_password ? ( ) : null}