1- import React , { useState } from "react" ;
1+ import React , { useState , useMemo , useCallback , useEffect } from "react" ;
22import * as S from "./styles" ;
33import ViewMarkdown from "@components/ViewMarkdown" ;
44
5- const CommentCreate = ( ) => {
5+ export interface ICommentCreate {
6+ isReplying ?: boolean ;
7+ isReplyOverReply ?: boolean ;
8+ replyId ?: number ;
9+ replyUser ?: string ;
10+ }
11+
12+ const CommentCreate : React . FC < ICommentCreate > = ( {
13+ isReplying = false ,
14+ isReplyOverReply = false ,
15+ replyId,
16+ replyUser,
17+ } ) => {
618 const [ comment , setComment ] = useState ( "" ) ;
719 const [ preview , setPreview ] = useState < boolean > ( false ) ;
820
9- const account = {
10- isLogged : true ,
11- user : {
12- userName : "Parlandim" ,
13- avatar : "https://avatar.iran.liara.run/public/10" ,
14- } ,
15- } ;
21+ const account = useMemo (
22+ ( ) => ( {
23+ isLogged : true ,
24+ user : {
25+ userName : "Parlandim" ,
26+ avatar : "https://avatar.iran.liara.run/public/10" ,
27+ } ,
28+ } ) ,
29+ [ ]
30+ ) ;
31+
32+ const clearComment = useCallback ( ( ) => {
33+ setComment ( "" ) ;
34+ } , [ ] ) ;
35+
36+ const withMention = useCallback (
37+ ( content : string ) => {
38+ if ( ! isReplyOverReply ) {
39+ return content ;
40+ }
41+
42+ if ( content . includes ( `@${ replyUser } ` ) ) {
43+ return content ;
44+ }
1645
17- const handleChange = ( e : React . ChangeEvent < HTMLTextAreaElement > ) => {
18- const { value } = e . target ;
46+ return `@${ replyUser } \n${ content } ` ;
47+ } ,
48+ [ isReplying , replyUser , isReplyOverReply ]
49+ ) ;
1950
20- const replaceString = value . replace ( / \n \n / g, "\n<br>" ) ;
51+ const handleChange = useCallback (
52+ ( e : React . ChangeEvent < HTMLTextAreaElement > ) => {
53+ const { value } = e . target ;
54+ const replaceString = value . replace ( / \n \n / g, "\n<br>" ) ;
55+ setComment ( withMention ( replaceString ) ) ;
56+ } ,
57+ [ withMention ]
58+ ) ;
2159
22- setComment ( replaceString ) ;
23- } ;
60+ const handlePreview = useCallback ( ( ) => {
61+ setPreview ( ( prev ) => ! prev ) ;
62+ } , [ ] ) ;
2463
25- const handlePreview = ( ) => {
26- setPreview ( ! preview ) ;
27- } ;
64+ const userSection = useMemo (
65+ ( ) => (
66+ < S . UserSection >
67+ < S . UserInfos >
68+ { account . isLogged && (
69+ < >
70+ < S . Avatar src = { account . user . avatar } />
71+ < S . UserName > { account . user . userName } </ S . UserName >
72+ </ >
73+ ) }
74+ </ S . UserInfos >
75+ < S . Button > { account . isLogged ? "comentar" : "fazer login" } </ S . Button >
76+ </ S . UserSection >
77+ ) ,
78+ [ account ]
79+ ) ;
2880
2981 return (
3082 < S . Container >
@@ -33,6 +85,8 @@ const CommentCreate = () => {
3385 < S . Button onClick = { handlePreview } >
3486 { preview ? "Escrever" : "Pré visualizar" }
3587 </ S . Button >
88+
89+ { isReplying && < S . ReplyText > Respondendo a @{ replyUser } </ S . ReplyText > }
3690 </ S . ButtonsWrapper >
3791
3892 { ! preview && (
@@ -49,17 +103,7 @@ const CommentCreate = () => {
49103 </ S . Preview >
50104 ) }
51105
52- < S . UserSection >
53- < S . UserInfos >
54- { account . isLogged && (
55- < >
56- < S . Avatar src = { account . user . avatar } />
57- < S . UserName > { account . user . userName } </ S . UserName >
58- </ >
59- ) }
60- </ S . UserInfos >
61- < S . Button > { account . isLogged ? "comentar" : "fazer login" } </ S . Button >
62- </ S . UserSection >
106+ { userSection }
63107 </ S . CommentSection >
64108 </ S . Container >
65109 ) ;
0 commit comments