From e188215331f928271d10cc6cc906c6bfdda729bb Mon Sep 17 00:00:00 2001 From: Xevion Date: Mon, 20 Feb 2023 00:31:00 -0600 Subject: [PATCH] Fix queued state modifications breaking lookup internally --- src/hooks/useLookup.tsx | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/src/hooks/useLookup.tsx b/src/hooks/useLookup.tsx index 3692c92..9eec786 100644 --- a/src/hooks/useLookup.tsx +++ b/src/hooks/useLookup.tsx @@ -1,4 +1,4 @@ -import React, {useEffect, useMemo, useState} from "react"; +import React, {useEffect, useMemo, useRef, useState} from "react"; import {domainMatchPredicate, getBestURL, getType} from "@/rdap"; import type {AutonomousNumber, Domain, IpNetwork, Register, RootRegistryType, TargetType} from "@/types"; import {registryURLs} from "@/constants"; @@ -9,10 +9,9 @@ import {ZodSchema} from "zod"; import {ParsedGeneric} from "@/components/Generic"; export type WarningHandler = (warning: { message: string }) => void; -type BootstrapMatcher = (value: string) => boolean; const useLookup = (warningHandler?: WarningHandler) => { - const [registryData, setRegistryData] = useState>({} as Record); + const registryDataRef = useRef>({} as Record) const [error, setError] = useState(null); const [target, setTarget] = useState(""); @@ -23,7 +22,7 @@ const useLookup = (warningHandler?: WarningHandler) => { // Fetch & load a specific registry's data into memory. async function loadBootstrap(type: RootRegistryType, force = false) { // Early preload exit condition - if (registryData[type] != null && !force) + if (registryDataRef.current[type] != null && !force) return; // Fetch the bootstrapping file from the registry @@ -37,14 +36,14 @@ const useLookup = (warningHandler?: WarningHandler) => { throw new Error(`Could not parse IANA bootstrap response (${type}).`) // Set it in state so we can use it. - setRegistryData((prev) => ({ - ...prev, + registryDataRef.current = { + ...registryDataRef.current, [type]: parsedRegister.data - })); + } } function getRegistryURL(type: RootRegistryType, lookupTarget: string): string { - const bootstrap = registryData[type]; + const bootstrap = registryDataRef.current[type]; if (bootstrap == null) throw new Error(`Cannot acquire RDAP URL without bootstrap data for ${type} lookup.`) let url: string | null = null; @@ -81,8 +80,8 @@ const useLookup = (warningHandler?: WarningHandler) => { if (uriType === 'unknown') return; const registryUri = RootRegistryEnum.safeParse(uriType); if (!registryUri.success) return; - console.log({registryData, registryUri: registryUri.data}); - if (registryData[registryUri.data] != null) return; + console.log({registryData: registryDataRef.current, registryUri: registryUri.data}); + if (registryDataRef.current[registryUri.data] != null) return; try { await loadBootstrap(registryUri.data);