"use client" import { useState } from "react" import { useMutation } from "@tanstack/react-query" import { toast } from "sonner" import { Sheet, SheetContent, SheetFooter, SheetHeader, SheetTitle } from "@/components/ui/sheet" import { Button } from "@/components/ui/button" import { Badge } from "@/components/ui/badge" import { Separator } from "@/components/ui/separator" import { AlertDialog, AlertDialogAction, AlertDialogCancel, AlertDialogContent, AlertDialogDescription, AlertDialogFooter, AlertDialogHeader, AlertDialogTitle, AlertDialogTrigger, } from "@/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" // // Mock functions (replace with your actual API calls) // const updateUser = async (id: string, data: any) => { // console.log(`Updating user ${id} with data:`, data) // await new Promise((resolve) => setTimeout(resolve, 500)) // Simulate API call // return { id, ...data } // } // const deleteUser = async (id: string) => { // console.log(`Deleting user ${id}`) // await new Promise((resolve) => setTimeout(resolve, 500)) // Simulate API call // return { success: true } // } // const sendPasswordRecovery = async (email: string) => { // console.log(`Sending password recovery email to ${email}`) // await new Promise((resolve) => setTimeout(resolve, 500)) // Simulate API call // return { success: true } // } // const sendMagicLink = async (email: string) => { // console.log(`Sending magic link to ${email}`) // await new Promise((resolve) => setTimeout(resolve, 500)) // Simulate API call // return { success: true } // } // const banUser = async (id: string) => { // console.log(`Banning user ${id}`) // await new Promise((resolve) => setTimeout(resolve, 500)) // Simulate API call // return { success: true } // } // const unbanUser = async (id: string) => { // console.log(`Unbanning user ${id}`) // await new Promise((resolve) => setTimeout(resolve, 500)) // Simulate API call // return { success: true } // } 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 deleteUserMutation = useMutation({ mutationFn: () => deleteUser(user.id), onSuccess: () => { toast.success("User deleted successfully") onUserUpdate() onOpenChange(false) }, onError: () => { toast.error("Failed to delete user") }, onSettled: () => { setIsDeleting(false) }, }) const sendPasswordRecoveryMutation = useMutation({ mutationFn: () => { if (!user.email) { throw new Error("User does not have an email address") } return sendPasswordRecovery(user.email) }, onSuccess: () => { toast.success("Password recovery email sent") }, onError: () => { toast.error("Failed to send password recovery email") }, }) const sendMagicLinkMutation = useMutation({ mutationFn: () => { if (!user.email) { throw new Error("User does not have an email address") } return sendMagicLink(user.email) }, onSuccess: () => { toast.success("Magic link sent successfully") }, onError: () => { toast.error("Failed to send magic link") }, }) const toggleBanMutation = useMutation({ mutationFn: () => { if (user.banned_until) { return unbanUser(user.id) } else { return banUser(user.id) } }, onSuccess: () => { toast.success("User ban status updated") onUserUpdate() }, onError: () => { toast.error("Failed to update user ban status") }, }) 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 {user.invited_at ? new Date(user.invited_at).toLocaleString() : "-"}
Confirmation sent at {user.confirmation_sent_at ? new Date(user.confirmation_sent_at).toLocaleString() : "-"}
Confirmed at {user.email_confirmed_at ? new Date(user.email_confirmed_at).toLocaleString() : "-"}
Last signed in {user.last_sign_in_at ? new Date(user.last_sign_in_at).toLocaleString() : "Never"}
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 { setIsDeleting(true) deleteUserMutation.mutate() }} className="bg-destructive text-destructive-foreground hover:bg-destructive/90" > {isDeleting ? ( <> Deleting... ) : ( "Delete" )}
{/* */}
) }