/* 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 './colors.css'; import './common.css'; import './theme.css'; import './metadataView.css'; import type { Metadata } from '@playwright/test'; import type { GitCommitInfo } from '@testIsomorphic/types'; import { CopyToClipboardContainer } from './copyToClipboard'; import { linkifyText } from '@web/renderUtils'; type MetadataEntries = [string, unknown][]; export const MetadataContext = React.createContext([]); export function MetadataProvider({ metadata, children }: React.PropsWithChildren<{ metadata: Metadata }>) { const entries = React.useMemo(() => { // TODO: do not plumb actualWorkers through metadata. return Object.entries(metadata).filter(([key]) => key !== 'actualWorkers'); }, [metadata]); return {children}; } export function useMetadata() { return React.useContext(MetadataContext); } export function useGitCommitInfo() { const metadataEntries = useMetadata(); return metadataEntries.find(([key]) => key === 'git.commit.info')?.[1] as GitCommitInfo | undefined; } class ErrorBoundary extends React.Component, { error: Error | null, errorInfo: React.ErrorInfo | null }> { override state: { error: Error | null, errorInfo: React.ErrorInfo | null } = { error: null, errorInfo: null, }; override componentDidCatch(error: Error, errorInfo: React.ErrorInfo) { this.setState({ error, errorInfo }); } override render() { if (this.state.error || this.state.errorInfo) { return (

An error was encountered when trying to render metadata.

{this.state.error?.message}
{this.state.error?.stack}
{this.state.errorInfo?.componentStack}

); } return this.props.children; } } export const MetadataView = () => { return ; }; const InnerMetadataView = () => { const metadataEntries = useMetadata(); const gitCommitInfo = useGitCommitInfo(); const entries = metadataEntries.filter(([key]) => key !== 'git.commit.info'); if (!gitCommitInfo && !entries.length) return null; return
{gitCommitInfo && <> {entries.length > 0 &&
} }
{entries.map(([propertyName, value]) => { const valueString = typeof value !== 'object' || value === null || value === undefined ? String(value) : JSON.stringify(value); const trimmedValue = valueString.length > 1000 ? valueString.slice(0, 1000) + '\u2026' : valueString; return (
{propertyName} : {linkifyText(trimmedValue)}
); })}
; }; const GitCommitInfoView: React.FC<{ info: GitCommitInfo }> = ({ info }) => { const email = info['revision.email'] ? ` <${info['revision.email']}>` : ''; const author = `${info['revision.author'] || ''}${email}`; let subject = info['revision.subject'] || ''; let link = info['revision.link']; let shortSubject = info['revision.id']?.slice(0, 7) || 'unknown'; if (info['pull.link'] && info['pull.title']) { subject = info['pull.title']; link = info['pull.link']; shortSubject = link ? 'Pull Request' : ''; } const shortTimestamp = Intl.DateTimeFormat(undefined, { dateStyle: 'medium' }).format(info['revision.timestamp']); const longTimestamp = Intl.DateTimeFormat(undefined, { dateStyle: 'full', timeStyle: 'long' }).format(info['revision.timestamp']); return
{link ? ( {subject} ) : {subject} }
{author} on {shortTimestamp} {info['ci.link'] && ( <> ยท Logs )}
{link ? ( {shortSubject} ) : !!shortSubject && {shortSubject}}
; };