import { Text } from 'theme-ui' import { useState, useEffect, ChangeEvent } from 'react' import { Flex, Label, Radio, Select } from 'theme-ui' import Page from '../../Components/Page' import DebouncedInput from '../../Components/DebouncedInput' import detachDebugger from '../../../utils/detachDebugger' import countryLocales from '../../../utils/countryLocales' import { ipData } from '../../../types' import configurations from '../../../utils/configurations' import CheckBox from '../../Components/CheckBox' import FooterLink from '../../Components/FooterLink' interface SystemPageProps { tab: string ipData?: ipData geolocation?: GeolocationCoordinates } const SystemPage = ({ tab, ipData, geolocation }: SystemPageProps) => { const [systemType, setSystemType] = useState('') const [timezone, setTimezone] = useState('') const [locale, setLocale] = useState('') const [lat, setLatitude] = useState('') const [lon, setLongitude] = useState('') const [configuration, setConfiguration] = useState('custom') // console.log(geolocation) useEffect(() => { chrome.storage.local.get( ['systemType', 'configuration', 'timezone', 'locale', 'lat', 'lon'], (storage) => { if (!storage.systemType || storage.systemType === 'default') { setTimezone(Intl.DateTimeFormat().resolvedOptions().timeZone) setLocale(Intl.DateTimeFormat().resolvedOptions().locale) if (geolocation) { setLatitude(`${geolocation.latitude}`) setLongitude(`${geolocation.longitude}`) } } if (storage.systemType === 'matchIp' && ipData) { setTimezone(ipData.timezone) setLocale(countryLocales[ipData.countryCode].locale) setLatitude(`${ipData.lat}`) setLongitude(`${ipData.lon}`) chrome.storage.local.set({ timezone: ipData.timezone, locale: countryLocales[ipData.countryCode].locale, lat: ipData.lat, lon: ipData.lon, }) } else if (storage.systemType === 'custom') { storage.configuration && setConfiguration(storage.configuration) storage.timezone && setTimezone(storage.timezone) storage.locale && setLocale(storage.locale) storage.lat && setLatitude(storage.lat) storage.lon && setLongitude(storage.lon) } storage.systemType ? setSystemType(storage.systemType) : setSystemType('default') } ) }, [geolocation, ipData]) const changeType = (e: ChangeEvent) => { detachDebugger() setSystemType(e.target.value) chrome.storage.local.set({ systemType: e.target.value }) if (e.target.value === 'default') { setTimezone(Intl.DateTimeFormat().resolvedOptions().timeZone) setLocale(Intl.DateTimeFormat().resolvedOptions().locale) if (geolocation) { setLatitude(`${geolocation.latitude}`) setLongitude(`${geolocation.longitude}`) } chrome.storage.local.set({ timezone: '', locale: '', lat: '', lon: '', }) } else if (e.target.value === 'matchIp') { if (ipData) { setTimezone(ipData.timezone) setLocale(countryLocales[ipData.countryCode].locale) setLatitude(`${ipData.lat}`) setLongitude(`${ipData.lon}`) chrome.storage.local.set({ timezone: ipData.timezone, locale: countryLocales[ipData.countryCode].locale, lat: ipData.lat, lon: ipData.lon, }) } } else if (e.target.value === 'custom') if (configuration !== 'custom') { setTimezone(configurations[configuration].timezone) setLocale(configurations[configuration].locale) setLatitude(configurations[configuration].lat) setLongitude(configurations[configuration].lon) chrome.storage.local.set({ timezone: configurations[configuration].timezone, locale: configurations[configuration].locale, lat: configurations[configuration].lat, lon: configurations[configuration].lon, }) } } const changeConfiguration = (e: ChangeEvent) => { detachDebugger() setConfiguration(e.target.value) chrome.storage.local.set({ configuration: e.target.value, }) if (e.target.value !== 'custom') { setTimezone(configurations[e.target.value].timezone) setLocale(configurations[e.target.value].locale) setLatitude(configurations[e.target.value].lat) setLongitude(configurations[e.target.value].lon) chrome.storage.local.set({ timezone: configurations[e.target.value].timezone, locale: configurations[e.target.value].locale, lat: configurations[e.target.value].lat, lon: configurations[e.target.value].lon, }) } } const changeInputText = () => { if (systemType !== 'custom' || configuration !== 'custom') { setConfiguration('custom') setSystemType('custom') chrome.storage.local.set({ configuration: 'custom', systemType: 'custom', }) } } return ( {systemType === 'custom' && ( <> )} {systemType !== 'default' && ( )} ) } export default SystemPage