All files / src/components/chat/Citations SiteIcon.tsx

100% Statements 8/8
100% Branches 7/7
100% Functions 3/3
100% Lines 8/8

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42                  18x 18x       25x       17x 17x 1x                 16x           1x            
import { Globe } from "lucide-react";
import React, { useState } from "react";
 
/**
 * A site's icon URL on the platform's own API. svc-core serves it from a
 * cache, fetching through svc-ai once a week at most — so no third party
 * learns which sites a user reads citations from (services/favicons.py).
 */
export function siteIconUrl(hostname: string): string {
  const base = String(import.meta.env.VITE_API_URL ?? "").replace(/\/+$/, "");
  return `${base}/favicons/${encodeURIComponent(hostname.toLowerCase())}`;
}
 
/** A cited site's icon, or a generic globe when it has none. */
const SiteIcon: React.FC<{ hostname: string; className?: string }> = ({
  hostname,
  className = "w-5 h-5",
}) => {
  const [failed, setFailed] = useState(false);
  if (!hostname || failed) {
    return (
      <span
        aria-hidden
        className={`${className} rounded bg-gray-100 dark:bg-gray-700 flex items-center justify-center shrink-0`}
      >
        <Globe className="w-3 h-3 text-gray-400" />
      </span>
    );
  }
  return (
    <img
      src={siteIconUrl(hostname)}
      alt=""
      loading="lazy"
      className={`${className} rounded shrink-0`}
      onError={() => setFailed(true)}
    />
  );
};
 
export default SiteIcon;