1
0
mirror of https://github.com/checktheroads/hyperglass synced 2024-05-11 05:55:08 +00:00

316 lines
9.1 KiB
JavaScript
Raw Normal View History

2020-04-19 09:50:31 -07:00
import * as React from "react";
import { useEffect, useState } from "react";
2020-01-17 02:50:57 -07:00
import {
2020-04-18 12:09:16 -07:00
AccordionItem,
AccordionHeader,
AccordionPanel,
Alert,
Box,
ButtonGroup,
css,
Flex,
Tooltip,
Text,
useColorMode
2020-01-17 02:50:57 -07:00
} from "@chakra-ui/core";
import styled from "@emotion/styled";
2020-04-16 23:43:02 -07:00
import LightningBolt from "~/components/icons/LightningBolt";
2020-01-17 02:50:57 -07:00
import useAxios from "axios-hooks";
import strReplace from "react-string-replace";
2020-04-18 07:58:46 -07:00
import format from "string-format";
2020-04-16 23:43:02 -07:00
import { startCase } from "lodash";
2020-01-20 00:37:04 -07:00
import useConfig from "~/components/HyperglassProvider";
2020-04-16 23:43:02 -07:00
import useMedia from "~/components/MediaProvider";
2020-01-17 02:50:57 -07:00
import CopyButton from "~/components/CopyButton";
import RequeryButton from "~/components/RequeryButton";
import ResultHeader from "~/components/ResultHeader";
2020-04-16 23:43:02 -07:00
import CacheTimeout from "~/components/CacheTimeout";
2020-01-17 02:50:57 -07:00
2020-04-18 07:58:46 -07:00
format.extend(String.prototype, {});
2020-01-17 02:50:57 -07:00
const FormattedError = ({ keywords, message }) => {
2020-04-18 12:09:16 -07:00
const patternStr = keywords.map(kw => `(${kw})`).join("|");
const pattern = new RegExp(patternStr, "gi");
let errorFmt;
try {
errorFmt = strReplace(message, pattern, match => (
<Text key={match} as="strong">
{match}
</Text>
));
} catch (err) {
errorFmt = <Text as="span">{message}</Text>;
}
return <Text as="span">{keywords.length !== 0 ? errorFmt : message}</Text>;
2020-01-17 02:50:57 -07:00
};
2020-01-20 00:37:04 -07:00
const AccordionHeaderWrapper = styled(Flex)`
2020-04-18 12:09:16 -07:00
justify-content: space-between;
&:hover {
background-color: ${props => props.hoverBg};
}
&:focus {
box-shadow: "outline";
}
2020-01-20 00:37:04 -07:00
`;
2020-04-18 12:09:16 -07:00
const statusMap = {
success: "success",
warning: "warning",
error: "warning",
danger: "error"
};
2020-04-16 23:43:02 -07:00
const bg = { dark: "gray.800", light: "blackAlpha.100" };
const color = { dark: "white", light: "black" };
const selectionBg = { dark: "white", light: "black" };
const selectionColor = { dark: "black", light: "white" };
2020-01-26 02:24:12 -07:00
2020-01-20 00:37:04 -07:00
const Result = React.forwardRef(
2020-04-18 12:09:16 -07:00
(
{
device,
timeout,
queryLocation,
queryType,
queryVrf,
queryTarget,
index,
resultsComplete,
setComplete
},
ref
) => {
const config = useConfig();
const { isSm } = useMedia();
const { colorMode } = useColorMode();
2020-04-19 09:50:31 -07:00
let [{ data, loading, error }, refetch] = useAxios({
2020-04-18 12:09:16 -07:00
url: "/api/query/",
method: "post",
data: {
query_location: queryLocation,
query_type: queryType,
query_vrf: queryVrf,
query_target: queryTarget
},
timeout: timeout,
useCache: false
});
2020-03-22 11:43:14 -07:00
2020-04-18 12:09:16 -07:00
const [isOpen, setOpen] = useState(false);
const [hasOverride, setOverride] = useState(false);
2020-03-22 11:43:14 -07:00
2020-04-18 12:09:16 -07:00
const handleToggle = () => {
setOpen(!isOpen);
setOverride(true);
};
2020-04-19 09:50:31 -07:00
let cleanOutput =
2020-04-18 12:09:16 -07:00
data &&
data.output
.split("\\n")
.join("\n")
.replace(/\n\n/g, "\n");
2020-01-17 02:50:57 -07:00
2020-04-18 12:09:16 -07:00
const errorKw = (error && error.response?.data?.keywords) || [];
2020-01-26 02:24:12 -07:00
2020-04-18 12:09:16 -07:00
let errorMsg;
if (error && error.response?.data?.output) {
errorMsg = error.response.data.output;
} else if (error && error.message.startsWith("timeout")) {
errorMsg = config.messages.request_timeout;
} else if (error?.response?.statusText) {
errorMsg = startCase(error.response.statusText);
} else if (error && error.message) {
errorMsg = startCase(error.message);
} else {
errorMsg = config.messages.general;
}
2020-01-26 02:24:12 -07:00
2020-04-19 09:50:31 -07:00
error && console.error(error);
2020-01-27 21:28:27 -07:00
2020-04-18 12:09:16 -07:00
const errorLevel =
(error?.response?.data?.level &&
statusMap[error.response?.data?.level]) ??
"error";
2020-01-26 02:24:12 -07:00
2020-04-18 12:09:16 -07:00
useEffect(() => {
!loading && resultsComplete === null && setComplete(index);
}, [loading, resultsComplete]);
2020-03-22 11:43:14 -07:00
2020-04-18 12:09:16 -07:00
useEffect(() => {
resultsComplete === index && !hasOverride && setOpen(true);
}, [resultsComplete, index]);
2020-04-19 09:50:31 -07:00
2020-04-18 12:09:16 -07:00
return (
<AccordionItem
isOpen={isOpen}
isDisabled={loading}
ref={ref}
css={css({
"&:last-of-type": { borderBottom: "none" },
"&:first-of-type": { borderTop: "none" }
})}
>
<AccordionHeaderWrapper hoverBg="blackAlpha.50">
<AccordionHeader
flex="1 0 auto"
py={2}
_hover={{}}
_focus={{}}
w="unset"
onClick={handleToggle}
>
<ResultHeader
title={device.display_name}
loading={loading}
error={error}
errorMsg={errorMsg}
errorLevel={errorLevel}
runtime={data?.runtime}
/>
</AccordionHeader>
<ButtonGroup px={3} py={2}>
<CopyButton
copyValue={cleanOutput}
variant="ghost"
isDisabled={loading}
/>
<RequeryButton
requery={refetch}
variant="ghost"
isDisabled={loading}
/>
</ButtonGroup>
</AccordionHeaderWrapper>
<AccordionPanel
pb={4}
overflowX="auto"
css={css({ WebkitOverflowScrolling: "touch" })}
>
<Flex direction="row" flexWrap="wrap">
<Flex
direction="column"
flex="1 0 auto"
maxW={error ? "100%" : null}
2020-01-17 02:50:57 -07:00
>
2020-04-18 12:09:16 -07:00
{data && !error && (
<Box
fontFamily="mono"
mt={5}
mx={2}
p={3}
border="1px"
borderColor="inherit"
rounded="md"
bg={bg[colorMode]}
color={color[colorMode]}
fontSize="sm"
whiteSpace="pre-wrap"
as="pre"
css={css({
"&::selection": {
backgroundColor: selectionBg[colorMode],
color: selectionColor[colorMode]
}
})}
2020-01-20 00:37:04 -07:00
>
2020-04-18 12:09:16 -07:00
{cleanOutput}
</Box>
)}
{error && (
<Alert rounded="lg" my={2} py={4} status={errorLevel}>
<FormattedError keywords={errorKw} message={errorMsg} />
</Alert>
)}
</Flex>
</Flex>
2020-03-23 01:10:27 -07:00
2020-04-18 12:09:16 -07:00
<Flex direction="row" flexWrap="wrap">
<Flex
px={3}
mt={2}
justifyContent={[
"flex-start",
"flex-start",
"flex-end",
"flex-end"
]}
flex="1 0 auto"
>
2020-04-19 09:50:31 -07:00
{config.cache.show_text && data && !error && (
<>
{!isSm && (
<CacheTimeout
timeout={config.cache.timeout}
text={config.web.text.cache_prefix}
/>
)}
<Tooltip
display={data?.cached ? null : "none"}
hasArrow
label={config.web.text.cache_icon.format({
time: data?.timestamp
})}
placement="top"
>
<Box ml={1} display={data?.cached ? "block" : "none"}>
<LightningBolt color={color[colorMode]} />
</Box>
</Tooltip>
{isSm && (
<CacheTimeout
timeout={config.cache.timeout}
text={config.web.text.cache_prefix}
/>
)}
</>
)}
{/* {config.cache.show_text && data && !error && isSm ? (
2020-04-18 12:09:16 -07:00
<>
<Tooltip
display={data?.cached ? null : "none"}
hasArrow
label={config.web.text.cache_icon.format({
time: data?.timestamp
})}
placement="top"
>
2020-04-19 09:50:31 -07:00
<Box mr={1} display={data?.cached ? "block" : "none"}>
2020-04-18 12:09:16 -07:00
<LightningBolt color={color[colorMode]} />
</Box>
</Tooltip>
<CacheTimeout
timeout={config.cache.timeout}
text={config.web.text.cache_prefix}
/>
</>
2020-04-19 09:50:31 -07:00
) : config.cache.show_text && data && !error ? (
2020-04-18 12:09:16 -07:00
<>
<CacheTimeout
timeout={config.cache.timeout}
text={config.web.text.cache_prefix}
/>
<Tooltip
display={data?.cached ? null : "none"}
hasArrow
label={config.web.text.cache_icon.format({
time: data?.timestamp
})}
placement="top"
>
2020-04-19 09:50:31 -07:00
<Box ml={1} display={data?.cached ? "block" : "none"}>
2020-04-18 12:09:16 -07:00
<LightningBolt color={color[colorMode]} />
</Box>
</Tooltip>
</>
2020-04-19 09:50:31 -07:00
) : null} */}
2020-04-18 12:09:16 -07:00
</Flex>
</Flex>
</AccordionPanel>
</AccordionItem>
);
}
2020-01-17 02:50:57 -07:00
);
2020-01-20 00:37:04 -07:00
Result.displayName = "HyperglassQueryResult";
export default Result;