Debian Codegraph @

babel-preset-modules/test/integration/react-hooks-demo/Comments.js

raw symbols SCIP
1 import React, { useState, useEffect, useContext, createContext } from "react";
2 import styled from "styled-components";
3
4 import TextArea from "react-textarea-autosize";
5
6 import Markdown from "./Markdown";
7 import Card from "./Card";
8 import Button from "./Button";
9
10 const CommentContext = createContext({});
11
12 function compare(a1, a2) {
13 if (JSON.stringify(a1) === JSON.stringify(a2)) {
14 return true;
15 }
16 return false;
17 }
18
19 function gen_comments(comments, colorindex, path) {
20 return comments.map((comment, i) => {
21 return (
22 <Comment
23 username={comment.username}
24 date={comment.date}
25 text={comment.text}
26 votes={comment.votes}
27 colorindex={colorindex}
28 key={i}
29 path={[...path, i]}
30 comments={comment.comments}
31 />
32 );
33 });
34 }
35
36 function Reply(props) {
37 const [text, setText] = useState("");
38 return (
39 <div {...props}>
40 <TextArea
41 placeholder="What are your thoughts?"
42 minRows={2}
43 defaultValue={text}
44 onChange={value => {
45 setText(value.target.value);
46 }}
47 />
48 <div className="panel">
49 <div className="comment_as">
50 Comment as{" "}
51 <a href="" className="username">
52 Kevin
53 </a>
54 </div>
55 <Button>COMMENT</Button>
56 </div>
57 </div>
58 );
59 }
60
61 Reply = styled(Reply)`
62 border-radius: 8px;
63 border: solid 1px #3d4953;
64 overflow: hidden;
65
66 &.hidden {
67 display: none;
68 }
69
70 textarea {
71 font-family: inherit;
72 -webkit-box-sizing: border-box;
73 -moz-box-sizing: border-box;
74 box-sizing: border-box;
75
76 resize: none;
77
78 background: #13181d;
79 padding: 12px;
80 color: #cccccc;
81 border: none;
82 max-width: 100%;
83 min-width: 100%;
84 }
85
86 .panel {
87 display: flex;
88 align-items: center;
89 background: #3d4953;
90 padding: 8px;
91
92 .comment_as {
93 font-size: 14px;
94 color: #cccccc;
95 margin-right: 8px;
96
97 .username {
98 display: inline-block;
99 color: #4f9eed;
100 }
101 }
102
103 ${Button} {
104 font-size: 14px;
105 margin-left: auto;
106 }
107 }
108 `;
109
110 function Rating(props) {
111 const [count, setCount] = useState(props.votes);
112 const [thumbsUp, setThumbsUp] = useState(false);
113 const [thumbsDown, setThumbsDown] = useState(false);
114
115 return (
116 <div {...props}>
117 <button
118 className={`material-icons ${thumbsUp ? "selected" : ""}`}
119 id="thumbs_up"
120 onClick={() => {
121 setThumbsUp(!thumbsUp);
122 setThumbsDown(false);
123 }}
124 >
125 keyboard_arrow_up
126 </button>
127 <div
128 className={`count ${thumbsUp ? "up" : ""} ${thumbsDown ? "down" : ""}`}
129 >
130 {thumbsUp ? count + 1 : ""}
131 {thumbsDown ? count - 1 : ""}
132 {thumbsUp || thumbsDown ? "" : count}
133 </div>
134 <button
135 className={`material-icons ${thumbsDown ? "selected" : ""}`}
136 id="thumbs_down"
137 onClick={() => {
138 setThumbsDown(!thumbsDown);
139 setThumbsUp(false);
140 }}
141 >
142 keyboard_arrow_down
143 </button>
144 </div>
145 );
146 }
147
148 Rating = styled(Rating)`
149 display: flex;
150 flex-direction: column;
151 margin-right: 12px;
152
153 .count {
154 font-weight: bold;
155 text-align: center;
156 color: #3d4953;
157
158 &.up {
159 color: #4f9eed;
160 }
161
162 &.down {
163 color: #ed4f4f;
164 }
165 }
166
167 button#thumbs_up,
168 button#thumbs_down {
169 border: none;
170 background: none;
171 cursor: pointer;
172 color: #3d4953;
173
174 -webkit-touch-callout: none; /* iOS Safari */
175 -webkit-user-select: none; /* Safari */
176 -khtml-user-select: none; /* Konqueror HTML */
177 -moz-user-select: none; /* Firefox */
178 -ms-user-select: none; /* Internet Explorer/Edge */
179 user-select: none; /* Non-prefixed version, currently
180 supported by Chrome and Opera */
181 }
182
183 #thumbs_up.selected {
184 color: #4f9eed;
185 }
186
187 #thumbs_down.selected {
188 color: #ed4f4f;
189 }
190 `;
191
192 function Comment(props) {
193 const [replying, setReplying] = useContext(CommentContext);
194 const [minimized, setMinimized] = useState(false);
195 const [hidden, setHidden] = useState(false);
196
197 useEffect(
198 async () => {
199 if (props.path.length > 2 && props.path.length % 2 === 0) {
200 setHidden(true);
201 }
202 if (props.path[props.path.length - 1] > 3) {
203 setHidden(true);
204 }
205 },
206 [props.path]
207 );
208
209 return (
210 <div {...props}>
211 {hidden ? (
212 <button
213 id="showMore"
214 onClick={() => {
215 setHidden(false);
216 }}
217 >
218 Show More Replies
219 </button>
220 ) : (
221 <>
222 <div id="left" className={minimized ? "hidden" : ""}>
223 <Rating votes={props.votes} />
224 </div>
225 <div id="right">
226 <div id="top">
227 <span
228 className="minimize"
229 onClick={() => {
230 setMinimized(!minimized);
231 }}
232 >
233 [{minimized ? "+" : "-"}]
234 </span>
235 <span id="username">
236 <a href="">{props.username}</a>
237 </span>
238 <span id="date">
239 <a href="">{props.date}</a>
240 </span>
241 </div>
242 <div id="content" className={minimized ? "hidden" : ""}>
243 <Markdown options={{ forceBlock: true }}>{props.text}</Markdown>
244 </div>
245 <div id="actions" className={minimized ? "hidden" : ""}>
246 <span
247 className={`${compare(replying, props.path) ? "selected" : ""}`}
248 onClick={() => {
249 if (compare(replying, props.path)) {
250 setReplying([]);
251 } else {
252 setReplying(props.path);
253 }
254 }}
255 >
256 reply
257 </span>
258 <span>report</span>
259 </div>
260 <Reply
261 className={
262 compare(replying, props.path) && !minimized ? "" : "hidden"
263 }
264 />
265 <div className={`comments ${minimized ? "hidden" : ""}`}>
266 {gen_comments(props.comments, props.colorindex + 1, [
267 ...props.path
268 ])}
269 </div>
270 </div>
271 </>
272 )}
273 </div>
274 );
275 }
276
277 Comment = styled(Comment)`
278 display: flex;
279 text-align: left;
280 background: ${props => (props.colorindex % 2 === 0 ? "#161C21" : "#13181D")};
281 padding: 16px 16px 16px 12px;
282 border: 0.1px solid #3d4953;
283 border-radius: 8px;
284
285 #showMore {
286 background: none;
287 border: none;
288 color: #53626f;
289 cursor: pointer;
290 font-size: 13px;
291 text-align: left;
292
293 &:hover {
294 text-decoration: underline;
295 }
296 }
297
298 .comments {
299 > * {
300 margin-bottom: 16px;
301
302 &:last-child {
303 margin-bottom: 0px;
304 }
305 }
306
307 &.hidden {
308 display: none;
309 }
310 }
311
312 #left {
313 text-align: center;
314 &.hidden {
315 visibility: hidden;
316 height: 0;
317 }
318 }
319
320 #right {
321 flex-grow: 1;
322
323 #top {
324 .minimize {
325 cursor: pointer;
326 color: #53626f;
327
328 -webkit-touch-callout: none; /* iOS Safari */
329 -webkit-user-select: none; /* Safari */
330 -khtml-user-select: none; /* Konqueror HTML */
331 -moz-user-select: none; /* Firefox */
332 -ms-user-select: none; /* Internet Explorer/Edge */
333 user-select: none; /* Non-prefixed version, currently
334 supported by Chrome and Opera */
335 }
336
337 #username {
338 color: #4f9eed;
339 }
340
341 #date {
342 display: inline-block;
343 color: #53626f;
344 }
345
346 > * {
347 margin-right: 8px;
348 }
349 }
350
351 #content {
352 color: #cccccc;
353
354 &.hidden {
355 display: none;
356 }
357 }
358
359 #actions {
360 color: #53626f;
361 margin-bottom: 12px;
362
363 -webkit-touch-callout: none; /* iOS Safari */
364 -webkit-user-select: none; /* Safari */
365 -khtml-user-select: none; /* Konqueror HTML */
366 -moz-user-select: none; /* Firefox */
367 -ms-user-select: none; /* Internet Explorer/Edge */
368 user-select: none; /* Non-prefixed version, currently
369 supported by Chrome and Opera */
370
371 &.hidden {
372 display: none;
373 }
374
375 > .selected {
376 font-weight: bold;
377 }
378
379 > * {
380 cursor: pointer;
381 margin-right: 8px;
382 }
383 }
384 }
385
386 ${Reply} {
387 margin-bottom: 12px;
388 }
389 `;
390
391 function Comments(props) {
392 var [replying, setReplying] = useState([]);
393 var [comments, setComments] = useState([
394 {
395 username: "Kevin",
396 date: "3 hours ago",
397 text: "#Hello\n>quote\n\n`code`",
398 votes: 12,
399 comments: [
400 {
401 username: "Kevin",
402 date: "2 hours ago",
403 text: "^ click the minimize button to hide threads",
404 votes: 8,
405 comments: [
406 {
407 username: "Kevin",
408 date: "1 hours ago",
409 text: "<- Click the arrows to vote",
410 votes: 3,
411 comments: []
412 }
413 ]
414 },
415 {
416 username: "Kevin",
417 date: "4 hours ago",
418 text: "click on reply to open up a text prompt",
419 votes: 5,
420 comments: []
421 },
422 {
423 username: "Kevin",
424 date: "4 hours ago",
425 text: "click on reply to open up a text prompt",
426 votes: 5,
427 comments: []
428 },
429 {
430 username: "Kevin",
431 date: "4 hours ago",
432 text: "click on reply to open up a text prompt",
433 votes: 5,
434 comments: []
435 },
436 {
437 username: "Kevin",
438 date: "10 mins ago",
439 text: "this",
440 votes: 2,
441 comments: [
442 {
443 username: "Kevin",
444 date: "8 mins ago",
445 text: "is",
446 votes: 1,
447 comments: [
448 {
449 username: "Kevin",
450 date: "5 mins ago",
451 text: "to",
452 votes: 0,
453 comments: [
454 {
455 username: "Kevin",
456 date: "4 mins ago",
457 text: "show",
458 votes: -1,
459 comments: [
460 {
461 username: "Kevin",
462 date: "2 mins ago",
463 text: "nesting",
464 votes: -200,
465 comments: []
466 }
467 ]
468 }
469 ]
470 }
471 ]
472 }
473 ]
474 }
475 ]
476 }
477 ]);
478
479 return (
480 <Card {...props}>
481 <span id="comments">Comments</span>
482 <span id="comments_count">(9)</span>
483 <Reply />
484 <CommentContext.Provider value={[replying, setReplying]}>
485 {gen_comments(comments, 0, [])}
486 </CommentContext.Provider>
487 </Card>
488 );
489 }
490
491 export default styled(Comments)`
492 max-width: 750px;
493 min-width: min-content;
494
495 > * {
496 margin-bottom: 16px;
497
498 &:last-child {
499 margin-bottom: 0px;
500 }
501 }
502
503 #comments,
504 #comments_count {
505 font-weight: 900;
506 font-size: 20px;
507 display: inline-block;
508 margin-right: 4px;
509 margin-bottom: 8px;
510 }
511
512 #comments {
513 color: #ffffff;
514 }
515
516 #comments_count {
517 color: #53626f;
518 }
519 `;