diff options
| author | 2024-08-30 15:47:20 +0200 | |
|---|---|---|
| committer | 2024-08-30 15:47:20 +0200 | |
| commit | fe61544bafafc8b4de78cc71cb641af2dfb7b72d (patch) | |
| tree | 7edff48c1638fcc26915dc1f40982ec2563cf3bf /src/components/Frames/Frames.tsx | |
| parent | f183536067dc694f37445148c15821f1621f5034 (diff) | |
| parent | 2f2048160fac06e94703bb03eea43185fc01f76c (diff) | |
| download | client-2.1.0.tar.gz client-2.1.0.tar.bz2 client-2.1.0.zip | |
Merge pull request #15 from tomvanderlee/feature/websocketsv2.1.0
Feature/websockets
Diffstat (limited to 'src/components/Frames/Frames.tsx')
| -rw-r--r-- | src/components/Frames/Frames.tsx | 75 |
1 files changed, 75 insertions, 0 deletions
diff --git a/src/components/Frames/Frames.tsx b/src/components/Frames/Frames.tsx new file mode 100644 index 0000000..83c4c96 --- /dev/null +++ b/src/components/Frames/Frames.tsx | |||
| @@ -0,0 +1,75 @@ | |||
| 1 | import { Frame } from "~/types"; | ||
| 2 | import React, { PropsWithChildren, useContext } from "react"; | ||
| 3 | import { Col, ListGroup, Row } from "react-bootstrap"; | ||
| 4 | import classNames from "classnames"; | ||
| 5 | import styles from "./Frames.module.scss"; | ||
| 6 | import dayjs from "dayjs"; | ||
| 7 | import ReactJson from "react-json-view"; | ||
| 8 | import { DarkModeContext } from "~/contexts/DarkMode"; | ||
| 9 | |||
| 10 | function isJson(data: any): boolean { | ||
| 11 | try { | ||
| 12 | JSON.parse(data); | ||
| 13 | return true; | ||
| 14 | } catch { | ||
| 15 | return false; | ||
| 16 | } | ||
| 17 | } | ||
| 18 | |||
| 19 | interface FramesProps { | ||
| 20 | frames: Frame[]; | ||
| 21 | } | ||
| 22 | |||
| 23 | export default function Frames({ | ||
| 24 | frames, | ||
| 25 | }: PropsWithChildren<FramesProps>): JSX.Element { | ||
| 26 | const { darkMode } = useContext(DarkModeContext); | ||
| 27 | return ( | ||
| 28 | <ListGroup variant="flush" as="ul" className={"flex-grow-1"}> | ||
| 29 | {frames.length > 0 ? ( | ||
| 30 | frames.map((frame) => { | ||
| 31 | // Inbound relative to the client | ||
| 32 | const inbound = frame.type !== "websocket_inbound"; | ||
| 33 | |||
| 34 | const body = | ||
| 35 | frame.type !== "websocket_disconnect" | ||
| 36 | ? atob(frame.payload.body) | ||
| 37 | : null; | ||
| 38 | |||
| 39 | return ( | ||
| 40 | <ListGroup.Item | ||
| 41 | as="li" | ||
| 42 | key={frame.payload.id} | ||
| 43 | className={classNames({ | ||
| 44 | "border-bottom": true, | ||
| 45 | })} | ||
| 46 | > | ||
| 47 | <Row> | ||
| 48 | <Col className="flex-grow-0 d-flex align-items-center text-nowrap text-muted"> | ||
| 49 | {dayjs(frame.payload?.timestamp).format("HH:mm:ss.SSS")} | ||
| 50 | </Col> | ||
| 51 | <Col | ||
| 52 | className={classNames( | ||
| 53 | styles.arrow, | ||
| 54 | "flex-grow-0 d-flex align-items-center flex-nowrap", | ||
| 55 | { | ||
| 56 | [styles.inbound]: inbound, | ||
| 57 | [styles.outbound]: !inbound, | ||
| 58 | } | ||
| 59 | )} | ||
| 60 | > | ||
| 61 | {inbound ? "▼" : "▲"} | ||
| 62 | </Col> | ||
| 63 | <Col className="flex-grow-1">{body}</Col> | ||
| 64 | </Row> | ||
| 65 | </ListGroup.Item> | ||
| 66 | ); | ||
| 67 | }) | ||
| 68 | ) : ( | ||
| 69 | <div className={styles.noRequest}> | ||
| 70 | <p>No messages</p> | ||
| 71 | </div> | ||
| 72 | )} | ||
| 73 | </ListGroup> | ||
| 74 | ); | ||
| 75 | } | ||
