From 58f9cdb4a5c7e062b2848d53948af405547d5afb Mon Sep 17 00:00:00 2001 From: harryob <55142896+harryob@users.noreply.github.com> Date: Wed, 23 Oct 2024 23:23:59 +0100 Subject: [PATCH] clear data when empty --- src/components/stickybanMatch.tsx | 27 +++++++++++++++++++-------- 1 file changed, 19 insertions(+), 8 deletions(-) diff --git a/src/components/stickybanMatch.tsx b/src/components/stickybanMatch.tsx index 8a20e55..977617f 100644 --- a/src/components/stickybanMatch.tsx +++ b/src/components/stickybanMatch.tsx @@ -20,11 +20,7 @@ export const StickybanMatch: React.FC = ( const { ip, ckey, cid } = props; - const getPath = () => { - if (ip) return `/Stickyban/Ip?ip=${ip}`; - if (ckey) return `/Stickyban/Ckey?ckey=${ckey}`; - return `/Stickyban/Cid?cid=${cid}`; - }; + const [lookedUp, setLookedUp] = useState(""); const getText = () => { if (ip) return "IP"; @@ -33,12 +29,27 @@ export const StickybanMatch: React.FC = ( }; useEffect(() => { - if (!stickyData) { + const getPath = () => { + if (ip) return `/Stickyban/Ip?ip=${ip}`; + if (ckey) return `/Stickyban/Ckey?ckey=${ckey}`; + return `/Stickyban/Cid?cid=${cid}`; + }; + + const to_use = ip || ckey || cid; + + if (stickyData && to_use != lookedUp) { + setStickyData(null); + } + + if (!stickyData && to_use != lookedUp) { callApi(getPath()).then((value) => - value.json().then((json) => setStickyData(json)) + value.json().then((json) => { + setStickyData(json); + setLookedUp(to_use!); + }) ); } - }); + }, [stickyData, cid, ckey, ip, lookedUp]); if (!stickyData?.length) return;