1+ import { CodeIcon , SendIcon } from "lucide-react" ;
2+ import { useState } from "react" ;
3+ import CommentContent from "./CommentContent" ;
4+
5+
6+ interface CommentFormProps {
7+ onSubmit : ( comment : string ) => Promise < void > ;
8+ isSubmitting : boolean ;
9+ }
10+
11+ function CommentForm ( { isSubmitting, onSubmit } : CommentFormProps ) {
12+ const [ comment , setComment ] = useState ( "" ) ;
13+ const [ isPreview , setIsPreview ] = useState ( false ) ;
14+
15+ const handleKeyDown = ( e : React . KeyboardEvent < HTMLTextAreaElement > ) => {
16+ if ( e . key === "Tab" ) {
17+ e . preventDefault ( ) ;
18+ const start = e . currentTarget . selectionStart ;
19+ const end = e . currentTarget . selectionEnd ;
20+ const newComment = comment . substring ( 0 , start ) + " " + comment . substring ( end ) ;
21+ setComment ( newComment ) ;
22+ e . currentTarget . selectionStart = e . currentTarget . selectionEnd = start + 2 ;
23+ }
24+ } ;
25+
26+ const handleSubmit = async ( e : React . FormEvent ) => {
27+ e . preventDefault ( ) ;
28+
29+ if ( ! comment . trim ( ) ) return ;
30+
31+ await onSubmit ( comment ) ;
32+
33+ setComment ( "" ) ;
34+ setIsPreview ( false ) ;
35+ } ;
36+
37+ return (
38+ < form onSubmit = { handleSubmit } className = "mb-8" >
39+ < div className = "bg-[#0a0a0f] rounded-xl border border-[#ffffff0a] overflow-hidden" >
40+ { /* Comment form header */ }
41+ < div className = "flex justify-end gap-2 px-4 pt-2" >
42+ < button
43+ type = "button"
44+ onClick = { ( ) => setIsPreview ( ! isPreview ) }
45+ className = { `text-sm px-3 py-1 rounded-md transition-colors ${
46+ isPreview ? "bg-blue-500/10 text-blue-400" : "hover:bg-[#ffffff08] text-gray-400"
47+ } `}
48+ >
49+ { isPreview ? "Edit" : "Preview" }
50+ </ button >
51+ </ div >
52+
53+ { /* Comment form body */ }
54+ { isPreview ? (
55+ < div className = "min-h-[120px] p-4 text-[#e1e1e3" >
56+ < CommentContent content = { comment } />
57+ </ div >
58+ ) : (
59+ < textarea
60+ value = { comment }
61+ onChange = { ( e ) => setComment ( e . target . value ) }
62+ onKeyDown = { handleKeyDown }
63+ placeholder = "Add to the discussion..."
64+ className = "w-full bg-transparent border-0 text-[#e1e1e3] placeholder:text-[#808086] outline-none
65+ resize-none min-h-[120px] p-4 font-mono text-sm"
66+ />
67+ ) }
68+
69+ { /* Comment Form Footer */ }
70+ < div className = "flex items-center justify-between gap-4 px-4 py-3 bg-[#080809] border-t border-[#ffffff0a]" >
71+ < div className = "hidden sm:block text-xs text-[#808086] space-y-1" >
72+ < div className = "flex items-center gap-2" >
73+ < CodeIcon className = "w-3.5 h-3.5" />
74+ < span > Format code with ```language</ span >
75+ </ div >
76+ < div className = "text-[#808086]/60 pl-5" >
77+ Tab key inserts spaces • Preview your comment before posting
78+ </ div >
79+ </ div >
80+ < button
81+ type = "submit"
82+ disabled = { isSubmitting || ! comment . trim ( ) }
83+ className = "flex items-center gap-2 px-4 py-2 bg-[#3b82f6] text-white rounded-lg hover:bg-[#2563eb] disabled:opacity-50 disabled:cursor-not-allowed transition-all ml-auto"
84+ >
85+ { isSubmitting ? (
86+ < >
87+ < div
88+ className = "w-4 h-4 border-2 border-white/30
89+ border-t-white rounded-full animate-spin"
90+ />
91+ < span > Posting...</ span >
92+ </ >
93+ ) : (
94+ < >
95+ < SendIcon className = "w-4 h-4" />
96+ < span > Comment</ span >
97+ </ >
98+ ) }
99+ </ button >
100+ </ div >
101+ </ div >
102+ </ form >
103+ ) ;
104+ }
105+ export default CommentForm ;
0 commit comments