import { useEffect, useState } from 'react'; import { officesService } from '@/features/offices/services/officesService'; import { Office } from '@/features/offices/types'; export function useOffice(officeId?: string) { const [office, setOffice] = useState(null); const [loading, setLoading] = useState(true); const [error, setError] = useState(null); const loadOffice = async () => { if (!officeId) { setError('No se indicó una oficina válida'); setLoading(false); return; } try { setError(null); setLoading(true); const data = await officesService.getById(officeId); if (!data) { setOffice(null); setError('La oficina no existe'); return; } setOffice(data); } catch (err: any) { setError(err?.message ?? 'No se pudo cargar la oficina'); } finally { setLoading(false); } }; useEffect(() => { loadOffice(); }, [officeId]); return { office, loading, error, reload: loadOffice, }; }