import { useMutation, useSuspenseQuery } from "@tanstack/react-query"; import { Shield, ShieldAlert, Trash2 } from "lucide-react"; import { useState } from "react"; import { toast } from "sonner"; import { getOrgMembersOptions, removeOrgMemberMutation, updateMemberRoleMutation, } from "~/client/api-client/@tanstack/react-query.gen"; import type { GetOrgMembersResponse } from "~/client/api-client/types.gen"; import { AlertDialog, AlertDialogAction, AlertDialogCancel, AlertDialogContent, AlertDialogDescription, AlertDialogFooter, AlertDialogHeader, AlertDialogTitle, AlertDialogTrigger, } from "~/client/components/ui/alert-dialog"; import { Badge } from "~/client/components/ui/badge"; import { Button } from "~/client/components/ui/button"; import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from "~/client/components/ui/table"; import { useOrganizationContext } from "~/client/hooks/use-org-context"; import { cn } from "~/client/lib/utils"; type Props = { initialMembers?: GetOrgMembersResponse; }; export function OrgMembersSection({ initialMembers }: Props) { const { activeMember } = useOrganizationContext(); const [memberToRemove, setMemberToRemove] = useState<{ id: string; name: string } | null>(null); const { data: { members }, } = useSuspenseQuery({ ...getOrgMembersOptions(), initialData: initialMembers }); const updateRole = useMutation({ ...updateMemberRoleMutation(), onSuccess: () => { toast.success("Member role updated"); }, onError: (error) => { toast.error("Failed to update role", { description: error.message }); }, }); const removeMember = useMutation({ ...removeOrgMemberMutation(), onSuccess: () => { toast.success("Member removed from organization"); setMemberToRemove(null); }, onError: (error) => { toast.error("Failed to remove member", { description: error.message }); }, }); return (
Member Role Actions 0 })}> No members found. {members.map((member) => { const isOwner = member.role === "owner"; const isSelf = member.id === activeMember?.id; return (
{member.user.name ?? member.user.email} {member.user.email}
{member.role}
!open && setMemberToRemove(null)} > Remove member Are you sure you want to remove {memberToRemove?.name} from this organization? They will lose access to all organization resources. Cancel removeMember.mutate({ path: { memberId: memberToRemove!.id } })} > Remove
); })}
); }