Skip to content

Commit

Permalink
fix: fix validator url icon (#720)
Browse files Browse the repository at this point in the history
  • Loading branch information
moo-onthelawn authored Jun 25, 2024
1 parent 6e1b664 commit 007634a
Showing 1 changed file with 21 additions and 14 deletions.
35 changes: 21 additions & 14 deletions src/components/ValidatorName.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import { layoutMixins } from '@/styles/layoutMixins';

import { Link } from './Link';

const URL_START = 'https://';

export const ValidatorFaviconIcon = ({
className,
url,
Expand All @@ -17,24 +19,29 @@ export const ValidatorFaviconIcon = ({
fallbackText?: string;
}) => {
const [iconFail, setIconFail] = useState<boolean>(false);
const fallback = fallbackText ? (
<$IconContainer className={className}>{fallbackText.charAt(0)}</$IconContainer>
) : null;

if (url && !iconFail) {
const parsedUrl = new URL(url);
const baseUrl = `${parsedUrl.protocol}//${parsedUrl.hostname}`;
return (
<$Img
className={className}
src={`${baseUrl}/favicon.ico`}
alt="validator favicon"
onError={() => setIconFail(true)}
/>
);
}
if (fallbackText) {
return <$IconContainer className={className}>{fallbackText.charAt(0)}</$IconContainer>;
try {
const parsedUrl = url.startsWith(URL_START) ? new URL(url) : new URL(`${URL_START}${url}`);
const baseUrl = `${parsedUrl.protocol}//${parsedUrl.hostname}`;

return (
<$Img
className={className}
src={`${baseUrl}/favicon.ico`}
alt="validator favicon"
onError={() => setIconFail(true)}
/>
);
} catch (error) {
return fallback;
}
}

return null;
return fallback;
};

export const ValidatorName = ({ validator }: { validator?: Validator }) => {
Expand Down

0 comments on commit 007634a

Please sign in to comment.