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