Skip to content

Commit ce4af9f

Browse files
authored
Forms: Adds country flags to dashboard based on IP. (#45643)
* Forms: Show country flag emoji for response IP addresses Added a utility to convert country codes to flag emojis and updated the response view and inbox table to display the country flag next to the IP address. Updated styles for flag display and extended the FormResponse type to include a country_code field. * changelog * Update to use globe icon and remove flag on response view if not there. * rebase * Improve the vertical alignment
1 parent c92a056 commit ce4af9f

File tree

7 files changed

+69
-3
lines changed

7 files changed

+69
-3
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Significance: minor
2+
Type: added
3+
4+
Forms: Add flag next to the IP address

projects/packages/forms/src/dashboard/components/response-view/body.tsx

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,12 @@ import CopyClipboardButton from '../../components/copy-clipboard-button';
3131
import Gravatar from '../../components/gravatar';
3232
import useInboxData from '../../hooks/use-inbox-data';
3333
import { useMarkAsSpam } from '../../hooks/use-mark-as-spam';
34-
import { getPath, updateMenuCounter, updateMenuCounterOptimistically } from '../../inbox/utils';
34+
import {
35+
getPath,
36+
updateMenuCounter,
37+
updateMenuCounterOptimistically,
38+
getCountryFlagEmoji,
39+
} from '../../inbox/utils';
3540
import { store as dashboardStore } from '../../store';
3641
import type { FormResponse } from '../../../types';
3742

@@ -473,6 +478,11 @@ const ResponseViewBody = ( {
473478
<th>{ __( 'IP address:', 'jetpack-forms' ) }&nbsp;</th>
474479
<td>
475480
<Tooltip text={ __( 'Lookup IP address', 'jetpack-forms' ) }>
481+
{ response.country_code && (
482+
<span className="jp-forms__inbox-response-meta-country-flag response-country-flag">
483+
{ getCountryFlagEmoji( response.country_code ) }
484+
</span>
485+
) }
476486
<ExternalLink href={ getRedirectUrl( 'ip-lookup', { path: response.ip } ) }>
477487
{ response.ip }
478488
</ExternalLink>

projects/packages/forms/src/dashboard/inbox/dataviews/index.js

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import { dateI18n, getSettings as getDateSettings } from '@wordpress/date';
1313
import { useCallback, useMemo, useState } from '@wordpress/element';
1414
import { decodeEntities } from '@wordpress/html-entities';
1515
import { __ } from '@wordpress/i18n';
16+
import { Icon, globe } from '@wordpress/icons';
1617
import { useEffect } from 'react';
1718
import { useSearchParams } from 'react-router';
1819
/**
@@ -23,7 +24,7 @@ import InboxStatusToggle from '../../components/inbox-status-toggle';
2324
import { ResponseMobileView, SingleResponseView } from '../../components/response-view';
2425
import useInboxData from '../../hooks/use-inbox-data';
2526
import EmptyResponses from '../empty-responses';
26-
import { getPath, getItemId } from '../utils.js';
27+
import { getPath, getItemId, getCountryFlagEmoji } from '../utils.js';
2728
import {
2829
viewAction,
2930
markAsSpamAction,
@@ -310,7 +311,22 @@ export default function InboxView() {
310311
filterBy: { operators: [ 'is' ] },
311312
enableSorting: false,
312313
},
313-
{ id: 'ip', label: __( 'IP Address', 'jetpack-forms' ), enableSorting: false },
314+
{
315+
id: 'ip',
316+
label: __( 'IP Address', 'jetpack-forms' ),
317+
enableSorting: false,
318+
render: ( { item } ) => {
319+
return (
320+
<>
321+
<span className="response-country-flag">
322+
{ ! item.country_code && <Icon icon={ globe } size={ 20 } /> }
323+
{ item.country_code && getCountryFlagEmoji( item.country_code ) }
324+
</span>
325+
{ item.ip || '' }
326+
</>
327+
);
328+
},
329+
},
314330
],
315331
[ filterOptions, dateSettings.formats.date ]
316332
);

projects/packages/forms/src/dashboard/inbox/style.scss

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,18 @@
171171
align-items: center;
172172
}
173173

174+
.response-country-flag {
175+
font-size: 16px;
176+
display: inline;
177+
padding-right: 4px;
178+
vertical-align: middle;
179+
}
180+
181+
.response-country-flag svg {
182+
fill: currentColor;
183+
vertical-align: text-top;
184+
}
185+
174186
.jp-forms__inbox-response-data-value .file-field {
175187
margin-top: 4px;
176188

projects/packages/forms/src/dashboard/inbox/utils.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,3 +73,21 @@ export const updateMenuCounterOptimistically = count => {
7373
* @return {string} The ID of the item.
7474
*/
7575
export const getItemId = item => item?.id?.toString() ?? '';
76+
77+
/**
78+
* Get the country flag emoji from a country code.
79+
*
80+
* @param {string|null} countryCode - Two-letter ISO 3166-1 alpha-2 country code (e.g., "US") or null if unknown.
81+
* @return {string} The country flag emoji.
82+
*/
83+
export const getCountryFlagEmoji = countryCode => {
84+
if ( ! countryCode ) {
85+
return '';
86+
}
87+
const upperCountryCode = countryCode.toUpperCase();
88+
const offset = 127397;
89+
return String.fromCodePoint(
90+
upperCountryCode.charCodeAt( 0 ) + offset,
91+
upperCountryCode.charCodeAt( 1 ) + offset
92+
);
93+
};

projects/packages/forms/src/types/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,8 @@ export interface FormResponse {
100100
author_avatar: string;
101101
/** The IP address of the response author. */
102102
ip: string;
103+
/** The country code of the response author. */
104+
country_code: string;
103105
/** The title of the form that the response was submitted to. */
104106
entry_title: string;
105107
/** The permalink of the form that the response was submitted to. */
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Significance: minor
2+
Type: enhancement
3+
4+
Forms: add flag next to the IP Address

0 commit comments

Comments
 (0)