mirror of
https://git.zhongyuan.online/ytke/tap.git
synced 2026-11-24 22:15:18 -08:00
55 lines
15 KiB
TypeScript
55 lines
15 KiB
TypeScript
'use client';
|
||
import { useEffect, useRef, useState } from 'react';
|
||
import { X, PencilLine, GitBranch, Download, ArrowRight, CircleAlert, CheckCircle2, Clock3, Info } from 'lucide-react';
|
||
import { Dialog, DialogContent, DialogTitle, DialogDescription } from '@/components/ui/dialog';
|
||
import { type Demand, statuses } from '@/lib/demand';
|
||
import { AlertDialog, AlertDialogContent, AlertDialogTitle, AlertDialogDescription, AlertDialogCancel, AlertDialogAction } from '@/components/ui/alert-dialog';
|
||
import { AttachmentList } from './attachments';
|
||
import { DemandReviewPanel } from './review';
|
||
import { reviewStage } from '@/lib/demand-review';
|
||
import { Status } from './dashboard';
|
||
import { WorkflowPanel } from './workflow';
|
||
import { planConfirmed, latestRelease } from '@/lib/workflow';
|
||
import { type SaveInput } from './form';
|
||
const date=(s:string)=>new Date(s).toLocaleString('zh-CN',{year:'numeric',month:'2-digit',day:'2-digit',hour:'2-digit',minute:'2-digit',hour12:false,timeZone:'Asia/Shanghai'});
|
||
function FieldView({label,value}:{label:string;value:string}){return <div className="detail-field"><span>{label}</span><p>{value||'待补充'}</p></div>}
|
||
export function DemandDetail({d,onClose,onEdit,onAction,busy,error,initialTab,onTabChange}:{initialTab?:string;onTabChange?:(tab:string)=>void;d:Demand;onClose:()=>void;onEdit:(d:Demand)=>void;onAction:(body:SaveInput)=>Promise<void>;busy:boolean;error:string}){
|
||
const [tab,setTab]=useState(initialTab??(d.status==='待评审'&&d.reviewReady?'需求评审':['已排期','开发中','待验收','待发布','已上线','已完成'].includes(d.status)?'交付流程':'需求详情'));const [reviewDirty,setReviewDirty]=useState(false);const [reviewExit,setReviewExit]=useState<{tab?:string}|null>(null);const [mode,setMode]=useState('');const [content,setContent]=useState('');const [reason,setReason]=useState('');const [results,setResults]=useState<string[]>(d.acceptance?.items.map(()=> '待测')??[]);const [notes,setNotes]=useState('');const [confirm,setConfirm]=useState(false);const [localError,setLocalError]=useState('');
|
||
const errorRef=useRef<HTMLDivElement>(null);
|
||
useEffect(()=>{if(error||localError)errorRef.current?.scrollIntoView({block:'nearest'});},[error,localError]);
|
||
const business=d.access?.business??true;
|
||
function leaveReview(nextTab?:string){setReviewDirty(false);if(nextTab){setTab(nextTab);onTabChange?.(nextTab);setMode('');setLocalError('')}else onClose();}
|
||
function requestLeave(nextTab?:string){if(busy)return;if(reviewDirty)setReviewExit({tab:nextTab});else leaveReview(nextTab);}
|
||
const f=d.fields;const current=statuses.indexOf(d.status as typeof statuses[number]);const emergency=['已上线','已完成'].includes(d.status)?latestRelease(d)?.emergency:undefined;
|
||
async function submit(){setLocalError('');if(mode==='change'&&(!content.trim()||!reason.trim())){setLocalError('请填写变更内容与原因。');return;}if(mode==='cancel'&&!reason.trim()){setLocalError('请填写取消需求的原因。');return;}if(mode==='accept'&&(results.some(r=>r==='待测')||!notes.trim()||!confirm)){setLocalError('请逐项完成验收、填写遗留问题及处理计划,并明确确认结论。');return;}
|
||
try{await onAction({action:mode,id:d.id,revision:d.revision,content,reason,results,notes,confirm,testVersion:d.acceptance?.testVersion,version:f.version,round:d.acceptance?.round});setMode('');setContent('');setReason('');}catch{}}
|
||
function exportRecord(){const lines=[`# ${d.id} · ${f.title}`,`状态:${d.status} | 文档版本:${f.version}`,`需求负责人:${f.owner} | 登记人:${f.proposer}`,`\n## 需求内容`,...Object.entries(f).map(([k,v])=>`${k}: ${v}`),'\n## 过程记录',...d.history.map(h=>`${date(h.time)} ${h.actor} ${h.action}\n${h.detail}`),'\n## 完整记录(含验收与变更)','```json',JSON.stringify(d,null,2),'```'];const url=URL.createObjectURL(new Blob([lines.join('\n')],{type:'text/markdown;charset=utf-8'}));const a=document.createElement('a');a.href=url;a.download=d.id+'.md';a.click();setTimeout(()=>URL.revokeObjectURL(url),1000);}
|
||
return <><Dialog open onOpenChange={open=>{if(!open&&!busy)requestLeave()}}><DialogContent className="demand-modal detail-modal" showCloseButton={false} onInteractOutside={e=>{if(mode||reviewDirty)e.preventDefault()}}><div className="detail-heading"><div className="detail-eyeline"><span>{d.id}</span><Status d={d}/><button className="icon-button" aria-label="关闭需求详情" onClick={()=>requestLeave()} disabled={busy}><X size={20}/></button></div><DialogTitle className="detail-title">{f.title}</DialogTitle><DialogDescription className="detail-description">{f.project} / {f.module} <span>·</span>{f.department} <span>·</span>负责人:{f.owner}</DialogDescription></div>
|
||
<div className="detail-progress">{statuses.map((s,i)=>{const done=i<current&&(s!=='待验收'||!emergency||emergency.followUp?.approved);return <div key={s} className={i===current?'current':done?'done':''}><span>{done?<CheckCircle2 size={15}/>:i+1}</span><b>{emergency&&s==='待验收'?'业务补验':emergency&&s==='待发布'?'紧急发布':s}</b></div>})}</div>
|
||
<div className="tabs detail-tabs" role="tablist" aria-label="需求详情栏目">{['需求详情','需求评审','交付流程','业务验收','缺陷','需求变更','进度记录'].map(t=><button role="tab" aria-selected={tab===t} className={tab===t?'selected':''} key={t} disabled={busy} onClick={()=>{if(t!==tab)requestLeave(t)}}>{t}{t==='需求变更'&&d.changes.length>0&&<small>{d.changes.length}</small>}</button>)}</div>
|
||
<div className="modal-scroll"><div className="detail-content">
|
||
{(error||localError)&&<div ref={errorRef} role="alert" className="error-banner"><CircleAlert size={17}/>{error||localError}</div>}
|
||
{d.access&&!d.access.business&&!d.access.developer&&<p className="info-banner admin-readonly-notice"><Info size={17}/>{d.access.canTakeOverDevelopment?'原开发账号已停用或不可用,可在“需求评审”中接手开发职责。接手前仅可查看需求及附件。':'管理员查看模式:可查看需求、评审记录和附件,修改及确认由对应责任人操作。'}</p>}
|
||
{mode==='change'||mode==='cancel'?<div className="action-form"><h2>{mode==='change'?'提出需求变更':'确认取消需求'}</h2><p>{mode==='change'?'记录希望调整的内容,开发负责人评估影响、双方确认后,再更新范围与计划。':'取消后会保留需求及过程记录。请填写明确的业务原因。'}</p>{mode==='change'&&<label className="form-field"><span>变更内容<em>*</em></span><textarea rows={4} value={content} onChange={e=>{setContent(e.target.value);setReviewDirty(true)}} maxLength={5000} placeholder="当前内容是什么?希望调整为什么?"/></label>}<label className="form-field"><span>{mode==='change'?'变更原因':'取消原因'}<em>*</em></span><textarea rows={3} value={reason} onChange={e=>{setReason(e.target.value);setReviewDirty(true)}} maxLength={3000} placeholder="请说明业务背景及必要性"/></label></div>:<>
|
||
{tab==='需求详情'&&<><div className="detail-overview"><FieldView label="建议优先级" value={f.priority+' · '+(f.priority==='P0'?'紧急':f.priority==='P1'?'高':'普通')}/><FieldView label="期望上线" value={f.noFixedDate?'无固定日期':f.expectedDate}/><FieldView label="文档版本" value={f.version}/><FieldView label="登记日期" value={d.createdAt.slice(0,10)}/></div>{d.plan&&planConfirmed(d)&&<section className="plan-strip"><h3><Clock3 size={17}/>已确认交付计划</h3><div><FieldView label="开发负责人" value={d.plan.developer}/><FieldView label="计划提测" value={d.plan.testDate}/><FieldView label="计划上线" value={d.plan.releaseDate}/></div></section>}
|
||
<section className="detail-section"><h2>业务背景与目标</h2><FieldView label="业务背景及现有处理方式" value={f.background}/><FieldView label="影响对象及业务依据" value={f.impact}/><FieldView label="预期目标" value={f.goal}/><FieldView label="期望日期依据" value={f.noFixedDate?'无固定日期':f.dateReason}/></section>
|
||
<section className="detail-section"><h2>评审方案</h2><p className="muted review-solution-note">{reviewStage(d)} · <button className="text-button" onClick={()=>requestLeave('需求评审')}>查看评审与确认记录</button></p><FieldView label="实施范围与功能说明" value={f.scope||'待需求评审时提出并确认'}/><FieldView label="范围外事项" value={f.outOfScope||'待需求评审时提出并确认'}/></section>
|
||
<section className="detail-section"><h2>验收与优先级</h2><FieldView label="验收标准" value={f.criteria}/><FieldView label="优先级依据" value={f.priorityReason}/></section>
|
||
<section className="detail-section"><h2>数据与参考材料</h2><FieldView label="数据来源" value={f.noData?'本需求不涉及数据来源':f.dataSource}/>{!f.noData&&<FieldView label="数据提供人" value={f.dataContact}/>}<FieldView label="依赖与风险" value={f.risk}/><FieldView label="待确认事项" value={f.pending}/><FieldView label="参考材料与链接" value={f.references||'暂无参考链接'}/><div className="detail-field"><span>文件附件</span><AttachmentList files={d.attachments??[]}/></div><FieldView label="提出人及来源" value={f.proposer+' · '+f.source}/></section></>}
|
||
{tab==='交付流程'&&<WorkflowPanel kind="delivery" d={d} onAction={onAction} busy={busy} onDirtyChange={setReviewDirty}/>}
|
||
{tab==='缺陷'&&<WorkflowPanel kind="defects" d={d} onAction={onAction} busy={busy} onDirtyChange={setReviewDirty}/>}
|
||
{tab==='需求评审'&&<DemandReviewPanel d={d} onAction={onAction} busy={busy} onDirtyChange={setReviewDirty}/>}
|
||
{tab==='进度记录'&&<div className="timeline">{[...d.history].reverse().map((h,i)=><article key={i}><div className="timeline-dot"/><div className="timeline-meta"><h3>{h.action}</h3><time>{date(h.time)}</time></div><p>{h.detail}</p><span>{h.actor}</span></article>)}</div>}
|
||
{tab==='需求变更'&&<WorkflowPanel kind="changes" d={d} onAction={onAction} busy={busy} onDirtyChange={setReviewDirty}/>}
|
||
{tab==='业务验收'&&<>{d.acceptance?<><div className="acceptance-intro"><h2>验收清单</h2><p>文档版本 {d.acceptance.documentVersion??f.version} <span>·</span>测试版本 {d.acceptance.testVersion}{d.acceptance.withdrawnAt?' · 已撤回,请以新一轮提测为准':''}</p><FieldView label="测试环境" value={d.acceptance.environment}/><FieldView label="操作与测试数据说明" value={d.acceptance.instructions}/><div className="deadline"><Clock3 size={15}/>反馈截止时间:{d.acceptance.deadline};超时不会自动通过。</div></div>
|
||
{d.acceptance.items.map((item,i)=><div className="acceptance-item" key={item.id}><div className="acceptance-item-top"><span>{item.id}</span><strong>{item.feature}</strong>{business&&d.status==='待验收'&&<select aria-label={item.id+' 验收结果'} value={results[i]} onChange={e=>{setResults(prev=>prev.map((r,j)=>i===j?e.target.value:r));setReviewDirty(true)}}><option>待测</option><option>通过</option><option>不通过</option></select>}</div><p><span>场景/操作</span>{item.scenario}</p><p><span>预期结果</span>{item.expected}</p></div>)}
|
||
{business&&d.status==='待验收'&&<div className="acceptance-confirm"><label className="form-field"><span>遗留问题、证据/缺陷及处理计划<em>*</em></span><textarea rows={3} value={notes} onChange={e=>{setNotes(e.target.value);setReviewDirty(true)}} maxLength={5000} placeholder="无问题请填“无”。有问题请注明验收项、复现步骤、实际结果、证据及处理计划。"/></label><label className="check-field"><input type="checkbox" checked={confirm} onChange={e=>{setConfirm(e.target.checked);setReviewDirty(true)}}/><span>我已逐项核对以上版本并确认验收结果;全部通过后,同意按约定发布计划上线。</span></label><button className="btn primary" disabled={busy} onClick={()=>{if(results.some(r=>r==='待测')||!notes.trim()||!confirm){setLocalError('请逐项完成验收、填写遗留问题及处理计划,并明确确认结论。');return;}onAction({action:'accept',id:d.id,revision:d.revision,results,notes,confirm,testVersion:d.acceptance?.testVersion,version:f.version,round:d.acceptance?.round}).catch(()=>{})}}><CheckCircle2 size={17}/>{busy?'提交中…':'提交验收结论'}</button></div>}
|
||
</>:<div className="empty-state"><CheckCircle2 size={32}/><h3>还未进入业务验收</h3><p>完成技术验证并提供测试版本后,即可逐项验收。</p></div>}
|
||
{d.acceptanceRecords?.map((r,i)=><div className="acceptance-record" key={i}><h3>{r.passed?'业务验收通过':'业务验收未通过'}</h3><p>文档 {r.version} · 测试 {r.testVersion}</p><p>遗留问题及处理计划:{r.notes}</p>{r.items&&<details><summary>逐项验收结果({r.items.length})</summary>{r.items.map(item=><div className="acceptance-item" key={item.id}><div className="acceptance-item-top"><span>{item.id}</span><strong>{item.feature}</strong><b>{item.result}</b></div><p><span>场景/操作</span>{item.scenario}</p><p><span>预期结果</span>{item.expected}</p></div>)}</details>}<span>{r.actor} · {date(r.time)}</span></div>)}</>}
|
||
</>}
|
||
</div></div>
|
||
<footer className="modal-footer"><div className="footer-left"><button className="text-button muted" onClick={exportRecord}><Download size={16}/>导出记录</button>{business&&!['已上线','已完成','已取消'].includes(d.status)&&!mode&&<button className="text-button danger-text" disabled={busy||reviewDirty} onClick={()=>{setMode('cancel');setReason('')}}>取消需求</button>}</div><div className="footer-right">{mode==='change'||mode==='cancel'?<><button className="btn" onClick={()=>requestLeave(tab)} disabled={busy}>返回详情</button><button className={'btn '+(mode==='change'?'primary':'danger-button')} onClick={submit} disabled={busy}>{busy?'保存中…':mode==='change'?'提交变更':'确认取消'}</button></>:<>{business&&['草稿','待评审'].includes(d.status)&&<button className="btn" disabled={busy} onClick={()=>{if(reviewDirty){setLocalError('请先保存评审输入,或切换栏目放弃后再编辑业务信息。');return;}onEdit(d)}}><PencilLine size={16}/>{d.status==='草稿'?'继续填写':'补充信息'}</button>}{business&&['已排期','开发中','待验收','待发布','已上线'].includes(d.status)&&<button className="btn" onClick={()=>requestLeave('需求变更')}><GitBranch size={16}/>提出变更</button>}{d.status==='待评审'&&tab!=='需求评审'&&<button className="btn primary" onClick={()=>requestLeave('需求评审')}>需求评审<ArrowRight size={16}/></button>}{tab!=='交付流程'&&d.status!=='草稿'&&<button className="btn" onClick={()=>requestLeave('交付流程')}>交付流程<ArrowRight size={16}/></button>}{business&&d.status==='待验收'&&tab!=='业务验收'&&<button className="btn primary" onClick={()=>requestLeave('业务验收')}>开始验收<ArrowRight size={16}/></button>}</>}</div></footer>
|
||
</DialogContent></Dialog>
|
||
<AlertDialog open={reviewExit!==null} onOpenChange={open=>{if(!open)setReviewExit(null)}}><AlertDialogContent className="form-close-confirm"><AlertDialogTitle>放弃未保存的输入?</AlertDialogTitle><AlertDialogDescription>离开后,本次未提交的操作内容将丢失,已保存的记录会保留。</AlertDialogDescription><div className="form-close-actions"><AlertDialogCancel className="btn">继续填写</AlertDialogCancel><AlertDialogAction className="btn danger-button" onClick={()=>{leaveReview(reviewExit?.tab);setReviewExit(null)}}>放弃输入并离开</AlertDialogAction></div></AlertDialogContent></AlertDialog></>
|
||
}
|