"use client"; import Link from "next/link"; import { useRouter } from "next/navigation"; 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; nickname: string | null; 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[]; } export function BusinessesDataTable({ businesses }: BusinessesDataTableProps) { const router = useRouter(); const [businessToDelete, setBusinessToDelete] = useState( null, ); const utils = api.useUtils(); const searchableBusinesses = businesses.map((b) => ({ ...b, searchValue: `${b.name} ${b.nickname ?? ""}`.trim(), })); const deleteBusinessMutation = api.businesses.delete.useMutation({ onSuccess: () => { toast.success("Business deleted successfully"); 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 handleRowClick = (business: Business) => { router.push(`/dashboard/businesses/${business.id}`); }; const columns: ColumnDef[] = [ { accessorKey: "name", header: ({ column }) => ( ), cell: ({ row }) => { const business = row.original; return (

{business.name}

{business.nickname ?? "—"}

); }, }, { accessorKey: "email", header: ({ column }) => ( ), cell: ({ row }) => row.original.email ?? "—", meta: { headerClassName: "hidden sm:table-cell", cellClassName: "hidden sm:table-cell", }, }, { accessorKey: "phone", header: ({ column }) => ( ), cell: ({ row }) => row.original.phone ?? "—", meta: { headerClassName: "hidden md:table-cell", cellClassName: "hidden md: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 */} ); }, }, { accessorKey: "searchValue", header: "Search", cell: () => null, meta: { headerClassName: "hidden", cellClassName: "hidden", }, }, { 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. ); }