import { useEffect, useState } from 'react'; import { officesService } from '@/features/offices/services/officesService'; import { Office } from '@/features/offices/types'; import { getErrorMessage } from '@/shared/lib/errors/getErrorMessage'; 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) { setError(getErrorMessage(err, 'No se pudo cargar la oficina')); } finally { setLoading(false); } }; useEffect(() => { loadOffice(); }, [officeId]); return { office, loading, error, reload: loadOffice, }; }