2023-05-10 02:53:01 +02:00
|
|
|
/**
|
|
|
|
|
* Copyright (c) Microsoft Corporation.
|
|
|
|
|
*
|
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
|
*
|
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
*
|
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
|
* limitations under the License.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
import * as React from 'react';
|
|
|
|
|
import './attachmentsTab.css';
|
2023-12-22 19:17:35 +01:00
|
|
|
import { ImageDiffView } from '@web/shared/imageDiffView';
|
2025-01-20 09:06:01 +01:00
|
|
|
import type { MultiTraceModel } from './modelUtil';
|
2023-09-02 05:12:05 +02:00
|
|
|
import { PlaceholderPanel } from './placeholderPanel';
|
2023-09-09 01:47:45 +02:00
|
|
|
import type { AfterActionTraceEventAttachment } from '@trace/trace';
|
2024-10-08 17:33:45 +02:00
|
|
|
import { CodeMirrorWrapper, lineHeight } from '@web/components/codeMirrorWrapper';
|
2024-07-08 20:16:14 +02:00
|
|
|
import { isTextualMimeType } from '@isomorphic/mimeType';
|
|
|
|
|
import { Expandable } from '@web/components/expandable';
|
2024-08-01 18:27:45 +02:00
|
|
|
import { linkifyText } from '@web/renderUtils';
|
2025-01-20 09:06:01 +01:00
|
|
|
import { clsx, useFlash } from '@web/uiUtils';
|
2023-09-09 01:47:45 +02:00
|
|
|
|
|
|
|
|
type Attachment = AfterActionTraceEventAttachment & { traceUrl: string };
|
2023-05-10 02:53:01 +02:00
|
|
|
|
2024-07-08 20:16:14 +02:00
|
|
|
type ExpandableAttachmentProps = {
|
|
|
|
|
attachment: Attachment;
|
2025-01-20 09:06:01 +01:00
|
|
|
reveal?: any;
|
2024-07-08 20:16:14 +02:00
|
|
|
};
|
|
|
|
|
|
2025-01-20 09:06:01 +01:00
|
|
|
const ExpandableAttachment: React.FunctionComponent<ExpandableAttachmentProps> = ({ attachment, reveal }) => {
|
2024-07-08 20:16:14 +02:00
|
|
|
const [expanded, setExpanded] = React.useState(false);
|
|
|
|
|
const [attachmentText, setAttachmentText] = React.useState<string | null>(null);
|
2024-07-09 18:58:59 +02:00
|
|
|
const [placeholder, setPlaceholder] = React.useState<string | null>(null);
|
2025-01-20 09:06:01 +01:00
|
|
|
const [flash, triggerFlash] = useFlash();
|
2024-11-06 10:22:15 +01:00
|
|
|
const ref = React.useRef<HTMLSpanElement>(null);
|
2024-07-08 20:16:14 +02:00
|
|
|
|
2024-07-09 18:58:59 +02:00
|
|
|
const isTextAttachment = isTextualMimeType(attachment.contentType);
|
2024-08-01 18:27:45 +02:00
|
|
|
const hasContent = !!attachment.sha1 || !!attachment.path;
|
2024-07-09 18:58:59 +02:00
|
|
|
|
2024-11-06 10:22:15 +01:00
|
|
|
React.useEffect(() => {
|
2025-01-20 09:06:01 +01:00
|
|
|
if (reveal) {
|
2024-11-06 10:22:15 +01:00
|
|
|
ref.current?.scrollIntoView({ behavior: 'smooth' });
|
2025-01-20 09:06:01 +01:00
|
|
|
return triggerFlash();
|
|
|
|
|
}
|
|
|
|
|
}, [reveal, triggerFlash]);
|
2024-11-06 10:22:15 +01:00
|
|
|
|
2024-07-09 18:58:59 +02:00
|
|
|
React.useEffect(() => {
|
|
|
|
|
if (expanded && attachmentText === null && placeholder === null) {
|
|
|
|
|
setPlaceholder('Loading ...');
|
2024-07-08 20:16:14 +02:00
|
|
|
fetch(attachmentURL(attachment)).then(response => response.text()).then(text => {
|
|
|
|
|
setAttachmentText(text);
|
2024-07-09 18:58:59 +02:00
|
|
|
setPlaceholder(null);
|
|
|
|
|
}).catch(e => {
|
|
|
|
|
setPlaceholder('Failed to load: ' + e.message);
|
|
|
|
|
});
|
2024-07-08 20:16:14 +02:00
|
|
|
}
|
2024-07-09 18:58:59 +02:00
|
|
|
}, [expanded, attachmentText, placeholder, attachment]);
|
|
|
|
|
|
2024-10-08 17:33:45 +02:00
|
|
|
const snippetHeight = React.useMemo(() => {
|
|
|
|
|
const lineCount = attachmentText ? attachmentText.split('\n').length : 0;
|
|
|
|
|
return Math.min(Math.max(5, lineCount), 20) * lineHeight;
|
|
|
|
|
}, [attachmentText]);
|
|
|
|
|
|
2024-11-06 10:22:15 +01:00
|
|
|
const title = <span style={{ marginLeft: 5 }} ref={ref} aria-label={attachment.name}>
|
2025-01-20 09:06:01 +01:00
|
|
|
<span>{linkifyText(attachment.name)}</span>
|
2024-11-06 10:22:15 +01:00
|
|
|
{hasContent && <a style={{ marginLeft: 5 }} href={downloadURL(attachment)}>download</a>}
|
2024-07-31 11:29:14 +02:00
|
|
|
</span>;
|
2024-07-09 18:58:59 +02:00
|
|
|
|
2024-08-01 18:27:45 +02:00
|
|
|
if (!isTextAttachment || !hasContent)
|
2024-07-09 18:58:59 +02:00
|
|
|
return <div style={{ marginLeft: 20 }}>{title}</div>;
|
2024-07-08 20:16:14 +02:00
|
|
|
|
2025-01-20 09:06:01 +01:00
|
|
|
return <div className={clsx(flash && 'yellow-flash')}>
|
2024-07-09 18:58:59 +02:00
|
|
|
<Expandable title={title} expanded={expanded} setExpanded={setExpanded} expandOnTitleClick={true}>
|
|
|
|
|
{placeholder && <i>{placeholder}</i>}
|
|
|
|
|
</Expandable>
|
2024-10-08 17:33:45 +02:00
|
|
|
{expanded && attachmentText !== null && <div className='vbox' style={{ height: snippetHeight }}>
|
|
|
|
|
<CodeMirrorWrapper
|
|
|
|
|
text={attachmentText}
|
|
|
|
|
readOnly
|
|
|
|
|
mimeType={attachment.contentType}
|
|
|
|
|
linkify={true}
|
|
|
|
|
lineNumbers={true}
|
|
|
|
|
wrapLines={false}>
|
|
|
|
|
</CodeMirrorWrapper>
|
|
|
|
|
</div>}
|
2025-01-20 09:06:01 +01:00
|
|
|
</div>;
|
2024-07-08 20:16:14 +02:00
|
|
|
};
|
|
|
|
|
|
2023-05-10 02:53:01 +02:00
|
|
|
export const AttachmentsTab: React.FunctionComponent<{
|
2023-07-10 21:56:56 +02:00
|
|
|
model: MultiTraceModel | undefined,
|
2025-01-20 09:06:01 +01:00
|
|
|
revealedAttachment?: [AfterActionTraceEventAttachment, number],
|
|
|
|
|
}> = ({ model, revealedAttachment }) => {
|
2023-09-09 01:47:45 +02:00
|
|
|
const { diffMap, screenshots, attachments } = React.useMemo(() => {
|
|
|
|
|
const attachments = new Set<Attachment>();
|
|
|
|
|
const screenshots = new Set<Attachment>();
|
2023-07-10 21:56:56 +02:00
|
|
|
|
2023-09-09 01:47:45 +02:00
|
|
|
for (const action of model?.actions || []) {
|
|
|
|
|
const traceUrl = action.context.traceUrl;
|
|
|
|
|
for (const attachment of action.attachments || [])
|
|
|
|
|
attachments.add({ ...attachment, traceUrl });
|
|
|
|
|
}
|
|
|
|
|
const diffMap = new Map<string, { expected: Attachment | undefined, actual: Attachment | undefined, diff: Attachment | undefined }>();
|
2023-06-02 05:29:32 +02:00
|
|
|
|
2023-09-09 01:47:45 +02:00
|
|
|
for (const attachment of attachments) {
|
|
|
|
|
if (!attachment.path && !attachment.sha1)
|
|
|
|
|
continue;
|
|
|
|
|
const match = attachment.name.match(/^(.*)-(expected|actual|diff)\.png$/);
|
|
|
|
|
if (match) {
|
|
|
|
|
const name = match[1];
|
|
|
|
|
const type = match[2] as 'expected' | 'actual' | 'diff';
|
|
|
|
|
const entry = diffMap.get(name) || { expected: undefined, actual: undefined, diff: undefined };
|
|
|
|
|
entry[type] = attachment;
|
|
|
|
|
diffMap.set(name, entry);
|
2024-07-31 11:29:14 +02:00
|
|
|
attachments.delete(attachment);
|
|
|
|
|
} else if (attachment.contentType.startsWith('image/')) {
|
2023-09-09 01:47:45 +02:00
|
|
|
screenshots.add(attachment);
|
|
|
|
|
attachments.delete(attachment);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return { diffMap, attachments, screenshots };
|
|
|
|
|
}, [model]);
|
2023-05-10 02:53:01 +02:00
|
|
|
|
2023-09-09 01:47:45 +02:00
|
|
|
if (!diffMap.size && !screenshots.size && !attachments.size)
|
|
|
|
|
return <PlaceholderPanel text='No attachments' />;
|
|
|
|
|
|
|
|
|
|
return <div className='attachments-tab'>
|
|
|
|
|
{[...diffMap.values()].map(({ expected, actual, diff }) => {
|
|
|
|
|
return <>
|
|
|
|
|
{expected && actual && <div className='attachments-section'>Image diff</div>}
|
2024-07-31 11:29:14 +02:00
|
|
|
{expected && actual && <ImageDiffView noTargetBlank={true} diff={{
|
2023-09-09 01:47:45 +02:00
|
|
|
name: 'Image diff',
|
2024-07-31 15:20:36 +02:00
|
|
|
expected: { attachment: { ...expected, path: downloadURL(expected) }, title: 'Expected' },
|
|
|
|
|
actual: { attachment: { ...actual, path: downloadURL(actual) } },
|
|
|
|
|
diff: diff ? { attachment: { ...diff, path: downloadURL(diff) } } : undefined,
|
2023-09-09 01:47:45 +02:00
|
|
|
}} />}
|
|
|
|
|
</>;
|
|
|
|
|
})}
|
2023-06-02 05:29:32 +02:00
|
|
|
{screenshots.size ? <div className='attachments-section'>Screenshots</div> : undefined}
|
2023-09-09 01:47:45 +02:00
|
|
|
{[...screenshots.values()].map((a, i) => {
|
|
|
|
|
const url = attachmentURL(a);
|
2023-06-02 05:29:32 +02:00
|
|
|
return <div className='attachment-item' key={`screenshot-${i}`}>
|
2023-08-20 23:47:18 +02:00
|
|
|
<div><img draggable='false' src={url} /></div>
|
2024-08-20 14:16:28 +02:00
|
|
|
<div><a target='_blank' href={url} rel='noreferrer'>{a.name}</a></div>
|
2023-06-02 05:29:32 +02:00
|
|
|
</div>;
|
|
|
|
|
})}
|
2023-09-09 01:47:45 +02:00
|
|
|
{attachments.size ? <div className='attachments-section'>Attachments</div> : undefined}
|
|
|
|
|
{[...attachments.values()].map((a, i) => {
|
2024-08-08 19:57:44 +02:00
|
|
|
return <div className='attachment-item' key={attachmentKey(a, i)}>
|
2024-11-06 10:22:15 +01:00
|
|
|
<ExpandableAttachment
|
|
|
|
|
attachment={a}
|
2025-01-20 09:06:01 +01:00
|
|
|
reveal={(!!revealedAttachment && isEqualAttachment(a, revealedAttachment[0])) ? revealedAttachment : undefined}
|
2024-11-06 10:22:15 +01:00
|
|
|
/>
|
2023-05-10 02:53:01 +02:00
|
|
|
</div>;
|
|
|
|
|
})}
|
2023-09-09 01:47:45 +02:00
|
|
|
</div>;
|
2023-05-10 02:53:01 +02:00
|
|
|
};
|
|
|
|
|
|
2024-11-06 10:22:15 +01:00
|
|
|
function isEqualAttachment(a: Attachment, b: AfterActionTraceEventAttachment): boolean {
|
|
|
|
|
return a.name === b.name && a.path === b.path && a.sha1 === b.sha1;
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-31 15:20:36 +02:00
|
|
|
function attachmentURL(attachment: Attachment, queryParams: Record<string, string> = {}) {
|
|
|
|
|
const params = new URLSearchParams(queryParams);
|
|
|
|
|
if (attachment.sha1) {
|
|
|
|
|
params.set('trace', attachment.traceUrl);
|
|
|
|
|
return 'sha1/' + attachment.sha1 + '?' + params.toString();
|
|
|
|
|
}
|
|
|
|
|
params.set('path', attachment.path!);
|
|
|
|
|
return 'file?' + params.toString();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function downloadURL(attachment: Attachment) {
|
|
|
|
|
const params = { dn: attachment.name } as Record<string, string>;
|
|
|
|
|
if (attachment.contentType)
|
|
|
|
|
params.dct = attachment.contentType;
|
|
|
|
|
return attachmentURL(attachment, params);
|
2023-05-10 02:53:01 +02:00
|
|
|
}
|
2024-08-08 19:57:44 +02:00
|
|
|
|
|
|
|
|
function attachmentKey(attachment: Attachment, index: number) {
|
|
|
|
|
return index + '-' + (attachment.sha1 ? `sha1-` + attachment.sha1 : `path-` + attachment.path);
|
|
|
|
|
}
|