260 lines
9.9 KiB
TypeScript
260 lines
9.9 KiB
TypeScript
import { useDeferredValue, useEffect, useRef, useState, useSyncExternalStore } from 'react'
|
||
import type { QAQuery } from './qaOps'
|
||
import { currentQAWeekValue, qaDateTimeToISO, qaWeekQueryRange } from './qaWeeks'
|
||
import { WeeklyDraftEditorModel } from './qaWeeklyDrafts'
|
||
import { weeklyTransport } from './qaWeeklyDraftAPI'
|
||
import { weeklyQualitySourceLabel } from './qaWeekly'
|
||
import { QAAnswerMarkdown } from './QAAnswerMarkdown'
|
||
import { copyRenderedWeeklyReport } from './qaWeeklyClipboard'
|
||
import { QAWeeklyMail } from './QAWeeklyMail'
|
||
import { QAPeriodInput } from './QAPeriodInput'
|
||
import './qaWeeklyEditor.css'
|
||
type LeaveGuard = (() => Promise<boolean>) | null
|
||
interface Props {
|
||
query?: QAQuery | null
|
||
onBeforeLeave?: (guard: LeaveGuard) => void
|
||
}
|
||
export function QAWeekly({ onBeforeLeave }: Props) {
|
||
const [model] = useState(() => new WeeklyDraftEditorModel(currentQAWeekValue(), weeklyTransport))
|
||
const state = useSyncExternalStore(model.subscribe, model.getSnapshot, model.getSnapshot)
|
||
const [replaceConfirm, setReplaceConfirm] = useState(false),
|
||
[reloadConfirm, setReloadConfirm] = useState(false),
|
||
[periodReset, setPeriodReset] = useState(0)
|
||
const [copying, setCopying] = useState(false)
|
||
const [copyFeedback, setCopyFeedback] = useState<{ error: boolean; message: string } | null>(null)
|
||
const copyBody = useRef<HTMLDivElement>(null)
|
||
const copyVersion = useRef(0)
|
||
useEffect(() => {
|
||
copyVersion.current += 1
|
||
setCopyFeedback(null)
|
||
}, [state.text, state.week])
|
||
const preview = useDeferredValue(state.text)
|
||
useEffect(() => {
|
||
void model.initialize()
|
||
const unload = (e: BeforeUnloadEvent) => {
|
||
if (model.dirty || model.uncertainPrefill) {
|
||
e.preventDefault()
|
||
e.returnValue = ''
|
||
}
|
||
}
|
||
window.addEventListener('beforeunload', unload)
|
||
return () => {
|
||
window.removeEventListener('beforeunload', unload)
|
||
if (model.dirty && !model.uncertainPrefill) void model.beforeLeave().finally(() => model.dispose())
|
||
else model.dispose()
|
||
}
|
||
}, [model])
|
||
useEffect(() => {
|
||
onBeforeLeave?.(() => model.beforeLeave())
|
||
return () => onBeforeLeave?.(null)
|
||
}, [model, onBeforeLeave])
|
||
const blocked = state.busy || state.loading || !state.document || model.uncertainPrefill
|
||
const status = state.loading
|
||
? 'loading'
|
||
: state.saving
|
||
? 'saving'
|
||
: state.busy
|
||
? 'working'
|
||
: state.error
|
||
? 'error'
|
||
: model.dirty
|
||
? 'dirty'
|
||
: state.document?.revision
|
||
? 'saved'
|
||
: 'empty'
|
||
const source = state.document?.seed
|
||
const generate = () => {
|
||
setReplaceConfirm(false)
|
||
setReloadConfirm(false)
|
||
void model.prefill()
|
||
}
|
||
const copy = async () => {
|
||
if (copying) return
|
||
const source = copyBody.current?.querySelector<HTMLElement>('.qa-answer-markdown')
|
||
if (!source) return
|
||
const version = copyVersion.current
|
||
setCopying(true)
|
||
setCopyFeedback(null)
|
||
try {
|
||
await copyRenderedWeeklyReport(source)
|
||
if (version === copyVersion.current) setCopyFeedback({ error: false, message: '已复制排版内容' })
|
||
} catch (reason) {
|
||
if (version === copyVersion.current)
|
||
setCopyFeedback({
|
||
error: true,
|
||
message: reason instanceof Error ? reason.message : '复制未成功,请在预览中选中文字后复制。',
|
||
})
|
||
} finally {
|
||
setCopying(false)
|
||
}
|
||
}
|
||
return (
|
||
<section className="qa-weekly qa-weekly-editor" aria-label="周报生成" data-weekly-week={state.week}>
|
||
<header className="qa-weekly-editor-header">
|
||
<h3>周报草稿</h3>
|
||
<QAPeriodInput
|
||
key={`${state.week}:${periodReset}`}
|
||
kind="week"
|
||
label="周报周次"
|
||
ariaLabel="周报周次"
|
||
value={state.week}
|
||
max={currentQAWeekValue()}
|
||
disabled={state.busy || state.loading}
|
||
onChange={(week) => {
|
||
setReplaceConfirm(false)
|
||
setReloadConfirm(false)
|
||
void model.switchWeek(week).then((ok) => {
|
||
if (!ok) setPeriodReset((v) => v + 1)
|
||
})
|
||
}}
|
||
/>
|
||
<div className="qa-weekly-editor-actions">
|
||
{replaceConfirm ? (
|
||
<>
|
||
<button type="button" className="qa-weekly-prefill" onClick={() => setReplaceConfirm(false)}>
|
||
取消
|
||
</button>
|
||
<button type="button" onClick={generate}>
|
||
确认替换预填
|
||
</button>
|
||
</>
|
||
) : (
|
||
<button
|
||
type="button"
|
||
className="qa-weekly-prefill"
|
||
disabled={blocked || state.errorKind === 'conflict'}
|
||
onClick={() => {
|
||
if (state.document?.revision || state.text) setReplaceConfirm(true)
|
||
else generate()
|
||
}}
|
||
>
|
||
{state.document?.revision ? '重新预填数据' : '生成并预填草稿'}
|
||
</button>
|
||
)}
|
||
<button
|
||
type="button"
|
||
disabled={blocked || !model.dirty || state.saving || state.errorKind === 'conflict'}
|
||
onClick={() => void model.beforeLeave()}
|
||
>
|
||
立即保存
|
||
</button>
|
||
<button type="button" disabled={blocked || !state.text.trim() || copying} onClick={() => void copy()}>
|
||
{copying ? '正在复制…' : '复制排版内容'}
|
||
</button>
|
||
{copyFeedback ? <span role={copyFeedback.error ? 'alert' : 'status'}>{copyFeedback.message}</span> : null}
|
||
</div>
|
||
</header>
|
||
<QAWeeklyMail week={state.week} busy={blocked} />
|
||
<div className="qa-weekly-editor-toolbar">
|
||
<span role="status" aria-live="polite" className="qa-weekly-editor-status" data-weekly-save-state={status}>
|
||
自动保存 ·{' '}
|
||
{
|
||
{
|
||
loading: '正在读取草稿…',
|
||
saving: '保存中…',
|
||
working: '正在处理…',
|
||
error: '未保存或尚未确认',
|
||
dirty: '待保存',
|
||
saved: '已保存',
|
||
empty: '尚未创建',
|
||
}[status]
|
||
}
|
||
{status === 'saved' && state.document?.updated_at
|
||
? ` · ${new Date(state.document.updated_at).toLocaleTimeString('zh-CN', { timeZone: 'Asia/Shanghai', hour12: false })}`
|
||
: ''}
|
||
</span>
|
||
</div>
|
||
{replaceConfirm ? (
|
||
<p className="qa-ops-warning">将先保存当前文字,再用最新统计替换正文;旧保存版本保留。请确认后继续。</p>
|
||
) : null}
|
||
{state.error ? (
|
||
<div role="alert" className="error-banner">
|
||
{state.error}
|
||
{state.errorKind === 'load' ? (
|
||
<button type="button" disabled={state.busy} onClick={() => void model.reload()}>
|
||
重试读取
|
||
</button>
|
||
) : state.errorKind === 'save' ? (
|
||
<button type="button" disabled={state.saving || state.busy} onClick={() => void model.beforeLeave()}>
|
||
重试保存
|
||
</button>
|
||
) : state.errorKind === 'prefill' ? (
|
||
<button type="button" disabled={state.busy} onClick={generate}>
|
||
重试确认预填
|
||
</button>
|
||
) : null}
|
||
{reloadConfirm ? (
|
||
<>
|
||
<button type="button" className="qa-weekly-reload" onClick={() => setReloadConfirm(false)}>
|
||
取消
|
||
</button>
|
||
<button
|
||
type="button"
|
||
disabled={state.saving || state.busy}
|
||
onClick={() => {
|
||
setReloadConfirm(false)
|
||
void model.reload()
|
||
}}
|
||
>
|
||
放弃本地修改并读取
|
||
</button>
|
||
</>
|
||
) : (
|
||
<button
|
||
type="button"
|
||
className="qa-weekly-reload"
|
||
disabled={state.busy || state.saving}
|
||
onClick={() => setReloadConfirm(true)}
|
||
>
|
||
重新读取服务器版本
|
||
</button>
|
||
)}
|
||
</div>
|
||
) : null}
|
||
<div ref={copyBody} hidden aria-hidden="true">
|
||
<QAAnswerMarkdown answer={state.text} />
|
||
</div>
|
||
<div className="qa-weekly-editor-layout" data-mode="both">
|
||
<div className="qa-weekly-writing">
|
||
<label htmlFor="qa-weekly-body">
|
||
周报草稿正文 <small>Markdown</small>
|
||
</label>
|
||
<textarea
|
||
id="qa-weekly-body"
|
||
aria-label="周报草稿正文"
|
||
value={state.text}
|
||
disabled={blocked}
|
||
spellCheck={false}
|
||
placeholder="生成并预填草稿,或直接输入。英文完整部分在上,中文在下;输入后自动保存。"
|
||
onChange={(e) => model.setText(e.target.value)}
|
||
onKeyDown={(e) => {
|
||
if ((e.ctrlKey || e.metaKey) && e.key.toLowerCase() === 's') {
|
||
e.preventDefault()
|
||
void model.beforeLeave()
|
||
}
|
||
}}
|
||
/>
|
||
</div>
|
||
<article className="qa-weekly-preview" aria-label="周报预览">
|
||
{preview ? <QAAnswerMarkdown answer={preview} /> : <p className="qa-ops-muted">尚无草稿正文。</p>}
|
||
</article>
|
||
</div>
|
||
{source && source.source === 'governance-master' ? (
|
||
<details className="qa-weekly-source">
|
||
<summary>预填来源</summary>
|
||
<p>
|
||
治理正式总表 · {String(source.week)} · 截至{' '}
|
||
{typeof source.end === 'string'
|
||
? new Date(source.end).toLocaleString('zh-CN', { timeZone: 'Asia/Shanghai', hour12: false })
|
||
: '—'}{' '}
|
||
· {weeklyQualitySourceLabel(source.quality_source)}
|
||
</p>
|
||
<p>
|
||
问题总数与新增是可编辑的系统统计建议;区域采用已保存配置及当前通讯录。未做新的FAQ调整,重点与英文文字请自行填写。草稿不会自动发送。
|
||
</p>
|
||
</details>
|
||
) : null}
|
||
</section>
|
||
)
|
||
}
|