Co-authored-by: renee-png <262607627+renee-png@users.noreply.github.com>
This commit is contained in:
gpt-engineer-app[bot]
2026-04-18 01:43:35 +00:00
co-authored by renee-png
parent 40f57cc2b7
commit e7a6ed5d53
3 changed files with 89 additions and 21 deletions
+41 -13
View File
@@ -6,11 +6,13 @@ import { Card, CardContent } from "@/components/ui/card";
import { Button } from "@/components/ui/button";
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 { useAuth } from "@/lib/auth";
import { toast } from "sonner";
import { Plus, Search, Mail, Phone, Building2, ChevronRight, Edit, Trash2, Briefcase, Users } from "lucide-react";
import { Plus, Search, Mail, Phone, Building2, ChevronRight, Edit, Trash2, Briefcase, Users, Archive, ArchiveRestore } from "lucide-react";
import { ContactFormDialog, CONTACT_TYPES, type ContactRecord } from "@/components/contacts/contact-form-dialog";
import { setArchived } from "@/lib/archive";
export const Route = createFileRoute("/contacts/")({
component: () => (
@@ -25,6 +27,7 @@ interface ContactRow extends Required<Pick<ContactRecord, "name">>, ContactRecor
created_by: string | null;
case_links: number;
client_links: number;
archived_at: string | null;
}
function ContactsIndex() {
@@ -33,6 +36,7 @@ function ContactsIndex() {
const [loading, setLoading] = useState(true);
const [q, setQ] = useState("");
const [typeFilter, setTypeFilter] = useState<string>("all");
const [view, setView] = useState<"active" | "archived">("active");
const [editing, setEditing] = useState<ContactRecord | null>(null);
const [open, setOpen] = useState(false);
@@ -65,6 +69,7 @@ function ContactsIndex() {
const filtered = useMemo(() => {
const term = q.trim().toLowerCase();
return rows.filter((r) => {
if (view === "archived" ? !r.archived_at : !!r.archived_at) return false;
if (typeFilter !== "all" && r.contact_type !== typeFilter) return false;
if (!term) return true;
return (
@@ -74,7 +79,10 @@ function ContactsIndex() {
(r.phone ?? "").toLowerCase().includes(term)
);
});
}, [rows, q, typeFilter]);
}, [rows, q, typeFilter, view]);
const archivedCount = rows.filter((r) => r.archived_at).length;
const activeCount = rows.length - archivedCount;
const remove = async (id: string, name: string) => {
if (!confirm(`Delete contact "${name}"? This will also remove all case and client links.`)) return;
@@ -87,6 +95,10 @@ function ContactsIndex() {
setRows((r) => r.filter((x) => x.id !== id));
};
const onArchive = async (id: string, archived: boolean) => {
if (await setArchived("contacts", id, archived)) load();
};
const startNew = () => {
setEditing(null);
setOpen(true);
@@ -110,6 +122,12 @@ function ContactsIndex() {
<Card className="mb-4">
<CardContent className="p-4 flex flex-col sm:flex-row gap-3">
<Tabs value={view} onValueChange={(v) => setView(v as "active" | "archived")}>
<TabsList>
<TabsTrigger value="active">Active ({activeCount})</TabsTrigger>
<TabsTrigger value="archived">Archived ({archivedCount})</TabsTrigger>
</TabsList>
</Tabs>
<div className="relative flex-1">
<Search className="absolute left-3 top-1/2 -translate-y-1/2 h-4 w-4 text-muted-foreground" />
<Input
@@ -138,7 +156,7 @@ function ContactsIndex() {
<p className="p-6 text-sm text-muted-foreground">Loading…</p>
) : filtered.length === 0 ? (
<p className="p-6 text-sm text-muted-foreground text-center">
{rows.length === 0 ? "No contacts yet." : "No contacts match your filters."}
{rows.length === 0 ? "No contacts yet." : view === "archived" ? "No archived contacts." : "No contacts match your filters."}
</p>
) : (
<ul className="divide-y">
@@ -171,16 +189,26 @@ function ContactsIndex() {
</div>
<ChevronRight className="h-4 w-4 text-muted-foreground" />
</Link>
{canEdit && (
<div className="flex gap-1">
<Button variant="ghost" size="icon" onClick={() => startEdit(c)}>
<Edit className="h-4 w-4" />
</Button>
<Button variant="ghost" size="icon" onClick={() => remove(c.id, c.name)}>
<Trash2 className="h-4 w-4 text-destructive" />
</Button>
</div>
)}
<div className="flex gap-1">
<Button
variant="ghost"
size="icon"
title={c.archived_at ? "Restore" : "Archive"}
onClick={() => onArchive(c.id, !c.archived_at)}
>
{c.archived_at ? <ArchiveRestore className="h-4 w-4" /> : <Archive className="h-4 w-4" />}
</Button>
{canEdit && (
<>
<Button variant="ghost" size="icon" onClick={() => startEdit(c)}>
<Edit className="h-4 w-4" />
</Button>
<Button variant="ghost" size="icon" onClick={() => remove(c.id, c.name)}>
<Trash2 className="h-4 w-4 text-destructive" />
</Button>
</>
)}
</div>
</div>
</li>
);