| 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 { |
| 10 | defaultBorderRadius, |
| 11 | lightGray, |
| 12 | vscCommandCenterInactiveBorder, |
| 13 | } from ".."; |
| 14 | import { ToolTip } from "../gui/Tooltip"; |
| 15 | |
| 16 | export const OptionDiv = styled.div<{ |
| 17 | isDisabled?: boolean; |
| 18 | isSelected?: boolean; |
| 19 | }>` |
| 20 | padding: 6px 12px; |
| 21 | |
| 22 | min-width: 0px; |
| 23 | |
| 24 | ${({ isDisabled, isSelected }) => |
| 25 | !isDisabled && |
| 26 | ` |
| 27 | cursor: pointer; |
| 28 | |
| 29 | &:hover { |
| 30 | background: ${lightGray}33; |
| 31 | } |
| 32 | |
| 33 | ${ |
| 34 | isSelected && |
| 35 | ` |
| 36 | background: ${lightGray}22; |
| 37 | ` |
| 38 | } |
| 39 | `} |
| 40 | |
| 41 | ${({ isDisabled }) => |
| 42 | isDisabled && |
| 43 | ` |
| 44 | opacity: 0.5; |
| 45 | `} |
| 46 | `; |
| 47 | |
| 48 | export const MAX_HEIGHT_PX = 300; |
| 49 | |
| 50 | export const Divider = styled.div` |
| 51 | height: 0.5px; |
| 52 | background-color: ${vscCommandCenterInactiveBorder}; |
| 53 | `; |
| 54 | |
| 55 | interface ModelOptionProps { |
| 56 | children: React.ReactNode; |
| 57 | idx: number; |
| 58 | disabled: boolean; |
| 59 | selected: boolean; |
| 60 | showConfigure: boolean; |
| 61 | onOpenConfig: () => void; |
| 62 | onClick: () => void; |
| 63 | errors?: ConfigValidationError[]; |
| 64 | onClickError?: (e: any) => void; |
| 65 | } |
| 66 | |
| 67 | const IconBase = styled.div<{ $hovered: boolean }>` |
| 68 | width: 1.2em; |
| 69 | height: 1.2em; |
| 70 | cursor: pointer; |
| 71 | padding: 4px; |
| 72 | border-radius: ${defaultBorderRadius}; |
| 73 | opacity: ${(props) => (props.$hovered ? 0.75 : 0)}; |
| 74 | visibility: ${(props) => (props.$hovered ? "visible" : "hidden")}; |
| 75 | |
| 76 | &:hover { |
| 77 | opacity: 1; |
| 78 | background-color: ${lightGray}33; |
| 79 | } |
| 80 | `; |
| 81 | |
| 82 | const StyledCog6ToothIcon = styled(IconBase).attrs({ as: Cog6ToothIcon })``; |
| 83 | const StyledArrowTopRightOnSquareIcon = styled(IconBase).attrs({ |
| 84 | as: ArrowTopRightOnSquareIcon, |
| 85 | })``; |
| 86 | const StyledExclamationTriangleIcon = styled(IconBase).attrs({ |
| 87 | as: ExclamationTriangleIcon, |
| 88 | })``; |
| 89 | |
| 90 | export function Option({ |
| 91 | children, |
| 92 | idx, |
| 93 | disabled, |
| 94 | onClick, |
| 95 | showConfigure, |
| 96 | selected, |
| 97 | errors, |
| 98 | onClickError, |
| 99 | onOpenConfig, |
| 100 | }: ModelOptionProps) { |
| 101 | const [hovered, setHovered] = useState(false); |
| 102 | |
| 103 | function handleOptionClick(e: any) { |
| 104 | if (disabled) { |
| 105 | e.preventDefault(); |
| 106 | e.stopPropagation(); |
| 107 | } |
| 108 | onClick(); |
| 109 | } |
| 110 | |
| 111 | return ( |
| 112 | <OptionDiv |
| 113 | key={idx} |
| 114 | isDisabled={disabled} |
| 115 | onMouseEnter={() => setHovered(true)} |
| 116 | onMouseLeave={() => setHovered(false)} |
| 117 | isSelected={selected} |
| 118 | onClick={!disabled ? handleOptionClick : undefined} |
| 119 | > |
| 120 | <div className="flex w-full flex-col gap-0.5"> |
| 121 | <div className="flex w-full items-center justify-between"> |
| 122 | {children} |
| 123 | <div className="ml-2 flex items-center"> |
| 124 | {!errors?.length ? ( |
| 125 | showConfigure ? ( |
| 126 | <StyledCog6ToothIcon |
| 127 | $hovered={hovered} |
| 128 | onClick={(e) => { |
| 129 | e.stopPropagation(); |
| 130 | e.preventDefault(); |
| 131 | onOpenConfig(); |
| 132 | }} |
| 133 | /> |
| 134 | ) : ( |
| 135 | <StyledArrowTopRightOnSquareIcon |
| 136 | $hovered={hovered} |
| 137 | onClick={(e) => { |
| 138 | e.stopPropagation(); |
| 139 | e.preventDefault(); |
| 140 | onOpenConfig(); |
| 141 | }} |
| 142 | /> |
| 143 | ) |
| 144 | ) : ( |
| 145 | <> |
| 146 | <StyledExclamationTriangleIcon |
| 147 | data-tooltip-id={`${idx}-errors-tooltip`} |
| 148 | $hovered={hovered} |
| 149 | className="cursor-pointer text-red-500" |
| 150 | onClick={onClickError} |
| 151 | /> |
| 152 | <ToolTip id={`${idx}-errors-tooltip`}> |
| 153 | <div className="font-semibold">Errors</div> |
| 154 | {JSON.stringify(errors, null, 2)} |
| 155 | </ToolTip> |
| 156 | </> |
| 157 | )} |
| 158 | </div> |
| 159 | </div> |
| 160 | </div> |
| 161 | </OptionDiv> |
| 162 | ); |
| 163 | } |