"use client"; import { useState } from "react"; import { useQuery, useMutation, useQueryClient } from "@tanstack/react-query"; import Link from "next/link"; import { Bookmark, Plus, Globe, Lock, Trash2 } from "lucide-react"; import { collectionsAPI } from "@/lib/api"; import type { Collection } from "@/lib/types"; export default function CollectionsPage() { const qc = useQueryClient(); const [showForm, setShowForm] = useState(false); const [newName, setNewName] = useState(""); const [newPublic, setNewPublic] = useState(false); const [formError, setFormError] = useState(""); const { data: collections, isLoading } = useQuery({ queryKey: ["collections"], queryFn: collectionsAPI.list, }); const createMutation = useMutation({ mutationFn: ({ name, is_public }: { name: string; is_public: boolean }) => collectionsAPI.create(name, is_public), onSuccess: () => { qc.invalidateQueries({ queryKey: ["collections"] }); setNewName(""); setNewPublic(false); setShowForm(false); setFormError(""); }, onError: () => setFormError("Failed to create collection. Try again."), }); const deleteMutation = useMutation({ mutationFn: (id: number) => collectionsAPI.delete(id), onSuccess: () => qc.invalidateQueries({ queryKey: ["collections"] }), }); function handleCreate(e: React.FormEvent) { e.preventDefault(); const name = newName.trim(); if (!name) { setFormError("Name is required"); return; } if (name.length > 100) { setFormError("Name must be ≤ 100 characters"); return; } setFormError(""); createMutation.mutate({ name, is_public: newPublic }); } return (

My Collections

{showForm && (
setNewName(e.target.value)} placeholder="e.g. Healthcare Watch" maxLength={100} className="w-full px-3 py-2 text-sm bg-background border border-border rounded-md focus:outline-none focus:ring-2 focus:ring-primary" autoFocus />
{formError &&

{formError}

}
)} {isLoading ? (
Loading collections…
) : !collections || collections.length === 0 ? (

No collections yet — create one to start grouping bills.

) : (
{collections.map((c: Collection) => (
{c.name} {c.bill_count} {c.bill_count === 1 ? "bill" : "bills"} {c.is_public ? ( Public ) : ( Private )}

Created {new Date(c.created_at).toLocaleDateString()}

))}
)}
); }