mirror of
https://github.com/aykhans/slash-e.git
synced 2025-12-15 05:09:19 +00:00
feat(web): use favicon provider
This commit is contained in:
36
frontend/web/src/components/LinkFavicon.tsx
Normal file
36
frontend/web/src/components/LinkFavicon.tsx
Normal file
@@ -0,0 +1,36 @@
|
||||
import { useState } from "react";
|
||||
import { useWorkspaceStore } from "@/stores";
|
||||
import Icon from "./Icon";
|
||||
|
||||
interface Props {
|
||||
url: string;
|
||||
}
|
||||
|
||||
const getFaviconUrlWithProvider = (url: string, provider: string) => {
|
||||
try {
|
||||
const searchParams = new URLSearchParams();
|
||||
searchParams.set("domain", new URL(url).hostname);
|
||||
return new URL(`?${searchParams.toString()}`, provider).toString();
|
||||
} catch (error) {
|
||||
return "";
|
||||
}
|
||||
};
|
||||
|
||||
const LinkFavicon = (props: Props) => {
|
||||
const { url } = props;
|
||||
const workspaceStore = useWorkspaceStore();
|
||||
const faviconProvider = workspaceStore.profile.faviconProvider || "https://www.google.com/s2/favicons";
|
||||
const [faviconUrl, setFaviconUrl] = useState<string>(getFaviconUrlWithProvider(url, faviconProvider));
|
||||
|
||||
const handleImgError = () => {
|
||||
setFaviconUrl("");
|
||||
};
|
||||
|
||||
return faviconUrl ? (
|
||||
<img className="w-full h-auto rounded" src={faviconUrl} decoding="async" loading="lazy" onError={handleImgError} />
|
||||
) : (
|
||||
<Icon.CircleSlash className="w-full h-auto text-gray-400" strokeWidth={1.5} />
|
||||
);
|
||||
};
|
||||
|
||||
export default LinkFavicon;
|
||||
Reference in New Issue
Block a user