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

ProviderInstanceIcon.tsx

151 lines5.4 KB

1import { type CSSProperties, memo } from "react";
2​
3import {
4 normalizeProviderAccentColor,
5 providerAccentForegroundColor,
6 providerInstanceInitials,
7} from "@t3tools/client-runtime/state/provider-instance-display";
8​
9import { ProviderDriverKind } from "@t3tools/contracts";
10import { AntigravityIcon, ClaudeAI, Icon, OpenAI } from "../Icons";
11​
12import { cn } from "~/lib/utils";
13import { providerClients } from "../settings/providerDriverMeta";
14import { ProviderPackageIcon } from "./ProviderPackageIcon";
15import {
16 AcpRegistryAgentIcon,
17 officialAcpRegistryIconUrlForAgentId,
18 resolveOfficialAcpRegistryIconUrl,
19} from "../settings/AcpRegistryIcon";
20​
21const PROVIDER_ICON_BY_PROVIDER: Partial<Record<ProviderDriverKind, Icon>> = {
22 [ProviderDriverKind.make("codex")]: OpenAI,
23 [ProviderDriverKind.make("claudeAgent")]: ClaudeAI,
24 [ProviderDriverKind.make("antigravity")]: AntigravityIcon,
25};
26​
27const PROVIDER_TEXT_COLOR_BY_PROVIDER: Partial<Record<ProviderDriverKind, string>> = {
28 [ProviderDriverKind.make("codex")]: "text-black dark:text-white",
29 [ProviderDriverKind.make("claudeAgent")]: "text-[#d97757]",
30 [ProviderDriverKind.make("antigravity")]: "text-[#5b87bf]",
31};
32​
33export function providerTextColor(driverKind: ProviderDriverKind): {
34 readonly className?: string;
35 readonly style?: CSSProperties;
36} {
37 const icon = providerClients.get(driverKind)?.icon;
38 if (icon) {
39 return {
40 className: "text-(--icon-light) dark:text-(--icon-dark)",
41 style: { "--icon-light": icon.fill.light, "--icon-dark": icon.fill.dark } as CSSProperties,
42 };
43 }
44 const className = PROVIDER_TEXT_COLOR_BY_PROVIDER[driverKind];
45 return className ? { className } : {};
47​
48export function resolveProviderInstanceAcpRegistryIconUrl(input: {
49 readonly driverKind: ProviderDriverKind;
50 readonly agentId?: string | undefined;
51 readonly iconUrl?: string | undefined;
52}): string | null {
53 if (input.driverKind !== "acpRegistry") return null;
54 return (
55 resolveOfficialAcpRegistryIconUrl(input.iconUrl ?? null) ??
56 officialAcpRegistryIconUrlForAgentId(input.agentId?.trim() || null)
57 );
59​
60export const ProviderInstanceIcon = memo(function ProviderInstanceIcon(props: {
61 driverKind: ProviderDriverKind;
62 displayName: string;
63 accentColor?: string | undefined;
64 acpRegistryAgentId?: string | undefined;
65 acpRegistryIconUrl?: string | undefined;
66 showBadge?: boolean;
67 badgeContent?: "initials" | "none";
68 size?: "default" | "compact";
69 className?: string;
70 iconClassName?: string;
71 badgeClassName?: string;
72 statusDotClassName?: string;
73 indicatorBackground?: string;
74}) {
75 const Icon = PROVIDER_ICON_BY_PROVIDER[props.driverKind] ?? null;
76 const packageIcon = providerClients.get(props.driverKind)?.icon;
77 const indicatorBackground = props.indicatorBackground ?? "var(--card)";
78 const accentColor = normalizeProviderAccentColor(props.accentColor);
79 const accentStyle = accentColor
80 ? ({
81 "--provider-accent": accentColor,
82 "--provider-accent-foreground": providerAccentForegroundColor(accentColor),
83 } as CSSProperties)
84 : undefined;
85 const badgeContent = props.badgeContent ?? "initials";
86 const iconClassName = cn(props.size === "compact" && "size-3.5", props.iconClassName);
87 const isAcpRegistry = props.driverKind === "acpRegistry";
88 const acpRegistryIconUrl = resolveProviderInstanceAcpRegistryIconUrl({
89 driverKind: props.driverKind,
90 agentId: props.acpRegistryAgentId,
91 iconUrl: props.acpRegistryIconUrl,
92 });
93​
94 return (
95 <span
96 className={cn(
97 "relative isolate z-30 inline-flex shrink-0 items-center justify-center overflow-visible",
98 props.className,
99 )}
100 style={accentStyle}
101 data-provider-accent-color={accentColor}
102 >
103 {isAcpRegistry ? (
104 <AcpRegistryAgentIcon
105 className={cn("size-5 rounded-none bg-transparent", iconClassName)}
106 fallbackClassName="size-full"
107 icon={acpRegistryIconUrl}
108 />
109 ) : packageIcon ? (
110 <ProviderPackageIcon
111 icon={packageIcon}
112 className={cn("size-5 shrink-0", iconClassName)}
113 aria-hidden
114 />
115 ) : Icon ? (
116 <Icon className={cn("size-5 shrink-0", iconClassName)} aria-hidden />
117 ) : (
118 <span className={cn("text-3xs font-semibold leading-none", iconClassName)}>
119 {providerInstanceInitials(props.displayName)}
120 </span>
121 )}
122 {props.statusDotClassName ? (
123 <span
124 className={cn(
125 "pointer-events-none absolute -left-0.5 -top-0.5 z-10 size-2 rounded-full",
126 props.statusDotClassName,
127 )}
128 style={{ boxShadow: `0 0 0 2px ${indicatorBackground}` }}
129 aria-hidden
130 />
131 ) : null}
132 {props.showBadge ? (
133 <span
134 className={cn(
135 "pointer-events-none absolute right-0 bottom-0 z-10 flex h-3.5 min-w-3.5 items-center justify-center rounded-full border px-0.5 text-4xs font-semibold leading-none shadow-sm",
136 accentColor
137 ? "bg-(--provider-accent) text-(--provider-accent-foreground)"
138 : "bg-card text-muted-foreground",
139 props.size === "compact" &&
140 "right-[-0.1875rem] bottom-[-0.1875rem] h-3 min-w-3 px-0.5 text-5xs",
141 props.badgeClassName,
142 )}
143 style={{ borderColor: indicatorBackground }}
144 aria-hidden
145 >
146 {badgeContent === "initials" ? providerInstanceInitials(props.displayName) : null}
147 </span>
148 ) : null}
149 </span>
150 );
151});