-
Notifications
You must be signed in to change notification settings - Fork 105
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Closes #155: Implement User IP Button
- Loading branch information
1 parent
d4db98d
commit f6d3dfe
Showing
14 changed files
with
309 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
import { useMemo } from 'react'; | ||
import dynamic from 'next/dynamic'; | ||
import { Button, chakra, Stack, Text, VStack, useDisclosure } from '@chakra-ui/react'; | ||
import { Prompt } from '~/components'; | ||
import { useConfig, useColorValue } from '~/context'; | ||
import { useStrf, useWtf } from '~/hooks'; | ||
|
||
import type { UserIPProps } from './types'; | ||
|
||
const RightArrow = chakra( | ||
dynamic<MeronexIcon>(() => import('@meronex/icons/fa').then(i => i.FaArrowCircleRight)), | ||
); | ||
|
||
export const UserIP = (props: UserIPProps): JSX.Element => { | ||
const { setTarget } = props; | ||
const { onOpen, ...disclosure } = useDisclosure(); | ||
const strF = useStrf(); | ||
const { web } = useConfig(); | ||
|
||
const errorColor = useColorValue('red.500', 'red.300'); | ||
|
||
const noIPv4 = strF(web.text.noIp, { protocol: 'IPv4' }); | ||
const noIPv6 = strF(web.text.noIp, { protocol: 'IPv6' }); | ||
|
||
const [ipv4, ipv6, query] = useWtf(); | ||
|
||
const hasResult = useMemo( | ||
() => (!ipv4.isError || !ipv6.isError) && (ipv4.data?.ip !== null || ipv6.data?.ip !== null), | ||
[ipv4, ipv6], | ||
); | ||
|
||
const show4 = useMemo(() => !ipv4.isError && ipv4.data?.ip !== null, [ipv4]); | ||
const show6 = useMemo(() => !ipv6.isError && ipv6.data?.ip !== null, [ipv6]); | ||
|
||
function handleOpen(): void { | ||
onOpen(); | ||
query(); | ||
} | ||
|
||
return ( | ||
<Prompt | ||
trigger={ | ||
<Button size="sm" onClick={handleOpen}> | ||
{web.text.ipButton} | ||
</Button> | ||
} | ||
onOpen={handleOpen} | ||
{...disclosure} | ||
> | ||
<VStack w="100%" spacing={4} justify="center"> | ||
{hasResult && ( | ||
<Text fontSize="sm" textAlign="center"> | ||
{web.text.ipSelect} | ||
</Text> | ||
)} | ||
<Stack spacing={2}> | ||
{show4 && ( | ||
<Button | ||
size="sm" | ||
fontSize="xs" | ||
fontFamily="mono" | ||
colorScheme="primary" | ||
isDisabled={ipv4.isError} | ||
isLoading={ipv4.isLoading} | ||
justifyContent="space-between" | ||
onClick={() => { | ||
ipv4?.data?.ip && setTarget(ipv4.data.ip); | ||
disclosure.onClose(); | ||
}} | ||
rightIcon={<RightArrow boxSize="18px" />} | ||
> | ||
{ipv4?.data?.ip ?? noIPv4} | ||
</Button> | ||
)} | ||
{show6 && ( | ||
<Button | ||
size="sm" | ||
fontSize="xs" | ||
fontFamily="mono" | ||
colorScheme="secondary" | ||
isDisabled={ipv6.isError} | ||
isLoading={ipv6.isLoading} | ||
justifyContent="space-between" | ||
onClick={() => { | ||
ipv6?.data?.ip && setTarget(ipv6.data.ip); | ||
disclosure.onClose(); | ||
}} | ||
rightIcon={<RightArrow boxSize="18px" />} | ||
> | ||
{ipv6?.data?.ip ?? noIPv6} | ||
</Button> | ||
)} | ||
{!hasResult && ( | ||
<Text fontSize="sm" textAlign="center" color={errorColor}> | ||
{web.text.ipError} | ||
</Text> | ||
)} | ||
</Stack> | ||
</VStack> | ||
</Prompt> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { | ||
Popover, | ||
PopoverBody, | ||
PopoverArrow, | ||
PopoverTrigger, | ||
PopoverContent, | ||
PopoverCloseButton, | ||
} from '@chakra-ui/react'; | ||
import { useColorValue } from '~/context'; | ||
|
||
import type { PromptProps } from './types'; | ||
|
||
export const DesktopPrompt = (props: PromptProps): JSX.Element => { | ||
const { trigger, children, ...disclosure } = props; | ||
const bg = useColorValue('white', 'gray.900'); | ||
|
||
return ( | ||
<Popover closeOnBlur={false} {...disclosure}> | ||
<PopoverTrigger>{trigger}</PopoverTrigger> | ||
<PopoverContent bg={bg}> | ||
<PopoverArrow bg={bg} /> | ||
<PopoverCloseButton /> | ||
<PopoverBody p={6}>{children}</PopoverBody> | ||
</PopoverContent> | ||
</Popover> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { useMobile } from '~/context'; | ||
import { DesktopPrompt } from './desktop'; | ||
import { MobilePrompt } from './mobile'; | ||
|
||
import type { PromptProps } from './types'; | ||
|
||
export const Prompt = (props: PromptProps): JSX.Element => { | ||
const isMobile = useMobile(); | ||
|
||
return isMobile ? <MobilePrompt {...props} /> : <DesktopPrompt {...props} />; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { Modal, ModalBody, ModalOverlay, ModalContent, ModalCloseButton } from '@chakra-ui/react'; | ||
import { useColorValue } from '~/context'; | ||
|
||
import type { PromptProps } from './types'; | ||
|
||
export const MobilePrompt = (props: PromptProps): JSX.Element => { | ||
const { children, trigger, ...disclosure } = props; | ||
const bg = useColorValue('white', 'gray.900'); | ||
return ( | ||
<> | ||
{trigger} | ||
<Modal | ||
size="xs" | ||
isCentered | ||
closeOnEsc={false} | ||
closeOnOverlayClick={false} | ||
motionPreset="slideInBottom" | ||
{...disclosure} | ||
> | ||
<ModalOverlay /> | ||
<ModalContent bg={bg}> | ||
<ModalCloseButton /> | ||
<ModalBody px={4} py={10}> | ||
{children} | ||
</ModalBody> | ||
</ModalContent> | ||
</Modal> | ||
</> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import type { UseDisclosureReturn } from '@chakra-ui/react'; | ||
|
||
type PromptPropsBase = React.PropsWithChildren< | ||
Omit<Partial<UseDisclosureReturn>, 'isOpen' | 'onClose'> & | ||
Pick<UseDisclosureReturn, 'isOpen' | 'onClose'> | ||
>; | ||
|
||
export interface PromptProps extends PromptPropsBase { | ||
trigger?: JSX.Element; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import { useQuery } from 'react-query'; | ||
import { fetchWithTimeout } from '~/util'; | ||
|
||
import type { | ||
QueryFunction, | ||
QueryFunctionContext, | ||
UseQueryOptions, | ||
UseQueryResult, | ||
} from 'react-query'; | ||
import type { WtfIsMyIP } from '~/types'; | ||
|
||
const URL_IP4 = 'https://ipv4.json.myip.wtf'; | ||
const URL_IP6 = 'https://ipv6.json.myip.wtf'; | ||
|
||
interface WtfIndividual { | ||
ip: string; | ||
isp: string; | ||
location: string; | ||
country: string; | ||
} | ||
|
||
type Wtf = [UseQueryResult<WtfIndividual>, UseQueryResult<WtfIndividual>, () => Promise<void>]; | ||
|
||
function transform(wtf: WtfIsMyIP): WtfIndividual { | ||
const { YourFuckingIPAddress, YourFuckingISP, YourFuckingLocation, YourFuckingCountryCode } = wtf; | ||
return { | ||
ip: YourFuckingIPAddress, | ||
isp: YourFuckingISP, | ||
location: YourFuckingLocation, | ||
country: YourFuckingCountryCode, | ||
}; | ||
} | ||
|
||
const query: QueryFunction<WtfIndividual, string> = async (ctx: QueryFunctionContext<string>) => { | ||
const controller = new AbortController(); | ||
const [url] = ctx.queryKey; | ||
|
||
const res = await fetchWithTimeout( | ||
url, | ||
{ | ||
headers: { accept: 'application/json' }, | ||
mode: 'cors', | ||
}, | ||
5000, | ||
controller, | ||
); | ||
const data = await res.json(); | ||
return transform(data); | ||
}; | ||
|
||
const common: UseQueryOptions<WtfIndividual, unknown, WtfIndividual, string> = { | ||
queryFn: query, | ||
enabled: false, | ||
refetchInterval: false, | ||
refetchOnMount: false, | ||
refetchOnReconnect: false, | ||
refetchOnWindowFocus: false, | ||
cacheTime: 120 * 1_000, // 2 minutes | ||
}; | ||
|
||
export function useWtf(): Wtf { | ||
const ipv4 = useQuery<WtfIndividual, unknown, WtfIndividual, string>({ | ||
queryKey: URL_IP4, | ||
...common, | ||
}); | ||
const ipv6 = useQuery<WtfIndividual, unknown, WtfIndividual, string>({ | ||
queryKey: URL_IP6, | ||
...common, | ||
}); | ||
|
||
async function refetch(): Promise<void> { | ||
await ipv4.refetch(); | ||
await ipv6.refetch(); | ||
} | ||
return [ipv4, ipv6, refetch]; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
/** | ||
* myip.wtf response. | ||
* | ||
* @see https://github.com/wtfismyip/wtfismyip | ||
* @see https://wtfismyip.com/automation | ||
*/ | ||
export interface WtfIsMyIP { | ||
YourFuckingIPAddress: string; | ||
YourFuckingLocation: string; | ||
YourFuckingISP: string; | ||
YourFuckingTorExit: boolean; | ||
YourFuckingCountryCode: string; | ||
} |