Hooks & Utilities
Relevant source files
The following files were used as context for generating this wiki page:
Hooks and Utilities form the foundational logic layer of the ElmecV2 Mobile App, providing essential services for cross-cutting concerns such as error handling, logging, and file management. These modules ensure that the application remains robust, maintainable, and observable across different environments, from development to production.
The utility layer is designed to be environment-aware, automatically adjusting its behavior (e.g., logging verbosity) based on whether the app is running in a production or development context. This modular approach allows UI components to remain focused on presentation while delegating complex operations like Supabase Storage integration and performance monitoring to specialized utility classes.
Sources: utils/logger.ts:37-41, README.md:144-150
Logging System
The application utilizes a centralized Logger class that manages application events with varying levels of severity. It supports local log persistence, console output, and remote logging for critical errors.
Architecture and Data Flow
The logger operates as a singleton instance, maintaining a local buffer of log entries and a unique session ID generated at runtime.
The flow shows how an application event is filtered by severity before being stored locally or transmitted to a remote server.
Sources: utils/logger.ts:44-93
Log Levels and Configuration
| Level | Value | Description |
|---|---|---|
| DEBUG | 0 | Detailed information for debugging purposes. |
| INFO | 1 | General highlights of application progress. |
| WARN | 2 | Potentially harmful situations. |
| ERROR | 3 | Error events that might still allow the app to continue. |
| FATAL | 4 | Severe error events that will presumably lead the application to abort. |
Sources: utils/logger.ts:14-22, utils/logger.ts:114-131
Specialized Logging Methods
The Logger includes specialized methods for monitoring specific application domains:
- Performance Tracking:
startTimer(name)returns a callback that logs the total duration of an operation in milliseconds. - User Actions:
logUserAction(action, userId, data)tracks specific UI interactions for analytics. - API Monitoring:
logApiCall(method, url, status, duration)captures network performance and failure rates. - Error Integration:
logAppError(error, context)maps customAppErrorseverities to standard log levels.
Sources: utils/logger.ts:205-236
Error Handling
ElmecV2 implements a structured error handling system that categorizes failures and provides a standardized AppError class.
Error Classification
Errors are classified by severity and category to facilitate automated response logic (such as retry attempts).
This diagram illustrates the relationship between the main error class and its metadata.
Sources: utils/logger.ts:1-203, utils/errorHandler.ts
File Management Utility
The fileUpload.ts utility facilitates interaction with Supabase Storage, handling the conversion of local assets to uploadable formats and managing remote storage operations.
File Upload Process
The system manages both single and multiple file uploads, incorporating strict validation for size and metadata.
The sequence describes the lifecycle of an upload, from local validation to public URL retrieval.
Sources: utils/fileUpload.ts:40-110, TEST_REPORT.md:94-115
Key Capabilities
- Standardized Naming: Generates names using timestamps and random strings to avoid collisions (
timestamp_random.ext). - Size Constraints: Enforces a 5MB limit per file.
- Metadata Extraction: Captures file name, size, path, and MIME type during the upload process.
- Storage Cleanup: Includes a
deleteFileFromStoragemethod for removing abandoned or deleted assets from therequest-filesbucket.
Sources: utils/fileUpload.ts:10-35, FIXES-APLICADOS.md:38-55
Framework & Common Hooks
The project employs custom hooks and common utility functions to handle repetitive tasks and framework initialization.
useFrameworkReady: Manages the initial loading state of the application, ensuring that fonts, assets, and database connections are established before the UI is rendered.commons.ts: Contains shared helper functions for data formatting, string manipulation, and standard transformations used across various screens.
Sources: hooks/useFrameworkReady.ts, utils/commons.ts, README.md:144-150
Summary
The Hooks & Utilities layer provides the essential infrastructure for ElmecV2. By centralizing error handling, file uploads to Supabase, and a severity-based logging system, the project ensures consistent behavior across its various modules (Requests, Chat, and Admin). These utilities enable robust performance monitoring and simplified interaction with backend services while maintaining high technical standards for production deployment.
Sources: VALIDACION-COMPLETA.md:5-15, TEST_REPORT.md:250-265