Open History to see the last commit
1import { Toolbar } from "@base-ui/react/toolbar";
2import { type ProviderInstanceId } from "@t3tools/contracts";
3import { memo, useLayoutEffect, useRef, useState } from "react";
4import { SparklesIcon, StarIcon } from "lucide-react";
5import { ProviderInstanceIcon } from "./ProviderInstanceIcon";
6import { Tooltip, TooltipPopup, TooltipTrigger } from "../ui/tooltip";
7import { cn } from "~/lib/utils";
8import {
9 isProviderInstancePickerReady,
10 shouldShowInstanceBadge,
11 type ProviderInstanceEntry,
12} from "../../providerInstances";
13
14function describeUnavailableInstance(entry: ProviderInstanceEntry): string {
15 const label = entry.displayName;
16 if (!entry.enabled || entry.status === "disabled") {
17 return `${label} — Disabled in settings.`;
18 }
19 if (entry.status === "ready" && entry.isAvailable) {
20 return label;
21 }
22 const kind =
23 entry.status === "error" ? "Unavailable" : entry.status === "warning" ? "Limited" : "Not ready";
24 const msg = entry.snapshot.message?.trim();
25 return msg ? `${label} — ${kind}. ${msg}` : `${label} — ${kind}.`;
26}
27
28const SELECTED_INDICATOR_CLASS =
29 "pointer-events-none absolute -right-1 top-1/2 z-10 h-5 w-0.75 -translate-y-1/2 rounded-l-full bg-primary";
30const BADGE_BASE_CLASS =
31 "pointer-events-none absolute -right-0.5 top-0.5 z-10 flex size-3.5 items-center justify-center rounded-full bg-transparent shadow-sm ";
32const NEW_BADGE_CLASS = `${BADGE_BASE_CLASS} text-update-foreground `;
33
34const PICKER_TOOLTIP_SIDE = "left" as const;
35const PICKER_TOOLTIP_SIDE_OFFSET = 8;
36
37export const ModelPickerSidebar = memo(function ModelPickerSidebar(props: {
38 selectedInstanceId: ProviderInstanceId | "favorites";
39 onSelectInstance: (instanceId: ProviderInstanceId | "favorites") => void;
40 onFocusSearch: () => void;
41 instanceEntries: ReadonlyArray<ProviderInstanceEntry>;
42 showFavorites?: boolean;
43 disabledInstanceIds?: ReadonlySet<ProviderInstanceId>;
44 selectableUnavailableInstanceIds?: ReadonlySet<ProviderInstanceId>;
45 getDisabledInstanceTooltip?: (entry: ProviderInstanceEntry) => string;
46 newBadgeInstanceIds?: ReadonlySet<ProviderInstanceId>;
47}) {
48 const handleSelect = (instanceId: ProviderInstanceId | "favorites") => {
49 props.onSelectInstance(instanceId);
50 };
51 const showFavorites = props.showFavorites ?? true;
52 const [hoveredInstanceId, setHoveredInstanceId] = useState<ProviderInstanceId | null>(null);
53 const sidebarContentRef = useRef<HTMLDivElement>(null);
54 const [selectedIndicatorTop, setSelectedIndicatorTop] = useState<number | null>(null);
55 useLayoutEffect(() => {
56 const content = sidebarContentRef.current;
57 if (!content) {
58 return;
59 }
60 const selectedItem = Array.from(
61 content.querySelectorAll<HTMLElement>("[data-model-picker-provider]"),
62 ).find((item) => item.dataset.modelPickerProvider === props.selectedInstanceId);
63 if (!selectedItem) {
64 setSelectedIndicatorTop(null);
65 return;
66 }
67 setSelectedIndicatorTop(selectedItem.offsetTop + selectedItem.offsetHeight / 2 - 10);
68 }, [props.instanceEntries, props.selectedInstanceId, showFavorites]);
69
70 return (
71 <Toolbar.Root
72 className="w-11 shrink-0 overflow-hidden bg-muted/30"
73 data-model-picker-sidebar="true"
74 aria-label="Providers"
75 orientation="vertical"
76 onKeyDown={(event) => {
77 if (event.altKey || event.ctrlKey || event.metaKey || event.shiftKey) return;
78 if (event.key === "ArrowRight") {
79 event.preventDefault();
80 props.onFocusSearch();
81 return;
82 }
83 }}
84 >
85 <div className="h-full overflow-y-auto overscroll-contain [-ms-overflow-style:none] [scrollbar-width:none] [&::-webkit-scrollbar]:hidden">
86 <div ref={sidebarContentRef} className="relative flex min-h-full flex-col gap-1 p-1">
87 {selectedIndicatorTop !== null ? (
88 <div
89 data-model-picker-selected-indicator="true"
90 className={cn(
91 SELECTED_INDICATOR_CLASS,
92 "right-0 translate-y-0 transition-[top] duration-200 ease-out",
93 )}
94 style={{ top: selectedIndicatorTop }}
95 />
96 ) : null}
97 {showFavorites ? (
98 <>
99 <div className="relative w-full" data-model-picker-provider="favorites">
100 <Tooltip>
101 <TooltipTrigger
102 render={
103 <Toolbar.Button
104 className={cn(
105 "relative isolate flex w-full cursor-pointer aspect-square items-center justify-center rounded-md transition-colors hover:bg-foreground/10 focus-visible:bg-foreground/10 focus-visible:outline-none",
106 )}
107 onClick={() => handleSelect("favorites")}
108 type="button"
109 aria-label="Favorites"
110 aria-pressed={props.selectedInstanceId === "favorites"}
111 >
112 <StarIcon className="size-5 fill-current shrink-0" aria-hidden />
113 </Toolbar.Button>
114 }
115 />
116 <TooltipPopup
117 side={PICKER_TOOLTIP_SIDE}
118 sideOffset={PICKER_TOOLTIP_SIDE_OFFSET}
119 align="center"
120 >
121 Favorites
122 </TooltipPopup>
123 </Tooltip>
124 </div>
125 <div className="border-b border-border/70" aria-hidden="true" />
126 </>
127 ) : null}
128
129 {props.instanceEntries.map((entry) => {
130 const isUnavailable = !isProviderInstancePickerReady(entry);
131 const isContextDisabled = props.disabledInstanceIds?.has(entry.instanceId) ?? false;
132 const unavailableSelectionIsReachable =
133 props.selectableUnavailableInstanceIds?.has(entry.instanceId) ?? false;
134 const isDisabled =
135 (isUnavailable && !unavailableSelectionIsReachable) || isContextDisabled;
136 const isSelected = props.selectedInstanceId === entry.instanceId;
137 const isHovered = hoveredInstanceId === entry.instanceId;
138 const showNewBadge = props.newBadgeInstanceIds?.has(entry.instanceId) ?? false;
139 const showInstanceBadge = shouldShowInstanceBadge(entry, props.instanceEntries);
140
141 const tooltip = isUnavailable
142 ? describeUnavailableInstance(entry)
143 : isContextDisabled
144 ? (props.getDisabledInstanceTooltip?.(entry) ?? entry.displayName)
145 : showNewBadge
146 ? `${entry.displayName} — New`
147 : entry.displayName;
148
149 const button = (
150 <Toolbar.Button
151 className={cn(
152 "relative isolate flex w-full cursor-pointer aspect-square items-center justify-center rounded-md transition-colors hover:bg-foreground/10 focus-visible:bg-foreground/10 focus-visible:outline-none",
153 isDisabled && "opacity-50 cursor-not-allowed hover:bg-transparent",
154 )}
155 data-provider-accent-color={entry.accentColor}
156 onClick={() => !isDisabled && handleSelect(entry.instanceId)}
157 onMouseEnter={() => setHoveredInstanceId(entry.instanceId)}
158 onMouseLeave={() =>
159 setHoveredInstanceId((current) => (current === entry.instanceId ? null : current))
160 }
161 onFocus={() => setHoveredInstanceId(entry.instanceId)}
162 onBlur={() =>
163 setHoveredInstanceId((current) => (current === entry.instanceId ? null : current))
164 }
165 disabled={isDisabled}
166 focusableWhenDisabled={!isDisabled}
167 aria-pressed={isSelected}
168 type="button"
169 aria-label={
170 isUnavailable || isContextDisabled
171 ? tooltip
172 : showNewBadge
173 ? `${entry.displayName}, new`
174 : entry.displayName
175 }
176 >
177 <ProviderInstanceIcon
178 driverKind={entry.driverKind}
179 displayName={entry.displayName}
180 accentColor={entry.accentColor}
181 acpRegistryAgentId={entry.acpRegistryAgentId}
182 acpRegistryIconUrl={entry.acpRegistryIconUrl}
183 showBadge={showInstanceBadge}
184 className="size-6 z-30"
185 iconClassName="size-5"
186 indicatorBackground={
187 isHovered && !isDisabled
188 ? "var(--muted)"
189 : isSelected
190 ? "var(--background)"
191 : "color-mix(in oklab, var(--muted) 30%, transparent)"
192 }
193 {...(entry.accentColor ? { badgeClassName: "h-3 min-w-3 px-0.5 text-5xs" } : {})}
194 />
195 {showNewBadge ? (
196 <span className={NEW_BADGE_CLASS} aria-hidden>
197 <SparklesIcon className="size-2" />
198 </span>
199 ) : null}
200 </Toolbar.Button>
201 );
202
203 const trigger = isDisabled ? (
204 <span className="relative block w-full">{button}</span>
205 ) : (
206 button
207 );
208
209 return (
210 <div
211 key={entry.instanceId}
212 className="relative w-full"
213 data-model-picker-provider={entry.instanceId}
214 >
215 <Tooltip>
216 <TooltipTrigger render={trigger} />
217 <TooltipPopup
218 side={PICKER_TOOLTIP_SIDE}
219 sideOffset={PICKER_TOOLTIP_SIDE_OFFSET}
220 align="center"
221 >
222 {tooltip}
223 </TooltipPopup>
224 </Tooltip>
225 </div>
226 );
227 })}
228 </div>
229 </div>
230 </Toolbar.Root>
231 );
232});