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

ModelPickerSidebar.tsx

239 lines10.7 KB

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}.`;
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 /** Non-ready instances whose selected unavailable model remains reachable. */
58 selectableUnavailableInstanceIds?: ReadonlySet<ProviderInstanceId>;
59 getDisabledInstanceTooltip?: (entry: ProviderInstanceEntry) => string;
60 /**
61 * Instance id values that should render the "new" sparkle badge. Callers
62 * pass the subset of default built-in ids they want flagged (custom
63 * instances are never flagged — the user just made them).
64 */
65 newBadgeInstanceIds?: ReadonlySet<ProviderInstanceId>;
66}) {
67 const handleSelect = (instanceId: ProviderInstanceId | "favorites") => {
68 props.onSelectInstance(instanceId);
69 };
70 const showFavorites = props.showFavorites ?? true;
71 const [hoveredInstanceId, setHoveredInstanceId] = useState<ProviderInstanceId | null>(null);
72 const sidebarContentRef = useRef<HTMLDivElement>(null);
73 const [selectedIndicatorTop, setSelectedIndicatorTop] = useState<number | null>(null);
74 useLayoutEffect(() => {
75 const content = sidebarContentRef.current;
76 if (!content) {
77 return;
78 }
79 const selectedItem = Array.from(
80 content.querySelectorAll<HTMLElement>("[data-model-picker-provider]"),
81 ).find((item) => item.dataset.modelPickerProvider === props.selectedInstanceId);
82 if (!selectedItem) {
83 setSelectedIndicatorTop(null);
84 return;
85 }
86 setSelectedIndicatorTop(selectedItem.offsetTop + selectedItem.offsetHeight / 2 - 10);
87 }, [props.instanceEntries, props.selectedInstanceId, showFavorites]);
88​
89 return (
90 <div className="w-11 shrink-0 overflow-hidden bg-muted/30" data-model-picker-sidebar="true">
91 <div className="h-full overflow-y-auto overscroll-contain [-ms-overflow-style:none] [scrollbar-width:none] [&::-webkit-scrollbar]:hidden">
92 <div ref={sidebarContentRef} className="relative flex min-h-full flex-col gap-1 p-1">
93 {selectedIndicatorTop !== null ? (
94 <div
95 data-model-picker-selected-indicator="true"
96 className={cn(
97 SELECTED_INDICATOR_CLASS,
98 "right-0 translate-y-0 transition-[top] duration-200 ease-out",
99 )}
100 style={{ top: selectedIndicatorTop }}
101 />
102 ) : null}
103 {/* Favorites section */}
104 {showFavorites ? (
105 <>
106 <div className="relative w-full" data-model-picker-provider="favorites">
107 <Tooltip>
108 <TooltipTrigger
109 render={
110 <button
111 className={cn(
112 "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",
113 )}
114 onClick={() => handleSelect("favorites")}
115 type="button"
116 aria-label="Favorites"
117 >
118 <StarIcon className="size-5 fill-current shrink-0" aria-hidden />
119 </button>
120 }
121 />
122 <TooltipPopup
123 side={PICKER_TOOLTIP_SIDE}
124 sideOffset={PICKER_TOOLTIP_SIDE_OFFSET}
125 align="center"
126 className={PICKER_TOOLTIP_CLASS}
127 >
128 Favorites
129 </TooltipPopup>
130 </Tooltip>
131 </div>
132 <div className="border-b border-border/70" aria-hidden="true" />
133 </>
134 ) : null}
135​
136 {/* Instance buttons (one per configured instance — built-in + custom) */}
137 {props.instanceEntries.map((entry) => {
138 const isUnavailable = !isProviderInstancePickerReady(entry);
139 const isContextDisabled = props.disabledInstanceIds?.has(entry.instanceId) ?? false;
140 const unavailableSelectionIsReachable =
141 props.selectableUnavailableInstanceIds?.has(entry.instanceId) ?? false;
142 const isDisabled =
143 (isUnavailable && !unavailableSelectionIsReachable) || isContextDisabled;
144 const isSelected = props.selectedInstanceId === entry.instanceId;
145 const isHovered = hoveredInstanceId === entry.instanceId;
146 const showNewBadge = props.newBadgeInstanceIds?.has(entry.instanceId) ?? false;
147 const showInstanceBadge = shouldShowInstanceBadge(entry, props.instanceEntries);
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(--contrast-foreground))] focus-visible:bg-[color-mix(in_srgb,var(--popover)_90%,var(--contrast-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 isUnavailable || isContextDisabled
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});