display 'error' if ip fetch fails

This commit is contained in:
z0ccc 2022-12-17 21:12:58 -05:00
parent d98b63ebc8
commit 65c01759f7

View file

@ -1,4 +1,4 @@
import { useState, useEffect, ChangeEvent } from 'react' import { useState, useEffect, ChangeEvent, useCallback } from 'react'
import { Box, Flex, Label, Radio, Select } from 'theme-ui' import { Box, Flex, Label, Radio, Select } from 'theme-ui'
import Page from '../../Components/Page' import Page from '../../Components/Page'
import DebouncedInput from '../../Components/DebouncedInput' import DebouncedInput from '../../Components/DebouncedInput'
@ -16,6 +16,7 @@ interface LocationPageProps {
const LocationPage = ({ tab }: LocationPageProps) => { const LocationPage = ({ tab }: LocationPageProps) => {
const [ipData, setIpData] = useState<ipData>() const [ipData, setIpData] = useState<ipData>()
const [ipInfo, setIpInfo] = useState('loading...')
const [locationType, setLocationType] = useState('') const [locationType, setLocationType] = useState('')
const [timezone, setTimezone] = useState('') const [timezone, setTimezone] = useState('')
const [locale, setLocale] = useState('') const [locale, setLocale] = useState('')
@ -23,12 +24,22 @@ const LocationPage = ({ tab }: LocationPageProps) => {
const [lon, setLongitude] = useState('') const [lon, setLongitude] = useState('')
const [configuration, setConfiguration] = useState('custom') const [configuration, setConfiguration] = useState('custom')
useEffect(() => { const reloadIp = useCallback(() => {
getIp().then((ipDataRes) => { setIpInfo('loading...')
setIpData(ipDataRes) getIp()
}) .then((ipDataRes) => {
setIpData(ipDataRes)
setIpInfo(`${getFlagEmoji(ipDataRes.countryCode)} ${ipDataRes.query}`)
})
.catch(() => {
setIpInfo('error')
})
}, []) }, [])
useEffect(() => {
reloadIp()
}, [reloadIp])
useEffect(() => { useEffect(() => {
chrome.storage.local.get( chrome.storage.local.get(
['locationType', 'configuration', 'timezone', 'locale', 'lat', 'lon'], ['locationType', 'configuration', 'timezone', 'locale', 'lat', 'lon'],
@ -202,9 +213,7 @@ const LocationPage = ({ tab }: LocationPageProps) => {
}} }}
title={ipData?.query} title={ipData?.query}
> >
{ipData {ipInfo}
? `${getFlagEmoji(ipData.countryCode)} ${ipData?.query}`
: 'loading...'}
</Box> </Box>
<RotateCw <RotateCw
size={20} size={20}
@ -212,10 +221,7 @@ const LocationPage = ({ tab }: LocationPageProps) => {
cursor: 'pointer', cursor: 'pointer',
}} }}
onClick={() => { onClick={() => {
setIpData(undefined) reloadIp()
getIp().then((ipDataRes) => {
setIpData(ipDataRes)
})
}} }}
/> />
</Flex> </Flex>