import { useEffect, useState } from "react"; import { X, Phone, PhoneOff, Pencil, Check, Mail, Copy } from "lucide-react"; import { userService, type AdminUserRecord } from "../api/userService"; import { cn } from "../lib/utils"; import { Button } from "./ui/button"; import { Input } from "./ui/input"; import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow, } from "./ui/table"; type Props = { onClose: () => void; }; export const AdminPanel = ({ onClose }: Props) => { const [users, setUsers] = useState([]); const [loading, setLoading] = useState(true); const [editingId, setEditingId] = useState(null); const [editValue, setEditValue] = useState(""); const [rowError, setRowError] = useState>({}); const [rowSuccess, setRowSuccess] = useState>({}); useEffect(() => { userService .adminListUsers() .then(setUsers) .catch(() => {}) .finally(() => setLoading(false)); }, []); const startEdit = (user: AdminUserRecord) => { setEditingId(user.id); setEditValue(user.whatsapp_number ?? ""); setRowError((p) => ({ ...p, [user.id]: "" })); setRowSuccess((p) => ({ ...p, [user.id]: "" })); }; const cancelEdit = () => { setEditingId(null); setEditValue(""); }; const saveWhatsapp = async (userId: string) => { setRowError((p) => ({ ...p, [userId]: "" })); try { const updated = await userService.adminSetWhatsapp(userId, editValue); setUsers((p) => p.map((u) => (u.id === userId ? updated : u))); setRowSuccess((p) => ({ ...p, [userId]: "Saved ✓" })); setEditingId(null); setTimeout(() => setRowSuccess((p) => ({ ...p, [userId]: "" })), 2000); } catch (err) { setRowError((p) => ({ ...p, [userId]: err instanceof Error ? err.message : "Failed to save", })); } }; const unlinkWhatsapp = async (userId: string) => { setRowError((p) => ({ ...p, [userId]: "" })); try { await userService.adminUnlinkWhatsapp(userId); setUsers((p) => p.map((u) => (u.id === userId ? { ...u, whatsapp_number: null } : u)), ); setRowSuccess((p) => ({ ...p, [userId]: "Unlinked ✓" })); setTimeout(() => setRowSuccess((p) => ({ ...p, [userId]: "" })), 2000); } catch (err) { setRowError((p) => ({ ...p, [userId]: err instanceof Error ? err.message : "Failed to unlink", })); } }; const toggleEmail = async (userId: string) => { setRowError((p) => ({ ...p, [userId]: "" })); try { const updated = await userService.adminToggleEmail(userId); setUsers((p) => p.map((u) => (u.id === userId ? updated : u))); setRowSuccess((p) => ({ ...p, [userId]: "Email enabled ✓" })); setTimeout(() => setRowSuccess((p) => ({ ...p, [userId]: "" })), 2000); } catch (err) { setRowError((p) => ({ ...p, [userId]: err instanceof Error ? err.message : "Failed to enable email", })); } }; const disableEmail = async (userId: string) => { setRowError((p) => ({ ...p, [userId]: "" })); try { await userService.adminDisableEmail(userId); setUsers((p) => p.map((u) => (u.id === userId ? { ...u, email_enabled: false, email_address: null } : u)), ); setRowSuccess((p) => ({ ...p, [userId]: "Email disabled ✓" })); setTimeout(() => setRowSuccess((p) => ({ ...p, [userId]: "" })), 2000); } catch (err) { setRowError((p) => ({ ...p, [userId]: err instanceof Error ? err.message : "Failed to disable email", })); } }; const copyToClipboard = (text: string, userId: string) => { navigator.clipboard.writeText(text); setRowSuccess((p) => ({ ...p, [userId]: "Copied ✓" })); setTimeout(() => setRowSuccess((p) => ({ ...p, [userId]: "" })), 2000); }; return (
e.target === e.currentTarget && onClose()} >
{/* Header */}

Admin · User Integrations

{/* Body */}
{loading ? (
Loading users…
) : ( Username Email WhatsApp Email Actions {users.map((user) => ( {user.username} {user.email} {editingId === user.id ? (
setEditValue(e.target.value)} placeholder="whatsapp:+15551234567" className="w-52" autoFocus onKeyDown={(e) => e.key === "Enter" && saveWhatsapp(user.id) } /> {rowError[user.id] && ( {rowError[user.id]} )}
) : (
{user.whatsapp_number ?? "—"} {rowSuccess[user.id] && ( {rowSuccess[user.id]} )} {rowError[user.id] && ( {rowError[user.id]} )}
)}
{user.email_enabled && user.email_address ? (
{user.email_address}
) : ( )}
{editingId === user.id ? (
) : (
{user.whatsapp_number && ( )} {user.email_enabled ? ( ) : ( )}
)}
))}
)}
); };