Changesets · Branches · Tags · Compare
| Changeset | 59f8d5e2e742e6d9c764a8d9d8737f58952d5187efef059001dac8abf2fae452 |
|---|---|
| Author | Dallin Romney <dallinromney@gmail.com> · 2025-05-23 18:05 -07:00 |
| Committer | GitHub <noreply@github.com> · 2025-05-23 18:05 -07:00 |
| Parents | 5c04796c86d6, cd22093c507b |
6 files changed
modified gui/src/components/TabBar/TabBar.tsx +12 −8
| @@ -1,5 +1,5 @@ | ||
| 1 | 1 | import { XMarkIcon } from "@heroicons/react/24/outline"; |
| 2 | -import { useCallback, useEffect } from "react"; | |
| 2 | +import React, { useCallback, useEffect } from "react"; | |
| 3 | 3 | import { useDispatch, useSelector } from "react-redux"; |
| 4 | 4 | import styled from "styled-components"; |
| 5 | 5 | import { defaultBorderRadius } from ".."; |
| @@ -35,6 +35,8 @@ | ||
| 35 | 35 | border-bottom: none; |
| 36 | 36 | position: relative; |
| 37 | 37 | margin-top: 2px; |
| 38 | + max-height: 100px; | |
| 39 | + overflow: auto; | |
| 38 | 40 | |
| 39 | 41 | /* Hide scrollbar but keep functionality */ |
| 40 | 42 | scrollbar-width: none; |
| @@ -125,7 +127,7 @@ | ||
| 125 | 127 | background-color: ${tabBackgroundVar}; |
| 126 | 128 | `; |
| 127 | 129 | |
| 128 | -export function TabBar() { | |
| 130 | +export const TabBar = React.forwardRef<HTMLDivElement>((_, ref) => { | |
| 129 | 131 | const dispatch = useDispatch<AppDispatch>(); |
| 130 | 132 | const currentSessionId = useSelector((state: RootState) => state.session.id); |
| 131 | 133 | const currentSessionTitle = useSelector( |
| @@ -141,7 +143,6 @@ | ||
| 141 | 143 | return Date.now().toString(36) + Math.random().toString(36).substring(2); |
| 142 | 144 | }, []); |
| 143 | 145 | |
| 144 | - // Handle session changes | |
| 145 | 146 | useEffect(() => { |
| 146 | 147 | if (!currentSessionId) return; |
| 147 | 148 | |
| @@ -224,10 +225,13 @@ | ||
| 224 | 225 | } |
| 225 | 226 | }; |
| 226 | 227 | |
| 227 | - return tabs.length === 1 ? ( | |
| 228 | - <></> | |
| 229 | - ) : ( | |
| 230 | - <TabBarContainer> | |
| 228 | + return ( | |
| 229 | + <TabBarContainer | |
| 230 | + ref={ref} | |
| 231 | + style={{ | |
| 232 | + display: tabs.length === 1 ? "none" : "flex", | |
| 233 | + }} | |
| 234 | + > | |
| 231 | 235 | {tabs.map((tab) => ( |
| 232 | 236 | <Tab |
| 233 | 237 | key={tab.id} |
| @@ -253,4 +257,4 @@ | ||
| 253 | 257 | </TabBarSpace> |
| 254 | 258 | </TabBarContainer> |
| 255 | 259 | ); |
| 256 | -} | |
| 260 | +}); |
modified gui/src/components/find/FindWidget.tsx +126 −292
| @@ -12,36 +12,25 @@ | ||
| 12 | 12 | useState, |
| 13 | 13 | } from "react"; |
| 14 | 14 | import { HeaderButton, Input } from ".."; |
| 15 | -import { useAppSelector } from "../../redux/hooks"; | |
| 16 | 15 | import HeaderButtonWithToolTip from "../gui/HeaderButtonWithToolTip"; |
| 17 | - | |
| 18 | -interface SearchMatch { | |
| 19 | - index: number; | |
| 20 | - textNode: Text; | |
| 21 | - overlayRectangle: Rectangle; | |
| 22 | -} | |
| 23 | - | |
| 24 | -type ScrollToMatchOption = "closest" | "first" | "none"; | |
| 25 | - | |
| 26 | -const SEARCH_DEBOUNCE = 300; | |
| 27 | -const RESIZE_DEBOUNCE = 200; | |
| 28 | - | |
| 29 | -interface Rectangle { | |
| 30 | - top: number; | |
| 31 | - left: number; | |
| 32 | - width: number; | |
| 33 | - height: number; | |
| 34 | -} | |
| 35 | - | |
| 36 | -interface HighlightOverlayProps extends Rectangle { | |
| 16 | +import { | |
| 17 | + Rectangle, | |
| 18 | + SearchMatch, | |
| 19 | + searchWithinContainer, | |
| 20 | +} from "./findWidgetSearch"; | |
| 21 | +import useDebounceValue from "./useDebounce"; | |
| 22 | +import { useElementSize } from "./useElementSize"; | |
| 23 | + | |
| 24 | +interface HighlightOverlayProps { | |
| 25 | + rectangle: Rectangle; | |
| 37 | 26 | isCurrent: boolean; |
| 38 | 27 | } |
| 39 | 28 | |
| 40 | 29 | const HighlightOverlay = (props: HighlightOverlayProps) => { |
| 41 | - const { isCurrent, top, left, width, height } = props; | |
| 30 | + const { top, left, width, height } = props.rectangle; | |
| 42 | 31 | return ( |
| 43 | 32 | <div |
| 44 | - className={isCurrent ? "bg-findMatch-selected/50" : "bg-findMatch/50"} | |
| 33 | + className={props.isCurrent ? "bg-findMatch-selected" : "bg-findMatch"} | |
| 45 | 34 | key={`highlight-${top}-${left}`} |
| 46 | 35 | style={{ |
| 47 | 36 | position: "absolute", |
| @@ -56,6 +45,8 @@ | ||
| 56 | 45 | ); |
| 57 | 46 | }; |
| 58 | 47 | |
| 48 | +type ScrollToMatchOption = "closest" | "first" | "none"; | |
| 49 | + | |
| 59 | 50 | /* |
| 60 | 51 | useFindWidget takes a container ref and returns |
| 61 | 52 | 1. A widget that can be placed anywhere to search the contents of that container |
| @@ -64,18 +55,22 @@ | ||
| 64 | 55 | |
| 65 | 56 | Container must have relative positioning |
| 66 | 57 | */ |
| 67 | -export const useFindWidget = (searchRef: RefObject<HTMLDivElement>) => { | |
| 58 | +export const useFindWidget = ( | |
| 59 | + searchRef: RefObject<HTMLDivElement>, | |
| 60 | + headerRef: RefObject<HTMLDivElement>, | |
| 61 | + disabled: boolean, | |
| 62 | +) => { | |
| 68 | 63 | // Search input, debounced |
| 69 | - const [input, setInput] = useState<string>(""); | |
| 70 | - const debouncedInput = useRef<string>(""); | |
| 71 | 64 | const inputRef = useRef<HTMLInputElement>(null); |
| 65 | + const [currentValue, setCurrentValue] = useState<string>(""); | |
| 66 | + const searchTerm = useDebounceValue(currentValue, 300); | |
| 72 | 67 | |
| 73 | 68 | // Widget open/closed state |
| 74 | 69 | const [open, setOpen] = useState<boolean>(false); |
| 75 | 70 | const openWidget = useCallback(() => { |
| 76 | 71 | setOpen(true); |
| 77 | 72 | inputRef?.current?.select(); |
| 78 | - }, [setOpen, inputRef]); | |
| 73 | + }, [inputRef]); | |
| 79 | 74 | |
| 80 | 75 | // Search settings and results |
| 81 | 76 | const [caseSensitive, setCaseSensitive] = useState<boolean>(false); |
| @@ -97,7 +92,7 @@ | ||
| 97 | 92 | behavior: "smooth", |
| 98 | 93 | }); |
| 99 | 94 | }, |
| 100 | - [searchRef.current], | |
| 95 | + [searchRef], | |
| 101 | 96 | ); |
| 102 | 97 | |
| 103 | 98 | const nextMatch = useCallback(() => { |
| @@ -143,327 +138,166 @@ | ||
| 143 | 138 | }, [inputRef, matches, nextMatch]); |
| 144 | 139 | |
| 145 | 140 | // Handle container resize changes - highlight positions must adjust |
| 146 | - const [isResizing, setIsResizing] = useState(false); | |
| 147 | - useEffect(() => { | |
| 148 | - let timeoutId: NodeJS.Timeout | null = null; | |
| 149 | - if (!searchRef?.current) return; | |
| 150 | - | |
| 151 | - const resizeObserver = new ResizeObserver((entries) => { | |
| 152 | - if (timeoutId) { | |
| 153 | - clearTimeout(timeoutId); | |
| 154 | - } | |
| 155 | - setIsResizing(true); | |
| 156 | - timeoutId = setTimeout(() => { | |
| 157 | - setIsResizing(false); | |
| 158 | - }, RESIZE_DEBOUNCE); | |
| 159 | - }); | |
| 160 | - | |
| 161 | - resizeObserver.observe(searchRef.current); | |
| 162 | - return () => { | |
| 163 | - if (timeoutId) { | |
| 164 | - clearTimeout(timeoutId); | |
| 165 | - } | |
| 166 | - if (searchRef.current) resizeObserver.unobserve(searchRef.current); | |
| 167 | - }; | |
| 168 | - }, [searchRef.current]); | |
| 141 | + const { clientHeight: headerHeight, isResizing: headerResizing } = | |
| 142 | + useElementSize(headerRef); | |
| 143 | + const { isResizing: containerResizing } = useElementSize(searchRef); | |
| 144 | + const isResizing = useMemo(() => { | |
| 145 | + return containerResizing || headerResizing; | |
| 146 | + }, [containerResizing, headerResizing]); | |
| 169 | 147 | |
| 170 | 148 | // Main function for finding matches and generating highlight overlays |
| 171 | 149 | const refreshSearch = useCallback( |
| 172 | - (scrollTo: ScrollToMatchOption = "none", clearFirst = false) => { | |
| 173 | - if (clearFirst) setMatches([]); | |
| 174 | - | |
| 175 | - const searchContainer = searchRef.current; | |
| 176 | - | |
| 177 | - const _query = debouncedInput.current; // trimStart - decided no because spaces should be fully searchable | |
| 178 | - if (!searchContainer || !_query) { | |
| 179 | - setMatches([]); | |
| 180 | - return; | |
| 181 | - } | |
| 182 | - const query = caseSensitive ? _query : _query.toLowerCase(); | |
| 183 | - | |
| 184 | - // First grab all text nodes | |
| 185 | - // Skips any elements with the "find-widget-skip" class | |
| 186 | - const textNodes: Text[] = []; | |
| 187 | - const walker = document.createTreeWalker( | |
| 188 | - searchRef.current, | |
| 189 | - NodeFilter.SHOW_ALL, | |
| 150 | + (scrollTo: ScrollToMatchOption = "none") => { | |
| 151 | + const { results, closestToMiddle } = searchWithinContainer( | |
| 152 | + searchRef, | |
| 153 | + searchTerm, | |
| 190 | 154 | { |
| 191 | - acceptNode: (node) => { | |
| 192 | - if (node.nodeType === Node.ELEMENT_NODE) { | |
| 193 | - if ((node as Element).classList.contains("find-widget-skip")) | |
| 194 | - return NodeFilter.FILTER_REJECT; | |
| 195 | - return NodeFilter.FILTER_ACCEPT; | |
| 196 | - } else if (node.nodeType === Node.TEXT_NODE) { | |
| 197 | - if (!node.nodeValue) return NodeFilter.FILTER_REJECT; | |
| 198 | - const nodeValue = caseSensitive | |
| 199 | - ? node.nodeValue | |
| 200 | - : node.nodeValue.toLowerCase(); | |
| 201 | - if (nodeValue.includes(query)) return NodeFilter.FILTER_ACCEPT; | |
| 202 | - } | |
| 203 | - return NodeFilter.FILTER_REJECT; | |
| 204 | - }, | |
| 155 | + caseSensitive, | |
| 156 | + useRegex, | |
| 157 | + offsetHeight: headerHeight, | |
| 205 | 158 | }, |
| 206 | 159 | ); |
| 207 | - | |
| 208 | - while (walker.nextNode()) { | |
| 209 | - if (walker.currentNode.nodeType === Node.ELEMENT_NODE) continue; | |
| 210 | - textNodes.push(walker.currentNode as Text); | |
| 211 | - } | |
| 212 | - | |
| 213 | - // Now walk through each node match and extract search results | |
| 214 | - // One node can have several matches | |
| 215 | - const newMatches: SearchMatch[] = []; | |
| 216 | - textNodes.forEach((textNode, idx) => { | |
| 217 | - // Hacky way to detect code blocks that be wider than client and cause absolute positioning to fail | |
| 218 | - const highlightFullLine = | |
| 219 | - textNode.parentElement?.className.includes("hljs"); | |
| 220 | - | |
| 221 | - let nodeTextValue = caseSensitive | |
| 222 | - ? textNode.nodeValue | |
| 223 | - : textNode.nodeValue.toLowerCase(); | |
| 224 | - let startIndex = 0; | |
| 225 | - while ((startIndex = nodeTextValue.indexOf(query, startIndex)) !== -1) { | |
| 226 | - // Create a range to measure the size and position of the match | |
| 227 | - const range = document.createRange(); | |
| 228 | - range.setStart(textNode, startIndex); | |
| 229 | - const endIndex = startIndex + query.length; | |
| 230 | - range.setEnd(textNode, endIndex); | |
| 231 | - const rect = range.getBoundingClientRect(); | |
| 232 | - range.detach(); | |
| 233 | - startIndex = endIndex; | |
| 234 | - | |
| 235 | - const top = | |
| 236 | - rect.top + searchContainer.clientTop + searchContainer.scrollTop; | |
| 237 | - const left = | |
| 238 | - rect.left + searchContainer.clientLeft + searchContainer.scrollLeft; | |
| 239 | - | |
| 240 | - // Build a match result and push to matches | |
| 241 | - const newMatch: SearchMatch = { | |
| 242 | - index: 0, // will set later | |
| 243 | - textNode, | |
| 244 | - overlayRectangle: { | |
| 245 | - top, | |
| 246 | - left: highlightFullLine ? 2 : left, | |
| 247 | - width: highlightFullLine | |
| 248 | - ? searchContainer.clientWidth - 4 | |
| 249 | - : rect.width, // equivalent of adding 2 px x padding | |
| 250 | - height: rect.height, | |
| 251 | - }, | |
| 252 | - }; | |
| 253 | - newMatches.push(newMatch); | |
| 254 | - | |
| 255 | - if (highlightFullLine) { | |
| 256 | - break; // Since highlighting full line no need for multiple overlays, will cause darker highlight | |
| 257 | - } | |
| 258 | - } | |
| 259 | - }); | |
| 260 | - | |
| 261 | - // There will still be duplicate full lines when multiple text nodes are in the same line (e.g. Code highlights) | |
| 262 | - // Filter them out by using the overlay rectangle as a hash key | |
| 263 | - const matchHash = Object.fromEntries( | |
| 264 | - newMatches.map((match) => [ | |
| 265 | - JSON.stringify(match.overlayRectangle), | |
| 266 | - match, | |
| 267 | - ]), | |
| 268 | - ); | |
| 269 | - const filteredMatches = Object.values(matchHash).map((match, index) => ({ | |
| 270 | - ...match, | |
| 271 | - index, | |
| 272 | - })); | |
| 273 | - | |
| 160 | + setMatches(results); | |
| 274 | 161 | // Find match closest to the middle of the view |
| 275 | - const verticalMiddle = | |
| 276 | - searchRef.current.scrollTop + searchRef.current.clientHeight / 2; | |
| 277 | - let closestDist = Infinity; | |
| 278 | - let closestMatchToMiddle: SearchMatch | null = null; | |
| 279 | - filteredMatches.forEach((match) => { | |
| 280 | - const dist = Math.abs(verticalMiddle - match.overlayRectangle.top); | |
| 281 | - if (dist < closestDist) { | |
| 282 | - closestDist = dist; | |
| 283 | - closestMatchToMiddle = match; | |
| 284 | - } | |
| 285 | - }); | |
| 286 | - | |
| 287 | - // Update matches and scroll to the closest or first match | |
| 288 | - setMatches(filteredMatches); | |
| 289 | - if (query.length > 1 && filteredMatches.length) { | |
| 162 | + if (searchTerm.length > 1 && results.length) { | |
| 290 | 163 | if (scrollTo === "first") { |
| 291 | - scrollToMatch(filteredMatches[0]); | |
| 164 | + scrollToMatch(results[0]); | |
| 292 | 165 | } |
| 293 | 166 | if (scrollTo === "closest") { |
| 294 | - if (closestMatchToMiddle) { | |
| 295 | - scrollToMatch(closestMatchToMiddle); | |
| 167 | + if (closestToMiddle) { | |
| 168 | + scrollToMatch(closestToMiddle); | |
| 296 | 169 | } |
| 297 | 170 | } |
| 298 | 171 | if (scrollTo === "none") { |
| 299 | - if (closestMatchToMiddle) { | |
| 300 | - setCurrentMatch(closestMatchToMiddle); | |
| 172 | + if (closestToMiddle) { | |
| 173 | + setCurrentMatch(closestToMiddle); | |
| 301 | 174 | } else { |
| 302 | - setCurrentMatch(filteredMatches[0]); | |
| 175 | + setCurrentMatch(results[0]); | |
| 303 | 176 | } |
| 304 | 177 | } |
| 305 | 178 | } |
| 306 | 179 | }, |
| 307 | - [searchRef.current, debouncedInput, scrollToMatch, caseSensitive, useRegex], | |
| 180 | + [ | |
| 181 | + searchTerm, | |
| 182 | + caseSensitive, | |
| 183 | + useRegex, | |
| 184 | + searchRef, | |
| 185 | + headerHeight, | |
| 186 | + scrollToMatch, | |
| 187 | + ], | |
| 308 | 188 | ); |
| 309 | 189 | |
| 310 | 190 | // Triggers that should cause immediate refresh of results to closest search value: |
| 311 | - // Input change (debounced) and window click | |
| 312 | - const lastUpdateRef = useRef(0); | |
| 313 | 191 | useEffect(() => { |
| 314 | - const debounce = () => { | |
| 315 | - debouncedInput.current = input; | |
| 316 | - lastUpdateRef.current = Date.now(); | |
| 317 | - refreshSearch("closest"); | |
| 318 | - }; | |
| 319 | - const timeSinceLastUpdate = Date.now() - lastUpdateRef.current; | |
| 320 | - if (timeSinceLastUpdate >= SEARCH_DEBOUNCE) { | |
| 321 | - debounce(); | |
| 192 | + if (disabled || isResizing || !open) { | |
| 193 | + setMatches([]); | |
| 322 | 194 | } else { |
| 323 | - const handler = setTimeout(() => { | |
| 324 | - debounce(); | |
| 325 | - }, SEARCH_DEBOUNCE - timeSinceLastUpdate); | |
| 326 | - return () => { | |
| 327 | - clearTimeout(handler); | |
| 328 | - }; | |
| 195 | + refreshSearch("closest"); | |
| 329 | 196 | } |
| 330 | - }, [refreshSearch, input]); | |
| 197 | + }, [refreshSearch, open, disabled, isResizing]); | |
| 331 | 198 | |
| 332 | - // Could consider doing any window click but I only handled search div here | |
| 333 | - // Since usually only clicks in search div will cause content changes in search div | |
| 199 | + // Clicks in search div can cause content changes that for some reason don't trigger resize | |
| 200 | + // Refresh clicking within container | |
| 334 | 201 | useEffect(() => { |
| 335 | 202 | const searchContainer = searchRef.current; |
| 336 | 203 | if (!open || !searchContainer) return; |
| 337 | 204 | const handleSearchRefClick = () => { |
| 338 | - refreshSearch("none"); | |
| 205 | + setTimeout(() => { | |
| 206 | + refreshSearch("none"); | |
| 207 | + }, 150); | |
| 339 | 208 | }; |
| 340 | 209 | searchContainer.addEventListener("click", handleSearchRefClick); |
| 341 | 210 | return () => { |
| 342 | 211 | searchContainer.removeEventListener("click", handleSearchRefClick); |
| 343 | 212 | }; |
| 344 | - }, [searchRef.current, refreshSearch, open]); | |
| 345 | - | |
| 346 | - // Triggers that should cause results to temporarily disappear and then reload | |
| 347 | - // Active = LLM is generating, etc. | |
| 348 | - const active = useAppSelector((state) => state.session.isStreaming); | |
| 349 | - | |
| 350 | - useEffect(() => { | |
| 351 | - if (active || isResizing) setMatches([]); | |
| 352 | - else refreshSearch("none"); | |
| 353 | - }, [refreshSearch, active]); | |
| 354 | - | |
| 355 | - useEffect(() => { | |
| 356 | - if (!open) setMatches([]); | |
| 357 | - else refreshSearch("closest"); | |
| 358 | - }, [refreshSearch, open]); | |
| 359 | - | |
| 360 | - useEffect(() => { | |
| 361 | - refreshSearch("closest"); | |
| 362 | - }, [refreshSearch, caseSensitive, useRegex]); | |
| 213 | + }, [searchRef, refreshSearch, open]); | |
| 363 | 214 | |
| 364 | 215 | // Find widget component |
| 365 | - const widget = useMemo(() => { | |
| 366 | - return ( | |
| 367 | - <div | |
| 368 | - className={`fixed top-0 z-50 transition-all ${open ? "" : "-translate-y-full"} bg-vsc-background right-0 flex flex-row items-center gap-1.5 rounded-bl-lg border-0 border-b border-l border-solid border-zinc-700 pl-[3px] pr-3 sm:gap-2`} | |
| 369 | - > | |
| 370 | - <Input | |
| 371 | - disabled={active} | |
| 372 | - type="text" | |
| 373 | - ref={inputRef} | |
| 374 | - value={input} | |
| 375 | - onChange={(e) => { | |
| 376 | - setInput(e.target.value); | |
| 377 | - }} | |
| 378 | - placeholder="Search..." | |
| 379 | - /> | |
| 380 | - <p className="xs:block hidden min-w-12 whitespace-nowrap px-1 text-center text-xs"> | |
| 381 | - {matches.length === 0 | |
| 382 | - ? "No results" | |
| 383 | - : `${(currentMatch?.index ?? 0) + 1} of ${matches.length}`} | |
| 384 | - </p> | |
| 385 | - <div className="hidden flex-row gap-0.5 sm:flex"> | |
| 386 | - <HeaderButtonWithToolTip | |
| 387 | - tooltipPlacement="top-end" | |
| 388 | - text={"Previous Match"} | |
| 389 | - onClick={(e) => { | |
| 390 | - e.stopPropagation(); | |
| 391 | - previousMatch(); | |
| 392 | - }} | |
| 393 | - className="h-4 w-4 focus:ring" | |
| 394 | - disabled={matches.length < 2 || active} | |
| 395 | - > | |
| 396 | - <ArrowUpIcon className="h-4 w-4" /> | |
| 397 | - </HeaderButtonWithToolTip> | |
| 398 | - <HeaderButtonWithToolTip | |
| 399 | - tooltipPlacement="top-end" | |
| 400 | - text={"Next Match"} | |
| 401 | - onClick={(e) => { | |
| 402 | - e.stopPropagation(); | |
| 403 | - nextMatch(); | |
| 404 | - }} | |
| 405 | - className="h-4 w-4 focus:ring" | |
| 406 | - disabled={matches.length < 2 || active} | |
| 407 | - > | |
| 408 | - <ArrowDownIcon className="h-4 w-4" /> | |
| 409 | - </HeaderButtonWithToolTip> | |
| 410 | - </div> | |
| 216 | + const widget = ( | |
| 217 | + <div | |
| 218 | + className={`fixed top-0 z-50 transition-all ${open ? "" : "-translate-y-full"} bg-vsc-background right-0 flex flex-row items-center gap-1.5 rounded-bl-lg border-0 border-b border-l border-solid border-zinc-700 pl-[3px] pr-3 sm:gap-2`} | |
| 219 | + > | |
| 220 | + <Input | |
| 221 | + disabled={disabled} | |
| 222 | + type="text" | |
| 223 | + ref={inputRef} | |
| 224 | + value={currentValue} | |
| 225 | + onChange={(e) => { | |
| 226 | + setCurrentValue(e.target.value); | |
| 227 | + }} | |
| 228 | + placeholder="Search..." | |
| 229 | + /> | |
| 230 | + <p className="xs:block hidden min-w-12 whitespace-nowrap px-1 text-center text-xs"> | |
| 231 | + {matches.length === 0 | |
| 232 | + ? "No results" | |
| 233 | + : `${(currentMatch?.index ?? 0) + 1} of ${matches.length}`} | |
| 234 | + </p> | |
| 235 | + <div className="hidden flex-row gap-0.5 sm:flex"> | |
| 411 | 236 | <HeaderButtonWithToolTip |
| 412 | - disabled={active} | |
| 413 | - inverted={caseSensitive} | |
| 414 | 237 | tooltipPlacement="top-end" |
| 415 | - text={ | |
| 416 | - caseSensitive | |
| 417 | - ? "Turn off case sensitivity" | |
| 418 | - : "Turn on case sensitivity" | |
| 419 | - } | |
| 238 | + text={"Previous Match"} | |
| 420 | 239 | onClick={(e) => { |
| 421 | 240 | e.stopPropagation(); |
| 422 | - setCaseSensitive((curr) => !curr); | |
| 241 | + previousMatch(); | |
| 423 | 242 | }} |
| 424 | - className="h-5 w-6 rounded-full border text-xs focus:outline-none focus:ring" | |
| 243 | + className="h-4 w-4 focus:ring" | |
| 244 | + disabled={matches.length < 2 || disabled} | |
| 425 | 245 | > |
| 426 | - Aa | |
| 246 | + <ArrowUpIcon className="h-4 w-4" /> | |
| 427 | 247 | </HeaderButtonWithToolTip> |
| 428 | - {/* TODO - add useRegex functionality */} | |
| 429 | - <HeaderButton | |
| 430 | - inverted={false} | |
| 431 | - onClick={() => setOpen(false)} | |
| 432 | - className="focus:ring" | |
| 248 | + <HeaderButtonWithToolTip | |
| 249 | + tooltipPlacement="top-end" | |
| 250 | + text={"Next Match"} | |
| 251 | + onClick={(e) => { | |
| 252 | + e.stopPropagation(); | |
| 253 | + nextMatch(); | |
| 254 | + }} | |
| 255 | + className="h-4 w-4 focus:ring" | |
| 256 | + disabled={matches.length < 2 || disabled} | |
| 433 | 257 | > |
| 434 | - <XMarkIcon className="h-4 w-4" /> | |
| 435 | - </HeaderButton> | |
| 258 | + <ArrowDownIcon className="h-4 w-4" /> | |
| 259 | + </HeaderButtonWithToolTip> | |
| 436 | 260 | </div> |
| 437 | - ); | |
| 438 | - }, [ | |
| 439 | - open, | |
| 440 | - input, | |
| 441 | - inputRef, | |
| 442 | - caseSensitive, | |
| 443 | - matches, | |
| 444 | - currentMatch, | |
| 445 | - previousMatch, | |
| 446 | - nextMatch, | |
| 447 | - ]); | |
| 261 | + <HeaderButtonWithToolTip | |
| 262 | + disabled={disabled} | |
| 263 | + inverted={caseSensitive} | |
| 264 | + tooltipPlacement="top-end" | |
| 265 | + text={ | |
| 266 | + caseSensitive | |
| 267 | + ? "Turn off case sensitivity" | |
| 268 | + : "Turn on case sensitivity" | |
| 269 | + } | |
| 270 | + onClick={(e) => { | |
| 271 | + e.stopPropagation(); | |
| 272 | + setCaseSensitive((curr) => !curr); | |
| 273 | + }} | |
| 274 | + className="h-5 w-6 rounded-full border text-xs focus:outline-none focus:ring" | |
| 275 | + > | |
| 276 | + Aa | |
| 277 | + </HeaderButtonWithToolTip> | |
| 278 | + {/* TODO - add useRegex functionality */} | |
| 279 | + <HeaderButton | |
| 280 | + inverted={false} | |
| 281 | + onClick={() => setOpen(false)} | |
| 282 | + className="focus:ring" | |
| 283 | + > | |
| 284 | + <XMarkIcon className="h-4 w-4" /> | |
| 285 | + </HeaderButton> | |
| 286 | + </div> | |
| 287 | + ); | |
| 448 | 288 | |
| 449 | 289 | // Generate the highlight overlay elements |
| 450 | 290 | const highlights = useMemo(() => { |
| 451 | 291 | return matches.map((match) => ( |
| 452 | 292 | <HighlightOverlay |
| 453 | - {...match.overlayRectangle} | |
| 293 | + rectangle={match.overlayRectangle} | |
| 454 | 294 | isCurrent={currentMatch?.index === match.index} |
| 455 | 295 | /> |
| 456 | 296 | )); |
| 457 | 297 | }, [matches, currentMatch]); |
| 458 | 298 | |
| 459 | 299 | return { |
| 460 | - matches, | |
| 461 | 300 | highlights, |
| 462 | - inputRef, | |
| 463 | - input: debouncedInput, | |
| 464 | - setInput, | |
| 465 | - open, | |
| 466 | - setOpen, | |
| 467 | 301 | widget, |
| 468 | 302 | }; |
| 469 | 303 | }; |
added gui/src/components/find/findWidgetSearch.ts +146 −0
| @@ -0,0 +1,146 @@ | ||
| 1 | +export interface Rectangle { | |
| 2 | + top: number; | |
| 3 | + left: number; | |
| 4 | + width: number; | |
| 5 | + height: number; | |
| 6 | +} | |
| 7 | + | |
| 8 | +export interface SearchMatch { | |
| 9 | + index: number; | |
| 10 | + textNode: Text; | |
| 11 | + overlayRectangle: Rectangle; | |
| 12 | +} | |
| 13 | + | |
| 14 | +interface SearchOptions { | |
| 15 | + caseSensitive: boolean; | |
| 16 | + useRegex: boolean; | |
| 17 | + offsetHeight: number; | |
| 18 | +} | |
| 19 | + | |
| 20 | +export const searchWithinContainer = ( | |
| 21 | + containerRef: React.RefObject<HTMLDivElement>, | |
| 22 | + searchQuery: string, | |
| 23 | + options: SearchOptions, | |
| 24 | +): { | |
| 25 | + results: SearchMatch[]; | |
| 26 | + closestToMiddle: SearchMatch | null; | |
| 27 | +} => { | |
| 28 | + const searchContainer = containerRef.current; | |
| 29 | + | |
| 30 | + if (!searchContainer || !searchQuery) { | |
| 31 | + return { | |
| 32 | + results: [], | |
| 33 | + closestToMiddle: null, | |
| 34 | + }; | |
| 35 | + } | |
| 36 | + const query = options.caseSensitive ? searchQuery : searchQuery.toLowerCase(); | |
| 37 | + | |
| 38 | + // First grab all text nodes | |
| 39 | + // Skips any elements with the "find-widget-skip" class | |
| 40 | + const textNodes: Text[] = []; | |
| 41 | + const walker = document.createTreeWalker( | |
| 42 | + searchContainer, | |
| 43 | + NodeFilter.SHOW_ALL, | |
| 44 | + { | |
| 45 | + acceptNode: (node) => { | |
| 46 | + if (node.nodeType === Node.ELEMENT_NODE) { | |
| 47 | + if ((node as Element).classList.contains("find-widget-skip")) | |
| 48 | + return NodeFilter.FILTER_REJECT; | |
| 49 | + return NodeFilter.FILTER_ACCEPT; | |
| 50 | + } else if (node.nodeType === Node.TEXT_NODE) { | |
| 51 | + if (!node.nodeValue) return NodeFilter.FILTER_REJECT; | |
| 52 | + const nodeValue = options.caseSensitive | |
| 53 | + ? node.nodeValue | |
| 54 | + : node.nodeValue.toLowerCase(); | |
| 55 | + if (nodeValue.includes(query)) return NodeFilter.FILTER_ACCEPT; | |
| 56 | + } | |
| 57 | + return NodeFilter.FILTER_REJECT; | |
| 58 | + }, | |
| 59 | + }, | |
| 60 | + ); | |
| 61 | + | |
| 62 | + while (walker.nextNode()) { | |
| 63 | + if (walker.currentNode.nodeType === Node.ELEMENT_NODE) continue; | |
| 64 | + textNodes.push(walker.currentNode as Text); | |
| 65 | + } | |
| 66 | + | |
| 67 | + // Now walk through each node match and extract search results | |
| 68 | + // One node can have several matches | |
| 69 | + const newMatches: SearchMatch[] = []; | |
| 70 | + textNodes.forEach((textNode, idx) => { | |
| 71 | + // Hacky way to detect code blocks that be wider than client and cause absolute positioning to fail | |
| 72 | + const highlightFullLine = | |
| 73 | + textNode.parentElement?.className.includes("hljs"); | |
| 74 | + | |
| 75 | + let nodeTextValue = options.caseSensitive | |
| 76 | + ? textNode.nodeValue | |
| 77 | + : textNode.nodeValue.toLowerCase(); | |
| 78 | + let startIndex = 0; | |
| 79 | + while ((startIndex = nodeTextValue.indexOf(query, startIndex)) !== -1) { | |
| 80 | + // Create a range to measure the size and position of the match | |
| 81 | + const range = document.createRange(); | |
| 82 | + range.setStart(textNode, startIndex); | |
| 83 | + const endIndex = startIndex + query.length; | |
| 84 | + range.setEnd(textNode, endIndex); | |
| 85 | + const rect = range.getBoundingClientRect(); | |
| 86 | + range.detach(); | |
| 87 | + startIndex = endIndex; | |
| 88 | + | |
| 89 | + const top = | |
| 90 | + rect.top + | |
| 91 | + searchContainer.clientTop + | |
| 92 | + searchContainer.scrollTop - | |
| 93 | + options.offsetHeight; | |
| 94 | + | |
| 95 | + const left = | |
| 96 | + rect.left + searchContainer.clientLeft + searchContainer.scrollLeft; | |
| 97 | + | |
| 98 | + // Build a match result and push to matches | |
| 99 | + const newMatch: SearchMatch = { | |
| 100 | + index: 0, // will set later | |
| 101 | + textNode, | |
| 102 | + overlayRectangle: { | |
| 103 | + top, | |
| 104 | + left: highlightFullLine ? 2 : left, | |
| 105 | + width: highlightFullLine | |
| 106 | + ? searchContainer.clientWidth - 4 | |
| 107 | + : rect.width, // equivalent of adding 2 px x padding | |
| 108 | + height: rect.height, | |
| 109 | + }, | |
| 110 | + }; | |
| 111 | + newMatches.push(newMatch); | |
| 112 | + | |
| 113 | + if (highlightFullLine) { | |
| 114 | + break; // Since highlighting full line no need for multiple overlays, will cause darker highlight | |
| 115 | + } | |
| 116 | + } | |
| 117 | + }); | |
| 118 | + | |
| 119 | + // There will still be duplicate full lines when multiple text nodes are in the same line (e.g. Code highlights) | |
| 120 | + // Filter them out by using the overlay rectangle as a hash key | |
| 121 | + const matchHash = Object.fromEntries( | |
| 122 | + newMatches.map((match) => [JSON.stringify(match.overlayRectangle), match]), | |
| 123 | + ); | |
| 124 | + const filteredMatches = Object.values(matchHash).map((match, index) => ({ | |
| 125 | + ...match, | |
| 126 | + index, | |
| 127 | + })); | |
| 128 | + | |
| 129 | + // Find the match closest to the vertical middle of the container | |
| 130 | + const verticalMiddle = | |
| 131 | + searchContainer.scrollTop + searchContainer.clientHeight / 2; | |
| 132 | + let closestDist = Infinity; | |
| 133 | + let closestMatchToMiddle: SearchMatch | null = null; | |
| 134 | + filteredMatches.forEach((match) => { | |
| 135 | + const dist = Math.abs(verticalMiddle - match.overlayRectangle.top); | |
| 136 | + if (dist < closestDist) { | |
| 137 | + closestDist = dist; | |
| 138 | + closestMatchToMiddle = match; | |
| 139 | + } | |
| 140 | + }); | |
| 141 | + | |
| 142 | + return { | |
| 143 | + results: filteredMatches, | |
| 144 | + closestToMiddle: closestMatchToMiddle, | |
| 145 | + }; | |
| 146 | +}; |
added gui/src/components/find/useDebounce.ts +19 −0
| @@ -0,0 +1,19 @@ | ||
| 1 | +import { useEffect, useState } from "react"; | |
| 2 | + | |
| 3 | +function useDebounceValue<T>(value: T, delay: number = 500): T { | |
| 4 | + const [debouncedValue, setDebouncedValue] = useState<T>(value); | |
| 5 | + | |
| 6 | + useEffect(() => { | |
| 7 | + const timer = setTimeout(() => { | |
| 8 | + setDebouncedValue(value); | |
| 9 | + }, delay); | |
| 10 | + | |
| 11 | + return () => { | |
| 12 | + clearTimeout(timer); | |
| 13 | + }; | |
| 14 | + }, [value, delay]); | |
| 15 | + | |
| 16 | + return debouncedValue; | |
| 17 | +} | |
| 18 | + | |
| 19 | +export default useDebounceValue; |
added gui/src/components/find/useElementSize.ts +81 −0
| @@ -0,0 +1,81 @@ | ||
| 1 | +import { RefObject, useCallback, useEffect, useState } from "react"; | |
| 2 | + | |
| 3 | +interface ElementSize { | |
| 4 | + clientWidth: number; | |
| 5 | + clientHeight: number; | |
| 6 | + scrollWidth: number; | |
| 7 | + scrollHeight: number; | |
| 8 | + isResizing: boolean; | |
| 9 | +} | |
| 10 | + | |
| 11 | +interface UseElementSizeOptions { | |
| 12 | + debounceMs?: number; | |
| 13 | +} | |
| 14 | + | |
| 15 | +export const useElementSize = ( | |
| 16 | + ref: RefObject<HTMLElement>, | |
| 17 | + debounceMs = 250, | |
| 18 | +): ElementSize => { | |
| 19 | + const [size, setSize] = useState<ElementSize>({ | |
| 20 | + clientWidth: 0, | |
| 21 | + clientHeight: 0, | |
| 22 | + scrollWidth: 0, | |
| 23 | + scrollHeight: 0, | |
| 24 | + isResizing: false, | |
| 25 | + }); | |
| 26 | + | |
| 27 | + // Debounced update function | |
| 28 | + const debouncedSetSize = useCallback( | |
| 29 | + (measurements: Omit<ElementSize, "isResizing">) => { | |
| 30 | + let timeoutId: NodeJS.Timeout; | |
| 31 | + | |
| 32 | + setSize((prev) => ({ ...prev, isResizing: true })); | |
| 33 | + | |
| 34 | + timeoutId = setTimeout(() => { | |
| 35 | + setSize({ | |
| 36 | + ...measurements, | |
| 37 | + isResizing: false, | |
| 38 | + }); | |
| 39 | + }, debounceMs); | |
| 40 | + | |
| 41 | + return () => clearTimeout(timeoutId); | |
| 42 | + }, | |
| 43 | + [debounceMs], | |
| 44 | + ); | |
| 45 | + | |
| 46 | + useEffect(() => { | |
| 47 | + if (!ref.current) return; | |
| 48 | + | |
| 49 | + const updateSize = () => { | |
| 50 | + const element = ref.current; | |
| 51 | + if (!element) return; | |
| 52 | + | |
| 53 | + const measurements = { | |
| 54 | + clientWidth: element.clientWidth, | |
| 55 | + clientHeight: element.clientHeight, | |
| 56 | + scrollWidth: element.scrollWidth, | |
| 57 | + scrollHeight: element.scrollHeight, | |
| 58 | + }; | |
| 59 | + | |
| 60 | + debouncedSetSize(measurements); | |
| 61 | + }; | |
| 62 | + | |
| 63 | + // Create ResizeObserver instance | |
| 64 | + const resizeObserver = new ResizeObserver(() => { | |
| 65 | + updateSize(); | |
| 66 | + }); | |
| 67 | + | |
| 68 | + // Start observing the element | |
| 69 | + resizeObserver.observe(ref.current); | |
| 70 | + | |
| 71 | + // Initial size calculation | |
| 72 | + updateSize(); | |
| 73 | + | |
| 74 | + // Cleanup | |
| 75 | + return () => { | |
| 76 | + resizeObserver.disconnect(); | |
| 77 | + }; | |
| 78 | + }, [ref, debouncedSetSize]); | |
| 79 | + | |
| 80 | + return size; | |
| 81 | +}; |
modified gui/src/pages/gui/Chat.tsx +10 −6
| @@ -44,16 +44,17 @@ | ||
| 44 | 44 | setDialogMessage, |
| 45 | 45 | setShowDialog, |
| 46 | 46 | } from "../../redux/slices/uiSlice"; |
| 47 | +import { streamResponseThunk } from "../../redux/thunks"; | |
| 47 | 48 | import { cancelStream } from "../../redux/thunks/cancelStream"; |
| 48 | 49 | import { streamEditThunk } from "../../redux/thunks/editMode"; |
| 49 | 50 | import { loadLastSession } from "../../redux/thunks/session"; |
| 50 | -import { streamResponseThunk } from "../../redux/thunks"; | |
| 51 | 51 | import { isJetBrains, isMetaEquivalentKeyPressed } from "../../util"; |
| 52 | 52 | import { |
| 53 | 53 | FREE_TRIAL_LIMIT_REQUESTS, |
| 54 | 54 | incrementFreeTrialCount, |
| 55 | 55 | } from "../../util/freeTrial"; |
| 56 | 56 | |
| 57 | +import AcceptRejectDiffButtons from "../../components/AcceptRejectDiffButtons"; | |
| 57 | 58 | import CodeToEditCard from "../../components/mainInput/CodeToEditCard"; |
| 58 | 59 | import EditModeDetails from "../../components/mainInput/EditModeDetails"; |
| 59 | 60 | import { getLocalStorage, setLocalStorage } from "../../util/localStorage"; |
| @@ -61,7 +62,6 @@ | ||
| 61 | 62 | import { ExploreDialogWatcher } from "./ExploreDialogWatcher"; |
| 62 | 63 | import { ToolCallDiv } from "./ToolCallDiv"; |
| 63 | 64 | import { useAutoScroll } from "./useAutoScroll"; |
| 64 | -import AcceptRejectDiffButtons from "../../components/AcceptRejectDiffButtons"; | |
| 65 | 65 | |
| 66 | 66 | const StepsDiv = styled.div` |
| 67 | 67 | position: relative; |
| @@ -113,6 +113,7 @@ | ||
| 113 | 113 | const [stepsOpen] = useState<(boolean | undefined)[]>([]); |
| 114 | 114 | const mainTextInputRef = useRef<HTMLInputElement>(null); |
| 115 | 115 | const stepsDivRef = useRef<HTMLDivElement>(null); |
| 116 | + const tabsRef = useRef<HTMLDivElement>(null); | |
| 116 | 117 | const history = useAppSelector((state) => state.session.history); |
| 117 | 118 | const showChatScrollbar = useAppSelector( |
| 118 | 119 | (state) => state.config.config.ui?.showChatScrollbar, |
| @@ -152,7 +153,11 @@ | ||
| 152 | 153 | }; |
| 153 | 154 | }, [isStreaming, jetbrains]); |
| 154 | 155 | |
| 155 | - const { widget, highlights } = useFindWidget(stepsDivRef); | |
| 156 | + const { widget, highlights } = useFindWidget( | |
| 157 | + stepsDivRef, | |
| 158 | + tabsRef, | |
| 159 | + isStreaming, | |
| 160 | + ); | |
| 156 | 161 | |
| 157 | 162 | const currentToolCallApplyState = useAppSelector( |
| 158 | 163 | selectCurrentToolCallApplyState, |
| @@ -286,13 +291,12 @@ | ||
| 286 | 291 | |
| 287 | 292 | return ( |
| 288 | 293 | <> |
| 294 | + {!!showSessionTabs && mode !== "edit" && <TabBar ref={tabsRef} />} | |
| 289 | 295 | {widget} |
| 290 | 296 | |
| 291 | - {!!showSessionTabs && mode !== "edit" && <TabBar />} | |
| 292 | - | |
| 293 | 297 | <StepsDiv |
| 294 | 298 | ref={stepsDivRef} |
| 295 | - className={`mt-3 overflow-y-scroll pt-[8px] ${showScrollbar ? "thin-scrollbar" : "no-scrollbar"} ${history.length > 0 ? "flex-1" : ""}`} | |
| 299 | + className={`overflow-y-scroll pt-[8px] ${showScrollbar ? "thin-scrollbar" : "no-scrollbar"} ${history.length > 0 ? "flex-1" : ""}`} | |
| 296 | 300 | > |
| 297 | 301 | {highlights} |
| 298 | 302 | {history.map((item, index: number) => ( |