celo-poll/components/social/CommentInput.tsx
2025-12-08 16:31:30 +03:00

76 lines
2.1 KiB
TypeScript

'use client'
import { useState } from 'react'
import { Send } from 'lucide-react'
import sdk from '@farcaster/frame-sdk'
interface CommentInputProps {
pollId: string
parentId?: string
onSubmit: () => void
}
export default function CommentInput({ pollId, parentId, onSubmit }: CommentInputProps) {
const [text, setText] = useState('')
const [submitting, setSubmitting] = useState(false)
const handleSubmit = async () => {
if (!text.trim() || submitting) return
setSubmitting(true)
const ctx = await sdk.context
const fid = ctx?.user?.fid?.toString() || '1'
const username = ctx?.user?.username || 'User'
const pfpUrl = ctx?.user?.pfpUrl
await fetch(`/api/poll/${pollId}/comments`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
text,
parentId,
authorFid: fid,
authorUsername: username,
authorPfp: pfpUrl
})
})
setText('')
setSubmitting(false)
onSubmit()
}
const handleKeyDown = (e: React.KeyboardEvent) => {
if (e.key === 'Enter' && (e.metaKey || e.ctrlKey)) {
handleSubmit()
}
}
return (
<div className="bg-white/10 rounded-xl p-4">
<textarea
value={text}
onChange={(e) => setText(e.target.value.slice(0, 500))}
onKeyDown={handleKeyDown}
placeholder="Add a comment... (use @username to mention)"
className="w-full bg-white/10 rounded-lg p-3 border border-white/20 focus:border-celo-green outline-none resize-none text-white placeholder-gray-400"
rows={3}
/>
<div className="flex items-center justify-between mt-2">
<span className="text-sm text-gray-400">
{text.length}/500 • Cmd+Enter to post
</span>
<button
onClick={handleSubmit}
disabled={!text.trim() || submitting}
className="flex items-center gap-2 bg-celo-green text-black px-4 py-2 rounded-lg font-semibold hover:bg-celo-green/90 transition disabled:opacity-50"
>
<Send size={16} />
{submitting ? 'Posting...' : 'Post'}
</button>
</div>
</div>
)
}