import { useState } from 'react'; import { Button } from '@/components/ui/button'; import { AlertDialog, AlertDialogAction, AlertDialogCancel, AlertDialogContent, AlertDialogDescription, AlertDialogFooter, AlertDialogHeader, AlertDialogTitle, AlertDialogTrigger } from '@/components/ui/alert-dialog'; import { Input } from '@/components/ui/input'; import { Label } from '@/components/ui/label'; import { UserX } from 'lucide-react'; import { supabase } from '@/lib/supabaseClient'; import { useAuth } from '@/hooks/useAuth'; import { useToast } from '@/hooks/use-toast'; import { getErrorMessage } from '@/lib/errorHandler'; interface UserBlockButtonProps { targetUserId: string; targetUsername: string; variant?: 'default' | 'outline' | 'ghost'; size?: 'default' | 'sm' | 'lg'; } export function UserBlockButton({ targetUserId, targetUsername, variant = 'outline', size = 'sm' }: UserBlockButtonProps) { const { user } = useAuth(); const { toast } = useToast(); const [reason, setReason] = useState(''); const [loading, setLoading] = useState(false); const handleBlock = async () => { if (!user) return; setLoading(true); try { const { error } = await supabase .from('user_blocks') .insert({ blocker_id: user.id, blocked_id: targetUserId, reason: reason.trim() || null }); if (error) throw error; toast({ title: 'User blocked', description: `You have blocked @${targetUsername}. They will no longer be able to interact with your content.` }); setReason(''); } catch (error: unknown) { toast({ title: 'Error', description: getErrorMessage(error), variant: 'destructive' }); } finally { setLoading(false); } }; // Don't show block button if user is not logged in or trying to block themselves if (!user || user.id === targetUserId) { return null; } return ( Block @{targetUsername} This user will no longer be able to see your content or interact with you. You can unblock them at any time from your privacy settings.
setReason(e.target.value)} maxLength={200} />
Cancel {loading ? 'Blocking...' : 'Block User'}
); }