From c3be23d369b580d7c0fadbc229014ed3e210c0a8 Mon Sep 17 00:00:00 2001 From: skidoodle Date: Wed, 19 Mar 2025 22:08:30 +0100 Subject: [PATCH] handle local domains --- components/ServerInfo.tsx | 29 +++++++++++++++++++++-------- hooks/useTabData.ts | 32 ++++++++++++++++++++++++-------- 2 files changed, 45 insertions(+), 16 deletions(-) diff --git a/components/ServerInfo.tsx b/components/ServerInfo.tsx index 93ac9fe..0d717b0 100644 --- a/components/ServerInfo.tsx +++ b/components/ServerInfo.tsx @@ -4,8 +4,8 @@ import { codes } from '@/utils/codes'; export default function ServerInfo({ data }: { data: ServerData }) { const countryName = data.country - ? codes[data.country.toLowerCase()] || "N/A" - : "N/A"; + ? codes[data.country.toLowerCase()] || "N/A" + : "N/A"; if (data.isBrowserResource) { return ( @@ -32,13 +32,26 @@ export default function ServerInfo({ data }: { data: ServerData }) { Internal Network -
- -
-

IP Address

-

{data.ip}

+ + {data.ip && ( +
+ +
+

IP Address

+

{data.ip}

+
-
+ )} + + {data.hostname && ( +
+ +
+

Hostname

+

{data.hostname}

+
+
+ )}
); } diff --git a/hooks/useTabData.ts b/hooks/useTabData.ts index 5d32c8d..c78dd71 100644 --- a/hooks/useTabData.ts +++ b/hooks/useTabData.ts @@ -1,7 +1,11 @@ -import { useState, useEffect } from 'react'; -import browser from 'webextension-polyfill'; -import { isPrivateIP } from '@/utils'; -import { FetchServerInfoRequest, FetchServerInfoResponse, ServerData } from '@/utils/model'; +import { useState, useEffect } from 'react' +import { isPrivateIP } from '@/utils' +import { + FetchServerInfoRequest, + FetchServerInfoResponse, + ServerData, +} from '@/utils/model' +import browser from 'webextension-polyfill' export function useTabData() { const [data, setData] = useState(null) @@ -39,7 +43,7 @@ export function useTabData() { return setData({ origin: '', ip: hostname, - hostname: url.href, + hostname: '', country: '', city: '', org: '', @@ -48,7 +52,10 @@ export function useTabData() { }) } - const response = await browser.runtime.sendMessage({ + const response = await browser.runtime.sendMessage< + FetchServerInfoRequest, + FetchServerInfoResponse + >({ type: 'FETCH_SERVER_INFO', hostname: hostname, }) @@ -58,7 +65,16 @@ export function useTabData() { } if (response.error) { - throw new Error(response.error) + return setData({ + origin: '', + ip: '', + hostname: hostname, + country: '', + city: '', + org: '', + isLocal: true, + isBrowserResource: false, + }) } if (!response.data?.ip) { @@ -68,7 +84,7 @@ export function useTabData() { setData(response.data) setError(null) } catch (err) { - setError(err instanceof Error ? err.message : 'Failed to fetch data') + setError(err instanceof Error ? err.message : 'No data found') setData(null) } finally { setLoading(false)