forked from mirrors/thatmattlove-hyperglass
refactor resolvedTarget to use a custom hook for DOH resolution instead of using react-query directly [skip ci]
This commit is contained in:
parent
5aabbab4bb
commit
7aa4573b8e
4 changed files with 58 additions and 27 deletions
|
|
@ -1,9 +1,8 @@
|
|||
import { useEffect, useMemo } from 'react';
|
||||
import { Button, Stack, Text, VStack } from '@chakra-ui/react';
|
||||
import { useQuery } from 'react-query';
|
||||
import { FiArrowRightCircle as RightArrow } from '@meronex/icons/fi';
|
||||
import { useConfig, useColorValue } from '~/context';
|
||||
import { useStrf, useLGState } from '~/hooks';
|
||||
import { useStrf, useLGState, useDNSQuery } from '~/hooks';
|
||||
|
||||
import type { DnsOverHttps } from '~/types';
|
||||
import type { TResolvedTarget } from './types';
|
||||
|
|
@ -34,32 +33,15 @@ export const ResolvedTarget = (props: TResolvedTarget) => {
|
|||
web.text.fqdn_message,
|
||||
]);
|
||||
|
||||
const { data: data4, isLoading: isLoading4, isError: isError4 } = useQuery(
|
||||
[fqdnTarget.value, 4],
|
||||
dnsQuery,
|
||||
const { data: data4, isLoading: isLoading4, isError: isError4 } = useDNSQuery(
|
||||
fqdnTarget.value,
|
||||
4,
|
||||
);
|
||||
|
||||
const { data: data6, isLoading: isLoading6, isError: isError6 } = useQuery(
|
||||
[fqdnTarget.value, 6],
|
||||
dnsQuery,
|
||||
const { data: data6, isLoading: isLoading6, isError: isError6 } = useDNSQuery(
|
||||
fqdnTarget.value,
|
||||
6,
|
||||
);
|
||||
async function dnsQuery(
|
||||
target: string,
|
||||
family: 4 | 6,
|
||||
): Promise<DnsOverHttps.Response | undefined> {
|
||||
let json;
|
||||
const type = family === 4 ? 'A' : family === 6 ? 'AAAA' : '';
|
||||
const controller = new AbortController();
|
||||
const timeout = setTimeout(() => controller.abort(), 1000);
|
||||
const res = await fetch(`${dnsUrl}?name=${target}&type=${type}`, {
|
||||
headers: { accept: 'application/dns-json' },
|
||||
signal: controller.signal,
|
||||
mode: 'cors',
|
||||
});
|
||||
json = await res.json();
|
||||
clearTimeout(timeout);
|
||||
return json;
|
||||
}
|
||||
|
||||
function handleOverride(value: string): void {
|
||||
setTarget({ field: 'query_target', value });
|
||||
|
|
@ -84,7 +66,7 @@ export const ResolvedTarget = (props: TResolvedTarget) => {
|
|||
<Text fontSize="sm" textAlign="center">
|
||||
{messageStart}
|
||||
<Text as="span" fontSize="sm" fontWeight="bold" color={color}>
|
||||
{fqdnTarget.value}
|
||||
{`${fqdnTarget.value}`.toLowerCase()}
|
||||
</Text>
|
||||
{messageEnd}
|
||||
</Text>
|
||||
|
|
|
|||
|
|
@ -1,10 +1,11 @@
|
|||
export * from './useASNDetail';
|
||||
export * from './useBooleanValue';
|
||||
export * from './useDevice';
|
||||
export * from './useDNSQuery';
|
||||
export * from './useGreeting';
|
||||
export * from './useLGQuery';
|
||||
export * from './useLGState';
|
||||
export * from './useOpposingColor';
|
||||
export * from './useScaledTitle';
|
||||
export * from './useStrf';
|
||||
export * from './useTableToString';
|
||||
export * from './useScaledTitle';
|
||||
|
|
|
|||
|
|
@ -17,3 +17,13 @@ export interface TUseASNDetailFn {
|
|||
pageParam?: QueryFunctionContext['pageParam'];
|
||||
queryKey: string;
|
||||
}
|
||||
|
||||
interface TUseDNSQueryParams {
|
||||
target: string;
|
||||
family: 4 | 6;
|
||||
}
|
||||
|
||||
export interface TUseDNSQueryFn {
|
||||
pageParam?: QueryFunctionContext['pageParam'];
|
||||
queryKey: [string | null, TUseDNSQueryParams];
|
||||
}
|
||||
|
|
|
|||
38
hyperglass/ui/hooks/useDNSQuery.ts
Normal file
38
hyperglass/ui/hooks/useDNSQuery.ts
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
import { useQuery } from 'react-query';
|
||||
import { useConfig } from '~/context';
|
||||
import { fetchWithTimeout } from '~/util';
|
||||
|
||||
import type { DnsOverHttps } from '~/types';
|
||||
import type { TUseDNSQueryFn } from './types';
|
||||
|
||||
async function dnsQuery(ctx: TUseDNSQueryFn): Promise<DnsOverHttps.Response | undefined> {
|
||||
const [url, { target, family }] = ctx.queryKey;
|
||||
|
||||
const controller = new AbortController();
|
||||
|
||||
let json;
|
||||
const type = family === 4 ? 'A' : family === 6 ? 'AAAA' : '';
|
||||
|
||||
if (url !== null) {
|
||||
const res = await fetchWithTimeout(
|
||||
`${url}?name=${target}&type=${type}`,
|
||||
{
|
||||
headers: { accept: 'application/dns-json' },
|
||||
mode: 'cors',
|
||||
},
|
||||
5000,
|
||||
controller,
|
||||
);
|
||||
|
||||
json = await res.json();
|
||||
}
|
||||
|
||||
return json;
|
||||
}
|
||||
|
||||
export function useDNSQuery(target: string | null, family: 4 | 6) {
|
||||
const { cache, web } = useConfig();
|
||||
return useQuery([web.dns_provider.url, { target, family }], dnsQuery, {
|
||||
cacheTime: cache.timeout * 1000,
|
||||
});
|
||||
}
|
||||
Loading…
Add table
Reference in a new issue