Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: minor
Type: added

Forms: use DataViews mediaField to display responders avatars
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { Hovercards } from '@gravatar-com/hovercards';
import '@gravatar-com/hovercards/dist/style.css';
import { useEffect, useRef } from '@wordpress/element';
import { __ } from '@wordpress/i18n';
import clsx from 'clsx';
import { sha256 } from 'js-sha256';
import './style.scss';

Expand All @@ -23,6 +24,7 @@ type GravatarProps = {
email: string;
size?: number;
useHovercard?: boolean;
isRounded?: boolean;
};

/**
Expand All @@ -40,6 +42,7 @@ export default function Gravatar( {
email,
size = 48,
useHovercard = true,
isRounded = true,
}: GravatarProps ): JSX.Element | null {
const profileImageRef = useRef( null );
const hovercardRef = useRef( null );
Expand Down Expand Up @@ -80,10 +83,14 @@ export default function Gravatar( {

const hashedEmail = sha256( email );

const classes = clsx( 'jp-forms__gravatar', {
'is-rounded': isRounded,
} );

return (
<img
alt={ displayName || '' }
className="jp-forms__gravatar"
className={ classes }
ref={ profileImageRef }
src={ `https://0.gravatar.com/avatar/${ hashedEmail }?d=${ defaultImage }&name=${ displayName }` }
width={ size }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
.jp-forms__gravatar {
background-color: var(--jp-gray-5);
border-radius: 50%;
flex-shrink: 0; // Prevent shrinking when Gravatar is inside flex containers
overflow: hidden;

&.is-rounded {
border-radius: 50%;
}
}
38 changes: 29 additions & 9 deletions projects/packages/forms/src/dashboard/inbox/dataviews/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -220,14 +220,42 @@ export default function InboxView() {
};
const fields = useMemo(
() => [
{
id: 'avatar',
label: __( 'Avatar', 'jetpack-forms' ),
render: ( { item } ) => {
const authorInfo = decodeEntities(
item.author_name || item.author_email || item.author_url || item.ip
);
const defaultImage = item.author_name || item.author_email ? 'initials' : 'mp';

return (
<>
<Gravatar
email={ item.author_email || item.ip } // With IP we still return placeholder image
defaultImage={ defaultImage }
displayName={ authorInfo }
key={ item.id }
size={ 32 }
useHovercard={ false }
isRounded={ false }
/>
</>
);
},
enableSorting: false,
enableHiding: true,
globalSearch: false,
type: 'media',
},
{
id: 'from',
label: __( 'From', 'jetpack-forms' ),
render: ( { item } ) => {
const authorInfo = decodeEntities(
item.author_name || item.author_email || item.author_url || item.ip
);
const defaultImage = item.author_name || item.author_email ? 'initials' : 'mp';

return (
<div className="jp-forms__inbox__author-field">
{ item.is_unread && (
Expand All @@ -238,14 +266,6 @@ export default function InboxView() {
</span>
) }
<Gravatar
email={ item.author_email || item.ip } // With IP we still return placeholder image
defaultImage={ defaultImage }
displayName={ authorInfo }
key={ item.id }
size={ 32 }
useHovercard={ false }
/>
{ wrapperUnread( item.is_unread, authorInfo ) }
</div>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ const defaultView = {
filters: [],
page: 1,
perPage: 20,
fields: [ 'from', 'date', 'source', 'ip' ],
fields: [ 'date', 'source', 'ip' ],
titleField: 'from',
mediaField: 'avatar',
};

export const defaultLayouts = {
Expand Down
10 changes: 9 additions & 1 deletion projects/packages/forms/src/dashboard/inbox/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,13 @@
border-right: 1px solid var(--jp-gray-5);
}

// Override DataViews default max-width for media in primary column
// to better fit smaller avatars (24x24 instead of larger preview images)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.dataviews-column-primary__media {
max-width: 32px;
}


.dataviews__view-actions {
align-items: center;

Expand Down Expand Up @@ -356,7 +363,8 @@
color: #d63638;
font-size: 8px;
position: absolute;
left: -12px;
left: -10px;
top: 5px;
cursor: pointer;
}
}
Expand Down
Loading