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

ModelPickerSidebar.tsx

237 lines10.0 KB

1import { Toolbar } from "@base-ui/react/toolbar";
2import { type ProviderInstanceId } from "@t3tools/contracts";
3import { memo, useLayoutEffect, useRef, useState } from "react";
4import { SparklesIcon, StarIcon } from "lucide-react";
5import { ProviderInstanceIcon } from "./ProviderInstanceIcon";
6import { Tooltip, TooltipPopup, TooltipTrigger } from "../ui/tooltip";
7import { cn } from "~/lib/utils";
8import {
9 isProviderInstancePickerReady,
10 shouldShowInstanceBadge,
11 type ProviderInstanceEntry,
12} from "../../providerInstances";
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-update-foreground `;
33​
34const PICKER_TOOLTIP_SIDE = "left" as const;
35const PICKER_TOOLTIP_SIDE_OFFSET = 8;
36​
37export const ModelPickerSidebar = memo(function ModelPickerSidebar(props: {
38 selectedInstanceId: ProviderInstanceId | "favorites";
39 onSelectInstance: (instanceId: ProviderInstanceId | "favorites") => void;
40 onFocusSearch: () => void;
41 instanceEntries: ReadonlyArray<ProviderInstanceEntry>;
42 showFavorites?: boolean;
43 disabledInstanceIds?: ReadonlySet<ProviderInstanceId>;
44 selectableUnavailableInstanceIds?: ReadonlySet<ProviderInstanceId>;
45 getDisabledInstanceTooltip?: (entry: ProviderInstanceEntry) => string;
46 newBadgeInstanceIds?: ReadonlySet<ProviderInstanceId>;
47}) {
48 const handleSelect = (instanceId: ProviderInstanceId | "favorites") => {
49 props.onSelectInstance(instanceId);
50 };
51 const showFavorites = props.showFavorites ?? true;
52 const [hoveredInstanceId, setHoveredInstanceId] = useState<ProviderInstanceId | null>(null);
53 const sidebarContentRef = useRef<HTMLDivElement>(null);
54 const [selectedIndicatorTop, setSelectedIndicatorTop] = useState<number | null>(null);
55 useLayoutEffect(() => {
56 const content = sidebarContentRef.current;
57 if (!content) {
58 return;
59 }
60 const selectedItem = Array.from(
61 content.querySelectorAll<HTMLElement>("[data-model-picker-provider]"),
62 ).find((item) => item.dataset.modelPickerProvider === props.selectedInstanceId);
63 if (!selectedItem) {
64 setSelectedIndicatorTop(null);
65 return;
66 }
67 setSelectedIndicatorTop(selectedItem.offsetTop + selectedItem.offsetHeight / 2 - 10);
68 }, [
69 // oxlint-disable-next-line react/exhaustive-effect-dependencies -- Adding, removing or reordering providers moves the selected item, so the indicator re-measures.
70 props.instanceEntries,
71 props.selectedInstanceId,
72 showFavorites,
73 ]);
74​
75 return (
76 <Toolbar.Root
77 className="w-11 shrink-0 overflow-hidden bg-muted/30"
78 data-model-picker-sidebar="true"
79 aria-label="Providers"
80 orientation="vertical"
81 onKeyDown={(event) => {
82 if (event.altKey || event.ctrlKey || event.metaKey || event.shiftKey) return;
83 if (event.key === "ArrowRight") {
84 event.preventDefault();
85 props.onFocusSearch();
86 return;
87 }
88 }}
89 >
90 <div className="h-full overflow-y-auto overscroll-contain [-ms-overflow-style:none] [scrollbar-width:none] [&::-webkit-scrollbar]:hidden">
91 <div ref={sidebarContentRef} className="relative flex min-h-full flex-col gap-1 p-1">
92 {selectedIndicatorTop !== null ? (
93 <div
94 data-model-picker-selected-indicator="true"
95 className={cn(
96 SELECTED_INDICATOR_CLASS,
97 "right-0 translate-y-0 transition-[top] duration-200 ease-out",
98 )}
99 style={{ top: selectedIndicatorTop }}
100 />
101 ) : null}
102 {showFavorites ? (
103 <>
104 <div className="relative w-full" data-model-picker-provider="favorites">
105 <Tooltip>
106 <TooltipTrigger
107 render={
108 <Toolbar.Button
109 className={cn(
110 "relative isolate flex w-full cursor-pointer aspect-square items-center justify-center rounded-md transition-colors hover:bg-foreground/10 focus-visible:bg-foreground/10 focus-visible:outline-none",
111 )}
112 onClick={() => handleSelect("favorites")}
113 type="button"
114 aria-label="Favorites"
115 aria-pressed={props.selectedInstanceId === "favorites"}
116 >
117 <StarIcon className="size-5 fill-current shrink-0" aria-hidden />
118 </Toolbar.Button>
119 }
120 />
121 <TooltipPopup
122 side={PICKER_TOOLTIP_SIDE}
123 sideOffset={PICKER_TOOLTIP_SIDE_OFFSET}
124 align="center"
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 {props.instanceEntries.map((entry) => {
135 const isUnavailable = !isProviderInstancePickerReady(entry);
136 const isContextDisabled = props.disabledInstanceIds?.has(entry.instanceId) ?? false;
137 const unavailableSelectionIsReachable =
138 props.selectableUnavailableInstanceIds?.has(entry.instanceId) ?? false;
139 const isDisabled =
140 (isUnavailable && !unavailableSelectionIsReachable) || isContextDisabled;
141 const isSelected = props.selectedInstanceId === entry.instanceId;
142 const isHovered = hoveredInstanceId === entry.instanceId;
143 const showNewBadge = props.newBadgeInstanceIds?.has(entry.instanceId) ?? false;
144 const showInstanceBadge = shouldShowInstanceBadge(entry, props.instanceEntries);
145​
146 const tooltip = isUnavailable
147 ? describeUnavailableInstance(entry)
148 : isContextDisabled
149 ? (props.getDisabledInstanceTooltip?.(entry) ?? entry.displayName)
150 : showNewBadge
151 ? `${entry.displayName} — New`
152 : entry.displayName;
153​
154 const button = (
155 <Toolbar.Button
156 className={cn(
157 "relative isolate flex w-full cursor-pointer aspect-square items-center justify-center rounded-md transition-colors hover:bg-foreground/10 focus-visible:bg-foreground/10 focus-visible:outline-none",
158 isDisabled && "opacity-50 cursor-not-allowed hover:bg-transparent",
159 )}
160 data-provider-accent-color={entry.accentColor}
161 onClick={() => !isDisabled && handleSelect(entry.instanceId)}
162 onMouseEnter={() => setHoveredInstanceId(entry.instanceId)}
163 onMouseLeave={() =>
164 setHoveredInstanceId((current) => (current === entry.instanceId ? null : current))
165 }
166 onFocus={() => setHoveredInstanceId(entry.instanceId)}
167 onBlur={() =>
168 setHoveredInstanceId((current) => (current === entry.instanceId ? null : current))
169 }
170 disabled={isDisabled}
171 focusableWhenDisabled={!isDisabled}
172 aria-pressed={isSelected}
173 type="button"
174 aria-label={
175 isUnavailable || isContextDisabled
176 ? tooltip
177 : showNewBadge
178 ? `${entry.displayName}, new`
179 : entry.displayName
180 }
181 >
182 <ProviderInstanceIcon
183 driverKind={entry.driverKind}
184 displayName={entry.displayName}
185 accentColor={entry.accentColor}
186 acpRegistryAgentId={entry.acpRegistryAgentId}
187 acpRegistryIconUrl={entry.acpRegistryIconUrl}
188 showBadge={showInstanceBadge}
189 className="size-6 z-30"
190 iconClassName="size-5"
191 indicatorBackground={
192 isHovered && !isDisabled
193 ? "var(--muted)"
194 : isSelected
195 ? "var(--background)"
196 : "color-mix(in oklab, var(--muted) 30%, transparent)"
197 }
198 {...(entry.accentColor ? { badgeClassName: "h-3 min-w-3 px-0.5 text-5xs" } : {})}
199 />
200 {showNewBadge ? (
201 <span className={NEW_BADGE_CLASS} aria-hidden>
202 <SparklesIcon className="size-2" />
203 </span>
204 ) : null}
205 </Toolbar.Button>
206 );
207​
208 const trigger = isDisabled ? (
209 <span className="relative block w-full">{button}</span>
210 ) : (
211 button
212 );
213​
214 return (
215 <div
216 key={entry.instanceId}
217 className="relative w-full"
218 data-model-picker-provider={entry.instanceId}
219 >
220 <Tooltip>
221 <TooltipTrigger render={trigger} />
222 <TooltipPopup
223 side={PICKER_TOOLTIP_SIDE}
224 sideOffset={PICKER_TOOLTIP_SIDE_OFFSET}
225 align="center"
226 >
227 {tooltip}
228 </TooltipPopup>
229 </Tooltip>
230 </div>
231 );
232 })}
233 </div>
234 </div>
235 </Toolbar.Root>
236 );
237});