"use client"; import { ColumnDef } from "@tanstack/react-table"; import { DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuTrigger } from "@app/components/ui/dropdown-menu"; import { Button } from "@app/components/ui/button"; import { ArrowRight, ArrowUpDown, Crown, MoreHorizontal } from "lucide-react"; import { UsersDataTable } from "./UsersDataTable"; import { useState } from "react"; import InviteUserForm from "./InviteUserForm"; import ConfirmDeleteDialog from "@app/components/ConfirmDeleteDialog"; import { useOrgContext } from "@app/hooks/useOrgContext"; import { useToast } from "@app/hooks/useToast"; import Link from "next/link"; import { useRouter } from "next/navigation"; import { formatAxiosError } from "@app/lib/api"; import { createApiClient } from "@app/lib/api"; import { useEnvContext } from "@app/hooks/useEnvContext"; import { useUserContext } from "@app/hooks/useUserContext"; export type UserRow = { id: string; email: string; status: string; role: string; isOwner: boolean; }; type UsersTableProps = { users: UserRow[]; }; export default function UsersTable({ users: u }: UsersTableProps) { const [isInviteModalOpen, setIsInviteModalOpen] = useState(false); const [isDeleteModalOpen, setIsDeleteModalOpen] = useState(false); const [selectedUser, setSelectedUser] = useState(null); const [users, setUsers] = useState(u); const router = useRouter(); const api = createApiClient(useEnvContext()); const { user, updateUser } = useUserContext(); const { org } = useOrgContext(); const { toast } = useToast(); const columns: ColumnDef[] = [ { id: "dots", cell: ({ row }) => { const userRow = row.original; return ( <>
{userRow.isOwner && ( )} {!userRow.isOwner && ( <> Manage User {userRow.email !== user?.email && ( { setIsDeleteModalOpen( true ); setSelectedUser( userRow ); }} > Remove User )} )}
); } }, { accessorKey: "email", header: ({ column }) => { return ( ); } }, { accessorKey: "status", header: ({ column }) => { return ( ); } }, { accessorKey: "role", header: ({ column }) => { return ( ); }, cell: ({ row }) => { const userRow = row.original; return (
{userRow.isOwner && ( )} {userRow.role}
); } }, { id: "actions", cell: ({ row }) => { const userRow = row.original; return (
{userRow.isOwner && ( )} {!userRow.isOwner && ( )}
); } } ]; async function removeUser() { if (selectedUser) { const res = await api .delete(`/org/${org!.org.orgId}/user/${selectedUser.id}`) .catch((e) => { toast({ variant: "destructive", title: "Failed to remove user", description: formatAxiosError( e, "An error occurred while removing the user." ) }); }); if (res && res.status === 200) { toast({ variant: "default", title: "User removed", description: `The user ${selectedUser.email} has been removed from the organization.` }); setUsers((prev) => prev.filter((u) => u.id !== selectedUser?.id) ); } } setIsDeleteModalOpen(false); } return ( <> { setIsDeleteModalOpen(val); setSelectedUser(null); }} dialog={

Are you sure you want to remove{" "} {selectedUser?.email} from the organization?

Once removed, this user will no longer have access to the organization. You can always re-invite them later, but they will need to accept the invitation again.

To confirm, please type the email address of the user below.

} buttonText="Confirm Remove User" onConfirm={removeUser} string={selectedUser?.email ?? ""} title="Remove User from Organization" /> { setIsInviteModalOpen(true); }} /> ); }