Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 | 1x 1x 1x 1x 1x 1x 1x 32x 32x 32x 32x 32x 32x 32x 32x 32x 32x 32x 32x 3x 3x 3x 32x 10x 10x 10x 10x 10x 32x 32x 11x 30x 30x 30x 32x 32x 11x 11x 11x 11x 11x 11x 11x 11x 11x 32x 32x 14x 32x 32x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 32x 13x 13x 2x 2x 32x 32x 10x 32x 32x 32x 11x 1x 1x 32x 32x 32x 32x 32x 32x 32x 32x 32x 32x 32x 1x 1x 1x 1x 32x 32x 32x 32x 32x 32x 32x 32x 32x 32x 32x 32x 32x 1x 1x 1x 32x 32x 32x 32x 32x 32x 32x 32x 32x 32x 32x 32x 32x 32x 1x 1x 1x 32x 32x 32x 32x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x | import { VSCodeTextField } from "@vscode/webview-ui-toolkit/react"
import Fuse from "fuse.js"
import React, { KeyboardEvent, memo, useEffect, useMemo, useRef, useState } from "react"
import styled from "styled-components"
import { highlight } from "../history/HistoryView"
export const OLLAMA_MODEL_PICKER_Z_INDEX = 1_000
export interface OllamaModelPickerProps {
ollamaModels: string[]
selectedModelId: string
onModelChange: (modelId: string) => void
placeholder?: string
}
const OllamaModelPicker: React.FC<OllamaModelPickerProps> = ({
ollamaModels,
selectedModelId,
onModelChange,
placeholder = "Search and select a model...",
}) => {
const [searchTerm, setSearchTerm] = useState(selectedModelId || "")
const [isDropdownVisible, setIsDropdownVisible] = useState(false)
const [selectedIndex, setSelectedIndex] = useState(-1)
const dropdownRef = useRef<HTMLDivElement>(null)
const itemRefs = useRef<(HTMLDivElement | null)[]>([])
const dropdownListRef = useRef<HTMLDivElement>(null)
const handleModelChange = (newModelId: string) => {
onModelChange(newModelId)
setSearchTerm(newModelId)
}
useEffect(() => {
const handleClickOutside = (event: MouseEvent) => {
if (dropdownRef.current && !dropdownRef.current.contains(event.target as Node)) {
setIsDropdownVisible(false)
}
}
document.addEventListener("mousedown", handleClickOutside)
return () => {
document.removeEventListener("mousedown", handleClickOutside)
}
}, [])
const searchableItems = useMemo(() => {
return ollamaModels.map((id) => ({
id,
html: id,
}))
}, [ollamaModels])
const fuse = useMemo(() => {
return new Fuse(searchableItems, {
keys: ["html"],
threshold: 0.6,
shouldSort: true,
isCaseSensitive: false,
ignoreLocation: false,
includeMatches: true,
minMatchCharLength: 1,
})
}, [searchableItems])
const modelSearchResults = useMemo(() => {
return searchTerm ? highlight(fuse.search(searchTerm), "ollama-model-item-highlight") : searchableItems
}, [searchableItems, searchTerm, fuse])
const handleKeyDown = (event: KeyboardEvent<HTMLInputElement>) => {
if (!isDropdownVisible) {
return
}
switch (event.key) {
case "ArrowDown":
event.preventDefault()
setSelectedIndex((prev) => (prev < modelSearchResults.length - 1 ? prev + 1 : prev))
break
case "ArrowUp":
event.preventDefault()
setSelectedIndex((prev) => (prev > 0 ? prev - 1 : prev))
break
case "Enter":
event.preventDefault()
if (selectedIndex >= 0 && selectedIndex < modelSearchResults.length) {
handleModelChange(modelSearchResults[selectedIndex].id)
setIsDropdownVisible(false)
}
break
case "Escape":
setIsDropdownVisible(false)
setSelectedIndex(-1)
break
}
}
useEffect(() => {
setSelectedIndex(-1)
if (dropdownListRef.current) {
dropdownListRef.current.scrollTop = 0
}
}, [searchTerm])
useEffect(() => {
if (selectedIndex >= 0 && itemRefs.current[selectedIndex]) {
itemRefs.current[selectedIndex]?.scrollIntoView({
block: "nearest",
behavior: "smooth",
})
}
}, [selectedIndex])
// Update search term when selectedModelId changes externally
useEffect(() => {
if (selectedModelId !== searchTerm) {
setSearchTerm(selectedModelId || "")
}
}, [selectedModelId])
return (
<div style={{ width: "100%" }}>
<style>
{`
.ollama-model-item-highlight {
background-color: var(--vscode-editor-findMatchHighlightBackground);
color: inherit;
}
`}
</style>
<DropdownWrapper ref={dropdownRef}>
<VSCodeTextField
id="ollama-model-search"
onFocus={() => setIsDropdownVisible(true)}
onInput={(e) => {
const value = (e.target as HTMLInputElement)?.value || ""
handleModelChange(value)
setIsDropdownVisible(true)
}}
onKeyDown={handleKeyDown}
placeholder={placeholder}
style={{
width: "100%",
zIndex: OLLAMA_MODEL_PICKER_Z_INDEX,
position: "relative",
}}
value={searchTerm}>
{searchTerm && (
<div
aria-label="Clear search"
className="input-icon-button codicon codicon-close"
onClick={() => {
handleModelChange("")
setIsDropdownVisible(true)
}}
slot="end"
style={{
display: "flex",
justifyContent: "center",
alignItems: "center",
height: "100%",
}}
/>
)}
</VSCodeTextField>
{isDropdownVisible && modelSearchResults.length > 0 && (
<DropdownList ref={dropdownListRef}>
{modelSearchResults.map((item, index) => (
<DropdownItem
isSelected={index === selectedIndex}
key={item.id}
onClick={() => {
handleModelChange(item.id)
setIsDropdownVisible(false)
}}
onMouseEnter={() => setSelectedIndex(index)}
ref={(el) => (itemRefs.current[index] = el)}>
<span dangerouslySetInnerHTML={{ __html: item.html }} />
</DropdownItem>
))}
</DropdownList>
)}
</DropdownWrapper>
</div>
)
}
export default memo(OllamaModelPicker)
// Dropdown styling
const DropdownWrapper = styled.div`
position: relative;
width: 100%;
`
const DropdownList = styled.div`
position: absolute;
top: calc(100% - 3px);
left: 0;
width: calc(100% - 2px);
max-height: 200px;
overflow-y: auto;
background-color: var(--vscode-dropdown-background);
border: 1px solid var(--vscode-list-activeSelectionBackground);
z-index: ${OLLAMA_MODEL_PICKER_Z_INDEX - 1};
border-bottom-left-radius: 3px;
border-bottom-right-radius: 3px;
`
const DropdownItem = styled.div<{ isSelected: boolean }>`
padding: 5px 10px;
cursor: pointer;
word-break: break-all;
white-space: normal;
background-color: ${({ isSelected }) => (isSelected ? "var(--vscode-list-activeSelectionBackground)" : "inherit")};
&:hover {
background-color: var(--vscode-list-activeSelectionBackground);
}
`
|