支持自部署需求管理、交付流程与飞书集成
This commit is contained in:
1 parent
2149aeb6ed
commit
51a2e548f2
152 files changed
+8652
-6394
No files matched your search
@@ -0,0 +1,63 @@
|
||||
'use client';
|
||||
|
||||
import { Component, useEffect, useRef, useState, type ComponentType, type ReactNode, type RefObject } from 'react';
|
||||
import { ArrowLeft, Download, ExternalLink, RotateCw, X } from 'lucide-react';
|
||||
import { Dialog, DialogContent, DialogTitle, DialogDescription } from '@/components/ui/dialog';
|
||||
import { FEISHU_TEMPLATE_DIRECTORY, findDocument, templateDownloadUrl, type DocumentId } from '@/lib/documents';
|
||||
import { appPath } from '@/lib/base-path';
|
||||
import type { TemplateDocumentProps } from './template-document';
|
||||
|
||||
function PreviewError({ message, onRetry }: { message: string; onRetry: () => void }) {
|
||||
return <div className="error-banner template-status" role="alert"><span>{message}</span><button type="button" className="btn" onClick={onRetry}><RotateCw size={15}/>重新加载</button></div>;
|
||||
}
|
||||
|
||||
class DocumentBoundary extends Component<{ children: ReactNode; onRetry: () => void }, { failed: boolean }> {
|
||||
state = { failed: false };
|
||||
static getDerivedStateFromError() { return { failed: true }; }
|
||||
render() { return this.state.failed ? <PreviewError message="模板暂时无法显示,请重试。已填内容仍然保留。" onRetry={this.props.onRetry}/> : this.props.children; }
|
||||
}
|
||||
|
||||
type PreviewContent = { id: DocumentId; text?: string; Body?: ComponentType<TemplateDocumentProps>; error?: string };
|
||||
|
||||
export default function TemplatePreview({ documentId, onClose, returnFocus }: { documentId: DocumentId; onClose: () => void; returnFocus: RefObject<HTMLElement | null> }) {
|
||||
const [current, setCurrent] = useState(documentId);
|
||||
const [history, setHistory] = useState<DocumentId[]>([]);
|
||||
const [attempt, setAttempt] = useState(0);
|
||||
const [content, setContent] = useState<PreviewContent | null>(null);
|
||||
const scroll = useRef<HTMLDivElement>(null);
|
||||
const info = findDocument(current)!;
|
||||
const loading = content?.id !== current;
|
||||
const Body = content?.Body;
|
||||
useEffect(() => {
|
||||
const controller = new AbortController();
|
||||
const text = fetch(appPath('/api/documents/' + encodeURIComponent(findDocument(current)!.filename) + '?format=json'), { signal: controller.signal, cache: 'no-store' })
|
||||
.then(async response => {
|
||||
const result = await response.json() as { content?: string; error?: string };
|
||||
if (!response.ok || typeof result.content !== 'string') throw new Error(result.error || '模板暂时无法加载,请重试。');
|
||||
return result.content;
|
||||
});
|
||||
// Keep the dialog and form mounted even when a renderer chunk fails. Calling
|
||||
// import again on retry avoids React.lazy's cached rejected promise.
|
||||
const renderer = import('./template-document').catch(() => { throw new Error('模板预览未能加载,请重试。已填内容仍然保留。'); });
|
||||
void Promise.all([text, renderer])
|
||||
.then(([text, module]) => { if (!controller.signal.aborted) setContent({ id: current, text, Body: module.default }); })
|
||||
.catch(error => {
|
||||
if (controller.signal.aborted) return;
|
||||
setContent({ id: current, error: error instanceof Error ? error.message : '模板加载失败,请重试。' });
|
||||
controller.abort();
|
||||
});
|
||||
return () => controller.abort();
|
||||
}, [current, attempt]);
|
||||
function retry() { setContent(null); setAttempt(value => value + 1); }
|
||||
function openDocument(id: DocumentId) { setHistory(previous => [...previous, current]); setCurrent(id); scroll.current?.scrollTo(0, 0); }
|
||||
return <Dialog open onOpenChange={open => { if (!open) onClose(); }}><DialogContent className="demand-modal template-modal" showCloseButton={false}
|
||||
onEscapeKeyDown={event => { event.preventDefault(); onClose(); }}
|
||||
onCloseAutoFocus={event => { event.preventDefault(); if (returnFocus.current?.isConnected) returnFocus.current.focus({ preventScroll: true }); }}>
|
||||
<div className="modal-heading"><div><DialogTitle className="modal-title">{info.title}</DialogTitle><DialogDescription className="modal-subtitle">{info.description}</DialogDescription></div><button type="button" className="icon-button" onClick={onClose} aria-label="关闭模板预览"><X size={20}/></button></div>
|
||||
<div className="template-toolbar">{history.length > 0 && <button type="button" className="text-button" onClick={() => { setCurrent(history.at(-1)!); setHistory(previous => previous.slice(0, -1)); scroll.current?.scrollTo(0, 0); }}><ArrowLeft size={15}/>返回上一份模板</button>}<a className="btn" href={templateDownloadUrl(current)} download><Download size={16}/>下载 Word 版本</a><a className="text-button" href={FEISHU_TEMPLATE_DIRECTORY} target="_blank" rel="noopener noreferrer"><ExternalLink size={15}/>飞书模板总目录</a></div>
|
||||
<div className="modal-scroll template-scroll" ref={scroll}>
|
||||
{loading ? <p className="template-status" role="status">正在加载模板…</p> : content?.error ? <PreviewError message={content.error} onRetry={retry}/> : Body && <DocumentBoundary key={current + ':' + attempt} onRetry={retry}><Body text={content?.text || ''} onOpenDocument={openDocument}/></DocumentBoundary>}
|
||||
</div>
|
||||
<footer className="template-footer"><p>可以下载后用 Word 或 WPS 填写,也可以前往飞书模板总目录创建副本。</p><button type="button" className="btn primary" onClick={onClose}>返回填写</button></footer>
|
||||
</DialogContent></Dialog>;
|
||||
}
|
||||
Reference in new issue
Block a user