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

ModelPickerSidebar.tsx

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