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'); } 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 reportText = useMemo( () => (error ? buildPlatformErrorReport(error) : ''), [error], ); useEffect( () => () => { if (resetTimerRef.current !== null) { window.clearTimeout(resetTimerRef.current); } }, [], ); useEffect(() => { setCopyState('idle'); }, [error?.source, error?.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' ? '复制失败' : '复制报错'} } > {error ? ( <>
来源
{error.source}
错误
{error.message}
) : null}
); }