import type { ColumnDef } from "@tanstack/react-table"; import { Crown, Mail, MessagesSquare, Paperclip, Search, ShoppingBag, User, UserCog, } from "lucide-react"; import Link from "next/link"; import { useRouter } from "next/router"; import toast from "react-hot-toast"; import { DataTable } from "~/components/custom_ui/data-table"; import { DataTableColumnHeader } from "~/components/custom_ui/dataTable-header"; import type { PinnedFiltro, SearchFiltro, } from "~/components/custom_ui/dataTable-toolbar"; import { UserAvatar } from "~/components/user_avatar"; import { cn } from "~/lib/utils"; import type { Users } from "~/schemas/public/Users"; import { api } from "~/utils/api"; import { ContextMenuContent, ContextMenuItem, ContextMenuSeparator, } from "../ui/context-menu"; type UserWChatInfo = Pick< Users, | "id" | "username" | "email" | "telefono" | "isAdmin" | "isBlocked" | "created_at" > & { chatid: string | null }; export const UsersTable = (props: { data: UserWChatInfo[] }) => { const router = useRouter(); const { mutateAsync: swap } = api.auth.swapUser.useMutation({ onSuccess: async () => { toast.success("Utente cambiato con successo"); await router.push("/area-riservata/load-page"); }, }); const { data } = props; const tabledata = data.map((user) => { return { chatid: user.chatid, created_at: user.created_at, email: user.email, telefono: user.telefono, id: user.id, isAdmin: user.isAdmin ? "Admin" : "Utente", isBanned: user.isBlocked ? "Bloccato" : "Attivo", username: user.username, }; }); const admin_options = [ { label: "Admin", value: "Admin" }, { label: "Utente", value: "Utente" }, ]; const columns_titles = { created_at: "Creato il", email: "Email", telefono: "Telefono", isAdmin: "Ruolo", isBanned: "Status", username: "User", }; type Column = (typeof tabledata)[number]; const columns: ColumnDef[] = [ { accessorKey: "username", cell: ({ row }) => { const user = row.original; return ( {user.username} ); }, enableHiding: false, header: ({ column }) => ( ), }, { accessorKey: "email", header: ({ column }) => ( ), }, { accessorKey: "telefono", header: ({ column }) => ( ), }, { accessorKey: "created_at", cell: ({ row }) => { const date = row.getValue("created_at"); return new Date(date as Date).toLocaleDateString(); }, enableHiding: false, header: ({ column }) => ( ), sortingFn: "datetime", }, { accessorKey: "isAdmin", cell: ({ row }) => { const isadmin = admin_options.find( (status) => status.value === row.getValue("isAdmin"), ); if (!isadmin) { return null; } return (
{isadmin.value === "Admin" && } {isadmin.label}
); }, filterFn: (row, id, value) => { return value.includes(row.getValue(id)); }, header: ({ column }) => ( ), }, { accessorKey: "isBanned", filterFn: (row, id, value) => { return value.includes(row.getValue(id)); }, header: ({ column }) => ( ), }, ]; const pinnedFiltri: PinnedFiltro[] = [ { columnName: "isAdmin", options: [ { label: "Admin", value: "Admin" }, { label: "Utente", value: "Utente" }, ], title: "Ruolo", }, { columnName: "isBanned", options: [ { label: "Bloccato", value: "Bloccato" }, { label: "Attivo", value: "Attivo" }, ], title: "Status", }, ]; const searchFiltro: SearchFiltro = { columnName: "username", placeholder: "Filtra utenti...", }; return (
( Ricerca Comunicazioni Profilo Ordini Allegati Chat )} data={tabledata} defaultColumnVisibility={{ actions: true, created_at: true, email: true, telefono: true, isAdmin: true, isBanned: false, username: true, }} defaultSort={[{ desc: true, id: "created_at" }]} pinnedFiltri={pinnedFiltri} searchColumn={searchFiltro} withContextMenu={true} />
); };