| 1 | import { ConfigValidationError } from "@continuedev/config-yaml"; |
| 2 | import { |
| 3 | ArrowTopRightOnSquareIcon, |
| 4 | Cog6ToothIcon, |
| 5 | ExclamationTriangleIcon, |
| 6 | } from "@heroicons/react/24/outline"; |
| 7 | import { useState } from "react"; |
| 8 | import styled from "styled-components"; |
| 9 | import { lightGray } from ".."; |
| 10 | import { cn } from "../../util/cn"; |
| 11 | import { ToolTip } from "../gui/Tooltip"; |
| 12 | |
| 13 | export const OptionDiv = styled.div<{ |
| 14 | isDisabled?: boolean; |
| 15 | isSelected?: boolean; |
| 16 | }>` |
| 17 | padding: 6px 12px; |
| 18 | |
| 19 | min-width: 0px; |
| 20 | |
| 21 | ${({ isDisabled, isSelected }) => |
| 22 | !isDisabled && |
| 23 | ` |
| 24 | cursor: pointer; |
| 25 | |
| 26 | &:hover { |
| 27 | background: ${lightGray}33; |
| 28 | } |
| 29 | |
| 30 | ${ |
| 31 | isSelected && |
| 32 | ` |
| 33 | background: ${lightGray}22; |
| 34 | ` |
| 35 | } |
| 36 | `} |
| 37 | |
| 38 | ${({ isDisabled }) => |
| 39 | isDisabled && |
| 40 | ` |
| 41 | opacity: 0.5; |
| 42 | `} |
| 43 | `; |
| 44 | |
| 45 | export const MAX_HEIGHT_PX = 300; |
| 46 | |
| 47 | interface ModelOptionProps { |
| 48 | children: React.ReactNode; |
| 49 | idx: number; |
| 50 | disabled: boolean; |
| 51 | selected: boolean; |
| 52 | showConfigure: boolean; |
| 53 | onOpenConfig: () => void; |
| 54 | onClick: () => void; |
| 55 | errors?: ConfigValidationError[]; |
| 56 | onClickError?: (e: any) => void; |
| 57 | } |
| 58 | |
| 59 | interface IconBaseProps { |
| 60 | $hovered: boolean; |
| 61 | onClick?: (e: any) => void; |
| 62 | className?: string; |
| 63 | children?: React.ReactNode; |
| 64 | [key: string]: any; |
| 65 | } |
| 66 | |
| 67 | function IconBase({ |
| 68 | $hovered, |
| 69 | onClick, |
| 70 | className, |
| 71 | children, |
| 72 | ...props |
| 73 | }: IconBaseProps) { |
| 74 | return ( |
| 75 | <div |
| 76 | className={cn( |
| 77 | "rounded-default h-[1.2em] w-[1.2em] cursor-pointer p-1", |
| 78 | $hovered ? "visible opacity-75" : "invisible opacity-0", |
| 79 | "hover:bg-lightgray/20 hover:opacity-100", |
| 80 | className, |
| 81 | )} |
| 82 | onClick={onClick} |
| 83 | {...props} |
| 84 | > |
| 85 | {children} |
| 86 | </div> |
| 87 | ); |
| 88 | } |
| 89 | |
| 90 | const StyledCog6ToothIcon = ({ |
| 91 | $hovered, |
| 92 | onClick, |
| 93 | }: { |
| 94 | $hovered: boolean; |
| 95 | onClick?: (e: any) => void; |
| 96 | }) => ( |
| 97 | <IconBase $hovered={$hovered} onClick={onClick}> |
| 98 | <Cog6ToothIcon /> |
| 99 | </IconBase> |
| 100 | ); |
| 101 | |
| 102 | const StyledArrowTopRightOnSquareIcon = ({ |
| 103 | $hovered, |
| 104 | onClick, |
| 105 | }: { |
| 106 | $hovered: boolean; |
| 107 | onClick?: (e: any) => void; |
| 108 | }) => ( |
| 109 | <IconBase $hovered={$hovered} onClick={onClick}> |
| 110 | <ArrowTopRightOnSquareIcon /> |
| 111 | </IconBase> |
| 112 | ); |
| 113 | |
| 114 | const StyledExclamationTriangleIcon = ({ |
| 115 | $hovered, |
| 116 | onClick, |
| 117 | className, |
| 118 | ...props |
| 119 | }: { |
| 120 | $hovered: boolean; |
| 121 | onClick?: (e: any) => void; |
| 122 | className?: string; |
| 123 | [key: string]: any; |
| 124 | }) => ( |
| 125 | <IconBase |
| 126 | $hovered={$hovered} |
| 127 | onClick={onClick} |
| 128 | className={className} |
| 129 | {...props} |
| 130 | > |
| 131 | <ExclamationTriangleIcon /> |
| 132 | </IconBase> |
| 133 | ); |
| 134 | |
| 135 | export function Option({ |
| 136 | children, |
| 137 | idx, |
| 138 | disabled, |
| 139 | onClick, |
| 140 | showConfigure, |
| 141 | selected, |
| 142 | errors, |
| 143 | onClickError, |
| 144 | onOpenConfig, |
| 145 | }: ModelOptionProps) { |
| 146 | const [hovered, setHovered] = useState(false); |
| 147 | |
| 148 | function handleOptionClick(e: any) { |
| 149 | if (disabled) { |
| 150 | e.preventDefault(); |
| 151 | e.stopPropagation(); |
| 152 | } |
| 153 | onClick(); |
| 154 | } |
| 155 | |
| 156 | return ( |
| 157 | <OptionDiv |
| 158 | key={idx} |
| 159 | isDisabled={disabled} |
| 160 | onMouseEnter={() => setHovered(true)} |
| 161 | onMouseLeave={() => setHovered(false)} |
| 162 | isSelected={selected} |
| 163 | onClick={!disabled ? handleOptionClick : undefined} |
| 164 | > |
| 165 | <div className="flex w-full flex-col gap-0.5"> |
| 166 | <div className="flex w-full items-center justify-between"> |
| 167 | {children} |
| 168 | <div className="ml-2 flex items-center"> |
| 169 | {!errors?.length ? ( |
| 170 | showConfigure ? ( |
| 171 | <StyledCog6ToothIcon |
| 172 | $hovered={hovered} |
| 173 | onClick={(e) => { |
| 174 | e.stopPropagation(); |
| 175 | e.preventDefault(); |
| 176 | onOpenConfig(); |
| 177 | }} |
| 178 | /> |
| 179 | ) : ( |
| 180 | <StyledArrowTopRightOnSquareIcon |
| 181 | $hovered={hovered} |
| 182 | onClick={(e) => { |
| 183 | e.stopPropagation(); |
| 184 | e.preventDefault(); |
| 185 | onOpenConfig(); |
| 186 | }} |
| 187 | /> |
| 188 | ) |
| 189 | ) : ( |
| 190 | <ToolTip |
| 191 | content={ |
| 192 | <> |
| 193 | <div className="font-semibold">Errors</div> |
| 194 | {JSON.stringify(errors, null, 2)} |
| 195 | </> |
| 196 | } |
| 197 | > |
| 198 | <StyledExclamationTriangleIcon |
| 199 | $hovered={hovered} |
| 200 | className="cursor-pointer text-red-500" |
| 201 | onClick={onClickError} |
| 202 | /> |
| 203 | </ToolTip> |
| 204 | )} |
| 205 | </div> |
| 206 | </div> |
| 207 | </div> |
| 208 | </OptionDiv> |
| 209 | ); |
| 210 | } |