Cleaned up user agent logic and added debouced input

This commit is contained in:
z0ccc 2022-10-08 21:06:03 -04:00
parent e84be393f3
commit 19e5199e84
6 changed files with 68 additions and 100 deletions

View file

@ -1,12 +1,12 @@
import { Label, Checkbox } from 'theme-ui' import { Label, Checkbox } from 'theme-ui'
interface SystemPageProps { interface CheckBoxProps {
title: string title: string
onChange?: () => void onChange?: () => void
checked?: boolean checked?: boolean
} }
const CheckBox = ({ title, onChange, checked }: SystemPageProps) => { const CheckBox = ({ title, onChange, checked }: CheckBoxProps) => {
return ( return (
<Label sx={{ mb: '8px' }}> <Label sx={{ mb: '8px' }}>
<Checkbox onChange={onChange} checked={checked} /> <Checkbox onChange={onChange} checked={checked} />

View file

@ -1,9 +1,9 @@
import { Dispatch, SetStateAction, ChangeEvent, useMemo } from 'react' import { Dispatch, SetStateAction, ChangeEvent, useMemo } from 'react'
import { Label, Input } from 'theme-ui' import { Label, Input } from 'theme-ui'
import detachDebugger from '../../../utils/detachDebugger' import detachDebugger from '../../utils/detachDebugger'
import debounce from 'lodash.debounce' import debounce from 'lodash.debounce'
interface SystemInputProps { interface DebouncedInputProps {
name: string name: string
title: string title: string
value: string value: string
@ -11,13 +11,13 @@ interface SystemInputProps {
onChange: () => void onChange: () => void
} }
const SystemInput = ({ const DebouncedInput = ({
name, name,
title, title,
value, value,
setValue, setValue,
onChange, onChange,
}: SystemInputProps) => { }: DebouncedInputProps) => {
const debouncedChangeHandler = useMemo( const debouncedChangeHandler = useMemo(
() => () =>
debounce((e) => { debounce((e) => {
@ -41,4 +41,4 @@ const SystemInput = ({
) )
} }
export default SystemInput export default DebouncedInput

View file

@ -1,6 +1,6 @@
import { useState, useEffect, ChangeEvent } from 'react' import { useState, useEffect, ChangeEvent } from 'react'
import { Box, Flex, Label, Radio, Select } from 'theme-ui' import { Box, Flex, Label, Radio, Select } from 'theme-ui'
import SystemInput from './SystemInput' import DebouncedInput from '../../Components/DebouncedInput'
import detachDebugger from '../../../utils/detachDebugger' import detachDebugger from '../../../utils/detachDebugger'
import countryLocales from '../../../utils/countryLocales' import countryLocales from '../../../utils/countryLocales'
import { ipData } from '../../../types' import { ipData } from '../../../types'
@ -173,28 +173,28 @@ const SystemPage = ({ tab, ipData }: SystemPageProps) => {
</Select> </Select>
</> </>
)} )}
<SystemInput <DebouncedInput
name="timezone" name="timezone"
title="Timezone" title="Timezone"
value={timezone} value={timezone}
setValue={setTimezone} setValue={setTimezone}
onChange={changeInputText} onChange={changeInputText}
/> />
<SystemInput <DebouncedInput
name="locale" name="locale"
title="Locale" title="Locale"
value={locale} value={locale}
setValue={setLocale} setValue={setLocale}
onChange={changeInputText} onChange={changeInputText}
/> />
<SystemInput <DebouncedInput
name="lat" name="lat"
title="Latitude" title="Latitude"
value={lat} value={lat}
setValue={setLatitude} setValue={setLatitude}
onChange={changeInputText} onChange={changeInputText}
/> />
<SystemInput <DebouncedInput
name="lon" name="lon"
title="Longitude" title="Longitude"
value={lon} value={lon}

View file

@ -1,50 +0,0 @@
import { Dispatch, SetStateAction, ChangeEvent } from 'react'
import { Label, Select } from 'theme-ui'
import configurations from '../../../utils/configurations'
import detachDebugger from '../../../utils/detachDebugger'
interface ConfigurationSelectProps {
title: string
configuration: string
setConfiguration: Dispatch<SetStateAction<string>>
}
const UserAgentSelect = ({
title,
configuration,
setConfiguration,
}: ConfigurationSelectProps) => {
const changeConfiguration = (e: ChangeEvent<HTMLSelectElement>) => {
detachDebugger()
chrome.storage.local.set({
configuration: e.target.value,
})
setConfiguration(e.target.value)
}
return (
<>
<Label htmlFor="configuration">{title}</Label>
<Select
name="configuration"
id="configuration"
value={configuration}
onChange={changeConfiguration}
mb={'8px'}
>
<option value="none">None</option>
<option value="match">Match IP</option>
<option value="custom">Custom</option>
<optgroup label="Locations">
{Object.keys(configurations).map((key) => (
<option value={key} key={key}>
{configurations[key].name}
</option>
))}
</optgroup>
</Select>
</>
)
}
export default UserAgentSelect

View file

@ -1,5 +1,6 @@
import { useState, useEffect, ChangeEvent } from 'react' import { useState, useEffect, ChangeEvent } from 'react'
import { Box, Label, Radio, Flex, Input, Select } from 'theme-ui' import { Box, Label, Radio, Flex, Select } from 'theme-ui'
import DebouncedInput from '../../Components/DebouncedInput'
import userAgents from '../../../utils/userAgents' import userAgents from '../../../utils/userAgents'
import detachDebugger from '../../../utils/detachDebugger' import detachDebugger from '../../../utils/detachDebugger'
@ -8,16 +9,16 @@ interface UserAgentPageProps {
} }
const UserAgentPage = ({ tab }: UserAgentPageProps) => { const UserAgentPage = ({ tab }: UserAgentPageProps) => {
const [type, setType] = useState('none') const [userAgentType, setUserAgentType] = useState('none')
const [operatingSystem, setOperatingSystem] = useState('Windows') const [operatingSystem, setOperatingSystem] = useState('Windows')
const [browser, setBrowser] = useState('Chrome') const [browser, setBrowser] = useState('Chrome')
const [userAgent, setUserAgent] = useState('') const [userAgent, setUserAgent] = useState('')
useEffect(() => { useEffect(() => {
chrome.storage.local.get( chrome.storage.local.get(
['type', 'operatingSystem', 'browser', 'userAgent'], ['userAgentType', 'operatingSystem', 'browser', 'userAgent'],
(storage) => { (storage) => {
storage.type && setType(storage.type) storage.userAgentType && setUserAgentType(storage.userAgentType)
storage.operatingSystem && setOperatingSystem(storage.operatingSystem) storage.operatingSystem && setOperatingSystem(storage.operatingSystem)
storage.browser && setBrowser(storage.browser) storage.browser && setBrowser(storage.browser)
storage.userAgent && setUserAgent(storage.userAgent) storage.userAgent && setUserAgent(storage.userAgent)
@ -25,44 +26,57 @@ const UserAgentPage = ({ tab }: UserAgentPageProps) => {
) )
}, []) }, [])
useEffect(() => {
detachDebugger()
chrome.storage.local.set({ userAgent })
}, [userAgent])
useEffect(() => {
type === 'preloaded' && setUserAgent(userAgents[operatingSystem][browser])
}, [operatingSystem, browser, type])
const changeType = (e: ChangeEvent<HTMLInputElement>) => { const changeType = (e: ChangeEvent<HTMLInputElement>) => {
detachDebugger() detachDebugger()
e.target.value === 'none' && setUserAgent('') setUserAgentType(e.target.value)
chrome.storage.local.set({ type: e.target.value }) chrome.storage.local.set({ userAgentType: e.target.value })
setType(e.target.value)
if (e.target.value === 'none') {
setUserAgent('')
chrome.storage.local.set({
userAgent: '',
})
} else if (e.target.value === 'preloaded') {
setUserAgent(userAgents[operatingSystem][browser])
chrome.storage.local.set({
userAgent: userAgents[operatingSystem][browser],
})
}
} }
const changeOperatingSystem = (e: ChangeEvent<HTMLSelectElement>) => { const changeOperatingSystem = (e: ChangeEvent<HTMLSelectElement>) => {
chrome.storage.local.set({ operatingSystem: e.target.value }) detachDebugger()
setOperatingSystem(e.target.value) setOperatingSystem(e.target.value)
setUserAgent(userAgents[e.target.value][browser])
chrome.storage.local.set({
userAgent: userAgents[e.target.value][browser],
operatingSystem: e.target.value,
})
} }
const changeBrowser = (e: ChangeEvent<HTMLSelectElement>) => { const changeBrowser = (e: ChangeEvent<HTMLSelectElement>) => {
chrome.storage.local.set({ browser: e.target.value }) detachDebugger()
setBrowser(e.target.value) setBrowser(e.target.value)
setUserAgent(userAgents[operatingSystem][e.target.value])
chrome.storage.local.set({
userAgent: userAgents[operatingSystem][e.target.value],
browser: e.target.value,
})
} }
const changeUserAgent = (e: ChangeEvent<HTMLInputElement>) => { const changeUserAgent = () => {
detachDebugger() if (userAgentType !== 'custom') {
chrome.storage.local.set({ userAgent: e.target.value }) setUserAgentType('custom')
chrome.storage.local.set({ type: 'custom' }) chrome.storage.local.set({
setUserAgent(e.target.value) userAgentType: 'custom',
setType('custom') })
}
} }
return ( return (
<Box <Box
sx={{ sx={{
display: tab === 'useragent' ? 'block' : 'none', display: tab === 'userAgent' ? 'block' : 'none',
}} }}
> >
<Box sx={{ fontSize: '21px', mb: '12px', fontWeight: '600' }}> <Box sx={{ fontSize: '21px', mb: '12px', fontWeight: '600' }}>
@ -76,33 +90,33 @@ const UserAgentPage = ({ tab }: UserAgentPageProps) => {
> >
<Label> <Label>
<Radio <Radio
name="type" name="userAgentType"
value="none" value="none"
onChange={changeType} onChange={changeType}
checked={type === 'none'} checked={userAgentType === 'none'}
/> />
None None
</Label> </Label>
<Label> <Label>
<Radio <Radio
name="type" name="userAgentType"
value="preloaded" value="preloaded"
onChange={changeType} onChange={changeType}
checked={type === 'preloaded'} checked={userAgentType === 'preloaded'}
/> />
Preloaded Preloaded
</Label> </Label>
<Label> <Label>
<Radio <Radio
name="type" name="userAgentType"
value="custom" value="custom"
onChange={changeType} onChange={changeType}
checked={type === 'custom'} checked={userAgentType === 'custom'}
/> />
Custom Custom
</Label> </Label>
</Flex> </Flex>
{type === 'preloaded' && ( {userAgentType === 'preloaded' && (
<Flex sx={{ gap: '16px' }}> <Flex sx={{ gap: '16px' }}>
<Box sx={{ width: '100%' }}> <Box sx={{ width: '100%' }}>
<Label htmlFor="operatingSystem">Operating System</Label> <Label htmlFor="operatingSystem">Operating System</Label>
@ -113,7 +127,6 @@ const UserAgentPage = ({ tab }: UserAgentPageProps) => {
onChange={changeOperatingSystem} onChange={changeOperatingSystem}
mb={'8px'} mb={'8px'}
> >
<option sx={{ display: 'none' }}></option>
{Object.keys(userAgents).map((key) => ( {Object.keys(userAgents).map((key) => (
<option value={key} key={key}> <option value={key} key={key}>
{key} {key}
@ -139,8 +152,13 @@ const UserAgentPage = ({ tab }: UserAgentPageProps) => {
</Box> </Box>
</Flex> </Flex>
)} )}
<Label htmlFor="userAgent">User Agent</Label> <DebouncedInput
<Input name="userAgent" value={userAgent} onChange={changeUserAgent} /> name="userAgent"
title="User Agent"
value={userAgent}
setValue={setUserAgent}
onChange={changeUserAgent}
/>
</Box> </Box>
) )
} }

View file

@ -23,7 +23,7 @@ import '../assets/global.css'
import OtherOptionsPage from './Pages/OtherOptionsPage' import OtherOptionsPage from './Pages/OtherOptionsPage'
const Popup = () => { const Popup = () => {
const [tab, setTab] = useState('webRtc') const [tab, setTab] = useState('userAgent')
const [ipData, setIpData] = useState<ipData | undefined>(undefined) const [ipData, setIpData] = useState<ipData | undefined>(undefined)
useEffect(() => { useEffect(() => {
@ -70,8 +70,8 @@ const Popup = () => {
/> />
<TabItem <TabItem
Icon={Globe} Icon={Globe}
active={tab === 'useragent'} active={tab === 'userAgent'}
onClick={() => setTab('useragent')} onClick={() => setTab('userAgent')}
/> />
<TabItem <TabItem
Icon={Sliders} Icon={Sliders}