Back

0% Statements 0/26
0% Branches 0/38
0% Functions 0/3
0% Lines 0/26

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189                                                                                                                                                                                                                                                                                                                                                                                         
/**
 * External dependencies
 */
import Gravatar from '@automattic/jetpack-components/gravatar';
import { dateI18n, getSettings as getDateSettings } from '@wordpress/date';
import { useId } from '@wordpress/element';
import { decodeEntities } from '@wordpress/html-entities';
import { __ } from '@wordpress/i18n';
import { Badge, Link, Stack, Text, Tooltip, VisuallyHidden } from '@wordpress/ui';
/**
 * Internal dependencies
 */
import CopyClipboardButton from '../../../components/copy-clipboard-button/index.tsx';
import { getPath } from '../../../inbox/utils.js';
import TextWithFlag from '../../text-with-flag/index.tsx';
import type { FormResponse } from '../../../../types/index.ts';
import './style.scss';
 
const getDisplayName = ( response: FormResponse ) => {
	const { author_name, author_email, author_url, ip } = response;
	return decodeEntities( author_name || author_email || author_url || ip );
};
 
/**
 * Render the contents of the Source cell for a feedback response.
 *
 * Test responses (submitted from form preview) link to the preview URL when
 * one is available; otherwise they fall through to a plain "Form Preview"
 * label. Real responses link to the page that hosted the form.
 *
 * @param props          - Component props.
 * @param props.response - The feedback response.
 * @return Source cell content.
 */
const SourceCell = ( { response }: { response: FormResponse } ) => {
	if ( response.is_test ) {
		const label = __( 'Form preview', 'jetpack-forms' );
		if ( response.preview_url ) {
			return (
				<Link openInNewTab href={ response.preview_url }>
					{ label }
				</Link>
			);
		}
		return <>{ label }</>;
	}
 
	if ( response.entry_permalink ) {
		return (
			<Link openInNewTab href={ response.entry_permalink }>
				{ decodeEntities( response.entry_title ) || getPath( response ) }
			</Link>
		);
	}
 
	return <>{ decodeEntities( response.entry_title ) }</>;
};
 
export type ResponseMetaProps = {
	response: FormResponse;
};
 
/**
 * Renders the response meta information including header and metadata.
 *
 * @param {object} props          - The props object.
 * @param {object} props.response - The response item.
 * @return {import('react').JSX.Element} The response meta component.
 */
const ResponseMeta = ( { response }: ResponseMetaProps ): import( 'react' ).JSX.Element => {
	const ipLookupId = useId();
	const dateSettings = getDateSettings();
	const displayName = getDisplayName( response );
	// Match the data view gravatar logic: use email or IP, and set defaultImage conditionally
	const gravatarEmail = response.author_email || response.ip;
	const gravatarDisplayName = response.author_name
		? decodeEntities( response.author_name )
		: response.author_email?.split( '@' )[ 0 ];
	const defaultImage = gravatarDisplayName ? 'initials' : 'mp';
 
	const responseAuthorEmailParts = response.author_email?.split( '@' ) ?? [];
 
	// Logged-in user row content: either shows display name and ID, username and ID, or just the ID.
	const loggedInUser = response?.logged_in_user?.id ? response.logged_in_user : null;
	const loggedInUserName = loggedInUser?.display_name || loggedInUser?.username || null;
	let loggedInUserDisplay = null;
	if ( loggedInUser ) {
		loggedInUserDisplay = loggedInUserName
			? `${ loggedInUserName } (#${ loggedInUser.id })`
			: `#${ loggedInUser.id }`;
	}
 
	return (
		<div className="jp-forms__inbox-response-meta">
			<Stack align="flex-start" direction="row" gap="md" justify="flex-start" wrap="nowrap">
				<Gravatar
					email={ gravatarEmail }
					defaultImage={ defaultImage }
					displayName={ gravatarDisplayName }
					key={ gravatarEmail }
				/>
				{ /* `justify` centres a short name against the 48px avatar. */ }
				<Stack className="jp-forms__inbox-response-meta-from" direction="column" justify="center">
					<Stack align="center" direction="row" gap="sm" justify="start">
						<Text className="jp-forms__inbox-response-meta-from-name" variant="heading-lg">
							{ displayName }
						</Text>
						{ response.is_test && (
							<Badge intent="none" aria-label={ __( 'Test response', 'jetpack-forms' ) }>
								{ __( 'Test', 'jetpack-forms' ) }
							</Badge>
						) }
					</Stack>
					{ response.author_email && displayName !== response.author_email && (
						<Stack
							align="center"
							className="jp-forms__inbox-response-meta-from-email"
							direction="row"
							gap="sm"
							justify="start"
						>
							<Text render={ <a href={ `mailto:${ response.author_email }` } /> } variant="body-md">
								{ responseAuthorEmailParts[ 0 ] }
								<wbr />@{ responseAuthorEmailParts[ 1 ] }
							</Text>
							<CopyClipboardButton text={ response.author_email } />
						</Stack>
					) }
				</Stack>
			</Stack>
			<table className="jp-forms__inbox-response-meta-table">
				<tbody>
					<tr>
						<th>{ __( 'Date:', 'jetpack-forms' ) }</th>
						<td>{ dateI18n( dateSettings.formats.datetime, response.date ) }</td>
					</tr>
					<tr>
						<th>{ __( 'Source:', 'jetpack-forms' ) }</th>
						<td>
							<SourceCell response={ response } />
						</td>
					</tr>
					<tr>
						<th>{ __( 'IP address:', 'jetpack-forms' ) }&nbsp;</th>
						<td>
							<TextWithFlag countryCode={ response.country_code }>
								<Tooltip.Root>
									<Tooltip.Trigger
										render={
											<Link
												aria-describedby={ ipLookupId }
												openInNewTab
												href={ `https://apps.db.ripe.net/db-web-ui/query?searchtext=${ encodeURIComponent(
													response.ip
												) }` }
											>
												{ response.ip }
											</Link>
										}
									/>
									{ /* The popup is visual-only, so this carries the sentence to screen readers. */ }
									<VisuallyHidden id={ ipLookupId } render={ <span /> }>
										{ __( 'Lookup IP address', 'jetpack-forms' ) }
									</VisuallyHidden>
									<Tooltip.Popup>{ __( 'Lookup IP address', 'jetpack-forms' ) }</Tooltip.Popup>
								</Tooltip.Root>
							</TextWithFlag>
						</td>
					</tr>
					{ response.browser && (
						<tr>
							<th>{ __( 'Browser:', 'jetpack-forms' ) }&nbsp;</th>
							<td>{ response.browser }</td>
						</tr>
					) }
					{ loggedInUserDisplay && (
						<tr>
							<th>{ __( 'Logged-in user:', 'jetpack-forms' ) }&nbsp;</th>
							<td>{ loggedInUserDisplay }</td>
						</tr>
					) }
				</tbody>
			</table>
		</div>
	);
};
 
export default ResponseMeta;