"use client"; import { useState } from "react"; import { useMutation } from "@tanstack/react-query"; import { toast } from "sonner"; import { Sheet, SheetContent, SheetFooter, SheetHeader, SheetTitle, } from "@/app/_components/ui/sheet"; import { Button } from "@/app/_components/ui/button"; import { Badge } from "@/app/_components/ui/badge"; import { Separator } from "@/app/_components/ui/separator"; import { AlertDialog, AlertDialogAction, AlertDialogCancel, AlertDialogContent, AlertDialogDescription, AlertDialogFooter, AlertDialogHeader, AlertDialogTitle, AlertDialogTrigger, } from "@/app/_components/ui/alert-dialog"; import { Mail, Trash2, Ban, SendHorizonal, CheckCircle, XCircle, Copy, Loader2, } from "lucide-react"; import { banUser, deleteUser, sendMagicLink, sendPasswordRecovery, unbanUser, } from "@/app/(protected)/(admin)/dashboard/user-management/action"; import { format } from "date-fns"; interface UserDetailsSheetProps { open: boolean; onOpenChange: (open: boolean) => void; user: any; onUserUpdate: () => void; } export function UserDetailsSheet({ open, onOpenChange, user, onUserUpdate, }: UserDetailsSheetProps) { const [isDeleting, setIsDeleting] = useState(false); const [isLoading, setIsLoading] = useState({ deleteUser: false, sendPasswordRecovery: false, sendMagicLink: false, toggleBan: false, }); const handleDeleteUser = async () => { setIsLoading((prev) => ({ ...prev, deleteUser: true })); setIsDeleting(true); try { await deleteUser(user.id); toast.success("User deleted successfully"); onUserUpdate(); onOpenChange(false); } catch { toast.error("Failed to delete user"); } finally { setIsLoading((prev) => ({ ...prev, deleteUser: false })); setIsDeleting(false); } }; const handleSendPasswordRecovery = async () => { setIsLoading((prev) => ({ ...prev, sendPasswordRecovery: true })); try { if (!user.email) { throw new Error("User does not have an email address"); } await sendPasswordRecovery(user.email); toast.success("Password recovery email sent"); } catch { toast.error("Failed to send password recovery email"); } finally { setIsLoading((prev) => ({ ...prev, sendPasswordRecovery: false })); } }; const handleSendMagicLink = async () => { setIsLoading((prev) => ({ ...prev, sendMagicLink: true })); try { if (!user.email) { throw new Error("User does not have an email address"); } await sendMagicLink(user.email); toast.success("Magic link sent successfully"); } catch { toast.error("Failed to send magic link"); } finally { setIsLoading((prev) => ({ ...prev, sendMagicLink: false })); } }; const handleToggleBan = async () => { setIsLoading((prev) => ({ ...prev, toggleBan: true })); try { if (user.banned_until) { await unbanUser(user.id); } else { await banUser(user.id); } toast.success("User ban status updated"); onUserUpdate(); } catch { toast.error("Failed to update user ban status"); } finally { setIsLoading((prev) => ({ ...prev, toggleBan: false })); } }; const handleCopyItem = (item: string) => { navigator.clipboard.writeText(item); toast.success("Copied to clipboard"); }; const formatDate = (date: string | undefined | null) => { return date ? format(new Date(date), "PPpp") : "-"; }; return ( {user.email} {user.banned_until && Banned} {!user.email_confirmed_at && ( Unconfirmed )} {!user.banned_until && user.email_confirmed_at && ( Active )}
{/* User Information Section */}

User Information

User UID
{user.id}
Created at {new Date(user.created_at).toLocaleString()}
Updated at {new Date( user.updated_at || user.created_at ).toLocaleString()}
Invited at {formatDate(user.invited_at)}
Confirmation sent at {formatDate(user.email_confirmation_sent_at)}
Confirmed at {formatDate(user.email_confirmed_at)}
Last signed in {formatDate(user.last_sign_in_at)}
SSO
{/* Provider Information Section */}

Provider Information

The user has the following providers

Email
Signed in with a email account via OAuth
Enabled

Reset password

Send a password recovery email to the user

Send magic link

Passwordless login via email for the user

{/* Danger Zone Section */}

Danger zone

Be wary of the following features as they cannot be undone.

Ban user

Revoke access to the project for a set duration

Delete user

User will no longer have access to the project

Are you absolutely sure? This action cannot be undone. This will permanently delete the user account and remove their data from our servers. Cancel {isDeleting ? ( <> Deleting... ) : ( "Delete" )}
{/* */}
); }