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

ModelPickerSidebar.tsx

263 lines10.4 KB

1import { type ProviderInstanceId } from "@t3tools/contracts";
2import { memo, useMemo } from "react";
3import { Clock3Icon, SparklesIcon, StarIcon } from "lucide-react";
4import { Gemini, GithubCopilotIcon } from "../Icons";
5import { ProviderInstanceIcon } from "./ProviderInstanceIcon";
6import { ScrollArea } from "../ui/scroll-area";
7import { Tooltip, TooltipPopup, TooltipTrigger } from "../ui/tooltip";
8import { cn } from "~/lib/utils";
9import type { ProviderInstanceEntry } from "../../providerInstances";
10​
11/**
12 * Build the hover tooltip for an instance button. Mirrors the old
13 * kind-based copy but uses the entry's configured `displayName` so custom
14 * instances get their user-authored name (e.g. "Codex Personal — Unavailable.").
15 */
16function describeUnavailableInstance(entry: ProviderInstanceEntry): string {
17 const label = entry.displayName;
18 if (entry.status === "ready") {
19 return label;
20 }
21 const kind =
22 entry.status === "error"
23 ? "Unavailable"
24 : entry.status === "warning"
25 ? "Limited"
26 : entry.status === "disabled"
27 ? "Disabled in settings"
28 : "Not ready";
29 const msg = entry.snapshot.message?.trim();
30 return msg ? `${label} — ${kind}. ${msg}` : `${label} — ${kind}.`;
32​
33const SELECTED_BUTTON_CLASS = "bg-background text-foreground shadow-sm";
34const SELECTED_INDICATOR_CLASS =
35 "pointer-events-none absolute -right-1 top-1/2 z-10 h-5 w-0.5 -translate-y-1/2 rounded-l-full bg-primary";
36const BADGE_BASE_CLASS =
37 "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 ";
38const NEW_BADGE_CLASS = `${BADGE_BASE_CLASS} text-amber-600 dark:text-amber-300 `;
39const SOON_BADGE_CLASS = `${BADGE_BASE_CLASS} text-muted-foreground `;
40​
41/** Opens toward the rail so the list stays readable (not over the model names). */
42const PICKER_TOOLTIP_SIDE = "left" as const;
43const PICKER_TOOLTIP_CLASS = "max-w-64 text-balance font-normal leading-snug";
44​
45export const ModelPickerSidebar = memo(function ModelPickerSidebar(props: {
46 selectedInstanceId: ProviderInstanceId | "favorites";
47 onSelectInstance: (instanceId: ProviderInstanceId | "favorites") => 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 /** Render non-configured coming-soon provider entries. Hidden in scoped rails. */
58 showComingSoon?: boolean;
59 /**
60 * Instance id values that should render the "new" sparkle badge. Callers
61 * pass the subset of default built-in ids they want flagged (custom
62 * instances are never flagged — the user just made them).
63 */
64 newBadgeInstanceIds?: ReadonlySet<ProviderInstanceId>;
65}) {
66 const handleSelect = (instanceId: ProviderInstanceId | "favorites") => {
67 props.onSelectInstance(instanceId);
68 };
69 const showFavorites = props.showFavorites ?? true;
70 const showComingSoon = props.showComingSoon ?? true;
71 const duplicateDriverCounts = useMemo(() => {
72 const counts = new Map<string, number>();
73 for (const entry of props.instanceEntries) {
74 counts.set(entry.driverKind, (counts.get(entry.driverKind) ?? 0) + 1);
75 }
76 return counts;
77 }, [props.instanceEntries]);
78​
79 return (
80 <ScrollArea
81 hideScrollbars
82 scrollFade
83 className="w-12 shrink-0 border-r bg-muted/30"
84 data-model-picker-sidebar="true"
85 >
86 <div className="flex min-h-full flex-col gap-1 p-1">
87 {/* Favorites section */}
88 {showFavorites ? (
89 <div className="pb-1 mb-1 border-b">
90 <div className="relative w-full">
91 {props.selectedInstanceId === "favorites" && (
92 <div className={SELECTED_INDICATOR_CLASS} />
93 )}
94 <Tooltip>
95 <TooltipTrigger
96 render={
97 <button
98 className={cn(
99 "relative isolate flex w-full cursor-pointer aspect-square items-center justify-center rounded transition-colors hover:bg-muted",
100 props.selectedInstanceId === "favorites" && SELECTED_BUTTON_CLASS,
101 )}
102 onClick={() => handleSelect("favorites")}
103 type="button"
104 data-model-picker-provider="favorites"
105 aria-label="Favorites"
106 >
107 <StarIcon className="size-5 fill-current shrink-0" aria-hidden />
108 </button>
109 }
110 />
111 <TooltipPopup
112 side={PICKER_TOOLTIP_SIDE}
113 align="center"
114 className={PICKER_TOOLTIP_CLASS}
115 >
116 Favorites
117 </TooltipPopup>
118 </Tooltip>
119 </div>
120 </div>
121 ) : null}
122​
123 {/* Instance buttons (one per configured instance — built-in + custom) */}
124 {props.instanceEntries.map((entry) => {
125 const isDisabled = !entry.isAvailable || entry.status !== "ready";
126 const isSelected = props.selectedInstanceId === entry.instanceId;
127 const showNewBadge = props.newBadgeInstanceIds?.has(entry.instanceId) ?? false;
128 const showInstanceBadge =
129 Boolean(entry.accentColor) || (duplicateDriverCounts.get(entry.driverKind) ?? 0) > 1;
130​
131 const tooltip = isDisabled
132 ? describeUnavailableInstance(entry)
133 : showNewBadge
134 ? `${entry.displayName} — New`
135 : entry.displayName;
136​
137 const button = (
138 <button
139 data-model-picker-provider={entry.instanceId}
140 className={cn(
141 "relative isolate flex w-full cursor-pointer aspect-square items-center justify-center rounded transition-colors hover:bg-muted",
142 isSelected && SELECTED_BUTTON_CLASS,
143 isDisabled && "opacity-50 cursor-not-allowed hover:bg-transparent",
144 )}
145 data-provider-accent-color={entry.accentColor}
146 onClick={() => !isDisabled && handleSelect(entry.instanceId)}
147 disabled={isDisabled}
148 type="button"
149 aria-label={
150 isDisabled
151 ? tooltip
152 : showNewBadge
153 ? `${entry.displayName}, new`
154 : entry.displayName
155 }
156 >
157 <ProviderInstanceIcon
158 driverKind={entry.driverKind}
159 displayName={entry.displayName}
160 accentColor={entry.accentColor}
161 showBadge={showInstanceBadge}
162 className="size-6"
163 iconClassName="size-5"
164 />
165 {showNewBadge ? (
166 <span className={NEW_BADGE_CLASS} aria-hidden>
167 <SparklesIcon className="size-2" />
168 </span>
169 ) : null}
170 </button>
171 );
172​
173 const trigger = isDisabled ? (
174 <span className="relative block w-full">{button}</span>
175 ) : (
176 button
177 );
178​
179 return (
180 <div key={entry.instanceId} className="relative w-full">
181 {isSelected && <div className={SELECTED_INDICATOR_CLASS} />}
182 <Tooltip>
183 <TooltipTrigger render={trigger} />
184 <TooltipPopup
185 side={PICKER_TOOLTIP_SIDE}
186 align="center"
187 className={PICKER_TOOLTIP_CLASS}
188 >
189 {tooltip}
190 </TooltipPopup>
191 </Tooltip>
192 </div>
193 );
194 })}
195​
196 {showComingSoon ? (
197 <>
198 {/* Gemini button (coming soon) */}
199 <Tooltip>
200 <TooltipTrigger
201 render={
202 <span className="relative block w-full">
203 <button
204 className={cn(
205 "relative isolate flex w-full aspect-square items-center justify-center rounded opacity-50 cursor-not-allowed transition-colors hover:bg-transparent",
206 )}
207 disabled
208 type="button"
209 data-model-picker-provider="gemini-coming-soon"
210 aria-label="Gemini — coming soon"
211 >
212 <Gemini className="size-5 text-muted-foreground/85" aria-hidden />
213 <span className={SOON_BADGE_CLASS} aria-hidden>
214 <Clock3Icon className="size-2" />
215 </span>
216 </button>
217 </span>
218 }
219 />
220 <TooltipPopup
221 side={PICKER_TOOLTIP_SIDE}
222 align="center"
223 className={PICKER_TOOLTIP_CLASS}
224 >
225 Gemini — Coming soon
226 </TooltipPopup>
227 </Tooltip>
228 {/* Github Copilot button (coming soon) */}
229 <Tooltip>
230 <TooltipTrigger
231 render={
232 <span className="relative block w-full">
233 <button
234 className={cn(
235 "relative isolate flex w-full aspect-square items-center justify-center rounded opacity-50 cursor-not-allowed transition-colors hover:bg-transparent",
236 )}
237 disabled
238 type="button"
239 data-model-picker-provider="github-copilot-coming-soon"
240 aria-label="Github Copilot — coming soon"
241 >
242 <GithubCopilotIcon className="size-5 text-muted-foreground/85" aria-hidden />
243 <span className={SOON_BADGE_CLASS} aria-hidden>
244 <Clock3Icon className="size-2" />
245 </span>
246 </button>
247 </span>
248 }
249 />
250 <TooltipPopup
251 side={PICKER_TOOLTIP_SIDE}
252 align="center"
253 className={PICKER_TOOLTIP_CLASS}
254 >
255 Github Copilot — Coming soon
256 </TooltipPopup>
257 </Tooltip>
258 </>
259 ) : null}
260 </div>
261 </ScrollArea>
262 );
263});