'use client'; import { useState, useEffect } from 'react'; import styles from './EditFlushModal.module.css'; import { containsBannedWords, sanitizeText, isAllowedEmoji } from '@/lib/content-filter'; interface EditFlushModalProps { isOpen: boolean; flushData: { uri: string; text: string; emoji: string; created_at: string; } | null; onSave: (text: string, emoji: string) => Promise; onDelete: () => Promise; onClose: () => void; } // Define approved emojis list const APPROVED_EMOJIS = [ '๐Ÿšฝ', '๐Ÿงป', '๐Ÿ’ฉ', '๐Ÿ’จ', '๐Ÿšพ', '๐Ÿงผ', '๐Ÿช ', '๐Ÿšป', '๐Ÿฉธ', '๐Ÿ’ง', '๐Ÿ’ฆ', '๐Ÿ˜Œ', '๐Ÿ˜ฃ', '๐Ÿคข', '๐Ÿคฎ', '๐Ÿฅด', '๐Ÿ˜ฎโ€๐Ÿ’จ', '๐Ÿ˜ณ', '๐Ÿ˜ต', '๐ŸŒพ', '๐Ÿฆ', '๐Ÿ“ฑ', '๐Ÿ“–', '๐Ÿ’ญ', '1๏ธโƒฃ', '2๏ธโƒฃ', '๐ŸŸก', '๐ŸŸค' ]; export default function EditFlushModal({ isOpen, flushData, onSave, onDelete, onClose }: EditFlushModalProps) { const [text, setText] = useState(''); const [selectedEmoji, setSelectedEmoji] = useState('๐Ÿšฝ'); const [isSubmitting, setIsSubmitting] = useState(false); const [error, setError] = useState(null); const [showDeleteConfirm, setShowDeleteConfirm] = useState(false); // Update form when flushData changes useEffect(() => { if (flushData) { // Remove "is " prefix if it exists in the stored text let displayText = flushData.text || ''; if (displayText.toLowerCase().startsWith('is ')) { displayText = displayText.substring(3); } setText(displayText); setSelectedEmoji(flushData.emoji || '๐Ÿšฝ'); setError(null); setShowDeleteConfirm(false); } }, [flushData]); if (!isOpen || !flushData) return null; const handleSave = async () => { setError(null); // Validate text if (containsBannedWords(text)) { setError('Uh oh, looks like you have a potty mouth. Try again with cleaner language please...'); return; } // Check character limit (text + "is " = 56 + 3 = 59) if (text.length > 56) { setError('Your flush status is too long! Please keep it under 56 characters (59 total with "is").'); return; } // Validate emoji if (!isAllowedEmoji(selectedEmoji)) { setError('Please select a valid emoji from the list.'); return; } setIsSubmitting(true); try { // Add "is " prefix when saving const fullText = text.trim() ? `is ${text.trim()}` : 'is flushing'; await onSave(sanitizeText(fullText), selectedEmoji); onClose(); } catch (err: any) { console.error('Error updating flush:', err); setError(err.message || 'Failed to update flush. Please try again.'); } finally { setIsSubmitting(false); } }; const handleDelete = async () => { setIsSubmitting(true); setError(null); try { await onDelete(); onClose(); } catch (err: any) { console.error('Error deleting flush:', err); setError(err.message || 'Failed to delete flush. Please try again.'); } finally { setIsSubmitting(false); setShowDeleteConfirm(false); } }; const handleBackdropClick = (e: React.MouseEvent) => { if (e.target === e.currentTarget && !isSubmitting) { onClose(); } }; return (

Edit Your Flush

{error && (
{error}
)}
is setText(e.target.value)} placeholder="flushing" maxLength={56} disabled={isSubmitting} className={styles.inputWithPrefix} />
{text.length + 3}/59
{APPROVED_EMOJIS.map((emoji) => ( ))}
{!showDeleteConfirm ? ( <>
) : (

Are you sure you want to delete this flush? This cannot be undone.

)}
); }