This repository has been archived by the owner on Oct 11, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathinputs.js
143 lines (136 loc) · 3.77 KB
/
inputs.js
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
// @flow
import React from 'react';
import Textarea from 'react-textarea-autosize';
import Dropzone from 'react-dropzone';
import {
ThreadInputs,
DropzoneWrapper,
ThreadTitle,
ThreadDescription,
DropImageOverlay,
RenderWrapper,
InputsGrid,
} from './style';
import { ThreadHeading } from 'src/views/thread/style';
import { SegmentedControl, Segment } from 'src/components/segmentedControl';
import MentionsInput from 'src/components/mentionsInput';
import ThreadRenderer from '../threadRenderer';
type Props = {
title: string,
body: ?string,
changeBody: Function,
changeTitle: Function,
uploadFiles: Function,
autoFocus?: boolean,
bodyRef?: Function,
onKeyDown?: Function,
isEditing: boolean,
};
export default (props: Props) => {
// $FlowIssue
const [showPreview, setShowPreview] = React.useState(false);
// $FlowIssue
const [previewBody, setPreviewBody] = React.useState(null);
const {
title,
body,
uploadFiles,
autoFocus,
changeBody,
changeTitle,
bodyRef,
onKeyDown,
isEditing,
} = props;
const onClick = (show: boolean) => {
setShowPreview(show);
if (show) {
setPreviewBody(null);
fetch('https://convert.spectrum.chat/from', {
method: 'POST',
body,
})
.then(res => {
if (res.status < 200 || res.status >= 300)
throw new Error('Oops, something went wrong');
return res.json();
})
.then(json => {
setPreviewBody(json);
});
}
};
return (
<InputsGrid isEditing={isEditing}>
<SegmentedControl
css={{
margin: '0',
position: 'sticky',
top: '0',
left: '0',
right: '0',
zIndex: '9999',
background: '#FFF',
minHeight: '52px',
}}
>
<Segment isActive={!showPreview} onClick={() => onClick(false)}>
Write
</Segment>
<Segment isActive={showPreview} onClick={() => onClick(true)}>
Preview
</Segment>
</SegmentedControl>
<ThreadInputs>
{showPreview ? (
/* $FlowFixMe */
<RenderWrapper>
<ThreadHeading>{title}</ThreadHeading>
{previewBody === null ? (
<p>Loading...</p>
) : (
<ThreadRenderer body={previewBody} />
)}
</RenderWrapper>
) : (
<Dropzone
accept={['image/gif', 'image/jpeg', 'image/png', 'video/mp4']}
disableClick
multiple={false}
onDropAccepted={uploadFiles}
>
{({ getRootProps, getInputProps, isDragActive }) => (
<DropzoneWrapper
{...getRootProps({
refKey: 'ref',
})}
>
<input {...getInputProps()} />
<Textarea
data-cy="composer-title-input"
onChange={changeTitle}
style={ThreadTitle}
value={title}
placeholder="What do you want to talk about?"
autoFocus={autoFocus}
/>
<MentionsInput
onChange={changeBody}
value={body === null ? 'Loading...' : body}
disabled={body === null}
style={ThreadDescription}
inputRef={bodyRef}
placeholder="Elaborate here if necessary (optional)"
className={'threadComposer'}
dataCy="rich-text-editor"
onKeyDown={onKeyDown}
/>
<DropImageOverlay visible={isDragActive} />
</DropzoneWrapper>
)}
</Dropzone>
)}
</ThreadInputs>
</InputsGrid>
);
};