import React, { useCallback, useRef, useState } from 'react' import ReactMarkdown from 'react-markdown' import EmojiPickerButton from '../comments/EmojiPickerButton' function BoldIcon() { return ( ) } function ItalicIcon() { return ( ) } function CodeIcon() { return ( ) } function LinkIcon() { return ( ) } function ListIcon() { return ( ) } function QuoteIcon() { return ( ) } function ToolbarBtn({ title, onClick, children }) { return ( ) } export default function CommentForm({ placeholder = 'Write a comment…', submitLabel = 'Post', onSubmit, onCancel, compact = false }) { const [content, setContent] = useState('') const [busy, setBusy] = useState(false) const [error, setError] = useState('') const [tab, setTab] = useState('write') const textareaRef = useRef(null) const wrapSelection = useCallback((before, after) => { const element = textareaRef.current if (!element) return const start = element.selectionStart const end = element.selectionEnd const selected = content.slice(start, end) const replacement = before + (selected || 'text') + after const next = content.slice(0, start) + replacement + content.slice(end) setContent(next) requestAnimationFrame(() => { const cursorPos = selected ? start + replacement.length : start + before.length const cursorEnd = selected ? start + replacement.length : start + before.length + 4 element.selectionStart = cursorPos element.selectionEnd = cursorEnd element.focus() }) }, [content]) const prefixLines = useCallback((prefix) => { const element = textareaRef.current if (!element) return const start = element.selectionStart const end = element.selectionEnd const selected = content.slice(start, end) const lines = selected ? selected.split('\n') : [''] const prefixed = lines.map((line) => prefix + line).join('\n') const next = content.slice(0, start) + prefixed + content.slice(end) setContent(next) requestAnimationFrame(() => { element.selectionStart = start element.selectionEnd = start + prefixed.length element.focus() }) }, [content]) const insertLink = useCallback(() => { const element = textareaRef.current if (!element) return const start = element.selectionStart const end = element.selectionEnd const selected = content.slice(start, end) const isUrl = /^https?:\/\//.test(selected) const replacement = isUrl ? `[link](${selected})` : `[${selected || 'link'}](https://)` const next = content.slice(0, start) + replacement + content.slice(end) setContent(next) requestAnimationFrame(() => { if (isUrl) { element.selectionStart = start + 1 element.selectionEnd = start + 5 } else { const urlStart = start + replacement.length - 1 element.selectionStart = urlStart - 8 element.selectionEnd = urlStart - 1 } element.focus() }) }, [content]) const insertAtCursor = useCallback((text) => { const element = textareaRef.current if (!element) { setContent((current) => current + text) return } const start = element.selectionStart ?? content.length const end = element.selectionEnd ?? content.length const next = content.slice(0, start) + text + content.slice(end) setContent(next) requestAnimationFrame(() => { element.selectionStart = start + text.length element.selectionEnd = start + text.length element.focus() }) }, [content]) const handleKeyDown = useCallback((event) => { const mod = event.ctrlKey || event.metaKey if (!mod) return switch (event.key.toLowerCase()) { case 'b': event.preventDefault() wrapSelection('**', '**') break case 'i': event.preventDefault() wrapSelection('*', '*') break case 'k': event.preventDefault() insertLink() break case 'e': event.preventDefault() wrapSelection('`', '`') break default: break } }, [insertLink, wrapSelection]) const handleSubmit = async (event) => { event.preventDefault() const trimmed = content.trim() if (!trimmed || busy) return setBusy(true) setError('') try { await onSubmit?.(trimmed) setContent('') setTab('write') } catch (submitError) { setError(submitError?.message || 'Unable to post comment.') } finally { setBusy(false) } } return (
9000 ? 'text-amber-400/80' : 'text-white/20', ].join(' ')} > {content.length > 0 && `${content.length.toLocaleString()}/10,000`}
{tab === 'write' && (
wrapSelection('**', '**')}> wrapSelection('*', '*')}> wrapSelection('`', '`')}>
prefixLines('- ')}> prefixLines('> ')}>
)} {tab === 'write' && (