Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

PreviewFaviconIcon.tsx

66 lines1.8 KB

1import type { ScopedThreadRef } from "@t3tools/contracts";
2import { type ReactNode, useState } from "react";
3​
4import { useFaviconForThreadUrl } from "~/browserFaviconStore";
5import { cn } from "~/lib/utils";
6​
7import { BrowserMockup } from "./BrowserMockup";
8​
9export function selectFaviconSource(
10 sources: ReadonlyArray<string>,
11 failed: ReadonlySet<string>,
12): string | null {
13 return sources.find((candidate) => !failed.has(candidate)) ?? null;
15​
16export function FaviconImage(props: {
17 sources: ReadonlyArray<string | null | undefined>;
18 fallback: ReactNode;
19 className?: string | undefined;
20}) {
21 const sources = props.sources.filter((source): source is string => Boolean(source));
22 return (
23 <FaviconImageAttempt
24 key={sources.join("\0")}
25 sources={sources}
26 fallback={props.fallback}
27 className={props.className}
28 />
29 );
31​
32function FaviconImageAttempt(props: {
33 sources: ReadonlyArray<string>;
34 fallback: ReactNode;
35 className?: string | undefined;
36}) {
37 const [failed, setFailed] = useState<ReadonlySet<string>>(() => new Set());
38 const source = selectFaviconSource(props.sources, failed);
39 if (!source) return props.fallback;
40 return (
41 <img
42 src={source}
43 alt=""
44 aria-hidden="true"
45 draggable={false}
46 className={props.className}
47 onError={() => setFailed((current) => new Set(current).add(source))}
48 />
49 );
51​
52export function PreviewFaviconIcon(props: {
53 threadRef: ScopedThreadRef;
54 url: string;
55 className?: string | undefined;
56}) {
57 const source = useFaviconForThreadUrl(props.threadRef, props.url);
58 const fallback = <BrowserMockup className={cn("size-7 shrink-0", props.className)} />;
59 return (
60 <FaviconImage
61 sources={[source]}
62 fallback={fallback}
63 className={cn("size-7 shrink-0 rounded object-contain", props.className)}
64 />
65 );