import { createFileRoute, Link, useNavigate } from "@tanstack/react-router"; import { useEffect, useState } from "react"; import { ProtectedLayout } from "@/components/protected-layout"; import { PageContainer, PageHeader } from "@/components/app-shell"; import { Button } from "@/components/ui/button"; import { Card, CardContent } from "@/components/ui/card"; import { Input } from "@/components/ui/input"; import { Badge } from "@/components/ui/badge"; import { Tabs, TabsList, TabsTrigger } from "@/components/ui/tabs"; import { supabase } from "@/integrations/supabase/client"; import { ClientFormDialog } from "@/components/clients/client-form-dialog"; import { Plus, Search, Building2, User, Archive, ArchiveRestore } from "lucide-react"; import { formatDate } from "@/lib/format"; import { setArchived } from "@/lib/archive"; export const Route = createFileRoute("/clients/")({ component: () => ( ), }); function ClientsList() { const navigate = useNavigate(); const [clients, setClients] = useState([]); const [q, setQ] = useState(""); const [open, setOpen] = useState(false); const [view, setView] = useState<"active" | "archived">("active"); const load = async () => { const { data } = await supabase .from("clients") .select("*") .order("name", { ascending: true }); setClients(data ?? []); }; useEffect(() => { load(); }, []); const visible = clients.filter((c) => view === "archived" ? c.archived_at : !c.archived_at, ); const filtered = visible.filter((c) => [c.name, c.management_company, c.primary_contact_name, c.primary_contact_email] .filter(Boolean) .join(" ") .toLowerCase() .includes(q.toLowerCase()), ); const archivedCount = clients.filter((c) => c.archived_at).length; const activeCount = clients.length - archivedCount; const typeIcon = (t: string) => t === "hoa" || t === "condo" ? Building2 : User; const onArchive = async (id: string, archived: boolean) => { if (await setArchived("clients", id, archived)) load(); }; return ( setOpen(true)}> New client } />
setView(v as "active" | "archived")}> Active ({activeCount}) Archived ({archivedCount})
setQ(e.target.value)} placeholder="Search by name, contact, manager…" className="pl-9" />
{filtered.length === 0 && ( )} {filtered.map((c) => { const Icon = typeIcon(c.client_type); return ( navigate({ to: "/clients/$clientId", params: { clientId: c.id } })} > ); })}
Name Type Contact Units Created
{visible.length === 0 ? view === "archived" ? "No archived clients." : "No clients yet. Add your first one." : "No matches."}
e.stopPropagation()} > {c.name} {c.management_company && (
{c.management_company}
)}
{c.client_type} {c.primary_contact_name || "—"} {c.primary_contact_email && (
{c.primary_contact_email}
)}
{c.num_units ?? "—"} {formatDate(c.created_at)} e.stopPropagation()}>
); }