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