| 1 | import { BookOpenIcon } from "@heroicons/react/24/outline"; |
| 2 | import React, { useEffect, useState } from "react"; |
| 3 | import styled from "styled-components"; |
| 4 | import { |
| 5 | defaultBorderRadius, |
| 6 | greenButtonColor, |
| 7 | lightGray, |
| 8 | vscFocusBorder, |
| 9 | } from ".."; |
| 10 | import { PackageDimension } from "../../pages/AddNewModel/configs/models"; |
| 11 | import { providers } from "../../pages/AddNewModel/configs/providers"; |
| 12 | import HeaderButtonWithToolTip from "../gui/HeaderButtonWithToolTip"; |
| 13 | import InfoHover from "../InfoHover"; |
| 14 | import { ModelProviderTag } from "./ModelProviderTag"; |
| 15 | import { ModelProviderTags } from "./utils"; |
| 16 | |
| 17 | interface ModelCardProps { |
| 18 | title: string; |
| 19 | description: string; |
| 20 | tags?: ModelProviderTags[]; |
| 21 | refUrl?: string; |
| 22 | icon?: string; |
| 23 | onClick?: ( |
| 24 | e: React.MouseEvent<HTMLDivElement, MouseEvent>, |
| 25 | dimensionChoices?: string[], |
| 26 | selectedProvider?: string, |
| 27 | ) => void; |
| 28 | disabled?: boolean; |
| 29 | dimensions?: PackageDimension[]; |
| 30 | providerOptions?: string[]; |
| 31 | } |
| 32 | |
| 33 | const Div = styled.div<{ color: string; disabled: boolean; hovered: boolean }>` |
| 34 | border: 1px solid ${lightGray}; |
| 35 | border-radius: ${defaultBorderRadius}; |
| 36 | position: relative; |
| 37 | width: 100%; |
| 38 | transition: all 0.5s; |
| 39 | |
| 40 | ${(props) => |
| 41 | props.disabled |
| 42 | ? ` |
| 43 | opacity: 0.5; |
| 44 | ` |
| 45 | : props.hovered |
| 46 | ? ` |
| 47 | border: 1px solid ${props.color}; |
| 48 | background-color: ${props.color}22; |
| 49 | cursor: pointer;` |
| 50 | : ""} |
| 51 | `; |
| 52 | |
| 53 | const DimensionsDiv = styled.div` |
| 54 | display: flex; |
| 55 | justify-content: flex-end; |
| 56 | margin-left: auto; |
| 57 | padding: 4px; |
| 58 | /* width: fit-content; */ |
| 59 | |
| 60 | flex-wrap: wrap; |
| 61 | row-gap: 12px; |
| 62 | |
| 63 | border-top: 1px solid ${lightGray}; |
| 64 | `; |
| 65 | |
| 66 | const DimensionOptionDiv = styled.div<{ selected: boolean }>` |
| 67 | display: flex; |
| 68 | flex-direction: column; |
| 69 | align-items: center; |
| 70 | margin-right: 8px; |
| 71 | background-color: ${lightGray}; |
| 72 | padding: 4px; |
| 73 | border-radius: ${defaultBorderRadius}; |
| 74 | outline: 0.5px solid ${lightGray}; |
| 75 | |
| 76 | ${(props) => |
| 77 | props.selected && |
| 78 | ` |
| 79 | background-color: ${greenButtonColor}; |
| 80 | color: white; |
| 81 | `} |
| 82 | |
| 83 | &:hover { |
| 84 | cursor: pointer; |
| 85 | outline: 1px solid ${vscFocusBorder}; |
| 86 | } |
| 87 | `; |
| 88 | |
| 89 | function ModelCard(props: ModelCardProps) { |
| 90 | const [dimensionChoices, setDimensionChoices] = useState<string[]>( |
| 91 | props.dimensions?.map((d) => Object.keys(d.options)[0]) || [], |
| 92 | ); |
| 93 | |
| 94 | const [hovered, setHovered] = useState(false); |
| 95 | |
| 96 | const [selectedProvider, setSelectedProvider] = useState<string | undefined>( |
| 97 | undefined, |
| 98 | ); |
| 99 | |
| 100 | useEffect(() => { |
| 101 | if (props.providerOptions?.length) { |
| 102 | setSelectedProvider(props.providerOptions[0]); |
| 103 | } |
| 104 | }, [props.providerOptions]); |
| 105 | |
| 106 | return ( |
| 107 | <Div |
| 108 | disabled={props.disabled || false} |
| 109 | color={greenButtonColor} |
| 110 | hovered={hovered} |
| 111 | > |
| 112 | <div |
| 113 | onMouseEnter={() => setHovered(true)} |
| 114 | onMouseLeave={() => setHovered(false)} |
| 115 | className="px-2 py-1" |
| 116 | onClick={ |
| 117 | props.disabled |
| 118 | ? undefined |
| 119 | : (e) => { |
| 120 | if ((e.target as any).closest("a")) { |
| 121 | return; |
| 122 | } |
| 123 | props.onClick?.(e, dimensionChoices, selectedProvider); |
| 124 | } |
| 125 | } |
| 126 | > |
| 127 | <div |
| 128 | className="mb-2" |
| 129 | style={{ |
| 130 | display: "flex", |
| 131 | alignItems: "center", |
| 132 | }} |
| 133 | > |
| 134 | {window.vscMediaUrl && props.icon && ( |
| 135 | <img |
| 136 | src={`${window.vscMediaUrl}/logos/${props.icon}`} |
| 137 | width="24px" |
| 138 | height="24px" |
| 139 | style={{ |
| 140 | borderRadius: "2px", |
| 141 | padding: "4px", |
| 142 | marginRight: "10px", |
| 143 | objectFit: "contain", |
| 144 | }} |
| 145 | /> |
| 146 | )} |
| 147 | <h3>{props.title}</h3> |
| 148 | </div> |
| 149 | |
| 150 | {props.tags?.map((tag, i) => <ModelProviderTag key={i} tag={tag} />)} |
| 151 | |
| 152 | <p>{props.description}</p> |
| 153 | |
| 154 | {props.refUrl && ( |
| 155 | <a |
| 156 | style={{ |
| 157 | position: "absolute", |
| 158 | right: "8px", |
| 159 | top: "8px", |
| 160 | }} |
| 161 | href={props.refUrl} |
| 162 | target="_blank" |
| 163 | > |
| 164 | <HeaderButtonWithToolTip text="Read the docs"> |
| 165 | <BookOpenIcon width="1.6em" height="1.6em" /> |
| 166 | </HeaderButtonWithToolTip> |
| 167 | </a> |
| 168 | )} |
| 169 | </div> |
| 170 | |
| 171 | {(props.dimensions?.length || props.providerOptions?.length) && ( |
| 172 | <DimensionsDiv> |
| 173 | {props.dimensions?.map((dimension, i) => { |
| 174 | return ( |
| 175 | <> |
| 176 | <div className="flex items-center justify-between"> |
| 177 | <div className="flex items-center"> |
| 178 | <InfoHover |
| 179 | id={dimension.name} |
| 180 | msg={dimension.description} |
| 181 | /> |
| 182 | <p className="mx-2 my-0 py-0 text-sm">{dimension.name}</p> |
| 183 | </div> |
| 184 | <div className="flex items-center"> |
| 185 | {Object.keys(dimension.options).map((key) => { |
| 186 | return ( |
| 187 | <DimensionOptionDiv |
| 188 | onClick={(e) => { |
| 189 | e.stopPropagation(); |
| 190 | const newChoices = [...dimensionChoices]; |
| 191 | newChoices[i] = key; |
| 192 | setDimensionChoices(newChoices); |
| 193 | }} |
| 194 | selected={dimensionChoices[i] === key} |
| 195 | > |
| 196 | {key} |
| 197 | </DimensionOptionDiv> |
| 198 | ); |
| 199 | })} |
| 200 | </div> |
| 201 | </div> |
| 202 | <br /> |
| 203 | </> |
| 204 | ); |
| 205 | })} |
| 206 | {props.providerOptions?.length && ( |
| 207 | <div className="rtl flex flex-wrap items-center justify-end"> |
| 208 | <div className="flex items-center"> |
| 209 | <InfoHover |
| 210 | id={"provider-info"} |
| 211 | msg={ |
| 212 | "Select the provider through which you will access the model" |
| 213 | } |
| 214 | /> |
| 215 | </div> |
| 216 | <div className="rtl flex flex-wrap items-center justify-end"> |
| 217 | {props.providerOptions?.map((option, i) => { |
| 218 | const info = providers[option]; |
| 219 | if (!info) { |
| 220 | return null; |
| 221 | } |
| 222 | return ( |
| 223 | <HeaderButtonWithToolTip |
| 224 | text={info.title} |
| 225 | className="mx-1 items-center p-2 text-center" |
| 226 | style={{ |
| 227 | backgroundColor: |
| 228 | (i === 0 && |
| 229 | typeof selectedProvider === "undefined") || |
| 230 | selectedProvider === option |
| 231 | ? greenButtonColor + "aa" |
| 232 | : undefined, |
| 233 | }} |
| 234 | onClick={() => { |
| 235 | setSelectedProvider(option); |
| 236 | }} |
| 237 | > |
| 238 | {window.vscMediaUrl && info.icon && ( |
| 239 | <img |
| 240 | src={`${window.vscMediaUrl}/logos/${info.icon}`} |
| 241 | height="24px" |
| 242 | /> |
| 243 | )} |
| 244 | </HeaderButtonWithToolTip> |
| 245 | ); |
| 246 | })} |
| 247 | </div> |
| 248 | </div> |
| 249 | )} |
| 250 | </DimensionsDiv> |
| 251 | )} |
| 252 | </Div> |
| 253 | ); |
| 254 | } |
| 255 | |
| 256 | export default ModelCard; |