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

ModelListRow.tsx

150 lines5.4 KB

1import { type ProviderDriverKind, type ProviderInstanceId } from "@t3tools/contracts";
2import { memo } from "react";
3import { CheckIcon, StarIcon } from "lucide-react";
4import { getDisplayModelName } from "./providerIconUtils";
5import { ComboboxItem } from "../ui/combobox";
6import { Button } from "../ui/button";
7import { Badge } from "../ui/badge";
8import { Kbd } from "../ui/kbd";
9import { Tooltip, TooltipPopup, TooltipTrigger } from "../ui/tooltip";
10import { cn } from "~/lib/utils";
11import { modelPickerModelKey } from "./modelPickerKeys";
12import { ProviderInstanceIcon } from "./ProviderInstanceIcon";
13​
14export const ModelListRow = memo(function ModelListRow(props: {
15 index: number;
16 slug: string;
17 name: string;
18 subProvider?: string | undefined;
19 instanceId: ProviderInstanceId;
20 driverKind: ProviderDriverKind;
21 providerDisplayName: string;
22 providerAccentColor?: string | undefined;
23 acpRegistryAgentId?: string | undefined;
24 acpRegistryIconUrl?: string | undefined;
25 isFavorite: boolean;
26 /** In Favorites every row is starred, so the star shows only on hover. */
27 favoriteStarOnHover?: boolean;
28 isSelected: boolean;
29 /** Marks this instance apart from another of the same provider. */
30 showInstanceBadge: boolean;
31 showNewBadge?: boolean;
32 unavailable?: boolean;
33 jumpLabel?: string | null;
34 disabledReason?: string | null;
35 onToggleFavorite: (instanceId: ProviderInstanceId, slug: string) => void;
36}) {
37 const model = {
38 slug: props.slug,
39 name: props.name,
40 subProvider: props.subProvider,
41 };
42​
43 const row = (
44 <ComboboxItem
45 hideIndicator
46 selectedFill={false}
47 index={props.index}
48 value={modelPickerModelKey(props.instanceId, props.slug)}
49 disabled={Boolean(props.disabledReason)}
50 className={cn(
51 "group relative w-full !min-w-0 max-w-full cursor-pointer",
52 props.disabledReason &&
53 "data-disabled:pointer-events-auto data-disabled:cursor-not-allowed",
54 )}
55 >
56 {/* One row shape in every tab: the logo names the provider, so Favorites needs no second line. */}
57 <ProviderInstanceIcon
58 driverKind={props.driverKind}
59 displayName={props.providerDisplayName}
60 accentColor={props.providerAccentColor}
61 acpRegistryAgentId={props.acpRegistryAgentId}
62 acpRegistryIconUrl={props.acpRegistryIconUrl}
63 showBadge={props.showInstanceBadge}
64 className="size-4"
65 iconClassName="size-4"
66 indicatorBackground="var(--popover)"
67 badgeClassName="right-[-0.125rem] bottom-[-0.125rem] h-3 min-w-3 px-0.5 text-5xs"
68 />
69 <div className="flex min-w-0 flex-1 items-center gap-2 text-left">
70 <div className="min-w-0 truncate text-sm font-normal leading-snug">
71 {getDisplayModelName(model)}
72 {/* The logo is decorative, so name the provider for screen readers. */}
73 <span className="sr-only">, {props.providerDisplayName}</span>
74 </div>
75 {props.subProvider ? (
76 <span className="min-w-0 shrink truncate text-xs text-muted-foreground/70">
77 {props.subProvider}
78 </span>
79 ) : null}
80 {props.showNewBadge ? (
81 <span
82 className="shrink-0 rounded border border-update/35 bg-update/15 px-0.5 py-px text-3xs font-bold uppercase leading-none tracking-wide text-update-foreground"
83 aria-label="New model"
84 >
85 New
86 </span>
87 ) : null}
88 {props.unavailable ? (
89 <Badge variant="outline" size="sm">
90 Unavailable
91 </Badge>
92 ) : null}
93 </div>
94​
95 <div className="flex shrink-0 items-center gap-1.5">
96 {props.jumpLabel ? <Kbd>{props.jumpLabel}</Kbd> : null}
97 <Tooltip>
98 <TooltipTrigger
99 render={
100 <Button
101 size="icon-xs"
102 variant="ghost-muted"
103 className="-mr-1 shrink-0"
104 onClick={(event) => {
105 event.stopPropagation();
106 props.onToggleFavorite(props.instanceId, props.slug);
107 }}
108 onKeyDown={(event) => {
109 event.stopPropagation();
110 }}
111 disabled={Boolean(props.disabledReason)}
112 aria-label={props.isFavorite ? "Remove from favorites" : "Add to favorites"}
113 >
114 <StarIcon
115 className={cn(
116 "size-3.5 sm:size-3",
117 props.isFavorite && "fill-current text-warning",
118 (!props.isFavorite || props.favoriteStarOnHover) &&
119 "opacity-0 group-hover:opacity-100 group-data-highlighted:opacity-100 in-focus-visible:opacity-100 pointer-coarse:opacity-100",
120 )}
121 />
122 </Button>
123 }
124 />
125 <TooltipPopup side="top" align="center">
126 {props.isFavorite ? "Remove from favorites" : "Add to favorites"}
127 </TooltipPopup>
128 </Tooltip>
129 {/* Last, and always reserved, so stars and the check keep fixed columns. */}
130 <CheckIcon
131 className={cn("size-3.5", !props.isSelected && "invisible")}
132 aria-hidden="true"
133 />
134 </div>
135 </ComboboxItem>
136 );
137​
138 if (!props.disabledReason) {
139 return row;
140 }
141​
142 return (
143 <Tooltip>
144 <TooltipTrigger render={row} />
145 <TooltipPopup side="left" align="center">
146 {props.disabledReason}
147 </TooltipPopup>
148 </Tooltip>
149 );
150});