Refactor ServerInfo and add icon and tab fallbacks

This commit is contained in:
2026-02-03 05:03:55 +01:00
parent 2973e038d6
commit 9292a4a6e2
5 changed files with 107 additions and 83 deletions
+2 -8
View File
@@ -5,15 +5,9 @@ const ICON_CACHE = new Map<string, Record<string, ImageData>>();
export const IconService = {
async update(tabId: number, countryCode: string | null, isLocal: boolean) {
try {
if (isLocal) {
const path = '/icon/128.png' as string;
await browser.action.setIcon({ tabId, path });
return;
}
const code = isLocal ? 'unknown' : (countryCode ? countryCode.toLowerCase() : 'unknown');
const code = countryCode ? countryCode.toLowerCase() : 'unknown';
const imageData = await this.getIconData(code);
await browser.action.setIcon({ tabId, imageData });
} catch (e) {
console.warn('Failed to update icon', e);
@@ -37,7 +31,6 @@ export const IconService = {
ICON_CACHE.set(code, data);
return data;
} catch {
// Fallback to unknown if specific flag fails
if (code !== 'unknown') return this.getIconData('unknown');
throw new Error('Failed to load fallback icon');
}
@@ -52,6 +45,7 @@ export const IconService = {
const offsetX = (canvas.width - bitmap.width * ratio) / 2;
const offsetY = (canvas.height - bitmap.height * ratio) / 2;
ctx.clearRect(0, 0, canvas.width, canvas.height);
ctx.drawImage(bitmap, 0, 0, bitmap.width, bitmap.height, offsetX, offsetY, bitmap.width * ratio, bitmap.height * ratio);
const sizes = [16, 32, 48, 128];
+24 -2
View File
@@ -32,7 +32,7 @@ export const Tab = {
return;
}
// Handle Local/Private IPs
// Handle Local/Private IPs (Bogons)
if (IpUtils.isLocalOrBogon(ip)) {
const localInfo: HostInfo = {
...initialState,
@@ -55,8 +55,30 @@ export const Tab = {
// Fetch Public Data
const geoData = await GeoService.resolve(ip);
// If Geo lookup fails (e.g. LAN domain with public IP, or API down),
// we still want to show the IP we captured.
if (!geoData) {
await StorageService.set(tabId, { ...initialState, loading: false, error: 'Failed to fetch host info' });
const fallbackInfo: HostInfo = {
...initialState,
loading: false,
network: {
ip,
hostname: null,
asn: null,
org: 'Unknown',
isLocal: false,
isBogon: false
},
location: {
countryCode: null,
countryName: 'Unknown Location',
city: null,
region: null,
timezone: null
}
};
await StorageService.set(tabId, fallbackInfo);
await IconService.update(tabId, 'unknown', false);
return;
}