Skip to content

Commit 6eb9ad4

Browse files
authored
Merge pull request #119 from Automattic/fix/hovercards-issues
Fix: URL params & error msg
2 parents aacc7bc + 7ee5898 commit 6eb9ad4

File tree

3 files changed

+30
-8
lines changed

3 files changed

+30
-8
lines changed

web/packages/hovercards/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -492,6 +492,7 @@ The following phrases are used:
492492
- `Edit your profile`
493493
- `View profile`
494494
- `Sorry, we are unable to load this Gravatar profile.`
495+
- `Profile not found.`
495496
- `Too Many Requests.`
496497
- `Internal Server Error.`
497498

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/**
2+
* Adds or updates a query parameter to the given URL.
3+
*
4+
* @param {string} url - The URL to which the query parameter will be added.
5+
* @param {string} key - The query parameter key to add or update.
6+
* @param {string} value - The value of the query parameter to add or update.
7+
* @return {string} - The updated URL with the new or updated query parameter, or an empty string if the URL is invalid.
8+
*/
9+
export default function addQueryArg( url: string, key: string, value: string ): string {
10+
const [ baseUrl, queryStr ] = url.split( '?' );
11+
const queryParams = new URLSearchParams( queryStr || '' );
12+
13+
queryParams.set( key, value );
14+
15+
return `${ baseUrl }?${ queryParams.toString() }`;
16+
}

web/packages/hovercards/src/core.ts

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type { Placement } from './compute-position';
22
import computePosition from './compute-position';
33
import { escUrl, escHtml } from './sanitizer';
4+
import addQueryArg from './add-query-arg';
45
import __ from './i18n';
56

67
interface AccountData {
@@ -255,7 +256,7 @@ export default class Hovercards {
255256
const hovercard = dc.createElement( 'div' );
256257
hovercard.className = `gravatar-hovercard${ additionalClass ? ` ${ additionalClass }` : '' }`;
257258

258-
const trackedProfileUrl = escUrl( `${ profileUrl }?utm_source=hovercard` );
259+
const trackedProfileUrl = escUrl( addQueryArg( profileUrl, 'utm_source', 'hovercard' ) );
259260
const username = escHtml( displayName );
260261
const isEditProfile = ! description && myHash === hash;
261262
const renderSocialLinks = verifiedAccounts
@@ -411,7 +412,7 @@ export default class Hovercards {
411412

412413
this._onFetchProfileStart( hash );
413414

414-
fetch( `${ BASE_API_URL }/${ hash }?source=hovercard` )
415+
fetch( addQueryArg( `${ BASE_API_URL }/${ hash }`, 'source', 'hovercard' ) )
415416
.then( ( res ) => {
416417
// API error handling
417418
if ( res.status !== 200 ) {
@@ -457,12 +458,16 @@ export default class Hovercards {
457458
.catch( ( code ) => {
458459
let message = __( this._i18n, 'Sorry, we are unable to load this Gravatar profile.' );
459460

460-
if ( code === 429 ) {
461-
message = __( this._i18n, 'Too Many Requests.' );
462-
}
463-
464-
if ( code === 500 ) {
465-
message = __( this._i18n, 'Internal Server Error.' );
461+
switch ( code ) {
462+
case 404:
463+
message = __( this._i18n, 'Profile not found.' );
464+
break;
465+
case 429:
466+
message = __( this._i18n, 'Too Many Requests.' );
467+
break;
468+
case 500:
469+
message = __( this._i18n, 'Internal Server Error.' );
470+
break;
466471
}
467472

468473
const hovercardInner = Hovercards.createHovercardError(

0 commit comments

Comments
 (0)