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

ModelPickerSidebar.tsx

250 lines10.5 KB

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