Skip to content

Commit

Permalink
Adds new Flag component (electricitymaps#50)
Browse files Browse the repository at this point in the history
  • Loading branch information
madsnedergaard authored Nov 25, 2022
1 parent a97c006 commit 22c59b9
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 33 deletions.
1 change: 1 addition & 0 deletions web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
"@types/d3-selection": "^3.0.3",
"@types/d3-shape": "^3.1.0",
"@types/sprintf-js": "^1.1.2",
"country-flag-icons": "^1.5.5",
"currency-symbol-map": "^5.1.0",
"d3-array": "^3.2.0",
"d3-scale": "^4.0.2",
Expand Down
6 changes: 6 additions & 0 deletions web/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

65 changes: 34 additions & 31 deletions web/src/components/Flag.tsx
Original file line number Diff line number Diff line change
@@ -1,42 +1,45 @@
import { resolvePath } from 'react-router-dom';
import flags from 'country-flag-icons/react/3x2';

const DEFAULT_FLAG_SIZE = 48;
const DEFAULT_FLAG_SIZE = 18;

const getFlagFileName = (zoneId: string) => {
// Return (zonesConfig[zoneId.toUpperCase()] || {}).flag_file_name; TODO: enable when config setup
if (zoneId === 'DK-DK1') {
return 'dk.png';
}

if (zoneId === 'ES') {
return 'es.png';
}
const SPECIAL_ZONE_NAMES = {
AUS: 'AU',
} as { [index: string]: string };

return 'us.png';
};
function getCountryName(zoneId: string) {
const country = zoneId.split('-')[0];
const flagName = SPECIAL_ZONE_NAMES[country] || country;
return flagName.toUpperCase();
}

const getFlagUri = function (zoneId: string, flagSize = DEFAULT_FLAG_SIZE) {
if (!zoneId) {
return;
}
const flagFile = getFlagFileName(zoneId);
return resolvePath(`flags_iso/${flagSize}/${flagFile}`).pathname;
};
type HTMLSVGElement = HTMLElement & SVGElement;
interface CountryFlagProps
extends React.HTMLAttributes<HTMLSVGElement>,
React.SVGAttributes<HTMLSVGElement> {
zoneId: string;
size?: number;
}

export function CountryFlag({
zoneId,
flagSize = DEFAULT_FLAG_SIZE,
size = DEFAULT_FLAG_SIZE,
...props
}: {
zoneId: string;
flagSize?: number;
className?: string;
}) {
const flagUri = getFlagUri(zoneId, flagSize);
}: CountryFlagProps) {
const countryName = getCountryName(zoneId) as keyof typeof flags;
const FlagIcon = flags[countryName];

if (flagUri === undefined) {
return <div>No flag</div>;
if (!FlagIcon) {
return <span className={`text-[14px]`}>🏴‍☠️</span>;
}

return <img src={flagUri} alt={zoneId} {...props} />;
return (
<FlagIcon
title="TODO"
width={size}
height={Math.floor((size / 3) * 2)}
style={{
minWidth: size,
}}
{...props}
/>
);
}
4 changes: 2 additions & 2 deletions web/src/features/panels/zone/CountryTag.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ export function CountryTag({ zoneId }: { zoneId: string }) {
const isACountry = zoneId !== 'US' && zoneId.length === 2; // TODO: set up proper method to check if zoneId is a country

if (isACountry) {
return <CountryFlag zoneId={zoneId} flagSize={16} className="inline-flex" />;
return <CountryFlag zoneId={zoneId} size={16} className="inline-flex" />;
}

return (
<span className="inline-flex items-center gap-1 rounded-full bg-gray-200 px-2 py-0.5 text-xs dark:bg-gray-900">
<CountryFlag zoneId={zoneId} flagSize={16} />
<CountryFlag zoneId={zoneId} size={16} />
<span>{countryName}</span>
</span>
);
Expand Down

0 comments on commit 22c59b9

Please sign in to comment.