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
14/**
15 * Build the hover tooltip for an instance button. Mirrors the old
16 * kind-based copy but uses the entry's configured `displayName` so custom
17 * instances get their user-authored name (e.g. "Codex Personal — Unavailable.").
18 */
19function describeUnavailableInstance(entry: ProviderInstanceEntry): string {
20 const label = entry.displayName;
21 if (!entry.enabled || entry.status === "disabled") {
22 return `${label} — Disabled in settings.`;
23 }
24 if (entry.status === "ready" && entry.isAvailable) {
25 return label;
26 }
27 const kind =
28 entry.status === "error" ? "Unavailable" : entry.status === "warning" ? "Limited" : "Not ready";
29 const msg = entry.snapshot.message?.trim();
30 return msg ? `${label} — ${kind}. ${msg}` : `${label} — ${kind}.`;
31}
32
33const SELECTED_INDICATOR_CLASS =
34 "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";
35const BADGE_BASE_CLASS =
36 "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 ";
37const NEW_BADGE_CLASS = `${BADGE_BASE_CLASS} text-update-foreground `;
38
39/** Opens toward the rail so the list stays readable (not over the model names). */
40const PICKER_TOOLTIP_SIDE = "left" as const;
41const PICKER_TOOLTIP_SIDE_OFFSET = 8;
42const PICKER_TOOLTIP_CLASS = "max-w-64 text-balance font-normal leading-snug";
43
44export const ModelPickerSidebar = memo(function ModelPickerSidebar(props: {
45 selectedInstanceId: ProviderInstanceId | "favorites";
46 onSelectInstance: (instanceId: ProviderInstanceId | "favorites") => void;
47 onFocusSearch: () => void;
48 /**
49 * Instance entries to render as rail buttons. Each entry becomes one icon
50 * keyed by `instanceId`, so the default built-in Codex and a user-authored
51 * `codex_personal` appear as two distinct rail items, each routing to
52 * their own model list.
53 */
54 instanceEntries: ReadonlyArray<ProviderInstanceEntry>;
55 /** Render the favorites rail entry. Hidden for locked-provider instance switching. */
56 showFavorites?: boolean;
57 /** Instance ids shown in the rail but unavailable for the current picker context. */
58 disabledInstanceIds?: ReadonlySet<ProviderInstanceId>;
59 /** Non-ready instances whose selected unavailable model remains reachable. */
60 selectableUnavailableInstanceIds?: ReadonlySet<ProviderInstanceId>;
61 getDisabledInstanceTooltip?: (entry: ProviderInstanceEntry) => string;
62 /**
63 * Instance id values that should render the "new" sparkle badge. Callers
64 * pass the subset of default built-in ids they want flagged (custom
65 * instances are never flagged — the user just made them).
66 */
67 newBadgeInstanceIds?: ReadonlySet<ProviderInstanceId>;
68}) {
69 const handleSelect = (instanceId: ProviderInstanceId | "favorites") => {
70 props.onSelectInstance(instanceId);
71 };
72 const showFavorites = props.showFavorites ?? true;
73 const [hoveredInstanceId, setHoveredInstanceId] = useState<ProviderInstanceId | null>(null);
74 const sidebarContentRef = useRef<HTMLDivElement>(null);
75 const [selectedIndicatorTop, setSelectedIndicatorTop] = useState<number | null>(null);
76 useLayoutEffect(() => {
77 const content = sidebarContentRef.current;
78 if (!content) {
79 return;
80 }
81 const selectedItem = Array.from(
82 content.querySelectorAll<HTMLElement>("[data-model-picker-provider]"),
83 ).find((item) => item.dataset.modelPickerProvider === props.selectedInstanceId);
84 if (!selectedItem) {
85 setSelectedIndicatorTop(null);
86 return;
87 }
88 setSelectedIndicatorTop(selectedItem.offsetTop + selectedItem.offsetHeight / 2 - 10);
89 }, [props.instanceEntries, props.selectedInstanceId, showFavorites]);
90
91 return (
92 <Toolbar.Root
93 className="w-11 shrink-0 overflow-hidden bg-muted/30"
94 data-model-picker-sidebar="true"
95 aria-label="Providers"
96 orientation="vertical"
97 onKeyDown={(event) => {
98 if (event.altKey || event.ctrlKey || event.metaKey || event.shiftKey) return;
99 if (event.key === "ArrowRight") {
100 event.preventDefault();
101 props.onFocusSearch();
102 return;
103 }
104 }}
105 >
106 <div className="h-full overflow-y-auto overscroll-contain [-ms-overflow-style:none] [scrollbar-width:none] [&::-webkit-scrollbar]:hidden">
107 <div ref={sidebarContentRef} className="relative flex min-h-full flex-col gap-1 p-1">
108 {selectedIndicatorTop !== null ? (
109 <div
110 data-model-picker-selected-indicator="true"
111 className={cn(
112 SELECTED_INDICATOR_CLASS,
113 "right-0 translate-y-0 transition-[top] duration-200 ease-out",
114 )}
115 style={{ top: selectedIndicatorTop }}
116 />
117 ) : null}
118 {/* Favorites section */}
119 {showFavorites ? (
120 <>
121 <div className="relative w-full" data-model-picker-provider="favorites">
122 <Tooltip>
123 <TooltipTrigger
124 render={
125 <Toolbar.Button
126 className={cn(
127 "relative isolate flex w-full cursor-pointer aspect-square items-center justify-center rounded-md transition-colors hover:bg-[color-mix(in_srgb,var(--popover)_90%,var(--contrast-foreground))] focus-visible:bg-[color-mix(in_srgb,var(--popover)_90%,var(--contrast-foreground))] focus-visible:outline-none",
128 )}
129 onClick={() => handleSelect("favorites")}
130 type="button"
131 aria-label="Favorites"
132 aria-pressed={props.selectedInstanceId === "favorites"}
133 >
134 <StarIcon className="size-5 fill-current shrink-0" aria-hidden />
135 </Toolbar.Button>
136 }
137 />
138 <TooltipPopup
139 side={PICKER_TOOLTIP_SIDE}
140 sideOffset={PICKER_TOOLTIP_SIDE_OFFSET}
141 align="center"
142 className={PICKER_TOOLTIP_CLASS}
143 >
144 Favorites
145 </TooltipPopup>
146 </Tooltip>
147 </div>
148 <div className="border-b border-border/70" aria-hidden="true" />
149 </>
150 ) : null}
151
152 {/* Instance buttons (one per configured instance — built-in + custom) */}
153 {props.instanceEntries.map((entry) => {
154 const isUnavailable = !isProviderInstancePickerReady(entry);
155 const isContextDisabled = props.disabledInstanceIds?.has(entry.instanceId) ?? false;
156 const unavailableSelectionIsReachable =
157 props.selectableUnavailableInstanceIds?.has(entry.instanceId) ?? false;
158 const isDisabled =
159 (isUnavailable && !unavailableSelectionIsReachable) || isContextDisabled;
160 const isSelected = props.selectedInstanceId === entry.instanceId;
161 const isHovered = hoveredInstanceId === entry.instanceId;
162 const showNewBadge = props.newBadgeInstanceIds?.has(entry.instanceId) ?? false;
163 const showInstanceBadge = shouldShowInstanceBadge(entry, props.instanceEntries);
164
165 const tooltip = isUnavailable
166 ? describeUnavailableInstance(entry)
167 : isContextDisabled
168 ? (props.getDisabledInstanceTooltip?.(entry) ?? entry.displayName)
169 : showNewBadge
170 ? `${entry.displayName} — New`
171 : entry.displayName;
172
173 const button = (
174 <Toolbar.Button
175 className={cn(
176 "relative isolate flex w-full cursor-pointer aspect-square items-center justify-center rounded-md transition-colors hover:bg-[color-mix(in_srgb,var(--popover)_90%,var(--contrast-foreground))] focus-visible:bg-[color-mix(in_srgb,var(--popover)_90%,var(--contrast-foreground))] focus-visible:outline-none",
177 isDisabled && "opacity-50 cursor-not-allowed hover:bg-transparent",
178 )}
179 data-provider-accent-color={entry.accentColor}
180 onClick={() => !isDisabled && handleSelect(entry.instanceId)}
181 onMouseEnter={() => setHoveredInstanceId(entry.instanceId)}
182 onMouseLeave={() =>
183 setHoveredInstanceId((current) => (current === entry.instanceId ? null : current))
184 }
185 onFocus={() => setHoveredInstanceId(entry.instanceId)}
186 onBlur={() =>
187 setHoveredInstanceId((current) => (current === entry.instanceId ? null : current))
188 }
189 disabled={isDisabled}
190 focusableWhenDisabled={!isDisabled}
191 aria-pressed={isSelected}
192 type="button"
193 aria-label={
194 isUnavailable || isContextDisabled
195 ? tooltip
196 : showNewBadge
197 ? `${entry.displayName}, new`
198 : entry.displayName
199 }
200 >
201 <ProviderInstanceIcon
202 driverKind={entry.driverKind}
203 displayName={entry.displayName}
204 accentColor={entry.accentColor}
205 showBadge={showInstanceBadge}
206 className="size-6 z-30"
207 iconClassName="size-5"
208 indicatorBackground={
209 isHovered && !isDisabled
210 ? "var(--muted)"
211 : isSelected
212 ? "var(--background)"
213 : "color-mix(in oklab, var(--muted) 30%, transparent)"
214 }
215 {...(entry.accentColor
216 ? { badgeClassName: "h-3 min-w-3 px-0.5 text-[7px]" }
217 : {})}
218 />
219 {showNewBadge ? (
220 <span className={NEW_BADGE_CLASS} aria-hidden>
221 <SparklesIcon className="size-2" />
222 </span>
223 ) : null}
224 </Toolbar.Button>
225 );
226
227 const trigger = isDisabled ? (
228 <span className="relative block w-full">{button}</span>
229 ) : (
230 button
231 );
232
233 return (
234 <div
235 key={entry.instanceId}
236 className="relative w-full"
237 data-model-picker-provider={entry.instanceId}
238 >
239 <Tooltip>
240 <TooltipTrigger render={trigger} />
241 <TooltipPopup
242 side={PICKER_TOOLTIP_SIDE}
243 sideOffset={PICKER_TOOLTIP_SIDE_OFFSET}
244 align="center"
245 className={PICKER_TOOLTIP_CLASS}
246 >
247 {tooltip}
248 </TooltipPopup>
249 </Tooltip>
250 </div>
251 );
252 })}
253 </div>
254 </div>
255 </Toolbar.Root>
256 );
257});