summaryrefslogtreecommitdiffstats
path: root/src/components/Details/Details.tsx
blob: d4bb07a0598f7ff8972c03056bfce795a9725686 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
import * as React from "react";
import {useCallback, useMemo, useState} from "react";
import {RequestResponse} from "~hooks/useRequests";
import styles from "./Details.module.scss";
import RequestSummary from "../RequestSummary/RequestSummary";
import Content from "../Content/Content";
import {getHost} from "../../utils";
import {Button, Card, Col, Container, Nav, Row, Table} from "react-bootstrap";


interface TimingProps {
  timing: number;
}


function Timing({ timing }: TimingProps) {
  const value = useMemo(() => (Math.round(timing * 1000) / 1000), [timing]);
  const showSeconds = useMemo(() => value > 1, [value]);

  return !Number.isNaN(value)
    ? (
      <>
        {`${showSeconds ? value : value * 1000}${ showSeconds ? 's' : 'ms' }`}
      </>
    )
    : null;
}

interface HeadersProps {
  title: string
  headers: {
    [key: string]: string
  }
}

function Headers({ title, headers }: HeadersProps) {
  return (
    <Card className="m-3">
        <Table
          striped
          responsive
          borderless
          hover
          className='mb-0'
        >
          <thead>
            <tr>
              <th colSpan={2} className="bg-dark text-white rounded-top">{ title }</th>
            </tr>
          </thead>
          <tbody>
          {
            Object.entries(headers).map(([key, value]) => (
              <tr>
                <td>{key}</td>
                <td>{value}</td>
              </tr>
            ))
          }
          </tbody>
        </Table>
    </Card>
  )
}

interface DetailsProps {
  requestResponse: RequestResponse
}

type Tab = 'headers' | 'request' | 'response'

export default function Details({ requestResponse }: DetailsProps) {
  const [tab, selectTab] = useState<Tab>('headers');
  const [raw, setRaw] = useState(false);

  const resend = useCallback(async () => fetch(
    `http://${getHost()}/resend/`,
    {
      method: 'POST',
      body: JSON.stringify({
        ...requestResponse.request,
        id: undefined
      })
    }
  ), [requestResponse]);

  return (
    <div className={styles.details}>
      <div className={styles.header}>
        <Row>
          <Col>
            <Container fluid style={{ fontSize: '1.5em' }} className="py-3">
              <RequestSummary requestResponse={requestResponse} className={styles.summary}/>
            </Container>
          </Col>
        </Row>
        <Row className="gx-0 d-flex">
          <Col>
            <Nav
              variant="tabs"
              activeKey={tab}
              onSelect={(tab) => selectTab(tab as Tab)}
            >
              <Nav.Item>
                <Nav.Link eventKey="headers">
                  Headers
                </Nav.Link >
              </Nav.Item>
              <Nav.Item>
                <Nav.Link eventKey="request">
                  Request
                </Nav.Link>
              </Nav.Item>
              <Nav.Item>
                <Nav.Link
                  eventKey="response"
                  disabled={requestResponse.response === undefined}
                >
                  Response
                </Nav.Link>
              </Nav.Item>
            </Nav>
          </Col>
          <Col className="border-bottom px-3 " xs="auto">
            <Timing timing={requestResponse.response?.timing ?? NaN} />
            <Button variant="outline-primary" onClick={() => resend()} className="ms-3">
              Resend
            </Button>
          </Col>
        </Row>
      </div>
      <div className={styles.content}>
        {
          tab === 'headers' && (
            <>
              <Headers
                title="request headers"
                headers={requestResponse.request.headers}
              />
              {
                requestResponse.response && (
                  <Headers
                    title="response headers"
                    headers={requestResponse.response.headers}
                  />
                )
              }
            </>
          )
        }
        {
          tab === 'request' && (
            <Content data={requestResponse.request} raw={raw} setRaw={setRaw}/>
          )
        }
        {
          tab === 'response' && requestResponse.response !== undefined && (
            <Content data={requestResponse.response} raw={raw} setRaw={setRaw}/>
          )
        }
      </div>
    </div>
  )
}