"use client" import * as React from "react" import { IconPlus } from "@tabler/icons-react" import { Plus } from "lucide-react" import { toast } from "sonner" import { getCustomers, createCustomer, updateCustomer, deleteCustomer, } from "@/app/actions/customers" import type { Customer } from "@/db/schema" import { Button } from "@/components/ui/button" import { CustomersTable } from "@/components/financials/customers-table" import { CustomerDialog } from "@/components/financials/customer-dialog" import { useRegisterPageActions } from "@/hooks/use-register-page-actions" export default function CustomersPage() { const [customers, setCustomers] = React.useState([]) const [loading, setLoading] = React.useState(true) const [dialogOpen, setDialogOpen] = React.useState(false) const [editing, setEditing] = React.useState(null) const openCreate = React.useCallback(() => { setEditing(null) setDialogOpen(true) }, []) const pageActions = React.useMemo( () => [ { id: "add-customer", label: "Add Customer", icon: Plus, onSelect: openCreate, }, ], [openCreate] ) useRegisterPageActions(pageActions) const load = async () => { try { const data = await getCustomers() setCustomers(data) } catch { toast.error("Failed to load customers") } finally { setLoading(false) } } React.useEffect(() => { load() }, []) const handleEdit = (customer: Customer) => { setEditing(customer) setDialogOpen(true) } const handleDelete = async (id: string) => { const result = await deleteCustomer(id) if (result.success) { toast.success("Customer deleted") await load() } else { toast.error(result.error || "Failed to delete customer") } } const handleSubmit = async (data: { name: string company: string email: string phone: string address: string notes: string }) => { if (editing) { const result = await updateCustomer(editing.id, data) if (result.success) { toast.success("Customer updated") } else { toast.error(result.error || "Failed to update customer") return } } else { const result = await createCustomer(data) if (result.success) { toast.success("Customer created") } else { toast.error(result.error || "Failed to create customer") return } } setDialogOpen(false) await load() } if (loading) { return (

Customers

Manage customer accounts

Loading...
) } return ( <>

Customers

Manage customer accounts

{customers.length === 0 ? (

No customers yet

Add your first customer to start tracking contacts and invoices.

) : ( )}
) }