Overview
The shared utilities module provides helper functions, custom React hooks, and error handling utilities used throughout the Trazea application.Class Name Utilities
cn()
Merges Tailwind CSS class names efficiently usingclsx and tailwind-merge.
Location: src/shared/lib/utils.ts
Signature
Usage
Why Use cn()?
- Deduplicates classes: Removes conflicting Tailwind classes
- Conditional logic: Supports boolean expressions
- Type safe: Full TypeScript support
- Performance: Optimized for Tailwind CSS
getLoadingText()
Retrieves contextual loading text based on the operation being performed. Location:src/shared/lib/utils.ts
Signature
Usage
Error Handling
handleSupabaseError()
Centralized error handler for Supabase operations that provides user-friendly error messages. Location:src/shared/lib/error-handler.ts
Signature
Types
Usage
Error Messages
The handler automatically maps Supabase error codes to friendly messages: Authentication Errors:invalid_credentials→ “Credenciales inválidas. Verifica tu email y contraseña.”email_not_confirmed→ “Por favor confirma tu email antes de iniciar sesión.”user_not_found→ “Usuario no encontrado.”session_not_found→ “Sesión expirada. Por favor inicia sesión nuevamente.”
23505→ “Este registro ya existe.”23503→ “No se puede eliminar este registro porque está siendo usado.”23502→ “Faltan campos requeridos.”42501→ “No tienes permisos para realizar esta acción.”PGRST116→ “No se encontró el registro.”
withErrorHandling()
Wrapper function that automatically handles errors from async operations. Location:src/shared/lib/error-handler.ts
Signature
Usage
Custom Hooks
useLoading()
Manages loading states and async operations with automatic error handling. Location:src/shared/lib/use-loading.ts
Return Type
Usage
useMultipleLoading()
Manages multiple independent loading states simultaneously. Location:src/shared/lib/use-loading.ts
Return Type
Usage
useIsMobile()
Detects if the user is on a mobile device based on screen width. Location:src/shared/lib/use-mobile.ts
Signature
Breakpoint
Mobile breakpoint is set at 768px (Tailwind’smd breakpoint).
Usage
Features
- Updates automatically on window resize
- Uses
matchMediaAPI for efficient detection - Returns
boolean(neverundefinedafter first render) - Cleans up event listeners on unmount
Best Practices
Error Handling
Error Handling
Always use the error handler utilities instead of showing raw error messages:
Loading States
Loading States
Use the loading hooks for consistent loading state management:
Responsive Design
Responsive Design
Use
useIsMobile() for responsive behavior:Related
- Supabase Client - Database client that uses error handlers
- UI Components - Components that use
cn()utility - React Query - Async data fetching patterns