"use client"; import Link from "next/link"; import type { ColumnDef } from "@tanstack/react-table"; import { Button } from "~/components/ui/button"; import { DataTable, DataTableColumnHeader } from "~/components/data/data-table"; import { Building, Pencil, Trash2, ExternalLink } from "lucide-react"; import { useState } from "react"; import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle, } from "~/components/ui/dialog"; import { api } from "~/trpc/react"; import { toast } from "sonner"; // Type for business data interface Business { id: string; name: string; email: string | null; phone: string | null; addressLine1: string | null; addressLine2: string | null; city: string | null; state: string | null; postalCode: string | null; country: string | null; website: string | null; taxId: string | null; logoUrl: string | null; createdById: string; createdAt: Date; updatedAt: Date | null; } interface BusinessesDataTableProps { businesses: Business[]; } const formatAddress = (business: Business) => { const parts = [ business.addressLine1, business.addressLine2, business.city, business.state, business.postalCode, ].filter(Boolean); return parts.join(", ") || "—"; }; export function BusinessesDataTable({ businesses: initialBusinesses, }: BusinessesDataTableProps) { const [businesses, setBusinesses] = useState(initialBusinesses); const [businessToDelete, setBusinessToDelete] = useState( null, ); const utils = api.useUtils(); const deleteBusinessMutation = api.businesses.delete.useMutation({ onSuccess: () => { toast.success("Business deleted successfully"); setBusinesses(businesses.filter((b) => b.id !== businessToDelete?.id)); setBusinessToDelete(null); void utils.businesses.getAll.invalidate(); }, onError: (error) => { toast.error(`Failed to delete business: ${error.message}`); }, }); const handleDelete = () => { if (!businessToDelete) return; deleteBusinessMutation.mutate({ id: businessToDelete.id }); }; const columns: ColumnDef[] = [ { accessorKey: "name", header: ({ column }) => ( ), cell: ({ row }) => { const business = row.original; return (

{business.name}

{business.email ?? "—"}

); }, }, { accessorKey: "phone", header: ({ column }) => ( ), cell: ({ row }) => row.original.phone ?? "—", meta: { headerClassName: "hidden md:table-cell", cellClassName: "hidden md:table-cell", }, }, { id: "address", header: "Address", cell: ({ row }) => formatAddress(row.original), meta: { headerClassName: "hidden lg:table-cell", cellClassName: "hidden lg:table-cell", }, }, { accessorKey: "taxId", header: ({ column }) => ( ), cell: ({ row }) => row.original.taxId ?? "—", meta: { headerClassName: "hidden xl:table-cell", cellClassName: "hidden xl:table-cell", }, }, { accessorKey: "website", header: ({ column }) => ( ), cell: ({ row }) => { const website = row.original.website; if (!website) return "—"; // Add https:// if not present const url = website.startsWith("http") ? website : `https://${website}`; return ( <> {/* Desktop: Show full URL */} {website} {/* Mobile: Show link button */} ); }, }, { id: "actions", cell: ({ row }) => { const business = row.original; return (
); }, }, ]; return ( <> {/* Delete confirmation dialog */} !open && setBusinessToDelete(null)} > Are you sure? This action cannot be undone. This will permanently delete the business "{businessToDelete?.name}" and remove all associated data. ); }