|
| 1 | +import { |
| 2 | + Button, |
| 3 | + Heading, |
| 4 | + HStack, |
| 5 | + Link, |
| 6 | + Modal, |
| 7 | + ModalBody, |
| 8 | + ModalCloseButton, |
| 9 | + ModalContent, |
| 10 | + ModalFooter, |
| 11 | + ModalHeader, |
| 12 | + ModalOverlay, |
| 13 | + Stack, |
| 14 | + Table, |
| 15 | + TableContainer, |
| 16 | + Tbody, |
| 17 | + Td, |
| 18 | + Text, |
| 19 | + Th, |
| 20 | + Thead, |
| 21 | + Tr, |
| 22 | + useDisclosure, |
| 23 | +} from "@chakra-ui/react"; |
| 24 | +import { cloneElement } from "react"; |
| 25 | +import { ExternalLinkIcon } from "@chakra-ui/icons"; |
| 26 | +import dayjs from "dayjs"; |
| 27 | +import { UserFeedRequest } from "../../../types"; |
| 28 | + |
| 29 | +interface Props { |
| 30 | + request: UserFeedRequest; |
| 31 | + trigger: React.ReactElement; |
| 32 | +} |
| 33 | + |
| 34 | +export const RequestDetails = ({ request, trigger }: Props) => { |
| 35 | + const { isOpen, onOpen, onClose } = useDisclosure(); |
| 36 | + |
| 37 | + const requestHeaders = request.headers |
| 38 | + ? (Object.entries(request.headers) as [string, string][]) |
| 39 | + : null; |
| 40 | + |
| 41 | + const responseHeaders = request.response.headers |
| 42 | + ? (Object.entries(request.response.headers) as [string, string][]) |
| 43 | + : null; |
| 44 | + |
| 45 | + const statusCode = request.response.statusCode || null; |
| 46 | + const durationMs = request.finishedAtIso |
| 47 | + ? `${new Date(request.finishedAtIso).getTime() - new Date(request.createdAtIso).getTime()}ms` |
| 48 | + : null; |
| 49 | + |
| 50 | + return ( |
| 51 | + <> |
| 52 | + {cloneElement(trigger, { onClick: onOpen })} |
| 53 | + <Modal isOpen={isOpen} onClose={onClose} size="6xl"> |
| 54 | + <ModalOverlay /> |
| 55 | + <ModalContent> |
| 56 | + <ModalHeader>Request Details</ModalHeader> |
| 57 | + <ModalCloseButton /> |
| 58 | + <ModalBody> |
| 59 | + <Stack spacing={8}> |
| 60 | + <Stack spacing={4}> |
| 61 | + <Heading size="md" as="h2"> |
| 62 | + Request |
| 63 | + </Heading> |
| 64 | + <HStack flexWrap="wrap" gap={16}> |
| 65 | + <Stack> |
| 66 | + <Heading size="sm" as="h3"> |
| 67 | + URL |
| 68 | + </Heading> |
| 69 | + <Link |
| 70 | + href={request.url} |
| 71 | + target="_blank" |
| 72 | + rel="noopener noreferrer" |
| 73 | + color="blue.300" |
| 74 | + > |
| 75 | + <HStack alignItems="center"> |
| 76 | + <Text wordBreak="break-all">{request.url}</Text> |
| 77 | + <ExternalLinkIcon /> |
| 78 | + </HStack> |
| 79 | + </Link> |
| 80 | + </Stack> |
| 81 | + <Stack> |
| 82 | + <Heading size="sm" as="h3"> |
| 83 | + Initiated At |
| 84 | + </Heading> |
| 85 | + <Text>{dayjs(request.createdAtIso).format("DD MMM YYYY, HH:mm:ss.SSS")}</Text> |
| 86 | + </Stack> |
| 87 | + </HStack> |
| 88 | + <Heading size="sm" as="h3"> |
| 89 | + Headers |
| 90 | + </Heading> |
| 91 | + {!requestHeaders && <Text color="gray.400">No request headers available.</Text>} |
| 92 | + {!!requestHeaders && ( |
| 93 | + <TableContainer bg="gray.800" p={4} borderRadius="md"> |
| 94 | + <Table size="sm" variant="simple"> |
| 95 | + <Thead> |
| 96 | + <Tr> |
| 97 | + <Th>Name</Th> |
| 98 | + <Th>Value</Th> |
| 99 | + </Tr> |
| 100 | + </Thead> |
| 101 | + <Tbody> |
| 102 | + {requestHeaders.map(([key, val]) => { |
| 103 | + return ( |
| 104 | + <Tr key={key}> |
| 105 | + <Td>{key}</Td> |
| 106 | + <Td overflow="auto">{val}</Td> |
| 107 | + </Tr> |
| 108 | + ); |
| 109 | + })} |
| 110 | + </Tbody> |
| 111 | + </Table> |
| 112 | + </TableContainer> |
| 113 | + )} |
| 114 | + </Stack> |
| 115 | + <Stack spacing={4}> |
| 116 | + <Heading size="md" as="h2"> |
| 117 | + Response |
| 118 | + </Heading> |
| 119 | + <HStack flexWrap="wrap" gap={16}> |
| 120 | + <Stack> |
| 121 | + <Heading size="sm" as="h3"> |
| 122 | + Status Code |
| 123 | + </Heading> |
| 124 | + <Text>{statusCode || "N/A"}</Text> |
| 125 | + </Stack> |
| 126 | + <Stack> |
| 127 | + <Heading size="sm" as="h3"> |
| 128 | + Received At |
| 129 | + </Heading> |
| 130 | + <Text> |
| 131 | + {request.finishedAtIso |
| 132 | + ? dayjs(request.finishedAtIso).format("DD MMM YYYY, HH:mm:ss.SSS") |
| 133 | + : "N/A"} |
| 134 | + </Text> |
| 135 | + </Stack> |
| 136 | + <Stack> |
| 137 | + <Heading size="sm" as="h3"> |
| 138 | + Duration |
| 139 | + </Heading> |
| 140 | + <Text>{durationMs || "N/A"}</Text> |
| 141 | + </Stack> |
| 142 | + </HStack> |
| 143 | + <Heading size="sm" as="h3"> |
| 144 | + Headers |
| 145 | + </Heading> |
| 146 | + {!responseHeaders && <Text color="gray.400">No response headers available.</Text>} |
| 147 | + {!!responseHeaders && ( |
| 148 | + <TableContainer bg="gray.800" p={4} borderRadius="md"> |
| 149 | + <Table size="sm" variant="simple"> |
| 150 | + <Thead> |
| 151 | + <Tr> |
| 152 | + <Th>Name</Th> |
| 153 | + <Th>Value</Th> |
| 154 | + </Tr> |
| 155 | + </Thead> |
| 156 | + <Tbody> |
| 157 | + {responseHeaders.map(([key, val]) => { |
| 158 | + return ( |
| 159 | + <Tr key={key}> |
| 160 | + <Td>{key}</Td> |
| 161 | + <Td>{val}</Td> |
| 162 | + </Tr> |
| 163 | + ); |
| 164 | + })} |
| 165 | + </Tbody> |
| 166 | + </Table> |
| 167 | + </TableContainer> |
| 168 | + )} |
| 169 | + </Stack> |
| 170 | + </Stack> |
| 171 | + </ModalBody> |
| 172 | + <ModalFooter> |
| 173 | + <Button colorScheme="blue" mr={3} onClick={onClose}> |
| 174 | + Close |
| 175 | + </Button> |
| 176 | + </ModalFooter> |
| 177 | + </ModalContent> |
| 178 | + </Modal> |
| 179 | + </> |
| 180 | + ); |
| 181 | +}; |
0 commit comments