This repository has been archived by the owner on Mar 23, 2023. It is now read-only.
/
Publish.tsx
152 lines (140 loc) 路 4.71 KB
/
Publish.tsx
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
import React, { useState, useEffect } from 'react'
import { Button, Fab, Switch, TextField } from '@mui/material'
import { Container, Paper } from '@mui/material'
import { Controller, useForm } from 'react-hook-form'
// import { highlight, languages } from 'prismjs'
import 'twin.macro'
// import 'prismjs/components/prism-clike'
// import 'prismjs/components/prism-javascript'
// import 'prismjs/components/prism-markup'
// import 'prismjs/components/prism-css'
import AddIcon from '@mui/icons-material/Add'
import Editor from 'react-simple-code-editor'
import { encryptData } from '@/util/crypt'
import { RouteComponentProps } from '@reach/router'
interface ISubmitData {
author: string
isPublic: boolean
filename: string
encrypt: boolean
password: string
}
function Publish(props: RouteComponentProps) {
const { handleSubmit, control, watch } = useForm()
const [text, setText] = useState('')
const watchEncrypt = watch('encrypt', false)
const onSubmit = async (data: ISubmitData) => {
console.debug(data)
const formData = new FormData()
if (data.author !== '') {
formData.append('author', data.author)
}
// formData.append('public', data.isPublic)
const filename = data.filename === '' ? 'plain.txt' : data.filename
let content: Uint8Array | string
if (data.encrypt) {
content = await encryptData(text, data.password)
formData.append('encrypt_algorithm', 'aes-gcm')
} else {
content = text
}
const blob = new Blob([content], { type: 'text/plain' })
formData.append('file', blob, filename)
const resp = await fetch(import.meta.env.VITE_API_URL + '/api/v0/upload', {
method: 'POST',
body: formData,
mode: 'cors',
})
if (resp.status === 201) {
const cid = await resp.json()
window.location.href = `/` + cid
}
}
return (
<Container maxWidth="md">
{/* <div tw="fixed right-[60px] bottom-[60px]">
<Fab color="primary" aria-label="add">
<AddIcon />
</Fab>
</div> */}
<Paper>
<div tw="flex m-6 pt-4 pb-6 w-full">
<form onSubmit={handleSubmit(onSubmit)}>
<div tw="w-[300px]">
<Controller
name="filename"
control={control}
defaultValue=""
render={({ field }) => <TextField size="small" fullWidth label="Filename" {...field} />}
/>
</div>
<div tw="my-4 min-w-[800px] max-w-[800px]">
<Paper variant="outlined">
{/* TODO replace editor with multiline textarea input */}
<Editor
value={text}
onValueChange={(text) => setText(text)}
highlight={(text) => text}
// highlight={(text) => highlight(text, languages.markup, 'markup')}
padding={14}
tabSize={4}
insertSpaces={true}
name="code"
placeholder="Put something here.."
style={{
minHeight: '400px',
fontFamily: '"Fira code", "Fira Mono", monospace',
fontSize: 13,
overflow: 'auto',
}}
/>
</Paper>
</div>
<div tw="my-4 w-[200px]">
<Controller
name="author"
control={control}
defaultValue=""
render={({ field }) => <TextField size="small" fullWidth label="Author" {...field} />}
/>
</div>
{/* <div>
<Controller
name="isPublic"
control={control}
defaultValue={true}
render={({ field }) => <Switch {...field} />}
/>
Show on gallery
</div> */}
<div>
<Controller
name="encrypt"
control={control}
defaultValue={false}
render={({ field }) => <Switch {...field} />}
/>
Encrypt
</div>
{watchEncrypt && (
<div tw="my-4 w-[200px]">
<Controller
name="password"
control={control}
defaultValue=""
render={({ field }) => <TextField size="small" fullWidth label="Password" {...field} />}
/>
</div>
)}
<div tw="mt-6">
<Button type="submit" color="primary" variant="contained">
Publish
</Button>
</div>
</form>
</div>
</Paper>
</Container>
)
}
export { Publish }