'use client'; import { useState, useCallback } from 'react'; import { api } from '@/lib/api/client'; export function useContacts() { const [contacts, setContacts] = useState([]); const [loading, setLoading] = useState(false); const [error, setError] = useState(null); const fetchContacts = useCallback(async (params?: { query?: string; limit?: number }) => { setLoading(true); setError(null); try { const result = await api.contacts.getAll(params); setContacts(result.data || []); } catch (err) { setError(err instanceof Error ? err.message : 'Failed to fetch contacts'); } finally { setLoading(false); } }, []); const createContact = async (data: any) => { const contact = await api.contacts.create(data); setContacts(prev => [contact, ...prev]); return contact; }; const updateContact = async (id: string, data: any) => { const updated = await api.contacts.update(id, data); setContacts(prev => prev.map(c => c.id === id ? updated : c)); return updated; }; const deleteContact = async (id: string) => { await api.contacts.delete(id); setContacts(prev => prev.filter(c => c.id !== id)); }; return { contacts, loading, error, fetchContacts, createContact, updateContact, deleteContact }; }