import { useState } from 'react'; import { Button } from '@/components/ui/button'; import { Badge } from '@/components/ui/badge'; import { Tooltip, TooltipContent, TooltipTrigger, } from '@/components/ui/tooltip'; import { Shield, Tag, Mail, Download, Trash2, X, CheckCircle2, Ban, Loader2, } from 'lucide-react'; import { toast } from 'sonner'; import { cn } from '@/lib/utils'; import { bulkExportUsers, bulkBanUsers, bulkDeleteUsers, bulkVerifyUsers } from '@/lib/actions/bulk-users'; import type { User } from '@/lib/types/user'; interface BulkActionBarProps { selectedUsers: User[]; onClearSelection: () => void; onOpenSuspendDialog: () => void; onOpenTagDialog: () => void; onOpenEmailComposer: () => void; onComplete: () => void; } export function BulkActionBar({ selectedUsers, onClearSelection, onOpenSuspendDialog, onOpenTagDialog, onOpenEmailComposer, onComplete, }: BulkActionBarProps) { const [loadingAction, setLoadingAction] = useState(null); const count = selectedUsers.length; if (count === 0) return null; const userIds = selectedUsers.map(u => u.id); const runAction = async (key: string, action: () => Promise<{ success: boolean; message: string }>) => { setLoadingAction(key); try { const res = await action(); if (res.success) { toast.success(res.message); onClearSelection(); onComplete(); } else { toast.error(res.message); } } catch { toast.error('Action failed'); } finally { setLoadingAction(null); } }; const handleExport = () => { runAction('export', async () => { const res = await bulkExportUsers(userIds); if (res.success && res.csvData) { // Trigger download const blob = new Blob([res.csvData], { type: 'text/csv' }); const url = URL.createObjectURL(blob); const a = document.createElement('a'); a.href = url; a.download = `users_export_${new Date().toISOString().slice(0, 10)}.csv`; a.click(); URL.revokeObjectURL(url); } return res; }); }; const handleBan = () => { if (!confirm(`Permanently ban ${count} user(s)? This is a severe action.`)) return; runAction('ban', () => bulkBanUsers(userIds, { reason: 'Bulk ban from action bar' })); }; const handleDelete = () => { if (!confirm(`Permanently delete ${count} user(s)? This CANNOT be undone.`)) return; runAction('delete', () => bulkDeleteUsers(userIds)); }; const handleVerify = () => { runAction('verify', () => bulkVerifyUsers(userIds)); }; const actions = [ { key: 'suspend', icon: Shield, label: 'Suspend', color: 'text-orange-500 hover:bg-orange-500/10', onClick: onOpenSuspendDialog }, { key: 'ban', icon: Ban, label: 'Ban', color: 'text-red-500 hover:bg-red-500/10', onClick: handleBan }, { key: 'tag', icon: Tag, label: 'Tag', color: 'text-blue-500 hover:bg-blue-500/10', onClick: onOpenTagDialog }, { key: 'email', icon: Mail, label: 'Email', color: 'text-violet-500 hover:bg-violet-500/10', onClick: onOpenEmailComposer }, { key: 'verify', icon: CheckCircle2, label: 'Verify', color: 'text-emerald-500 hover:bg-emerald-500/10', onClick: handleVerify }, { key: 'export', icon: Download, label: 'Export CSV', color: 'text-sky-500 hover:bg-sky-500/10', onClick: handleExport }, { key: 'delete', icon: Trash2, label: 'Delete', color: 'text-red-600 hover:bg-red-600/10', onClick: handleDelete, destructive: true }, ]; return (
{/* Selection Count */}
{count} User{count > 1 ? 's' : ''} Selected
{/* Action Buttons */}
{actions.map((action, i) => ( {action.destructive &&
} {action.label} ))}
); }