({
title,
faviconUrl,
url,
size,
className,
plateClassName,
fallback,
}: ToolLogoProps)
| 65 | // service otherwise, falling back to the initial only when both are missing or |
| 66 | // the image fails to load. |
| 67 | export const ToolLogo = ({ |
| 68 | title, |
| 69 | faviconUrl, |
| 70 | url, |
| 71 | size, |
| 72 | className, |
| 73 | plateClassName, |
| 74 | fallback, |
| 75 | }: ToolLogoProps): ReactElement => { |
| 76 | const [hasFailed, setHasFailed] = useState(false); |
| 77 | const [lightSrc, setLightSrc] = useState<string>(); |
| 78 | const src = faviconUrl || (url ? getSiteIconUrl({ url, size }) : null); |
| 79 | const showsLogo = !!src && !hasFailed; |
| 80 | |
| 81 | // Reading pixels needs CORS, which the stored logos allow and the icon |
| 82 | // service's favicons don't, so only stored logos are probed. |
| 83 | useEffect(() => { |
| 84 | if (!plateClassName || !faviconUrl) { |
| 85 | return undefined; |
| 86 | } |
| 87 | |
| 88 | const probe = new Image(); |
| 89 | probe.crossOrigin = 'anonymous'; |
| 90 | probe.onload = () => { |
| 91 | if (isLightLogo(probe)) { |
| 92 | setLightSrc(faviconUrl); |
| 93 | } |
| 94 | }; |
| 95 | probe.src = faviconUrl; |
| 96 | |
| 97 | return () => { |
| 98 | probe.onload = null; |
| 99 | }; |
| 100 | }, [faviconUrl, plateClassName]); |
| 101 | |
| 102 | if (fallback && !showsLogo) { |
| 103 | return <>{fallback}</>; |
| 104 | } |
| 105 | |
| 106 | return ( |
| 107 | <span |
| 108 | className={classNames( |
| 109 | 'grid flex-none place-items-center overflow-hidden', |
| 110 | className, |
| 111 | showsLogo && |
| 112 | plateClassName && [ |
| 113 | plateClassName, |
| 114 | lightSrc === src ? 'bg-black' : 'bg-white', |
| 115 | ], |
| 116 | )} |
| 117 | > |
| 118 | {!showsLogo ? ( |
| 119 | <span aria-hidden className="font-bold text-text-tertiary"> |
| 120 | {title.charAt(0).toUpperCase()} |
| 121 | </span> |
| 122 | ) : ( |
| 123 | <img |
| 124 | key={src} |
nothing calls this directly
no test coverage detected