From bc3683575c71a035b28459ad4558b3b24520589a Mon Sep 17 00:00:00 2001 From: Pawan Paudel Date: Sat, 14 Dec 2024 21:17:42 +0545 Subject: [PATCH] fix: resolve issue with placeholder & related code --- src/components/HeadAuth.tsx | 4 ++-- src/components/popup/HeadV2.tsx | 2 +- src/utils/urls/getAppIconPlaceholder.ts | 21 ++++++++++++++++++++- 3 files changed, 23 insertions(+), 4 deletions(-) diff --git a/src/components/HeadAuth.tsx b/src/components/HeadAuth.tsx index 51de22397..bd00b80f0 100644 --- a/src/components/HeadAuth.tsx +++ b/src/components/HeadAuth.tsx @@ -47,8 +47,8 @@ export const HeadAuth: React.FC = ({ fallbackName || new URL(url).hostname.split(".").slice(-2).join("."), logo: appInfo.logo || fallbackLogo, - type: appLogoPlaceholder.type, - placeholder: appLogoPlaceholder.placeholder + type: appLogoPlaceholder?.type, + placeholder: appLogoPlaceholder?.placeholder }); } diff --git a/src/components/popup/HeadV2.tsx b/src/components/popup/HeadV2.tsx index b644d502d..f450d2d5e 100644 --- a/src/components/popup/HeadV2.tsx +++ b/src/components/popup/HeadV2.tsx @@ -100,7 +100,7 @@ export default function HeadV2({ const hardwareApi = useHardwareApi(); const appName = appInfo?.name; - const appIconPlaceholderText = appInfo.placeholder; + const appIconPlaceholderText = appInfo?.placeholder; const SquircleWrapper = onAppInfoClick ? ButtonSquircle : React.Fragment; diff --git a/src/utils/urls/getAppIconPlaceholder.ts b/src/utils/urls/getAppIconPlaceholder.ts index 89dac5a91..c8a1d3317 100644 --- a/src/utils/urls/getAppIconPlaceholder.ts +++ b/src/utils/urls/getAppIconPlaceholder.ts @@ -1,5 +1,22 @@ import type { AppLogoInfo } from "~applications/application"; import { isGateway } from "./isGateway"; + +/** + * Ensures a URL has a valid HTTP/HTTPS protocol prefix. + * If no protocol is present, HTTPS is added as the default. + * + * @param url - The URL string to normalize + * @returns The URL with a valid protocol prefix + */ +function ensureUrlProtocol(url: string): string { + try { + const hasProtocol = /^https?:\/\//i.test(url); + return hasProtocol ? url : `https://${url}`; + } catch { + return url; + } +} + /** * Generates a logo placeholder based on the base domain. * If the URL is a gateway (determined by a GET call), it uses the first two letters of the subdomain. @@ -11,6 +28,7 @@ export async function generateLogoPlaceholder( url: string ): Promise { try { + url = ensureUrlProtocol(url); const { hostname } = new URL(url); const parts = hostname.split("."); @@ -21,7 +39,8 @@ export async function generateLogoPlaceholder( parts.length > 1 ? parts.slice(1).join(".") : null; const isGatewayUrl = - !!candidateGatewayUrl && (await isGateway(candidateGatewayUrl)); + !!candidateGatewayUrl && + (await isGateway(ensureUrlProtocol(candidateGatewayUrl))); if (isGatewayUrl) { // For gateways, take the first two letters of the first subdomain