Skip to content

Instantly share code, notes, and snippets.

@chalkpe
Last active May 17, 2021 10:05
Show Gist options
  • Save chalkpe/84e28a2af9bbb222d23116bfbf7c72b0 to your computer and use it in GitHub Desktop.
Save chalkpe/84e28a2af9bbb222d23116bfbf7c72b0 to your computer and use it in GitHub Desktop.
React component for IP address w/ country flag emoji
import React from 'react';
import axios from 'axios';
import { countryCodeEmoji } from 'country-code-emoji';
type TextRenderer = (v: string) => React.ReactNode | string;
type SetCountry = React.Dispatch<React.SetStateAction<string>>;
type CountryEffect = (ip: string, setter: SetCountry) => void | (() => void);
interface IPAddressProps {
ip: string;
port?: string;
style?: React.CSSProperties;
hiddenPorts?: string[];
hiddenCountries?: string[];
renderIp?: TextRenderer;
renderPort?: TextRenderer;
renderCountry?: TextRenderer;
countryEffect?: CountryEffect;
}
const defaultRenderIp = (ip: string) => (
<span aria-label="IP address">{ip}</span>
);
const defaultRenderPort = (port: string) => (
<span aria-label="Port number" style={{ color: 'rgba(0, 0, 0, 0.45)' }}>
:{port}
</span>
);
const defaultRenderCountry = (country: string) => (
<span aria-label={country} role="img" style={{ marginLeft: '0.2em' }}>
{countryCodeEmoji(country)}
</span>
);
const defaultCountryEffect: CountryEffect = (ip, setCountry) => {
const url = `https://api.hostip.info/country.php?ip=${ip}`;
const { token: cancelToken, cancel } = axios.CancelToken.source();
axios
.get<string>(url, { cancelToken })
.then((res) => setCountry(res.data.trim()))
.catch((err) => axios.isCancel(err) || console.error(err));
return () => cancel();
};
function IPAddress({
ip,
port,
style,
hiddenPorts = ['80', '443'],
hiddenCountries = ['XX'],
renderIp = defaultRenderIp,
renderPort = defaultRenderPort,
renderCountry = defaultRenderCountry,
countryEffect = defaultCountryEffect,
}: IPAddressProps) {
const [country, setCountry] = React.useState('');
React.useEffect(() => countryEffect(ip, setCountry), [ip, countryEffect]);
return (
<span className="IPAddress" style={style}>
{ip && renderIp(ip)}
{port && !hiddenPorts.includes(port) && renderPort(port)}
{country && !hiddenCountries.includes(country) && renderCountry(country)}
</span>
);
}
export default React.memo(IPAddress);
export type { IPAddressProps, TextRenderer, SetCountry, CountryEffect };
MIT License
Copyright (c) 2021 Chalk
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment