Project Data Utility
The Project Data utility provides functions for fetching project data from the AWS S3 Bucket. It serves as a data provider for the DataContext, enabling the application to access and display project information throughout the app.
Core Functionality
The utility exports a primary function:
fetchCSVFromS3
This function:
- Attempts to fetch project data from the API endpoint
- Handles environment-specific URLs (development vs production)
- Implements error handling with fallback to local CSV data
- Returns structured project data for consumption by the DataContext
Implementation Details
The function follows a robust error-handling pattern:
- Primary Request: Attempts to fetch data from the API endpoint
- Uses
localhost:5001/api/csvin development -
Uses
/api/csvin production -
Fallback Mechanism: If the primary request fails, attempts to load local CSV data
- Parses the CSV text into a structured object
-
Displays an error toast notification to inform the user
-
Final Error Handling: If both primary and fallback requests fail
- Returns
nullto indicate failure - Displays an error toast notification
Integration with DataContext
The DataContext uses this utility to:
- Fetch project data on initial application load
- Refresh project data when requested by the user
- Cache the returned data to minimize redundant API calls
Example usage within DataContext:
const getCsvData = useCallback(
async (forceRefresh = false) => {
// Check cache first unless forceRefresh is true
if (csvData && !forceRefresh) {
return csvData;
}
// Check for pending request
if (pendingRequests.csv) {
return pendingRequests.csv;
}
// Create new request
const request = fetchCSVFromS3();
pendingRequests.csv = request;
try {
const data = await request;
setCsvData(data);
return data;
} finally {
pendingRequests.csv = null;
}
},
[csvData, pendingRequests]
);
Error Handling
The utility implements comprehensive error handling:
- Catches and logs network errors
- Provides user feedback via toast notifications
- Attempts to load fallback data when primary sources fail
- Returns
nullwhen all data sources are unavailable