import React, { useEffect, useRef } from "react"; import { animated } from "@react-spring/web"; import styled from "styled-components"; import { useHighlightHover } from "../Actions/utils"; import { getValueString } from "../Filters/utils"; import { NameAndCountContainer } from "../utils"; export const ResultsContainer = styled(animated.div)` background-color: ${({ theme }) => theme.backgroundDark}; border: 1px solid ${({ theme }) => theme.backgroundDarkBorder}; border-radius: 2px; box-shadow: 0 2px 20px ${({ theme }) => theme.backgroundDark}; box-sizing: border-box; position: absolute; width: auto; z-index: 801; padding: 0 0.5rem; width: 100%; `; const ResultContainer = animated(NameAndCountContainer); const ScrollResultsContainer = styled.div` margin-left: -0.5rem; margin-right: -0.5rem; max-height: 330px; overflow-y: scroll; scrollbar-width: none; &::-webkit-scrollbar { width: 0px; background: transparent; display: none; } &::-webkit-scrollbar-thumb { width: 0px; display: none; } `; type ResultValue = [T, number]; interface ResultProps { result: T; count: number; highlight: string; active: boolean | null; onClick: () => void; color: string; } const Result = ({ active, highlight, onClick, color, result, count, }: ResultProps) => { const props = useHighlightHover( false, active ? active : null, result === null ? highlight : null ); const ref = useRef(); const wasActive = useRef(false); const [text, coloring] = getValueString(result); useEffect(() => { if (active && ref.current && !wasActive.current) { wasActive.current = true; } else if (!active) { wasActive.current = false; } active && ref.current.scrollIntoView(); }, [active]); return ( {text} {typeof count === "number" && {count.toLocaleString()}} ); }; interface ResultsProps { results: ResultValue[]; highlight: string; onSelect: (value: T) => void; active: string | null; alignRight?: boolean; color: string; } const Results = ({ color, onSelect, results, highlight, active = undefined, }: ResultsProps) => { return ( {results.map((result) => ( key={String(result[0])} result={result[0]} count={result[1]} highlight={highlight} onClick={() => onSelect(result[0])} active={active === result[0]} color={color} /> ))} ); }; export default Results;