Skip to content

Commit 3a22510

Browse files
authored
Forms: add countryname-tooltip to IP-flags (#45886)
1 parent a83b8b6 commit 3a22510

File tree

7 files changed

+50
-28
lines changed

7 files changed

+50
-28
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: changed
3+
4+
Forms: show country name as tooltip in IP flags

projects/packages/forms/src/blocks/field-telephone/edit.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,13 @@ import { useCallback, useEffect, useMemo, useState } from '@wordpress/element';
1515
import { __ } from '@wordpress/i18n';
1616
import { globe } from '@wordpress/icons';
1717
import clsx from 'clsx';
18+
import { getTranslatedCountryName } from '../../util/country-names-translated';
1819
import JetpackFieldControls from '../shared/components/jetpack-field-controls';
1920
import useFieldSelected from '../shared/hooks/use-field-selected';
2021
import useFormWrapper from '../shared/hooks/use-form-wrapper';
2122
import useJetpackFieldStyles from '../shared/hooks/use-jetpack-field-styles';
2223
import useSyncRequiredIndicator from '../shared/hooks/use-sync-required-indicator';
2324
import { countries } from './country-list';
24-
import { getTranslatedCountryName } from './country-names-translated';
2525

2626
const EMPTY_ARRAY = [];
2727

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/**
2+
* External dependencies
3+
*/
4+
import { Tooltip } from '@wordpress/components';
5+
import { getTranslatedCountryName } from '../../../util/country-names-translated';
6+
7+
type FlagProps = {
8+
countryCode?: string;
9+
};
10+
11+
/**
12+
* Render the country flag emoji based on a country code.
13+
*
14+
* @param {FlagProps} props - The component props.
15+
* @param {string|undefined} props.countryCode - Two-letter ISO 3166-1 alpha-2 country code (e.g., "US") or undefined if unknown.
16+
* @return {JSX.Element} The Flag component
17+
*/
18+
export default function Flag( { countryCode }: FlagProps ): JSX.Element | null {
19+
if ( ! countryCode ) {
20+
return null;
21+
}
22+
const upperCountryCode = countryCode.toUpperCase();
23+
const offset = 127397;
24+
25+
const flag = String.fromCodePoint(
26+
upperCountryCode.charCodeAt( 0 ) + offset,
27+
upperCountryCode.charCodeAt( 1 ) + offset
28+
);
29+
30+
const countryName = getTranslatedCountryName( countryCode );
31+
32+
return (
33+
<Tooltip text={ countryName }>
34+
<span aria-label={ countryName } role="img">
35+
{ flag }
36+
</span>
37+
</Tooltip>
38+
);
39+
}

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

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,11 @@ import photon from 'photon';
2828
*/
2929
import useConfigValue from '../../../hooks/use-config-value';
3030
import CopyClipboardButton from '../../components/copy-clipboard-button';
31+
import Flag from '../../components/flag';
3132
import Gravatar from '../../components/gravatar';
3233
import useInboxData from '../../hooks/use-inbox-data';
3334
import { useMarkAsSpam } from '../../hooks/use-mark-as-spam';
34-
import {
35-
getPath,
36-
updateMenuCounter,
37-
updateMenuCounterOptimistically,
38-
getCountryFlagEmoji,
39-
} from '../../inbox/utils';
35+
import { getPath, updateMenuCounter, updateMenuCounterOptimistically } from '../../inbox/utils';
4036
import { store as dashboardStore } from '../../store';
4137
import type { FormResponse } from '../../../types';
4238

@@ -482,7 +478,7 @@ const ResponseViewBody = ( {
482478
<td>
483479
{ response.country_code && (
484480
<span className="jp-forms__inbox-response-meta-country-flag response-country-flag">
485-
{ getCountryFlagEmoji( response.country_code ) }
481+
<Flag countryCode={ response.country_code } />
486482
</span>
487483
) }
488484
<Tooltip text={ __( 'Lookup IP address', 'jetpack-forms' ) }>

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,13 @@ import { useSearchParams } from 'react-router';
2121
/**
2222
* Internal dependencies
2323
*/
24+
import Flag from '../../components/flag';
2425
import Gravatar from '../../components/gravatar';
2526
import InboxStatusToggle from '../../components/inbox-status-toggle';
2627
import { ResponseMobileView, SingleResponseView } from '../../components/response-view';
2728
import useInboxData from '../../hooks/use-inbox-data';
2829
import EmptyResponses from '../empty-responses';
29-
import { getPath, getItemId, getCountryFlagEmoji } from '../utils.js';
30+
import { getPath, getItemId } from '../utils.js';
3031
import {
3132
viewAction,
3233
markAsSpamAction,
@@ -368,7 +369,7 @@ export default function InboxView() {
368369
<>
369370
<span className="response-country-flag">
370371
{ ! item.country_code && <Icon icon={ globe } size={ 20 } /> }
371-
{ item.country_code && getCountryFlagEmoji( item.country_code ) }
372+
{ item.country_code && <Flag countryCode={ item.country_code } /> }
372373
</span>
373374
{ item.ip || '' }
374375
</>

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

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -73,21 +73,3 @@ 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-
};

0 commit comments

Comments
 (0)