File size: 631 Bytes
e3278e4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import * as React from "react";
import { getCountryFromIP } from "./ip_lookup";

interface CountryCellProps {
  ipAddress: string | null;
}

export const CountryCell: React.FC<CountryCellProps> = ({ ipAddress }) => {
  const [country, setCountry] = React.useState<string>("-");

  React.useEffect(() => {
    if (!ipAddress) return;
    
    let mounted = true;
    getCountryFromIP(ipAddress)
      .then(result => {
        if (mounted) setCountry(result);
      })
      .catch(() => {
        if (mounted) setCountry("-");
      });

    return () => { mounted = false };
  }, [ipAddress]);

  return <span>{country}</span>;
};