|
| 1 | +/** |
| 2 | + * Copyright (c) Microsoft Corporation. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +import * as React from 'react'; |
| 18 | +import { Link, LinkProps } from './links'; |
| 19 | +import { clsx, useFlash } from '@web/uiUtils'; |
| 20 | +import type { TestAttachment, TestCaseSummary, TestResult } from './types'; |
| 21 | +import { TreeItem } from './treeItem'; |
| 22 | +import * as icons from './icons'; |
| 23 | +import { useAnchor } from './anchor'; |
| 24 | +import { linkifyText } from '@web/renderUtils'; |
| 25 | +import { CopyToClipboard } from './copyToClipboard'; |
| 26 | +import { generateTraceUrl } from './url'; |
| 27 | +import './colors.css'; |
| 28 | +import './linkVariants.css'; |
| 29 | + |
| 30 | +export const LinkBadge: React.FunctionComponent<LinkProps & { dim?: boolean }> = ({ className, ...props }) => <Link {...props} className={clsx('link-badge', props.dim && 'link-badge-dim', className)} />; |
| 31 | + |
| 32 | +export const ProjectLink: React.FunctionComponent<{ |
| 33 | + projectNames: string[], |
| 34 | + projectName: string, |
| 35 | +}> = ({ projectNames, projectName }) => { |
| 36 | + const encoded = encodeURIComponent(projectName); |
| 37 | + const value = projectName === encoded ? projectName : `"${encoded.replace(/%22/g, '%5C%22')}"`; |
| 38 | + return <Link href={`#?q=p:${value}`}> |
| 39 | + <span className={clsx('label', `label-color-${projectNames.indexOf(projectName) % 6}`)} style={{ margin: '6px 0 0 6px' }}> |
| 40 | + {projectName} |
| 41 | + </span> |
| 42 | + </Link>; |
| 43 | +}; |
| 44 | + |
| 45 | +const kMissingContentType = 'x-playwright/missing'; |
| 46 | + |
| 47 | +export const AttachmentLink: React.FunctionComponent<{ |
| 48 | + attachment: TestAttachment, |
| 49 | + result: TestResult, |
| 50 | + href?: string, |
| 51 | + linkName?: string, |
| 52 | + openInNewTab?: boolean, |
| 53 | +}> = ({ attachment, result, href, linkName, openInNewTab }) => { |
| 54 | + const [flash, triggerFlash] = useFlash(); |
| 55 | + useAnchor('attachment-' + result.attachments.indexOf(attachment), triggerFlash); |
| 56 | + return <TreeItem title={<span> |
| 57 | + {attachment.contentType === kMissingContentType ? icons.warning() : icons.attachment()} |
| 58 | + {attachment.path && ( |
| 59 | + openInNewTab |
| 60 | + ? <a href={href || attachment.path} target='_blank' rel='noreferrer'>{linkName || attachment.name}</a> |
| 61 | + : <a href={href || attachment.path} download={downloadFileNameForAttachment(attachment)}>{linkName || attachment.name}</a> |
| 62 | + )} |
| 63 | + {!attachment.path && ( |
| 64 | + openInNewTab |
| 65 | + ? ( |
| 66 | + <a |
| 67 | + href={URL.createObjectURL(new Blob([attachment.body!], { type: attachment.contentType }))} |
| 68 | + target='_blank' rel='noreferrer' |
| 69 | + onClick={e => e.stopPropagation() /* dont expand the tree item */} |
| 70 | + > |
| 71 | + {attachment.name} |
| 72 | + </a> |
| 73 | + ) |
| 74 | + : <span>{linkifyText(attachment.name)}</span> |
| 75 | + )} |
| 76 | + </span>} loadChildren={attachment.body ? () => { |
| 77 | + return [<div key={1} className='attachment-body'><CopyToClipboard value={attachment.body!}/>{linkifyText(attachment.body!)}</div>]; |
| 78 | + } : undefined} depth={0} style={{ lineHeight: '32px' }} flash={flash}></TreeItem>; |
| 79 | +}; |
| 80 | + |
| 81 | +export const TraceLink: React.FC<{ test: TestCaseSummary, trailingSeparator?: boolean, dim?: boolean }> = ({ test, trailingSeparator, dim }) => { |
| 82 | + const firstTraces = test.results.map(result => result.attachments.filter(attachment => attachment.name === 'trace')).filter(traces => traces.length > 0)[0]; |
| 83 | + if (!firstTraces) |
| 84 | + return undefined; |
| 85 | + |
| 86 | + return ( |
| 87 | + <> |
| 88 | + <LinkBadge |
| 89 | + href={generateTraceUrl(firstTraces)} |
| 90 | + title='View Trace' |
| 91 | + className='button link-trace' |
| 92 | + dim={dim}> |
| 93 | + {icons.trace()} |
| 94 | + <span>View Trace</span> |
| 95 | + </LinkBadge> |
| 96 | + {trailingSeparator && <div className='link-trace-separator'>|</div>} |
| 97 | + </> |
| 98 | + ); |
| 99 | +}; |
| 100 | + |
| 101 | +function downloadFileNameForAttachment(attachment: TestAttachment): string { |
| 102 | + if (attachment.name.includes('.') || !attachment.path) |
| 103 | + return attachment.name; |
| 104 | + const firstDotIndex = attachment.path.indexOf('.'); |
| 105 | + if (firstDotIndex === -1) |
| 106 | + return attachment.name; |
| 107 | + return attachment.name + attachment.path.slice(firstDotIndex, attachment.path.length); |
| 108 | +} |
0 commit comments