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