import { 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"; import { useAdminUsers } from "../hooks/useAdminUsers"; type Props = { onClose: () => void; }; export const AdminPanel = ({ onClose }: Props) => { const { users, loading, updateUser } = useAdminUsers(); const [editingId, setEditingId] = useState(null); const [editValue, setEditValue] = useState(""); const [rowError, setRowError] = useState>({}); const [rowSuccess, setRowSuccess] = useState>({}); 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); updateUser(userId, () => updated); 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); updateUser(userId, (u) => ({ ...u, whatsapp_number: null })); 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); updateUser(userId, () => updated); 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); updateUser(userId, (u) => ({ ...u, email_enabled: false, email_address: null })); 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()} >

Admin ยท User Integrations

{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 ?? "\u2014"} {rowSuccess[user.id] && ( {rowSuccess[user.id]} )} {rowError[user.id] && ( {rowError[user.id]} )}
)}
{user.email_enabled && user.email_address ? (
{user.email_address}
) : ( \u2014 )}
{editingId === user.id ? (
) : (
{user.whatsapp_number && ( )} {user.email_enabled ? ( ) : ( )}
)}
))}
)}
); };