Skip to content

Commit

Permalink
fix: resolve issue with placeholder & related code
Browse files Browse the repository at this point in the history
  • Loading branch information
pawanpaudel93 committed Dec 14, 2024
1 parent f7dd3c1 commit bc36835
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/components/HeadAuth.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ export const HeadAuth: React.FC<HeadAuthProps> = ({
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
});
}

Expand Down
2 changes: 1 addition & 1 deletion src/components/popup/HeadV2.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
21 changes: 20 additions & 1 deletion src/utils/urls/getAppIconPlaceholder.ts
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -11,6 +28,7 @@ export async function generateLogoPlaceholder(
url: string
): Promise<AppLogoInfo | undefined> {
try {
url = ensureUrlProtocol(url);
const { hostname } = new URL(url);

const parts = hostname.split(".");
Expand All @@ -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
Expand Down

0 comments on commit bc36835

Please sign in to comment.