import React, { useState, useEffect, useRef } from 'react' import countryLocales from '../../utils/countryLocales' const LocaleSettings = ({ ip }) => { const [value, setValue] = useState('') const [matchIP, setMatchIP] = useState(false) const locale = useRef(null) useEffect(() => { if (ip) { locale.current = countryLocales[ip.countryCode].locale chrome.storage.sync.get(['locale', 'localeMatchIP'], (result) => { result.localeMatchIP && setMatchIP(result.localeMatchIP) if (result.localeMatchIP) { setValue(locale.current) chrome.storage.sync.set({ locale: locale.current }) } else if (result.locale) { setValue(result.locale) } }) } }, [ip]) const changeTextValue = (e) => { chrome.storage.sync.set({ locale: e.target.value }) setValue(e.target.value) if (matchIP) { chrome.storage.sync.set({ localeMatchIP: !matchIP }) setMatchIP(!matchIP) } } const toggleMatchIP = (e) => { chrome.storage.sync.set({ locale: locale.current, localeMatchIP: !matchIP }) !matchIP && setValue(locale.current) setMatchIP(e.target.value) } return (
) } export default LocaleSettings