aboutsummaryrefslogtreecommitdiff
path: root/frontend/src/components/renderers/Raw.js
blob: d4dc8308be9388ad1b6dd8dc47bcb8acb20593db (plain) (blame)
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
import React, { useState, useEffect } from 'react';
import styled from 'styled-components'
import { FetchPaste } from '../../helpers/httpHelper'

const RawText = styled.pre`
    word-wrap: break-word;
    white-space: pre-wrap;
    line-height: initial;
    font-size: 0.8em;
    padding: 0 1em;
`

const Raw = ({hash}) => {
    const [content, setContent] = useState('');

    useEffect(() => {
        FetchPaste(hash)
        .then((response) => {
            const data = response.data
            setContent(data.content)
        }).catch((error) => {
            const resp = error.response

            // catch 401 unauth (password protected)
            if (resp.status === 401) {
                setContent('err: password protected')
                return
            }

            // some weird err
            if (resp !== undefined) {
                const errTxt = `${resp.statusText}: ${resp.data}`
                setContent(errTxt)
                return
            }

            // some weird err (e.g. network)
            setContent(error)
    })}, [hash])

    return (
        <RawText>{content}</RawText>
    );
}

export default Raw