From c135be47b8c39f13e95d6404b23a2fd8137fa3f4 Mon Sep 17 00:00:00 2001 From: z0ccc Date: Mon, 17 Oct 2022 20:51:53 -0400 Subject: [PATCH] Set true default values for system data --- src/Popup/Pages/AutofillPage/index.tsx | 42 +++++++++++++------------- src/Popup/Pages/SystemPage/index.tsx | 38 +++++++++++++++-------- src/Popup/Popup.tsx | 35 +++++++++++++-------- 3 files changed, 69 insertions(+), 46 deletions(-) diff --git a/src/Popup/Pages/AutofillPage/index.tsx b/src/Popup/Pages/AutofillPage/index.tsx index 9aaa91a..6b74b2d 100644 --- a/src/Popup/Pages/AutofillPage/index.tsx +++ b/src/Popup/Pages/AutofillPage/index.tsx @@ -9,10 +9,10 @@ import TableRow from '../../Components/TableRow' interface AutofillPageProps { tab: string ipData?: ipData - reverseGeocoding: any + // reverseGeocoding: any } -const AutofillPage = ({ tab, ipData, reverseGeocoding }: AutofillPageProps) => { +const AutofillPage = ({ tab, ipData }: AutofillPageProps) => { const [country, setCountry] = useState('') const [city, setCity] = useState('') const [region, setRegion] = useState('') @@ -67,25 +67,25 @@ const AutofillPage = ({ tab, ipData, reverseGeocoding }: AutofillPageProps) => { // }) }, [ipData, setCity, setPostCode, setRegion]) - useEffect(() => { - if (!postCode && reverseGeocoding?.postcode) { - setPostCode(reverseGeocoding?.postcode) - chrome.storage.local.set({ - postCode: reverseGeocoding?.postcode, - }) - } - if (reverseGeocoding?.house_number && reverseGeocoding?.road) { - setAddress(`${reverseGeocoding.house_number} ${reverseGeocoding.road}`) - chrome.storage.local.set({ - address: `${reverseGeocoding.house_number} ${reverseGeocoding.road}`, - }) - } else if (reverseGeocoding?.road) { - setAddress(reverseGeocoding.road) - chrome.storage.local.set({ - address: reverseGeocoding.road, - }) - } - }, [postCode, reverseGeocoding, setAddress]) + // useEffect(() => { + // if (!postCode && reverseGeocoding?.postcode) { + // setPostCode(reverseGeocoding?.postcode) + // chrome.storage.local.set({ + // postCode: reverseGeocoding?.postcode, + // }) + // } + // if (reverseGeocoding?.house_number && reverseGeocoding?.road) { + // setAddress(`${reverseGeocoding.house_number} ${reverseGeocoding.road}`) + // chrome.storage.local.set({ + // address: `${reverseGeocoding.house_number} ${reverseGeocoding.road}`, + // }) + // } else if (reverseGeocoding?.road) { + // setAddress(reverseGeocoding.road) + // chrome.storage.local.set({ + // address: reverseGeocoding.road, + // }) + // } + // }, [postCode, reverseGeocoding, setAddress]) // const changeUserAgent = () => { // // if (userAgentType !== 'custom') { diff --git a/src/Popup/Pages/SystemPage/index.tsx b/src/Popup/Pages/SystemPage/index.tsx index 03a41fd..381b272 100644 --- a/src/Popup/Pages/SystemPage/index.tsx +++ b/src/Popup/Pages/SystemPage/index.tsx @@ -11,9 +11,10 @@ import CheckBox from '../../Components/CheckBox' interface SystemPageProps { tab: string ipData?: ipData + geolocation?: GeolocationCoordinates } -const SystemPage = ({ tab, ipData }: SystemPageProps) => { +const SystemPage = ({ tab, ipData, geolocation }: SystemPageProps) => { const [systemType, setSystemType] = useState('') const [timezone, setTimezone] = useState('') const [locale, setLocale] = useState('') @@ -21,22 +22,33 @@ const SystemPage = ({ tab, ipData }: SystemPageProps) => { const [lon, setLongitude] = useState('') const [configuration, setConfiguration] = useState('custom') + // console.log(geolocation) + useEffect(() => { chrome.storage.local.get( ['systemType', 'configuration', 'timezone', 'locale', 'lat', 'lon'], (storage) => { - console.log(ipData) - if (storage.systemType === 'matchIp' && ipData) { - setTimezone(ipData.timezone) - setLocale(countryLocales[ipData.countryCode].locale) - setLatitude(`${ipData.lat}`) - setLongitude(`${ipData.lon}`) + if (ipData) { chrome.storage.local.set({ timezone: ipData.timezone, locale: countryLocales[ipData.countryCode].locale, lat: ipData.lat, lon: ipData.lon, }) + } + 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}`) } else if (storage.systemType === 'custom') { storage.configuration && setConfiguration(storage.configuration) storage.timezone && setTimezone(storage.timezone) @@ -49,7 +61,7 @@ const SystemPage = ({ tab, ipData }: SystemPageProps) => { : setSystemType('default') } ) - }, [ipData]) + }, [geolocation, ipData]) const changeType = (e: ChangeEvent) => { detachDebugger() @@ -57,10 +69,12 @@ const SystemPage = ({ tab, ipData }: SystemPageProps) => { chrome.storage.local.set({ systemType: e.target.value }) if (e.target.value === 'default') { - setTimezone('') - setLocale('') - setLatitude('') - setLongitude('') + setTimezone(Intl.DateTimeFormat().resolvedOptions().timeZone) + setLocale(Intl.DateTimeFormat().resolvedOptions().locale) + if (geolocation) { + setLatitude(`${geolocation.latitude}`) + setLongitude(`${geolocation.longitude}`) + } chrome.storage.local.set({ timezone: '', locale: '', diff --git a/src/Popup/Popup.tsx b/src/Popup/Popup.tsx index 1e2f5a9..dcf0ee8 100644 --- a/src/Popup/Popup.tsx +++ b/src/Popup/Popup.tsx @@ -18,27 +18,36 @@ import WebRtcPage from './Pages/WebRtcPage' import ConnectionPage from './Pages/ConnectionPage' import { ipData } from '../types' import getIp from '../utils/getIp' -import getReverseGeocoding from '../utils/getReverseGeocoding' +// import getReverseGeocoding from '../utils/getReverseGeocoding' import '../assets/global.css' import OtherOptionsPage from './Pages/OtherOptionsPage' const Popup = () => { const [tab, setTab] = useState('autofill') - const [ipData, setIpData] = useState(undefined) - const [reverseGeocoding, setReverseGeocoding] = useState(undefined) + const [ipData, setIpData] = useState() + // const [reverseGeocoding, setReverseGeocoding] = useState(undefined) + const [geolocation, setGeolocation] = useState() useEffect(() => { getIp().then((ipDataRes) => { setIpData(ipDataRes) - if (ipDataRes.lat && ipDataRes.lon) { - getReverseGeocoding(ipDataRes.lat, ipDataRes.lon).then( - (reverseGeocodingRes) => { - setReverseGeocoding(reverseGeocodingRes) - console.log(reverseGeocodingRes) - } - ) - } + // if (ipDataRes.lat && ipDataRes.lon) { + // getReverseGeocoding(ipDataRes.lat, ipDataRes.lon).then( + // (reverseGeocodingRes) => { + // setReverseGeocoding(reverseGeocodingRes) + // console.log(reverseGeocodingRes) + // } + // ) + // } }) + navigator.geolocation.getCurrentPosition( + (pos) => setGeolocation(pos.coords), + (err) => console.warn(`ERROR(${err.code}): ${err.message}`), + { + enableHighAccuracy: true, + timeout: 5000, + } + ) }, []) return ( @@ -99,11 +108,11 @@ const Popup = () => { - +