Open History to see the last commit
1import { type ProviderKind, type ServerProvider } from "@t3tools/contracts";
2import { memo } from "react";
3import { Clock3Icon, SparklesIcon, StarIcon } from "lucide-react";
4import { Gemini, GithubCopilotIcon } from "../Icons";
5import { AVAILABLE_PROVIDER_OPTIONS, PROVIDER_ICON_BY_PROVIDER } from "./providerIconUtils";
6import { Tooltip, TooltipPopup, TooltipTrigger } from "../ui/tooltip";
7import { cn } from "~/lib/utils";
8import { getProviderSnapshot } from "../../providerModels";
9
10function describeUnavailableProvider(label: string, live: ServerProvider | undefined): string {
11 if (!live) {
12 return `${label} — waiting for provider status…`;
13 }
14 if (live.status === "ready") {
15 return label;
16 }
17 const kind =
18 live.status === "error"
19 ? "Unavailable"
20 : live.status === "warning"
21 ? "Limited"
22 : live.status === "disabled"
23 ? "Disabled in settings"
24 : "Not ready";
25 const msg = live.message?.trim();
26 return msg ? `${label} — ${kind}. ${msg}` : `${label} — ${kind}.`;
27}
28
29const SELECTED_BUTTON_CLASS = "bg-background text-foreground shadow-sm";
30const SELECTED_INDICATOR_CLASS =
31 "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";
32const BADGE_BASE_CLASS =
33 "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 ";
34const NEW_BADGE_CLASS = `${BADGE_BASE_CLASS} text-amber-600 dark:text-amber-300 `;
35const SOON_BADGE_CLASS = `${BADGE_BASE_CLASS} text-muted-foreground `;
36
37/** Opens toward the rail so the list stays readable (not over the model names). */
38const PICKER_TOOLTIP_SIDE = "left" as const;
39const PICKER_TOOLTIP_CLASS = "max-w-64 text-balance font-normal leading-snug";
40
41export const ModelPickerSidebar = memo(function ModelPickerSidebar(props: {
42 selectedProvider: ProviderKind | "favorites";
43 onSelectProvider: (provider: ProviderKind | "favorites") => void;
44 providers?: ReadonlyArray<ServerProvider>;
45}) {
46 const handleProviderClick = (provider: ProviderKind | "favorites") => {
47 props.onSelectProvider(provider);
48 };
49
50 return (
51 <div className="flex flex-col w-12 border-r bg-muted/30 p-1 overflow-y-auto gap-1">
52 {/* Favorites section */}
53 <div className="pb-1 mb-1 border-b">
54 <div className="relative w-full">
55 {props.selectedProvider === "favorites" && <div className={SELECTED_INDICATOR_CLASS} />}
56 <Tooltip>
57 <TooltipTrigger
58 render={
59 <button
60 className={cn(
61 "relative isolate flex w-full cursor-pointer aspect-square items-center justify-center rounded transition-colors hover:bg-muted",
62 props.selectedProvider === "favorites" && SELECTED_BUTTON_CLASS,
63 )}
64 onClick={() => handleProviderClick("favorites")}
65 type="button"
66 data-model-picker-provider="favorites"
67 aria-label="Favorites"
68 >
69 <StarIcon className="size-5 fill-current shrink-0" aria-hidden />
70 </button>
71 }
72 />
73 <TooltipPopup
74 side={PICKER_TOOLTIP_SIDE}
75 align="center"
76 className={PICKER_TOOLTIP_CLASS}
77 >
78 Favorites
79 </TooltipPopup>
80 </Tooltip>
81 </div>
82 </div>
83
84 {/* Provider buttons */}
85 {AVAILABLE_PROVIDER_OPTIONS.map((option) => {
86 const OptionIcon = PROVIDER_ICON_BY_PROVIDER[option.value];
87 const liveProvider = props.providers
88 ? getProviderSnapshot(props.providers, option.value)
89 : undefined;
90
91 const isDisabled = !liveProvider || liveProvider.status !== "ready";
92 const isSelected = props.selectedProvider === option.value;
93 const badge = option.pickerSidebarBadge;
94
95 const providerTooltip = isDisabled
96 ? describeUnavailableProvider(option.label, liveProvider)
97 : badge === "new"
98 ? `${option.label} — New`
99 : option.label;
100
101 const button = (
102 <button
103 data-model-picker-provider={option.value}
104 className={cn(
105 "relative isolate flex w-full cursor-pointer aspect-square items-center justify-center rounded transition-colors hover:bg-muted",
106 isSelected && SELECTED_BUTTON_CLASS,
107 isDisabled && "opacity-50 cursor-not-allowed hover:bg-transparent",
108 )}
109 onClick={() => !isDisabled && handleProviderClick(option.value)}
110 disabled={isDisabled}
111 type="button"
112 aria-label={
113 isDisabled
114 ? (providerTooltip ?? option.label)
115 : badge === "new"
116 ? `${option.label}, new`
117 : option.label
118 }
119 >
120 <OptionIcon className="size-5 shrink-0" aria-hidden />
121 {badge === "new" ? (
122 <span className={NEW_BADGE_CLASS} aria-hidden>
123 <SparklesIcon className="size-2" />
124 </span>
125 ) : badge === "soon" ? (
126 <span className={SOON_BADGE_CLASS} aria-hidden>
127 <Clock3Icon className="size-2" />
128 </span>
129 ) : null}
130 </button>
131 );
132
133 const trigger = isDisabled ? (
134 <span className="relative block w-full">{button}</span>
135 ) : (
136 button
137 );
138
139 return (
140 <div key={option.value} className="relative w-full">
141 {isSelected && <div className={SELECTED_INDICATOR_CLASS} />}
142 <Tooltip>
143 <TooltipTrigger render={trigger} />
144 <TooltipPopup
145 side={PICKER_TOOLTIP_SIDE}
146 align="center"
147 className={PICKER_TOOLTIP_CLASS}
148 >
149 {providerTooltip}
150 </TooltipPopup>
151 </Tooltip>
152 </div>
153 );
154 })}
155
156 {/* Gemini button (coming soon) */}
157 <Tooltip>
158 <TooltipTrigger
159 render={
160 <span className="relative block w-full">
161 <button
162 className={cn(
163 "relative isolate flex w-full aspect-square items-center justify-center rounded opacity-50 cursor-not-allowed transition-colors hover:bg-transparent",
164 )}
165 disabled
166 type="button"
167 data-model-picker-provider="gemini-coming-soon"
168 aria-label="Gemini — coming soon"
169 >
170 <Gemini className="size-5 text-muted-foreground/85" aria-hidden />
171 <span className={SOON_BADGE_CLASS} aria-hidden>
172 <Clock3Icon className="size-2" />
173 </span>
174 </button>
175 </span>
176 }
177 />
178 <TooltipPopup side={PICKER_TOOLTIP_SIDE} align="center" className={PICKER_TOOLTIP_CLASS}>
179 Gemini — Coming soon
180 </TooltipPopup>
181 </Tooltip>
182 {/* Github Copilot button (coming soon) */}
183 <Tooltip>
184 <TooltipTrigger
185 render={
186 <span className="relative block w-full">
187 <button
188 className={cn(
189 "relative isolate flex w-full aspect-square items-center justify-center rounded opacity-50 cursor-not-allowed transition-colors hover:bg-transparent",
190 )}
191 disabled
192 type="button"
193 data-model-picker-provider="github-copilot-coming-soon"
194 aria-label="Github Copilot — coming soon"
195 >
196 <GithubCopilotIcon className="size-5 text-muted-foreground/85" aria-hidden />
197 <span className={SOON_BADGE_CLASS} aria-hidden>
198 <Clock3Icon className="size-2" />
199 </span>
200 </button>
201 </span>
202 }
203 />
204 <TooltipPopup side={PICKER_TOOLTIP_SIDE} align="center" className={PICKER_TOOLTIP_CLASS}>
205 Github Copilot — Coming soon
206 </TooltipPopup>
207 </Tooltip>
208 </div>
209 );
210});