import { useState, useEffect, ChangeEvent } from 'react' import { Box, Flex, Label, Radio, Select } from 'theme-ui' import LocationInput from './LocationInput' import detachDebugger from '../../../utils/detachDebugger' import countryLocales from '../../../utils/countryLocales' import { ipData } from '../../../types' import configurations from '../../../utils/configurations' interface SystemPageProps { tab: string ipData?: ipData } const SystemPage = ({ tab, ipData }: 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') useEffect(() => { chrome.storage.local.get( ['systemType', 'configuration', 'timezone', 'locale', 'lat', 'lon'], (storage) => { if (storage.systemType === 'custom') { console.log(storage.configuration) 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') } ) }, []) const changeType = (e: ChangeEvent) => { detachDebugger() setSystemType(e.target.value) chrome.storage.local.set({ systemType: e.target.value }) if (e.target.value === 'default') { setTimezone('') setLocale('') setLatitude('') setLongitude('') 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') { console.log(2) setConfiguration('custom') setSystemType('custom') chrome.storage.local.set({ configuration: 'custom', systemType: 'custom', }) } } return ( System Data {systemType === 'custom' && ( <> )} ) } export default SystemPage