import React, { useCallback, useEffect, useState } from "react"; import { useCombobox } from "downshift"; import styled from "styled-components"; import { buttonColor, defaultBorderRadius, lightGray, secondaryDark, vscBackground, } from "."; import CodeBlock from "./CodeBlock"; import { RangeInFile } from "../../../src/client"; import PillButton from "./PillButton"; import HeaderButtonWithText from "./HeaderButtonWithText"; import { Trash, LockClosed, LockOpen, Plus, DocumentPlus, } from "@styled-icons/heroicons-outline"; import { HighlightedRangeContext } from "../../../schema/FullState"; // #region styled components const mainInputFontSize = 13; const EmptyPillDiv = styled.div` padding: 8px; border-radius: ${defaultBorderRadius}; border: 1px dashed ${lightGray}; color: ${lightGray}; background-color: ${vscBackground}; overflow: hidden; display: flex; align-items: center; text-align: center; cursor: pointer; font-size: 13px; &:hover { background-color: ${lightGray}; color: ${vscBackground}; } `; const ContextDropdown = styled.div` position: absolute; padding: 4px; width: calc(100% - 16px - 8px); background-color: ${secondaryDark}; color: white; border-bottom-right-radius: ${defaultBorderRadius}; border-bottom-left-radius: ${defaultBorderRadius}; /* border: 1px solid white; */ border-top: none; margin: 8px; outline: 1px solid orange; z-index: 5; `; const MainTextInput = styled.textarea` resize: none; padding: 8px; font-size: ${mainInputFontSize}px; font-family: inherit; border-radius: ${defaultBorderRadius}; margin: 8px auto; height: auto; width: 100%; background-color: ${secondaryDark}; color: white; z-index: 1; &:focus { outline: 1px solid ${lightGray}; } `; const UlMaxHeight = 300; const Ul = styled.ul<{ hidden: boolean; showAbove: boolean; ulHeightPixels: number; }>` ${(props) => props.showAbove ? `transform: translateY(-${props.ulHeightPixels + 8}px);` : `transform: translateY(${2 * mainInputFontSize}px);`} position: absolute; background: ${vscBackground}; background-color: ${secondaryDark}; color: white; max-height: ${UlMaxHeight}px; overflow-y: scroll; overflow-x: hidden; padding: 0; ${({ hidden }) => hidden && "display: none;"} border-radius: ${defaultBorderRadius}; border: 0.5px solid gray; z-index: 2; // Get rid of scrollbar and its padding scrollbar-width: none; -ms-overflow-style: none; &::-webkit-scrollbar { width: 0px; background: transparent; /* make scrollbar transparent */ } `; const Li = styled.li<{ highlighted: boolean; selected: boolean; isLastItem: boolean; }>` ${({ highlighted }) => highlighted && "background: #ff000066;"} ${({ selected }) => selected && "font-weight: bold;"} padding: 0.5rem 0.75rem; display: flex; flex-direction: column; ${({ isLastItem }) => isLastItem && "border-bottom: 1px solid gray;"} border-top: 1px solid gray; cursor: pointer; `; // #endregion interface ComboBoxProps { items: { name: string; description: string }[]; onInputValueChange: (inputValue: string) => void; disabled?: boolean; onEnter: (e: React.KeyboardEvent) => void; highlightedCodeSections: HighlightedRangeContext[]; deleteContextItems: (indices: number[]) => void; onTogglePin: () => void; onToggleAddContext: () => void; addingHighlightedCode: boolean; } const ComboBox = React.forwardRef((props: ComboBoxProps, ref) => { const [history, setHistory] = React.useState([]); // The position of the current command you are typing now, so the one that will be appended to history once you press enter const [positionInHistory, setPositionInHistory] = React.useState(0); const [items, setItems] = React.useState(props.items); const [hoveringButton, setHoveringButton] = React.useState(false); const [hoveringContextDropdown, setHoveringContextDropdown] = React.useState(false); const [pinned, setPinned] = useState(false); const [highlightedCodeSections, setHighlightedCodeSections] = React.useState( props.highlightedCodeSections || [] ); useEffect(() => { setHighlightedCodeSections(props.highlightedCodeSections || []); }, [props.highlightedCodeSections]); const { isOpen, getToggleButtonProps, getLabelProps, getMenuProps, getInputProps, highlightedIndex, getItemProps, selectedItem, setInputValue, } = useCombobox({ onInputValueChange({ inputValue }) { if (!inputValue) return; props.onInputValueChange(inputValue); setItems( props.items.filter((item) => item.name.toLowerCase().startsWith(inputValue.toLowerCase()) ) ); }, items, itemToString(item) { return item ? item.name : ""; }, }); const divRef = React.useRef(null); const ulRef = React.useRef(null); const showAbove = () => { return (divRef.current?.getBoundingClientRect().top || 0) > UlMaxHeight; }; return ( <>
{/* {highlightedCodeSections.length > 1 && ( <> { props.deleteContextItems( highlightedCodeSections.map((_, idx) => idx) ); }} > )} */} {highlightedCodeSections.map((section, idx) => ( { if (props.deleteContextItems) { props.deleteContextItems([idx]); } setHighlightedCodeSections((prev) => { const newSections = [...prev]; newSections.splice(idx, 1); return newSections; }); }} onHover={(val: boolean) => { if (val) { setHoveringButton(val); } else { setTimeout(() => { setHoveringButton(val); }, 100); } }} /> ))} {props.highlightedCodeSections.length > 0 && (props.addingHighlightedCode ? ( { props.onToggleAddContext(); }} > Highlight to Add Context ) : ( { props.onToggleAddContext(); }} > ))}
{/* Highlight code to include as context. Currently open file included by default. {highlightedCodeSections.length === 0 && ""} */} { setHoveringContextDropdown(true); }} onMouseLeave={() => { setHoveringContextDropdown(false); }} hidden={true || (!hoveringContextDropdown && !hoveringButton)} > {highlightedCodeSections.map((section, idx) => ( <>

{section.range.filepath}

{section.range.contents} ))}
); }); export default ComboBox;