Skip to content
Merged
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
5 changes: 5 additions & 0 deletions src/App.css
Original file line number Diff line number Diff line change
Expand Up @@ -866,6 +866,11 @@
white-space: nowrap;
}

.country-list__flag {
display: inline-block;
margin-right: 8px;
}

.country-list__bar {
height: 12px;
overflow: hidden;
Expand Down
17 changes: 15 additions & 2 deletions src/components/CountryList.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import React from 'react';

import { formatNumber, formatPercent, getCountryName } from '../lib/formatters';
import {
formatNumber,
formatPercent,
getCountryFlag,
getCountryName,
} from '../lib/formatters';

export default function CountryList(props) {
return (
Expand All @@ -10,12 +15,20 @@ export default function CountryList(props) {
const count = Number(entry[1].masternodes || 0);
const share = props.enabledCount ? (count / props.enabledCount) * 100 : 0;
const minimumWidth = props.minimumWidth === undefined ? 4 : props.minimumWidth;
const flag = getCountryFlag(code);

return (
<div key={code} className="country-list__row">
<div className="country-list__label">
<span className="country-list__count">{formatNumber(count)} nodes</span>
<strong>{getCountryName(code)}</strong>
<strong>
{flag ? (
<span className="country-list__flag" aria-hidden="true">
{flag}
</span>
) : null}
{getCountryName(code)}
</strong>
</div>
<div className="country-list__bar">
<span style={{ width: `${Math.max(share, minimumWidth)}%` }} />
Expand Down
41 changes: 41 additions & 0 deletions src/lib/formatters.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,31 @@ const COUNTRY_NAMES = {
USA: 'United States',
};

const COUNTRY_FLAG_ALPHA2 = {
ARE: 'AE',
AUT: 'AT',
BGR: 'BG',
BRA: 'BR',
CAN: 'CA',
CYP: 'CY',
DEU: 'DE',
DNK: 'DK',
FIN: 'FI',
FRA: 'FR',
GBR: 'GB',
IND: 'IN',
ITA: 'IT',
JPN: 'JP',
LTU: 'LT',
NLD: 'NL',
NOR: 'NO',
POL: 'PL',
SGP: 'SG',
SWE: 'SE',
TUR: 'TR',
USA: 'US',
};

export function parseNumber(value) {
if (typeof value === 'number') {
return Number.isFinite(value) ? value : 0;
Expand Down Expand Up @@ -222,6 +247,22 @@ export function getCountryName(code) {
return COUNTRY_NAMES[code] || code;
}

export function getCountryFlag(code) {
if (code === 'IRN') {
return '🌐';
}

const alpha2 = COUNTRY_FLAG_ALPHA2[code];
if (!alpha2) {
return '';
}

return alpha2
.split('')
.map((letter) => String.fromCodePoint(127397 + letter.charCodeAt(0)))
.join('');
}

export function getProposalDurationMonths(startEpoch, endEpoch) {
if (!startEpoch || !endEpoch || endEpoch <= startEpoch) {
return 1;
Expand Down
8 changes: 8 additions & 0 deletions src/lib/formatters.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
formatDayMonth,
formatShortDate,
formatUtcTime,
getCountryFlag,
getCountryName,
} from './formatters';

Expand All @@ -23,4 +24,11 @@ describe('country formatters', () => {
expect(getCountryName('ARE')).toBe('United Arab Emirates');
expect(getCountryName('IRN')).toBe('Middle East');
});

test('formats country flags from alpha-3 country codes', () => {
expect(getCountryFlag('ARE')).toBe('🇦🇪');
expect(getCountryFlag('DEU')).toBe('🇩🇪');
expect(getCountryFlag('IRN')).toBe('🌐');
expect(getCountryFlag('UNKNOWN')).toBe('');
});
});
Loading