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

ModelPickerSidebar.tsx

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