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

VscodeEntryIcon.tsx

37 lines1.1 KB

1import { memo, useMemo, useState } from "react";
2import { getVscodeIconUrlForEntry } from "../../vscode-icons";
3import { FileIcon, FolderIcon } from "lucide-react";
4import { cn } from "~/lib/utils";
5​
6export const VscodeEntryIcon = memo(function VscodeEntryIcon(props: {
7 pathValue: string;
8 kind: "file" | "directory";
9 theme: "light" | "dark";
10 className?: string;
11}) {
12 const [failedIconUrl, setFailedIconUrl] = useState<string | null>(null);
13 const iconUrl = useMemo(
14 () => getVscodeIconUrlForEntry(props.pathValue, props.kind, props.theme),
15 [props.kind, props.pathValue, props.theme],
16 );
17 const failed = failedIconUrl === iconUrl;
18​
19 if (failed) {
20 return props.kind === "directory" ? (
21 <FolderIcon className={cn("size-4 text-muted-foreground/80", props.className)} />
22 ) : (
23 <FileIcon className={cn("size-4 text-muted-foreground/80", props.className)} />
24 );
25 }
26​
27 return (
28 <img
29 src={iconUrl}
30 alt=""
31 aria-hidden="true"
32 className={cn("size-4 shrink-0", props.className)}
33 loading="lazy"
34 onError={() => setFailedIconUrl(iconUrl)}
35 />
36 );
37});