import React, { useCallback, useState } from "react" import { CSSTransition } from "react-transition-group" import styled from "styled-components" import { collapseTransition, Text, TransitionDuration } from "components" import { color } from "utils" import { Timings } from "utils/questdb" type Props = Timings & Readonly<{ count: number rowCount: number }> const Wrapper = styled.div` display: flex; flex-direction: column; margin-top: 0.2rem; overflow: hidden; ${collapseTransition}; svg { margin-right: 0.2rem; color: ${color("draculaForeground")}; } ` const Details = styled.div` display: flex; margin-top: 0.5rem; padding: 1rem; flex-direction: column; user-select: none; background: ${color("draculaBackground")}; ` const DetailsLink = styled(Text)` &:hover { cursor: pointer; } ` const roundTiming = (time: number): number => Math.round((time + Number.EPSILON) * 100) / 100 const addColor = (timing: string) => { if (timing === "0") { return 0 } return {timing} } const formatTiming = (nanos: number) => { if (nanos === 0) { return "0" } if (nanos > 1e9) { return `${roundTiming(nanos / 1e9)}s` } if (nanos > 1e6) { return `${roundTiming(nanos / 1e6)}ms` } if (nanos > 1e3) { return `${roundTiming(nanos / 1e3)}μs` } return `${nanos}ns` } const QueryResult = ({ compiler, count, execute, fetch, rowCount }: Props) => { const [expanded, setExpanded] = useState(false) const handleClick = useCallback(() => { setExpanded(!expanded) }, [expanded]) return (
{rowCount} row{rowCount > 1 ? "s" : ""} in  {formatTiming(fetch)}  ( {expanded ? "hide" : "show"} details )
Fetch: {addColor(formatTiming(fetch))} Execute: {addColor(formatTiming(execute))} Count: {addColor(formatTiming(count))} Compile: {addColor(formatTiming(compiler))}
) } export default QueryResult