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

ModelPickerSidebar.tsx

239 lines10.6 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 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 <div className="w-11 shrink-0 overflow-hidden bg-muted/30" data-model-picker-sidebar="true">
93 <div className="h-full overflow-y-auto overscroll-contain [-ms-overflow-style:none] [scrollbar-width:none] [&::-webkit-scrollbar]:hidden">
94 <div ref={sidebarContentRef} className="relative flex min-h-full flex-col gap-1 p-1">
95 {selectedIndicatorTop !== null ? (
96 <div
97 data-model-picker-selected-indicator="true"
98 className={cn(
99 SELECTED_INDICATOR_CLASS,
100 "right-0 translate-y-0 transition-[top] duration-200 ease-out",
101 )}
102 style={{ top: selectedIndicatorTop }}
103 />
104 ) : null}
105 {/* Favorites section */}
106 {showFavorites ? (
107 <>
108 <div className="relative w-full" data-model-picker-provider="favorites">
109 <Tooltip>
110 <TooltipTrigger
111 render={
112 <button
113 className={cn(
114 "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",
115 )}
116 onClick={() => handleSelect("favorites")}
117 type="button"
118 aria-label="Favorites"
119 >
120 <StarIcon className="size-5 fill-current shrink-0" aria-hidden />
121 </button>
122 }
123 />
124 <TooltipPopup
125 side={PICKER_TOOLTIP_SIDE}
126 sideOffset={PICKER_TOOLTIP_SIDE_OFFSET}
127 align="center"
128 className={PICKER_TOOLTIP_CLASS}
129 >
130 Favorites
131 </TooltipPopup>
132 </Tooltip>
133 </div>
134 <div className="border-b border-border/70" aria-hidden="true" />
135 </>
136 ) : null}
137​
138 {/* Instance buttons (one per configured instance — built-in + custom) */}
139 {props.instanceEntries.map((entry) => {
140 const isUnavailable = !isProviderInstancePickerReady(entry);
141 const isContextDisabled = props.disabledInstanceIds?.has(entry.instanceId) ?? false;
142 const isDisabled = isUnavailable || isContextDisabled;
143 const isSelected = props.selectedInstanceId === entry.instanceId;
144 const isHovered = hoveredInstanceId === entry.instanceId;
145 const showNewBadge = props.newBadgeInstanceIds?.has(entry.instanceId) ?? false;
146 const showInstanceBadge =
147 Boolean(entry.accentColor) || (duplicateDriverCounts.get(entry.driverKind) ?? 0) > 1;
148​
149 const tooltip = isUnavailable
150 ? describeUnavailableInstance(entry)
151 : isContextDisabled
152 ? (props.getDisabledInstanceTooltip?.(entry) ?? entry.displayName)
153 : showNewBadge
154 ? `${entry.displayName} — New`
155 : entry.displayName;
156​
157 const button = (
158 <button
159 className={cn(
160 "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",
161 isDisabled && "opacity-50 cursor-not-allowed hover:bg-transparent",
162 )}
163 data-provider-accent-color={entry.accentColor}
164 onClick={() => !isDisabled && handleSelect(entry.instanceId)}
165 onMouseEnter={() => setHoveredInstanceId(entry.instanceId)}
166 onMouseLeave={() =>
167 setHoveredInstanceId((current) => (current === entry.instanceId ? null : current))
168 }
169 onFocus={() => setHoveredInstanceId(entry.instanceId)}
170 onBlur={() =>
171 setHoveredInstanceId((current) => (current === entry.instanceId ? null : current))
172 }
173 disabled={isDisabled}
174 type="button"
175 aria-label={
176 isDisabled
177 ? tooltip
178 : showNewBadge
179 ? `${entry.displayName}, new`
180 : entry.displayName
181 }
182 >
183 <ProviderInstanceIcon
184 driverKind={entry.driverKind}
185 displayName={entry.displayName}
186 accentColor={entry.accentColor}
187 showBadge={showInstanceBadge}
188 className="size-6"
189 iconClassName="size-5"
190 indicatorBackground={
191 isHovered && !isDisabled
192 ? "var(--muted)"
193 : isSelected
194 ? "var(--background)"
195 : "color-mix(in oklab, var(--muted) 30%, transparent)"
196 }
197 {...(entry.accentColor
198 ? { badgeClassName: "h-3 min-w-3 px-0.5 text-[7px]" }
199 : {})}
200 />
201 {showNewBadge ? (
202 <span className={NEW_BADGE_CLASS} aria-hidden>
203 <SparklesIcon className="size-2" />
204 </span>
205 ) : null}
206 </button>
207 );
208​
209 const trigger = isDisabled ? (
210 <span className="relative block w-full">{button}</span>
211 ) : (
212 button
213 );
214​
215 return (
216 <div
217 key={entry.instanceId}
218 className="relative w-full"
219 data-model-picker-provider={entry.instanceId}
220 >
221 <Tooltip>
222 <TooltipTrigger render={trigger} />
223 <TooltipPopup
224 side={PICKER_TOOLTIP_SIDE}
225 sideOffset={PICKER_TOOLTIP_SIDE_OFFSET}
226 align="center"
227 className={PICKER_TOOLTIP_CLASS}
228 >
229 {tooltip}
230 </TooltipPopup>
231 </Tooltip>
232 </div>
233 );
234 })}
235 </div>
236 </div>
237 </div>
238 );
239});