aboutsummaryrefslogtreecommitdiff
path: root/frontend/src/components/NewPaste.js
blob: b3223512747a78e9d6236a27b8efc778b30165a9 (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
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
import React, { useEffect, useState, useRef } from 'react';
import { Text, Code } from './Inputs'
import OptionsContainer from './Options'
import Error from './Err'
import { PostNewPaste } from '../helpers/httpHelper'
import PasteModal from './modals/PasteModal'
import styled from 'styled-components'
import CodeRenderer from './renderers/Code'
import Latex from './renderers/Latex'
import Markdown from './renderers/Markdown'
import {Button, SubmitButton} from "./Common/Button";

const Flex = styled.div`
    display: flex;
    flex-direction: row;
`

const FlexLeft = styled.div`
    flex: 0 0 calc(50% - 1em - 2px);
`

const FlexRight = styled.div`
    flex: 0 0 50%;
    max-width: calc(50% - 1em + 2px);
    margin-left: 2em;
`

const PreviewWrapper = styled.div`
    margin: 2em;
`

const NewPaste = () =>  {
    const [title, setTitle] = useState('');
    const [content, setContent] = useState('');
    const [pass, setPass] = useState('');
    const [language, setLanguage] = useState('detect');
    const [expiry, setExpiry] = useState('');
    const [hash, setHash] = useState('');
    const [isPreview, setIsPreview] = useState(false);
    const ErrorLabel = useRef(null);

    useEffect(() => {
        if (title === "") {
            document.title = `ctrl-v`;
        } else {
            document.title = `ctrl-v | ${title}`;
        }
    }, [title])

    function handleSubmit(e) {
        e.preventDefault();

        // prevent resubmission
        if (!hash) {
            PostNewPaste(title, content, language, pass, expiry)
                .then((response) => {
                    // on success, redir
                    setHash(response.data.hash)
                }).catch((error) => {
                    const resp = error.response
    
                    // some weird err
                    if (resp !== undefined) {
                        const errTxt = `${resp.status}: ${resp.data}`
                        ErrorLabel.current.showMessage(errTxt)
                    } else {
                        // some weird err (e.g. network)
                        ErrorLabel.current.showMessage(error)
                    }
                });
        }
    }

    function renderPreview() {
        const pasteInput = <Code
            setContentCallback={setContent}
            content={content}
            maxLength="100000" />

        if (isPreview) {
            var preview
            switch (language) {
                case 'latex':
                    preview =
                        <PreviewWrapper>
                            <Latex
                                content={content} />
                        </PreviewWrapper>
                    break
                case 'markdown':
                    preview =
                        <PreviewWrapper className='md' >
                            <Markdown
                                content={content} />
                        </PreviewWrapper>
                    break
                default:
                    preview =
                        <CodeRenderer
                            lang={language}
                            theme='atom'
                            content={content} />
            }

            return (
                <Flex>
                    <FlexLeft>
                        {pasteInput}
                    </FlexLeft>
                    <FlexRight className='preview' >
                        {preview}
                    </FlexRight>
                </Flex>
            );
        } else {
            return (
                pasteInput
            );
        }
    }

    return (
        <form onSubmit={handleSubmit}>
            <PasteModal hash={hash} />
            <Text
                label="title"
                onChange={(e) => {setTitle(e.target.value)}}
                value={title}
                autoFocus
                maxLength="100"
                id="titleInput" />
            {renderPreview()}
            <OptionsContainer
                pass={pass}
                expiry={expiry}
                lang={language}
                onPassChange={(e) => { setPass(e.target.value) }} 
                onLangChange={(e) => { setLanguage(e.target.value) }} 
                onExpiryChange={(e) => { setExpiry(e.target.value) }} />
            <div>
              <SubmitButton type="submit" value="new paste" />
              {language !== 'detect' && <Button
                secondary
                type="button"
                onClick={() => setIsPreview(!isPreview)}>
                preview
              </Button>}
            </div>
            <br />
            <Error ref={ErrorLabel} />
        </form>
    );
}

export default NewPaste