import type { EnvironmentProject } from "@t3tools/client-runtime/state/shell"; import { getProjectFaviconResourceKey, isProjectFaviconFallbackUrl, } from "@t3tools/shared/projectFavicon"; import { FolderCodeIcon } from "lucide-react"; import type { IconName } from "lucide-react/dynamic"; import type { ComponentType } from "react"; import { lazy, Suspense, useState } from "react"; import { useAtomValue } from "@effect/atom-react"; import { projectFaviconUrlAtom } from "../state/assets"; import { deriveProjectIdentity } from "../projectIdentity"; import { projectIconColorClassName } from "../projectIconColors"; import { ProjectMonogram } from "./ProjectMonogram"; import { cn } from "~/lib/utils"; const DynamicIcon = lazy(() => import("lucide-react/dynamic").then((module) => ({ default: module.DynamicIcon })), ); function DynamicProjectIconFallback() { return ; } // The slice of a project that decides its icon. Every surface must pass the // project record itself (or a snapshot spread from it) so the saved title, favicon // and icon override always travel together. Passing a display label as the title // changes the automatic icon, which is how the command palette drifted once. export type ProjectFaviconProject = Pick< EnvironmentProject, "environmentId" | "workspaceRoot" | "title" | "faviconPath" | "projectIcon" >; export function ProjectFavicon(input: { project: ProjectFaviconProject; className?: string | undefined; fallbackIcon?: ComponentType<{ className?: string }>; }) { const { project } = input; const src = useAtomValue( projectFaviconUrlAtom({ environmentId: project.environmentId, cwd: project.workspaceRoot, faviconPath: project.faviconPath, }), ); if (project.projectIcon?.kind === "monogram") { return ( ); } if (project.projectIcon?.kind === "emoji") { return ( ); } if (project.projectIcon?.kind === "lucide") { const colorClassName = projectIconColorClassName(project.projectIcon.color); const iconClassName = cn( "inline-flex size-3.5 shrink-0 items-center justify-center", colorClassName, input.className, ); return ( ); } const FallbackIcon = input.fallbackIcon ?? FolderCodeIcon; if (!src || isProjectFaviconFallbackUrl(src)) { return ( ); } const cacheKey = getProjectFaviconResourceKey( project.environmentId, project.workspaceRoot, project.faviconPath, ); return ( ); } function ProjectFaviconFallback({ className, icon: Icon, emoji, projectName, }: { readonly className?: string | undefined; readonly icon: ComponentType<{ className?: string }>; readonly emoji?: string | undefined; readonly projectName?: string | undefined; }) { if (projectName && projectName.trim().length > 0) { const identity = deriveProjectIdentity(projectName); return ( ); } if (emoji) { return ( ); } return ; } function ProjectFaviconImage({ src, className, fallbackIcon: FallbackIcon, fallbackProjectName, }: { readonly src: string; readonly className?: string | undefined; readonly fallbackIcon: ComponentType<{ className?: string }>; readonly fallbackProjectName?: string | undefined; }) { const [displayedSrc, setDisplayedSrc] = useState(() => src.startsWith("data:image/") ? src : null, ); const isLoading = displayedSrc !== src; const handleLoadError = (failedSrc: string) => { setDisplayedSrc((currentSrc) => (currentSrc === failedSrc ? null : currentSrc)); }; return ( <> {displayedSrc === null ? ( ) : null} {displayedSrc ? ( handleLoadError(displayedSrc)} /> ) : null} {isLoading ? ( { setDisplayedSrc(src); }} onError={() => handleLoadError(src)} /> ) : null} ); }