import { Check, Copy } from 'lucide-react'; import { useEffect, useMemo, useRef, useState } from 'react'; import { copyTextToClipboard } from '../../services/clipboard'; import { UnifiedModal } from '../common/UnifiedModal'; export type PlatformErrorDialogPayload = { source: string; message: string; }; type PlatformErrorDialogProps = { error: PlatformErrorDialogPayload | null; onClose: () => void; overlayClassName?: string; panelClassName?: string; }; function buildPlatformErrorReport(error: PlatformErrorDialogPayload) { return [`来源:${error.source}`, `错误:${error.message}`].join('\n'); } function isBlacklistedPlatformError(error: PlatformErrorDialogPayload | null) { // 中文注释:入口关闭是平台开关状态,不作为全局错误弹窗打扰用户。 return Boolean(error?.message.includes('creation_entry_disabled')); } export function PlatformErrorDialog({ error, onClose, overlayClassName = 'platform-theme platform-theme--light !items-center', panelClassName = 'platform-remap-surface rounded-[1.5rem]', }: PlatformErrorDialogProps) { const [copyState, setCopyState] = useState<'idle' | 'copied' | 'failed'>( 'idle', ); const resetTimerRef = useRef(null); const dialogError = isBlacklistedPlatformError(error) ? null : error; const reportText = useMemo( () => (dialogError ? buildPlatformErrorReport(dialogError) : ''), [dialogError], ); useEffect( () => () => { if (resetTimerRef.current !== null) { window.clearTimeout(resetTimerRef.current); } }, [], ); useEffect(() => { setCopyState('idle'); }, [dialogError?.source, dialogError?.message]); const copyError = () => { if (!reportText) { return; } void copyTextToClipboard(reportText).then((copied) => { setCopyState(copied ? 'copied' : 'failed'); if (resetTimerRef.current !== null) { window.clearTimeout(resetTimerRef.current); } resetTimerRef.current = window.setTimeout(() => { resetTimerRef.current = null; setCopyState('idle'); }, 1400); }); }; return ( {copyState === 'copied' ? ( ) : ( )} {copyState === 'copied' ? '已复制' : copyState === 'failed' ? '复制失败' : '复制报错'} } > {dialogError ? ( <>
来源
{dialogError.source}
错误
{dialogError.message}
) : null}
); }