diff --git a/packages/trace-viewer/src/ui/attachmentsTab.tsx b/packages/trace-viewer/src/ui/attachmentsTab.tsx index 09a5e73b1a..2b373f3690 100644 --- a/packages/trace-viewer/src/ui/attachmentsTab.tsx +++ b/packages/trace-viewer/src/ui/attachmentsTab.tsx @@ -27,27 +27,15 @@ import { Expandable } from '@web/components/expandable'; type Attachment = AfterActionTraceEventAttachment & { traceUrl: string }; type TextAttachmentProps = { - attachment: Attachment; + attachmentText: string; label?: string; }; -const TextAttachment: React.FunctionComponent = ({ attachment, label }) => { - const [text, setText] = React.useState(null); - - React.useEffect(() => { - let isMounted = true; - fetch(attachmentURL(attachment)).then(response => response.text()).then(text => { - if (isMounted) - setText(text); - }); - return () => { - isMounted = false; - }; - }, [attachment]); +const TextAttachment: React.FunctionComponent = ({ attachmentText, label }) => { return
{ - text ? - : + attachmentText ? + :
Loading...
}
; @@ -55,6 +43,18 @@ const TextAttachment: React.FunctionComponent = ({ attachme const ExpandableAttachment: React.FunctionComponent<{ attachment: Attachment }> = ({ attachment }) => { const [expanded, setExpanded] = React.useState(false); + const [loaded, setLoaded] = React.useState(false); + const [attachmentText, setAttachmentText] = React.useState(null); + + React.useMemo(() => { + if (expanded && !loaded) { + fetch(attachmentURL(attachment)).then(response => response.text()).then(text => { + setAttachmentText(text); + setLoaded(true); + }); + } + }, [attachment, expanded, loaded]); + return {attachment.name} @@ -66,7 +66,7 @@ const ExpandableAttachment: React.FunctionComponent<{ attachment: Attachment }> } expanded={expanded} expandOnTitleClick={true} setExpanded={exp => setExpanded(exp)}> {isTextualMimeType(attachment.contentType) ? - : + :
no preview available
}
;